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