1993
|
1 // Template array classes |
1988
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
1988
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #include "Array-flags.h" |
|
25 #include "idx-vector.h" |
|
26 #include "lo-error.h" |
|
27 |
|
28 template <class T> |
|
29 Array2<T> |
|
30 Array2<T>::value (void) |
|
31 { |
|
32 Array2<T> retval; |
|
33 |
|
34 int n_idx = index_count (); |
|
35 |
|
36 if (n_idx == 2) |
|
37 { |
|
38 idx_vector *tmp = get_idx (); |
|
39 idx_vector idx_i = tmp[0]; |
|
40 idx_vector idx_j = tmp[1]; |
|
41 |
2382
|
42 return index (idx_i, idx_j); |
1988
|
43 } |
|
44 else if (n_idx == 1) |
|
45 { |
2382
|
46 idx_vector *tmp = get_idx (); |
|
47 idx_vector idx = tmp[0]; |
1988
|
48 |
2382
|
49 return index (idx); |
1988
|
50 } |
|
51 else |
|
52 (*current_liboctave_error_handler) |
|
53 ("invalid number of indices for matrix expression"); |
|
54 |
|
55 clear_index (); |
|
56 |
|
57 return retval; |
|
58 } |
|
59 |
|
60 template <class T> |
2382
|
61 Array2<T> |
|
62 Array2<T>::index (idx_vector& idx) const |
|
63 { |
|
64 Array2<T> retval; |
|
65 |
|
66 int nr = d1; |
|
67 int nc = d2; |
|
68 |
|
69 if (nr == 1 && nc == 1) |
|
70 { |
|
71 Array<T> tmp = Array<T>::index (idx); |
|
72 |
|
73 int len = tmp.length (); |
|
74 |
|
75 if (len == 0) |
|
76 retval = Array2<T> (0, 0); |
|
77 else |
|
78 { |
|
79 if (liboctave_pcv_flag) |
|
80 retval = Array2<T> (tmp, len, 1); |
|
81 else |
|
82 retval = Array2<T> (tmp, 1, len); |
|
83 } |
|
84 } |
|
85 else if (nr == 1 || nc == 1) |
|
86 { |
3243
|
87 int result_is_column_vector = (nc == 1 || idx.is_colon ()); |
2382
|
88 |
|
89 Array<T> tmp = Array<T>::index (idx); |
|
90 |
|
91 int len = tmp.length (); |
|
92 |
|
93 if (len == 0) |
|
94 retval = Array2<T> (0, 0); |
|
95 else |
|
96 { |
|
97 if (result_is_column_vector) |
|
98 retval = Array2<T> (tmp, len, 1); |
|
99 else |
|
100 retval = Array2<T> (tmp, 1, len); |
|
101 } |
|
102 } |
3164
|
103 else if (liboctave_dfi_flag || idx.is_colon ()) |
2382
|
104 { |
|
105 // This code is only for indexing matrices. The vector |
|
106 // cases are handled above. |
|
107 |
2830
|
108 idx.freeze (nr * nc, "matrix"); |
2382
|
109 |
|
110 if (idx) |
|
111 { |
|
112 int result_nr = idx.orig_rows (); |
|
113 int result_nc = idx.orig_columns (); |
|
114 |
|
115 if (idx.is_colon ()) |
|
116 { |
|
117 result_nr = nr * nc; |
3050
|
118 result_nc = result_nr ? 1 : 0; |
2382
|
119 } |
|
120 else if (idx.one_zero_only ()) |
|
121 { |
|
122 result_nr = idx.ones_count (); |
|
123 result_nc = (result_nr > 0 ? 1 : 0); |
|
124 } |
|
125 |
|
126 retval.resize (result_nr, result_nc); |
|
127 |
|
128 int k = 0; |
|
129 for (int j = 0; j < result_nc; j++) |
|
130 { |
|
131 for (int i = 0; i < result_nr; i++) |
|
132 { |
|
133 int ii = idx.elem (k++); |
|
134 int fr = ii % nr; |
|
135 int fc = (ii - fr) / nr; |
|
136 retval.elem (i, j) = elem (fr, fc); |
|
137 } |
|
138 } |
|
139 } |
|
140 // idx_vector::freeze() printed an error message for us. |
|
141 } |
|
142 else |
|
143 (*current_liboctave_error_handler) |
|
144 ("single index only valid for row or column vector"); |
|
145 |
|
146 return retval; |
|
147 } |
|
148 |
|
149 template <class T> |
|
150 Array2<T> |
|
151 Array2<T>::index (idx_vector& idx_i, idx_vector& idx_j) const |
|
152 { |
|
153 Array2<T> retval; |
|
154 |
|
155 int nr = d1; |
|
156 int nc = d2; |
|
157 |
2830
|
158 int n = idx_i.freeze (nr, "row"); |
|
159 int m = idx_j.freeze (nc, "column"); |
2382
|
160 |
|
161 if (idx_i && idx_j) |
|
162 { |
2663
|
163 if (idx_i.orig_empty () || idx_j.orig_empty ()) |
2382
|
164 { |
2663
|
165 retval.resize (n, m); |
|
166 } |
|
167 else if (n == 0) |
|
168 { |
2673
|
169 if (m == 0) |
2382
|
170 retval.resize (0, 0); |
2663
|
171 else if (idx_j.is_colon_equiv (nc, 1)) |
|
172 retval.resize (0, nc); |
3164
|
173 else if (idx_i.is_colon_equiv (nr, 1)) |
|
174 retval.resize (0, m); |
2382
|
175 else |
2663
|
176 (*current_liboctave_error_handler) ("invalid row index = 0"); |
2382
|
177 } |
|
178 else if (m == 0) |
|
179 { |
2663
|
180 if (n == 0) |
2382
|
181 retval.resize (0, 0); |
2663
|
182 else if (idx_i.is_colon_equiv (nr, 1)) |
|
183 retval.resize (nr, 0); |
3164
|
184 else if (idx_j.is_colon_equiv (nc, 1)) |
|
185 retval.resize (n, 0); |
2382
|
186 else |
2663
|
187 (*current_liboctave_error_handler) ("invalid column index = 0"); |
2382
|
188 } |
|
189 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) |
|
190 { |
|
191 retval = *this; |
|
192 } |
|
193 else |
|
194 { |
|
195 retval.resize (n, m); |
|
196 |
|
197 for (int j = 0; j < m; j++) |
|
198 { |
|
199 int jj = idx_j.elem (j); |
|
200 for (int i = 0; i < n; i++) |
|
201 { |
|
202 int ii = idx_i.elem (i); |
|
203 retval.elem (i, j) = elem (ii, jj); |
|
204 } |
|
205 } |
|
206 } |
|
207 } |
|
208 |
|
209 // idx_vector::freeze() printed an error message for us. |
|
210 |
|
211 return retval; |
|
212 } |
|
213 |
|
214 template <class T> |
1988
|
215 void |
3472
|
216 Array2<T>::maybe_delete_elements (idx_vector& idx_i) |
|
217 { |
|
218 int nr = d1; |
|
219 int nc = d2; |
|
220 |
|
221 if (nr == 0 && nc == 0) |
|
222 return; |
|
223 |
|
224 int n; |
|
225 if (nr == 1) |
|
226 n = nc; |
|
227 else if (nc == 1) |
|
228 n = nr; |
|
229 else |
|
230 { |
|
231 (*current_liboctave_error_handler) |
|
232 ("A(idx) = []: expecting A to be row or column vector or scalar"); |
|
233 |
|
234 return; |
|
235 } |
|
236 |
|
237 if (idx_i.is_colon_equiv (n, 1)) |
|
238 { |
|
239 // Either A(:) = [] or A(idx) = [] with idx enumerating all |
|
240 // elements, so we delete all elements and return [](0x0). To |
|
241 // preserve the orientation of the vector, you have to use |
|
242 // A(idx,:) = [] (delete rows) or A(:,idx) (delete columns). |
|
243 |
|
244 resize (0, 0); |
|
245 return; |
|
246 } |
|
247 |
|
248 idx_i.sort (true); |
|
249 |
|
250 int num_to_delete = idx_i.length (n); |
|
251 |
|
252 if (num_to_delete != 0) |
|
253 { |
|
254 int new_n = n; |
|
255 |
|
256 int idx = 0; |
|
257 |
|
258 for (int i = 0; i < n; i++) |
|
259 if (i == idx_i.elem (idx)) |
|
260 { |
|
261 idx++; |
|
262 new_n--; |
|
263 |
|
264 if (idx == num_to_delete) |
|
265 break; |
|
266 } |
|
267 |
|
268 if (new_n > 0) |
|
269 { |
|
270 T *new_data = new T [new_n]; |
|
271 |
|
272 int ii = 0; |
|
273 idx = 0; |
|
274 for (int i = 0; i < n; i++) |
|
275 { |
|
276 if (idx < num_to_delete && i == idx_i.elem (idx)) |
|
277 idx++; |
|
278 else |
|
279 { |
|
280 if (nr == 1) |
|
281 new_data[ii] = elem (0, i); |
|
282 else |
|
283 new_data[ii] = elem (i, 0); |
|
284 |
|
285 ii++; |
|
286 } |
|
287 } |
|
288 |
|
289 if (--rep->count <= 0) |
|
290 delete rep; |
|
291 |
|
292 rep = new ArrayRep (new_data, new_n); |
|
293 |
|
294 if (nr == 1) |
|
295 { |
|
296 d1 = 1; |
|
297 d2 = new_n; |
|
298 } |
|
299 else |
|
300 { |
|
301 d1 = new_n; |
|
302 d2 = 1; |
|
303 } |
|
304 |
|
305 set_max_indices (2); |
|
306 } |
|
307 else |
|
308 (*current_liboctave_error_handler) |
|
309 ("A(idx) = []: index out of range"); |
|
310 } |
|
311 } |
|
312 |
|
313 template <class T> |
|
314 void |
1988
|
315 Array2<T>::maybe_delete_elements (idx_vector& idx_i, idx_vector& idx_j) |
|
316 { |
|
317 int nr = d1; |
|
318 int nc = d2; |
|
319 |
|
320 if (nr == 0 && nc == 0) |
|
321 return; |
|
322 |
3472
|
323 if (idx_i.is_colon ()) |
|
324 { |
|
325 if (idx_j.is_colon ()) |
|
326 { |
|
327 // A(:,:) -- We are deleting columns and rows, so the result |
|
328 // is [](0x0). |
|
329 |
|
330 resize (0, 0); |
|
331 return; |
|
332 } |
|
333 |
|
334 if (idx_j.is_colon_equiv (nc, 1)) |
|
335 { |
|
336 // A(:,j) -- We are deleting columns by enumerating them, |
|
337 // If we enumerate all of them, we should have zero columns |
|
338 // with the same number of rows that we started with. |
|
339 |
|
340 resize (nr, 0); |
|
341 return; |
|
342 } |
|
343 } |
|
344 |
|
345 if (idx_j.is_colon () && idx_i.is_colon_equiv (nr, 1)) |
|
346 { |
|
347 // A(i,:) -- We are deleting rows by enumerating them. If we |
|
348 // enumerate all of them, we should have zero rows with the |
|
349 // same number of columns that we started with. |
|
350 |
|
351 resize (0, nc); |
|
352 return; |
|
353 } |
|
354 |
1988
|
355 if (idx_i.is_colon_equiv (nr, 1)) |
|
356 { |
|
357 if (idx_j.is_colon_equiv (nc, 1)) |
|
358 resize (0, 0); |
|
359 else |
|
360 { |
3079
|
361 idx_j.sort (true); |
|
362 |
1988
|
363 int num_to_delete = idx_j.length (nc); |
|
364 |
|
365 if (num_to_delete != 0) |
|
366 { |
|
367 if (nr == 1 && num_to_delete == nc) |
|
368 resize (0, 0); |
|
369 else |
|
370 { |
2245
|
371 int new_nc = nc; |
|
372 |
|
373 int idx = 0; |
|
374 |
|
375 for (int j = 0; j < nc; j++) |
|
376 if (j == idx_j.elem (idx)) |
|
377 { |
|
378 idx++; |
|
379 new_nc--; |
2917
|
380 |
|
381 if (idx == num_to_delete) |
|
382 break; |
2245
|
383 } |
|
384 |
1988
|
385 if (new_nc > 0) |
|
386 { |
|
387 T *new_data = new T [nr * new_nc]; |
|
388 |
|
389 int jj = 0; |
2245
|
390 idx = 0; |
1988
|
391 for (int j = 0; j < nc; j++) |
|
392 { |
2917
|
393 if (idx < num_to_delete && j == idx_j.elem (idx)) |
1988
|
394 idx++; |
|
395 else |
|
396 { |
|
397 for (int i = 0; i < nr; i++) |
|
398 new_data[nr*jj+i] = elem (i, j); |
|
399 jj++; |
|
400 } |
|
401 } |
|
402 |
|
403 if (--rep->count <= 0) |
|
404 delete rep; |
|
405 |
|
406 rep = new ArrayRep (new_data, nr * new_nc); |
|
407 |
|
408 d2 = new_nc; |
|
409 |
|
410 set_max_indices (2); |
|
411 } |
|
412 else |
|
413 (*current_liboctave_error_handler) |
|
414 ("A(idx) = []: index out of range"); |
|
415 } |
|
416 } |
|
417 } |
|
418 } |
2714
|
419 else if (idx_j.is_colon_equiv (nc, 1)) |
1988
|
420 { |
2714
|
421 if (idx_i.is_colon_equiv (nr, 1)) |
1988
|
422 resize (0, 0); |
|
423 else |
|
424 { |
3079
|
425 idx_i.sort (true); |
|
426 |
1988
|
427 int num_to_delete = idx_i.length (nr); |
|
428 |
|
429 if (num_to_delete != 0) |
|
430 { |
|
431 if (nc == 1 && num_to_delete == nr) |
|
432 resize (0, 0); |
|
433 else |
|
434 { |
2245
|
435 int new_nr = nr; |
|
436 |
|
437 int idx = 0; |
|
438 |
|
439 for (int i = 0; i < nr; i++) |
|
440 if (i == idx_i.elem (idx)) |
|
441 { |
|
442 idx++; |
|
443 new_nr--; |
2917
|
444 |
|
445 if (idx == num_to_delete) |
|
446 break; |
2245
|
447 } |
|
448 |
1988
|
449 if (new_nr > 0) |
|
450 { |
|
451 T *new_data = new T [new_nr * nc]; |
|
452 |
|
453 int ii = 0; |
2245
|
454 idx = 0; |
1988
|
455 for (int i = 0; i < nr; i++) |
|
456 { |
2917
|
457 if (idx < num_to_delete && i == idx_i.elem (idx)) |
1988
|
458 idx++; |
|
459 else |
|
460 { |
|
461 for (int j = 0; j < nc; j++) |
|
462 new_data[new_nr*j+ii] = elem (i, j); |
|
463 ii++; |
|
464 } |
|
465 } |
|
466 |
|
467 if (--rep->count <= 0) |
|
468 delete rep; |
|
469 |
|
470 rep = new ArrayRep (new_data, new_nr * nc); |
|
471 |
|
472 d1 = new_nr; |
|
473 |
|
474 set_max_indices (2); |
|
475 } |
|
476 else |
|
477 (*current_liboctave_error_handler) |
|
478 ("A(idx) = []: index out of range"); |
|
479 } |
|
480 } |
|
481 } |
|
482 } |
|
483 } |
|
484 |
2589
|
485 #define MAYBE_RESIZE_LHS \ |
|
486 do \ |
|
487 { \ |
|
488 if (liboctave_rre_flag) \ |
|
489 { \ |
|
490 int max_row_idx = idx_i_is_colon ? rhs_nr : idx_i.max () + 1; \ |
|
491 int max_col_idx = idx_j_is_colon ? rhs_nc : idx_j.max () + 1; \ |
|
492 \ |
|
493 int new_nr = max_row_idx > lhs_nr ? max_row_idx : lhs_nr; \ |
|
494 int new_nc = max_col_idx > lhs_nc ? max_col_idx : lhs_nc; \ |
|
495 \ |
|
496 lhs.resize (new_nr, new_nc, 0.0); \ |
|
497 } \ |
|
498 } \ |
|
499 while (0) |
|
500 |
1988
|
501 template <class LT, class RT> |
|
502 int |
|
503 assign (Array2<LT>& lhs, const Array2<RT>& rhs) |
|
504 { |
|
505 int retval = 1; |
|
506 |
|
507 int n_idx = lhs.index_count (); |
|
508 |
|
509 int lhs_nr = lhs.rows (); |
|
510 int lhs_nc = lhs.cols (); |
|
511 |
|
512 int rhs_nr = rhs.rows (); |
|
513 int rhs_nc = rhs.cols (); |
|
514 |
|
515 if (n_idx == 2) |
|
516 { |
|
517 idx_vector *tmp = lhs.get_idx (); |
|
518 |
|
519 idx_vector idx_i = tmp[0]; |
|
520 idx_vector idx_j = tmp[1]; |
|
521 |
2830
|
522 int n = idx_i.freeze (lhs_nr, "row", liboctave_rre_flag); |
1988
|
523 |
2830
|
524 int m = idx_j.freeze (lhs_nc, "column", liboctave_rre_flag); |
1988
|
525 |
|
526 int idx_i_is_colon = idx_i.is_colon (); |
|
527 int idx_j_is_colon = idx_j.is_colon (); |
|
528 |
|
529 if (idx_i_is_colon) |
2575
|
530 n = lhs_nr > 0 ? lhs_nr : rhs_nr; |
1988
|
531 |
|
532 if (idx_j_is_colon) |
2575
|
533 m = lhs_nc > 0 ? lhs_nc : rhs_nc; |
1988
|
534 |
|
535 if (idx_i && idx_j) |
|
536 { |
|
537 if (rhs_nr == 0 && rhs_nc == 0) |
|
538 { |
|
539 lhs.maybe_delete_elements (idx_i, idx_j); |
|
540 } |
|
541 else |
|
542 { |
2570
|
543 if (rhs_nr == 1 && rhs_nc == 1 && n > 0 && m > 0) |
1988
|
544 { |
2589
|
545 MAYBE_RESIZE_LHS; |
|
546 |
1988
|
547 RT scalar = rhs.elem (0, 0); |
|
548 |
|
549 for (int j = 0; j < m; j++) |
|
550 { |
|
551 int jj = idx_j.elem (j); |
|
552 for (int i = 0; i < n; i++) |
|
553 { |
|
554 int ii = idx_i.elem (i); |
|
555 lhs.elem (ii, jj) = scalar; |
|
556 } |
|
557 } |
|
558 } |
2570
|
559 else if (n == rhs_nr && m == rhs_nc) |
|
560 { |
2589
|
561 MAYBE_RESIZE_LHS; |
|
562 |
2570
|
563 for (int j = 0; j < m; j++) |
|
564 { |
|
565 int jj = idx_j.elem (j); |
|
566 for (int i = 0; i < n; i++) |
|
567 { |
|
568 int ii = idx_i.elem (i); |
|
569 lhs.elem (ii, jj) = rhs.elem (i, j); |
|
570 } |
|
571 } |
|
572 } |
3177
|
573 else if (n == 0 && m == 0) |
|
574 { |
|
575 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
576 || (rhs_nr == 0 && rhs_nc == 0))) |
|
577 { |
|
578 (*current_liboctave_error_handler) |
|
579 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
580 |
|
581 retval = 0; |
|
582 } |
|
583 } |
1988
|
584 else |
|
585 { |
|
586 (*current_liboctave_error_handler) |
|
587 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
588 (*current_liboctave_error_handler) |
|
589 ("match the number of rows in X and the number of elements in J must"); |
|
590 (*current_liboctave_error_handler) |
|
591 ("match the number of columns in X"); |
|
592 |
|
593 retval = 0; |
|
594 } |
|
595 } |
|
596 } |
|
597 // idx_vector::freeze() printed an error message for us. |
|
598 } |
|
599 else if (n_idx == 1) |
|
600 { |
2112
|
601 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
602 |
|
603 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
1988
|
604 { |
|
605 idx_vector *tmp = lhs.get_idx (); |
|
606 |
|
607 idx_vector idx = tmp[0]; |
|
608 |
|
609 int lhs_len = lhs.length (); |
|
610 |
2830
|
611 int n = idx.freeze (lhs_len, 0, liboctave_rre_flag); |
1988
|
612 |
|
613 if (idx) |
|
614 { |
|
615 if (rhs_nr == 0 && rhs_nc == 0) |
|
616 { |
|
617 if (n != 0 && (lhs_nr != 0 || lhs_nc != 0)) |
3472
|
618 lhs.maybe_delete_elements (idx); |
1988
|
619 } |
2112
|
620 else if (! liboctave_dfi_flag && lhs_is_empty |
|
621 && idx.is_colon () |
|
622 && ! (rhs_nr == 1 || rhs_nc == 1)) |
|
623 { |
|
624 (*current_liboctave_error_handler) |
|
625 ("A(:) = X: X must be a vector"); |
|
626 } |
1988
|
627 else |
|
628 { |
|
629 if (assign ((Array<LT>&) lhs, (Array<RT>&) rhs)) |
|
630 { |
|
631 int len = lhs.length (); |
|
632 |
|
633 if (len > 0) |
|
634 { |
|
635 int idx_nr = idx.orig_rows (); |
|
636 int idx_nc = idx.orig_columns (); |
|
637 |
2112
|
638 // lhs_is_empty now means that lhs was |
3130
|
639 // *originally* empty, and lhs_len is the |
|
640 // *original* length of the lhs. |
2112
|
641 |
1988
|
642 if (liboctave_dfi_flag |
3130
|
643 || (idx_nr == 1 && idx_nc == 1) |
|
644 || (rhs_nr == 1 && rhs_nc == 1 && lhs_len == 1)) |
1988
|
645 { |
|
646 if (liboctave_pcv_flag) |
|
647 { |
|
648 lhs.d1 = lhs.length (); |
|
649 lhs.d2 = 1; |
|
650 } |
|
651 else |
|
652 { |
|
653 lhs.d1 = 1; |
|
654 lhs.d2 = lhs.length (); |
|
655 } |
|
656 } |
2112
|
657 else if (lhs_is_empty && idx.is_colon ()) |
|
658 { |
|
659 lhs.d1 = rhs.d1; |
|
660 lhs.d2 = rhs.d2; |
|
661 } |
3130
|
662 else if (rhs_nr == 1 |
|
663 && (idx_nr == 1 || lhs_len == 1)) |
1988
|
664 { |
|
665 lhs.d1 = 1; |
|
666 lhs.d2 = lhs.length (); |
|
667 } |
3130
|
668 else if (rhs_nc == 1 |
|
669 && (idx_nc == 1 || lhs_len == 1)) |
1988
|
670 { |
|
671 lhs.d1 = lhs.length (); |
|
672 lhs.d2 = 1; |
|
673 } |
3177
|
674 else if (idx_nr == 0 && idx_nc == 0) |
|
675 { |
|
676 if (! ((rhs.d1 == 1 && rhs.d2 == 1) |
|
677 || (rhs.d1 == 0 && rhs.d2 == 0))) |
|
678 (*current_liboctave_error_handler) |
|
679 ("A([]) = X: X must be an empty matrix or scalar"); |
|
680 } |
1988
|
681 else |
|
682 (*current_liboctave_error_handler) |
|
683 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
684 } |
|
685 else |
|
686 { |
|
687 lhs.d1 = 0; |
|
688 lhs.d2 = 0; |
|
689 } |
|
690 } |
|
691 else |
|
692 retval = 0; |
|
693 } |
|
694 } |
|
695 // idx_vector::freeze() printed an error message for us. |
|
696 } |
|
697 else if (lhs_nr == 1) |
|
698 { |
|
699 idx_vector *tmp = lhs.get_idx (); |
|
700 |
|
701 idx_vector idx = tmp[0]; |
|
702 |
2830
|
703 idx.freeze (lhs_nc, "vector", liboctave_rre_flag); |
1988
|
704 |
|
705 if (idx) |
|
706 { |
|
707 if (rhs_nr == 0 && rhs_nc == 0) |
3472
|
708 lhs.maybe_delete_elements (idx); |
1988
|
709 else |
|
710 { |
|
711 if (assign ((Array<LT>&) lhs, (Array<RT>&) rhs)) |
|
712 lhs.d2 = lhs.length (); |
|
713 else |
|
714 retval = 0; |
|
715 } |
|
716 } |
|
717 // idx_vector::freeze() printed an error message for us. |
|
718 } |
|
719 else if (lhs_nc == 1) |
|
720 { |
|
721 idx_vector *tmp = lhs.get_idx (); |
|
722 |
|
723 idx_vector idx = tmp[0]; |
|
724 |
2830
|
725 idx.freeze (lhs_nr, "vector", liboctave_rre_flag); |
1988
|
726 |
|
727 if (idx) |
|
728 { |
|
729 if (rhs_nr == 0 && rhs_nc == 0) |
3472
|
730 lhs.maybe_delete_elements (idx); |
1988
|
731 else |
|
732 { |
|
733 if (assign ((Array<LT>&) lhs, (Array<RT>&) rhs)) |
|
734 lhs.d1 = lhs.length (); |
|
735 else |
|
736 retval = 0; |
|
737 } |
|
738 } |
|
739 // idx_vector::freeze() printed an error message for us. |
|
740 } |
|
741 else if (liboctave_dfi_flag) |
|
742 { |
|
743 idx_vector *tmp = lhs.get_idx (); |
|
744 idx_vector idx = tmp[0]; |
|
745 |
2830
|
746 int len = idx.freeze (lhs_nr * lhs_nc, "matrix"); |
1988
|
747 |
|
748 if (idx) |
|
749 { |
3180
|
750 if (len == 0) |
|
751 { |
|
752 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
753 || (rhs_nr == 0 && rhs_nc == 0))) |
|
754 (*current_liboctave_error_handler) |
|
755 ("A([]) = X: X must be an empty matrix or scalar"); |
|
756 } |
|
757 else if (len == rhs_nr * rhs_nc) |
1988
|
758 { |
|
759 int k = 0; |
|
760 for (int j = 0; j < rhs_nc; j++) |
|
761 { |
|
762 for (int i = 0; i < rhs_nr; i++) |
|
763 { |
|
764 int ii = idx.elem (k++); |
|
765 int fr = ii % lhs_nr; |
|
766 int fc = (ii - fr) / lhs_nr; |
|
767 lhs.elem (fr, fc) = rhs.elem (i, j); |
|
768 } |
|
769 } |
|
770 } |
3180
|
771 else if (rhs_nr == 1 && rhs_nc == 1 && len <= lhs_nr * lhs_nc) |
3177
|
772 { |
3180
|
773 RT scalar = rhs.elem (0, 0); |
|
774 |
|
775 for (int i = 0; i < len; i++) |
|
776 { |
|
777 int ii = idx.elem (i); |
|
778 int fr = ii % lhs_nr; |
|
779 int fc = (ii - fr) / lhs_nr; |
|
780 lhs.elem (fr, fc) = scalar; |
|
781 } |
3177
|
782 } |
1988
|
783 else |
|
784 { |
|
785 (*current_liboctave_error_handler) |
|
786 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
787 |
|
788 retval = 0; |
|
789 } |
|
790 } |
|
791 // idx_vector::freeze() printed an error message for us. |
|
792 } |
|
793 else |
|
794 { |
|
795 (*current_liboctave_error_handler) |
|
796 ("single index only valid for row or column vector"); |
|
797 |
|
798 retval = 0; |
|
799 } |
|
800 } |
|
801 else |
|
802 { |
|
803 (*current_liboctave_error_handler) |
|
804 ("invalid number of indices for matrix expression"); |
|
805 |
|
806 retval = 0; |
|
807 } |
|
808 |
|
809 lhs.clear_index (); |
|
810 |
|
811 return retval; |
|
812 } |
|
813 |
|
814 /* |
|
815 ;;; Local Variables: *** |
|
816 ;;; mode: C++ *** |
|
817 ;;; End: *** |
|
818 */ |