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 { |
3513
|
46 return index (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> |
3513
|
59 Array2<T>::index (idx_vector& idx_arg) const |
2382
|
60 { |
|
61 Array2<T> retval; |
|
62 |
|
63 int nr = d1; |
|
64 int nc = d2; |
|
65 |
3513
|
66 int idx_orig_rows = idx_arg.orig_rows (); |
|
67 int idx_orig_columns = idx_arg.orig_columns (); |
3482
|
68 |
3725
|
69 if (idx_arg.is_colon ()) |
|
70 { |
|
71 // Fast magic colon processing. |
|
72 |
|
73 int result_nr = nr * nc; |
|
74 int result_nc = result_nr ? 1 : 0; |
|
75 |
|
76 retval = Array2<T> (*this, result_nr, result_nc); |
|
77 } |
|
78 else if (nr == 1 && nc == 1) |
2382
|
79 { |
3513
|
80 Array<T> tmp = Array<T>::index (idx_arg); |
2382
|
81 |
3722
|
82 if (tmp.length () != 0) |
|
83 retval = Array2<T> (tmp, idx_orig_rows, idx_orig_columns); |
|
84 else |
|
85 retval = Array2<T> (tmp, 0, 0); |
2382
|
86 } |
|
87 else if (nr == 1 || nc == 1) |
|
88 { |
3725
|
89 int result_is_column_vector = (nc == 1); |
2382
|
90 |
3513
|
91 Array<T> tmp = Array<T>::index (idx_arg); |
2382
|
92 |
|
93 int len = tmp.length (); |
|
94 |
|
95 if (len == 0) |
|
96 retval = Array2<T> (0, 0); |
|
97 else |
|
98 { |
|
99 if (result_is_column_vector) |
|
100 retval = Array2<T> (tmp, len, 1); |
|
101 else |
|
102 retval = Array2<T> (tmp, 1, len); |
|
103 } |
|
104 } |
3482
|
105 else if (liboctave_dfi_flag |
3513
|
106 || (idx_arg.one_zero_only () |
3482
|
107 && idx_orig_rows == nr |
|
108 && idx_orig_columns == nc)) |
2382
|
109 { |
|
110 // This code is only for indexing matrices. The vector |
|
111 // cases are handled above. |
|
112 |
3513
|
113 idx_arg.freeze (nr * nc, "matrix"); |
2382
|
114 |
3513
|
115 if (idx_arg) |
2382
|
116 { |
3482
|
117 int result_nr = idx_orig_rows; |
|
118 int result_nc = idx_orig_columns; |
2382
|
119 |
3725
|
120 if (idx_arg.one_zero_only ()) |
2382
|
121 { |
3513
|
122 result_nr = idx_arg.ones_count (); |
2382
|
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 { |
3513
|
133 int ii = idx_arg.elem (k++); |
2382
|
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 |
3513
|
216 Array2<T>::maybe_delete_elements (idx_vector& idx_arg) |
3472
|
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 |
3513
|
237 if (idx_arg.is_colon_equiv (n, 1)) |
3472
|
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 |
3513
|
248 idx_arg.sort (true); |
3472
|
249 |
3513
|
250 int num_to_delete = idx_arg.length (n); |
3472
|
251 |
|
252 if (num_to_delete != 0) |
|
253 { |
|
254 int new_n = n; |
|
255 |
3514
|
256 int iidx = 0; |
3472
|
257 |
|
258 for (int i = 0; i < n; i++) |
3514
|
259 if (i == idx_arg.elem (iidx)) |
3472
|
260 { |
3514
|
261 iidx++; |
3472
|
262 new_n--; |
|
263 |
3514
|
264 if (iidx == num_to_delete) |
3472
|
265 break; |
|
266 } |
|
267 |
|
268 if (new_n > 0) |
|
269 { |
|
270 T *new_data = new T [new_n]; |
|
271 |
|
272 int ii = 0; |
3514
|
273 iidx = 0; |
3472
|
274 for (int i = 0; i < n; i++) |
|
275 { |
3514
|
276 if (iidx < num_to_delete && i == idx_arg.elem (iidx)) |
|
277 iidx++; |
3472
|
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 |
3515
|
373 int iidx = 0; |
2245
|
374 |
|
375 for (int j = 0; j < nc; j++) |
3515
|
376 if (j == idx_j.elem (iidx)) |
2245
|
377 { |
3515
|
378 iidx++; |
2245
|
379 new_nc--; |
2917
|
380 |
3515
|
381 if (iidx == num_to_delete) |
2917
|
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; |
3515
|
390 iidx = 0; |
1988
|
391 for (int j = 0; j < nc; j++) |
|
392 { |
3515
|
393 if (iidx < num_to_delete && j == idx_j.elem (iidx)) |
|
394 iidx++; |
1988
|
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 |
3514
|
437 int iidx = 0; |
2245
|
438 |
|
439 for (int i = 0; i < nr; i++) |
3514
|
440 if (i == idx_i.elem (iidx)) |
2245
|
441 { |
3514
|
442 iidx++; |
2245
|
443 new_nr--; |
2917
|
444 |
3514
|
445 if (iidx == num_to_delete) |
2917
|
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; |
3514
|
454 iidx = 0; |
1988
|
455 for (int i = 0; i < nr; i++) |
|
456 { |
3514
|
457 if (iidx < num_to_delete && i == idx_i.elem (iidx)) |
|
458 iidx++; |
1988
|
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 \ |
3742
|
496 lhs.resize (new_nr, new_nc, static_cast<LT> (0)); \ |
2589
|
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 |
3482
|
515 idx_vector *tmp = lhs.get_idx (); |
|
516 |
|
517 idx_vector idx_i; |
|
518 idx_vector idx_j; |
|
519 |
|
520 if (n_idx > 1) |
|
521 idx_j = tmp[1]; |
|
522 |
|
523 if (n_idx > 0) |
|
524 idx_i = tmp[0]; |
|
525 |
1988
|
526 if (n_idx == 2) |
|
527 { |
2830
|
528 int n = idx_i.freeze (lhs_nr, "row", liboctave_rre_flag); |
1988
|
529 |
2830
|
530 int m = idx_j.freeze (lhs_nc, "column", liboctave_rre_flag); |
1988
|
531 |
|
532 int idx_i_is_colon = idx_i.is_colon (); |
|
533 int idx_j_is_colon = idx_j.is_colon (); |
|
534 |
|
535 if (idx_i_is_colon) |
2575
|
536 n = lhs_nr > 0 ? lhs_nr : rhs_nr; |
1988
|
537 |
|
538 if (idx_j_is_colon) |
2575
|
539 m = lhs_nc > 0 ? lhs_nc : rhs_nc; |
1988
|
540 |
|
541 if (idx_i && idx_j) |
|
542 { |
|
543 if (rhs_nr == 0 && rhs_nc == 0) |
|
544 { |
|
545 lhs.maybe_delete_elements (idx_i, idx_j); |
|
546 } |
|
547 else |
|
548 { |
2570
|
549 if (rhs_nr == 1 && rhs_nc == 1 && n > 0 && m > 0) |
1988
|
550 { |
2589
|
551 MAYBE_RESIZE_LHS; |
|
552 |
1988
|
553 RT scalar = rhs.elem (0, 0); |
|
554 |
|
555 for (int j = 0; j < m; j++) |
|
556 { |
|
557 int jj = idx_j.elem (j); |
|
558 for (int i = 0; i < n; i++) |
|
559 { |
|
560 int ii = idx_i.elem (i); |
|
561 lhs.elem (ii, jj) = scalar; |
|
562 } |
|
563 } |
|
564 } |
2570
|
565 else if (n == rhs_nr && m == rhs_nc) |
|
566 { |
2589
|
567 MAYBE_RESIZE_LHS; |
|
568 |
2570
|
569 for (int j = 0; j < m; j++) |
|
570 { |
|
571 int jj = idx_j.elem (j); |
|
572 for (int i = 0; i < n; i++) |
|
573 { |
|
574 int ii = idx_i.elem (i); |
|
575 lhs.elem (ii, jj) = rhs.elem (i, j); |
|
576 } |
|
577 } |
|
578 } |
3177
|
579 else if (n == 0 && m == 0) |
|
580 { |
|
581 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
582 || (rhs_nr == 0 && rhs_nc == 0))) |
|
583 { |
|
584 (*current_liboctave_error_handler) |
|
585 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
586 |
|
587 retval = 0; |
|
588 } |
|
589 } |
1988
|
590 else |
|
591 { |
|
592 (*current_liboctave_error_handler) |
|
593 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
594 (*current_liboctave_error_handler) |
|
595 ("match the number of rows in X and the number of elements in J must"); |
|
596 (*current_liboctave_error_handler) |
|
597 ("match the number of columns in X"); |
|
598 |
|
599 retval = 0; |
|
600 } |
|
601 } |
|
602 } |
|
603 // idx_vector::freeze() printed an error message for us. |
|
604 } |
|
605 else if (n_idx == 1) |
|
606 { |
2112
|
607 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
608 |
|
609 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
1988
|
610 { |
|
611 int lhs_len = lhs.length (); |
|
612 |
3482
|
613 int n = idx_i.freeze (lhs_len, 0, liboctave_rre_flag); |
1988
|
614 |
3482
|
615 if (idx_i) |
1988
|
616 { |
|
617 if (rhs_nr == 0 && rhs_nc == 0) |
|
618 { |
|
619 if (n != 0 && (lhs_nr != 0 || lhs_nc != 0)) |
3482
|
620 lhs.maybe_delete_elements (idx_i); |
1988
|
621 } |
2112
|
622 else if (! liboctave_dfi_flag && lhs_is_empty |
3482
|
623 && idx_i.is_colon () |
2112
|
624 && ! (rhs_nr == 1 || rhs_nc == 1)) |
|
625 { |
|
626 (*current_liboctave_error_handler) |
|
627 ("A(:) = X: X must be a vector"); |
|
628 } |
1988
|
629 else |
|
630 { |
|
631 if (assign ((Array<LT>&) lhs, (Array<RT>&) rhs)) |
|
632 { |
|
633 int len = lhs.length (); |
|
634 |
|
635 if (len > 0) |
|
636 { |
3482
|
637 int idx_nr = idx_i.orig_rows (); |
|
638 int idx_nc = idx_i.orig_columns (); |
1988
|
639 |
2112
|
640 // lhs_is_empty now means that lhs was |
3130
|
641 // *originally* empty, and lhs_len is the |
|
642 // *original* length of the lhs. |
2112
|
643 |
1988
|
644 if (liboctave_dfi_flag |
3130
|
645 || (idx_nr == 1 && idx_nc == 1) |
|
646 || (rhs_nr == 1 && rhs_nc == 1 && lhs_len == 1)) |
1988
|
647 { |
|
648 if (liboctave_pcv_flag) |
|
649 { |
|
650 lhs.d1 = lhs.length (); |
|
651 lhs.d2 = 1; |
|
652 } |
|
653 else |
|
654 { |
|
655 lhs.d1 = 1; |
|
656 lhs.d2 = lhs.length (); |
|
657 } |
|
658 } |
3482
|
659 else if (lhs_is_empty && idx_i.is_colon ()) |
2112
|
660 { |
|
661 lhs.d1 = rhs.d1; |
|
662 lhs.d2 = rhs.d2; |
|
663 } |
3680
|
664 else if (lhs_is_empty && idx_i.one_zero_only ()) |
|
665 { |
|
666 lhs.d1 = idx_nr; |
|
667 lhs.d2 = idx_nc; |
|
668 } |
3130
|
669 else if (rhs_nr == 1 |
|
670 && (idx_nr == 1 || lhs_len == 1)) |
1988
|
671 { |
|
672 lhs.d1 = 1; |
|
673 lhs.d2 = lhs.length (); |
|
674 } |
3130
|
675 else if (rhs_nc == 1 |
|
676 && (idx_nc == 1 || lhs_len == 1)) |
1988
|
677 { |
|
678 lhs.d1 = lhs.length (); |
|
679 lhs.d2 = 1; |
|
680 } |
3177
|
681 else if (idx_nr == 0 && idx_nc == 0) |
|
682 { |
|
683 if (! ((rhs.d1 == 1 && rhs.d2 == 1) |
|
684 || (rhs.d1 == 0 && rhs.d2 == 0))) |
|
685 (*current_liboctave_error_handler) |
|
686 ("A([]) = X: X must be an empty matrix or scalar"); |
|
687 } |
1988
|
688 else |
|
689 (*current_liboctave_error_handler) |
|
690 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
691 } |
|
692 else |
|
693 { |
|
694 lhs.d1 = 0; |
|
695 lhs.d2 = 0; |
|
696 } |
|
697 } |
|
698 else |
|
699 retval = 0; |
|
700 } |
|
701 } |
|
702 // idx_vector::freeze() printed an error message for us. |
|
703 } |
|
704 else if (lhs_nr == 1) |
|
705 { |
3482
|
706 idx_i.freeze (lhs_nc, "vector", liboctave_rre_flag); |
1988
|
707 |
3482
|
708 if (idx_i) |
1988
|
709 { |
|
710 if (rhs_nr == 0 && rhs_nc == 0) |
3482
|
711 lhs.maybe_delete_elements (idx_i); |
1988
|
712 else |
|
713 { |
|
714 if (assign ((Array<LT>&) lhs, (Array<RT>&) rhs)) |
|
715 lhs.d2 = lhs.length (); |
|
716 else |
|
717 retval = 0; |
|
718 } |
|
719 } |
|
720 // idx_vector::freeze() printed an error message for us. |
|
721 } |
|
722 else if (lhs_nc == 1) |
|
723 { |
3482
|
724 idx_i.freeze (lhs_nr, "vector", liboctave_rre_flag); |
1988
|
725 |
3482
|
726 if (idx_i) |
1988
|
727 { |
|
728 if (rhs_nr == 0 && rhs_nc == 0) |
3482
|
729 lhs.maybe_delete_elements (idx_i); |
1988
|
730 else |
|
731 { |
|
732 if (assign ((Array<LT>&) lhs, (Array<RT>&) rhs)) |
|
733 lhs.d1 = lhs.length (); |
|
734 else |
|
735 retval = 0; |
|
736 } |
|
737 } |
|
738 // idx_vector::freeze() printed an error message for us. |
|
739 } |
3482
|
740 else if (liboctave_dfi_flag |
|
741 || idx_i.is_colon () |
|
742 || (idx_i.one_zero_only () |
|
743 && idx_i.orig_rows () == lhs_nr |
|
744 && idx_i.orig_columns () == lhs_nc)) |
1988
|
745 { |
3482
|
746 int len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
1988
|
747 |
3482
|
748 if (idx_i) |
1988
|
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 { |
3482
|
764 int ii = idx_i.elem (k++); |
1988
|
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 { |
3482
|
777 int ii = idx_i.elem (i); |
3180
|
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 */ |