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 |
2382
|
69 if (nr == 1 && nc == 1) |
|
70 { |
3513
|
71 Array<T> tmp = Array<T>::index (idx_arg); |
2382
|
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 { |
3513
|
87 int result_is_column_vector = (nc == 1 || idx_arg.is_colon ()); |
2382
|
88 |
3513
|
89 Array<T> tmp = Array<T>::index (idx_arg); |
2382
|
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 } |
3482
|
103 else if (liboctave_dfi_flag |
3513
|
104 || idx_arg.is_colon () |
|
105 || (idx_arg.one_zero_only () |
3482
|
106 && idx_orig_rows == nr |
|
107 && idx_orig_columns == nc)) |
2382
|
108 { |
|
109 // This code is only for indexing matrices. The vector |
|
110 // cases are handled above. |
|
111 |
3513
|
112 idx_arg.freeze (nr * nc, "matrix"); |
2382
|
113 |
3513
|
114 if (idx_arg) |
2382
|
115 { |
3482
|
116 int result_nr = idx_orig_rows; |
|
117 int result_nc = idx_orig_columns; |
2382
|
118 |
3513
|
119 if (idx_arg.is_colon ()) |
2382
|
120 { |
|
121 result_nr = nr * nc; |
3050
|
122 result_nc = result_nr ? 1 : 0; |
2382
|
123 } |
3513
|
124 else if (idx_arg.one_zero_only ()) |
2382
|
125 { |
3513
|
126 result_nr = idx_arg.ones_count (); |
2382
|
127 result_nc = (result_nr > 0 ? 1 : 0); |
|
128 } |
|
129 |
|
130 retval.resize (result_nr, result_nc); |
|
131 |
|
132 int k = 0; |
|
133 for (int j = 0; j < result_nc; j++) |
|
134 { |
|
135 for (int i = 0; i < result_nr; i++) |
|
136 { |
3513
|
137 int ii = idx_arg.elem (k++); |
2382
|
138 int fr = ii % nr; |
|
139 int fc = (ii - fr) / nr; |
|
140 retval.elem (i, j) = elem (fr, fc); |
|
141 } |
|
142 } |
|
143 } |
|
144 // idx_vector::freeze() printed an error message for us. |
|
145 } |
|
146 else |
|
147 (*current_liboctave_error_handler) |
|
148 ("single index only valid for row or column vector"); |
|
149 |
|
150 return retval; |
|
151 } |
|
152 |
|
153 template <class T> |
|
154 Array2<T> |
|
155 Array2<T>::index (idx_vector& idx_i, idx_vector& idx_j) const |
|
156 { |
|
157 Array2<T> retval; |
|
158 |
|
159 int nr = d1; |
|
160 int nc = d2; |
|
161 |
2830
|
162 int n = idx_i.freeze (nr, "row"); |
|
163 int m = idx_j.freeze (nc, "column"); |
2382
|
164 |
|
165 if (idx_i && idx_j) |
|
166 { |
2663
|
167 if (idx_i.orig_empty () || idx_j.orig_empty ()) |
2382
|
168 { |
2663
|
169 retval.resize (n, m); |
|
170 } |
|
171 else if (n == 0) |
|
172 { |
2673
|
173 if (m == 0) |
2382
|
174 retval.resize (0, 0); |
2663
|
175 else if (idx_j.is_colon_equiv (nc, 1)) |
|
176 retval.resize (0, nc); |
3164
|
177 else if (idx_i.is_colon_equiv (nr, 1)) |
|
178 retval.resize (0, m); |
2382
|
179 else |
2663
|
180 (*current_liboctave_error_handler) ("invalid row index = 0"); |
2382
|
181 } |
|
182 else if (m == 0) |
|
183 { |
2663
|
184 if (n == 0) |
2382
|
185 retval.resize (0, 0); |
2663
|
186 else if (idx_i.is_colon_equiv (nr, 1)) |
|
187 retval.resize (nr, 0); |
3164
|
188 else if (idx_j.is_colon_equiv (nc, 1)) |
|
189 retval.resize (n, 0); |
2382
|
190 else |
2663
|
191 (*current_liboctave_error_handler) ("invalid column index = 0"); |
2382
|
192 } |
|
193 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) |
|
194 { |
|
195 retval = *this; |
|
196 } |
|
197 else |
|
198 { |
|
199 retval.resize (n, m); |
|
200 |
|
201 for (int j = 0; j < m; j++) |
|
202 { |
|
203 int jj = idx_j.elem (j); |
|
204 for (int i = 0; i < n; i++) |
|
205 { |
|
206 int ii = idx_i.elem (i); |
|
207 retval.elem (i, j) = elem (ii, jj); |
|
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 |
|
293 if (--rep->count <= 0) |
|
294 delete rep; |
|
295 |
|
296 rep = new ArrayRep (new_data, new_n); |
|
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 |
|
407 if (--rep->count <= 0) |
|
408 delete rep; |
|
409 |
|
410 rep = new ArrayRep (new_data, nr * new_nc); |
|
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 |
|
471 if (--rep->count <= 0) |
|
472 delete rep; |
|
473 |
|
474 rep = new ArrayRep (new_data, new_nr * nc); |
|
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 \ |
|
500 lhs.resize (new_nr, new_nc, 0.0); \ |
|
501 } \ |
|
502 } \ |
|
503 while (0) |
|
504 |
1988
|
505 template <class LT, class RT> |
|
506 int |
|
507 assign (Array2<LT>& lhs, const Array2<RT>& rhs) |
|
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 { |
2589
|
571 MAYBE_RESIZE_LHS; |
|
572 |
2570
|
573 for (int j = 0; j < m; j++) |
|
574 { |
|
575 int jj = idx_j.elem (j); |
|
576 for (int i = 0; i < n; i++) |
|
577 { |
|
578 int ii = idx_i.elem (i); |
|
579 lhs.elem (ii, jj) = rhs.elem (i, j); |
|
580 } |
|
581 } |
|
582 } |
3177
|
583 else if (n == 0 && m == 0) |
|
584 { |
|
585 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
586 || (rhs_nr == 0 && rhs_nc == 0))) |
|
587 { |
|
588 (*current_liboctave_error_handler) |
|
589 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
590 |
|
591 retval = 0; |
|
592 } |
|
593 } |
1988
|
594 else |
|
595 { |
|
596 (*current_liboctave_error_handler) |
|
597 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
598 (*current_liboctave_error_handler) |
|
599 ("match the number of rows in X and the number of elements in J must"); |
|
600 (*current_liboctave_error_handler) |
|
601 ("match the number of columns in X"); |
|
602 |
|
603 retval = 0; |
|
604 } |
|
605 } |
|
606 } |
|
607 // idx_vector::freeze() printed an error message for us. |
|
608 } |
|
609 else if (n_idx == 1) |
|
610 { |
2112
|
611 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
612 |
|
613 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
1988
|
614 { |
|
615 int lhs_len = lhs.length (); |
|
616 |
3482
|
617 int n = idx_i.freeze (lhs_len, 0, liboctave_rre_flag); |
1988
|
618 |
3482
|
619 if (idx_i) |
1988
|
620 { |
|
621 if (rhs_nr == 0 && rhs_nc == 0) |
|
622 { |
|
623 if (n != 0 && (lhs_nr != 0 || lhs_nc != 0)) |
3482
|
624 lhs.maybe_delete_elements (idx_i); |
1988
|
625 } |
2112
|
626 else if (! liboctave_dfi_flag && lhs_is_empty |
3482
|
627 && idx_i.is_colon () |
2112
|
628 && ! (rhs_nr == 1 || rhs_nc == 1)) |
|
629 { |
|
630 (*current_liboctave_error_handler) |
|
631 ("A(:) = X: X must be a vector"); |
|
632 } |
1988
|
633 else |
|
634 { |
|
635 if (assign ((Array<LT>&) lhs, (Array<RT>&) rhs)) |
|
636 { |
|
637 int len = lhs.length (); |
|
638 |
|
639 if (len > 0) |
|
640 { |
3482
|
641 int idx_nr = idx_i.orig_rows (); |
|
642 int idx_nc = idx_i.orig_columns (); |
1988
|
643 |
2112
|
644 // lhs_is_empty now means that lhs was |
3130
|
645 // *originally* empty, and lhs_len is the |
|
646 // *original* length of the lhs. |
2112
|
647 |
1988
|
648 if (liboctave_dfi_flag |
3130
|
649 || (idx_nr == 1 && idx_nc == 1) |
|
650 || (rhs_nr == 1 && rhs_nc == 1 && lhs_len == 1)) |
1988
|
651 { |
|
652 if (liboctave_pcv_flag) |
|
653 { |
|
654 lhs.d1 = lhs.length (); |
|
655 lhs.d2 = 1; |
|
656 } |
|
657 else |
|
658 { |
|
659 lhs.d1 = 1; |
|
660 lhs.d2 = lhs.length (); |
|
661 } |
|
662 } |
3482
|
663 else if (lhs_is_empty && idx_i.is_colon ()) |
2112
|
664 { |
|
665 lhs.d1 = rhs.d1; |
|
666 lhs.d2 = rhs.d2; |
|
667 } |
3130
|
668 else if (rhs_nr == 1 |
|
669 && (idx_nr == 1 || lhs_len == 1)) |
1988
|
670 { |
|
671 lhs.d1 = 1; |
|
672 lhs.d2 = lhs.length (); |
|
673 } |
3130
|
674 else if (rhs_nc == 1 |
|
675 && (idx_nc == 1 || lhs_len == 1)) |
1988
|
676 { |
|
677 lhs.d1 = lhs.length (); |
|
678 lhs.d2 = 1; |
|
679 } |
3177
|
680 else if (idx_nr == 0 && idx_nc == 0) |
|
681 { |
|
682 if (! ((rhs.d1 == 1 && rhs.d2 == 1) |
|
683 || (rhs.d1 == 0 && rhs.d2 == 0))) |
|
684 (*current_liboctave_error_handler) |
|
685 ("A([]) = X: X must be an empty matrix or scalar"); |
|
686 } |
1988
|
687 else |
|
688 (*current_liboctave_error_handler) |
|
689 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
690 } |
|
691 else |
|
692 { |
|
693 lhs.d1 = 0; |
|
694 lhs.d2 = 0; |
|
695 } |
|
696 } |
|
697 else |
|
698 retval = 0; |
|
699 } |
|
700 } |
|
701 // idx_vector::freeze() printed an error message for us. |
|
702 } |
|
703 else if (lhs_nr == 1) |
|
704 { |
3482
|
705 idx_i.freeze (lhs_nc, "vector", liboctave_rre_flag); |
1988
|
706 |
3482
|
707 if (idx_i) |
1988
|
708 { |
|
709 if (rhs_nr == 0 && rhs_nc == 0) |
3482
|
710 lhs.maybe_delete_elements (idx_i); |
1988
|
711 else |
|
712 { |
|
713 if (assign ((Array<LT>&) lhs, (Array<RT>&) rhs)) |
|
714 lhs.d2 = lhs.length (); |
|
715 else |
|
716 retval = 0; |
|
717 } |
|
718 } |
|
719 // idx_vector::freeze() printed an error message for us. |
|
720 } |
|
721 else if (lhs_nc == 1) |
|
722 { |
3482
|
723 idx_i.freeze (lhs_nr, "vector", liboctave_rre_flag); |
1988
|
724 |
3482
|
725 if (idx_i) |
1988
|
726 { |
|
727 if (rhs_nr == 0 && rhs_nc == 0) |
3482
|
728 lhs.maybe_delete_elements (idx_i); |
1988
|
729 else |
|
730 { |
|
731 if (assign ((Array<LT>&) lhs, (Array<RT>&) rhs)) |
|
732 lhs.d1 = lhs.length (); |
|
733 else |
|
734 retval = 0; |
|
735 } |
|
736 } |
|
737 // idx_vector::freeze() printed an error message for us. |
|
738 } |
3482
|
739 else if (liboctave_dfi_flag |
|
740 || idx_i.is_colon () |
|
741 || (idx_i.one_zero_only () |
|
742 && idx_i.orig_rows () == lhs_nr |
|
743 && idx_i.orig_columns () == lhs_nc)) |
1988
|
744 { |
3482
|
745 int len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
1988
|
746 |
3482
|
747 if (idx_i) |
1988
|
748 { |
3180
|
749 if (len == 0) |
|
750 { |
|
751 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
752 || (rhs_nr == 0 && rhs_nc == 0))) |
|
753 (*current_liboctave_error_handler) |
|
754 ("A([]) = X: X must be an empty matrix or scalar"); |
|
755 } |
|
756 else if (len == rhs_nr * rhs_nc) |
1988
|
757 { |
|
758 int k = 0; |
|
759 for (int j = 0; j < rhs_nc; j++) |
|
760 { |
|
761 for (int i = 0; i < rhs_nr; i++) |
|
762 { |
3482
|
763 int ii = idx_i.elem (k++); |
1988
|
764 int fr = ii % lhs_nr; |
|
765 int fc = (ii - fr) / lhs_nr; |
|
766 lhs.elem (fr, fc) = rhs.elem (i, j); |
|
767 } |
|
768 } |
|
769 } |
3180
|
770 else if (rhs_nr == 1 && rhs_nc == 1 && len <= lhs_nr * lhs_nc) |
3177
|
771 { |
3180
|
772 RT scalar = rhs.elem (0, 0); |
|
773 |
|
774 for (int i = 0; i < len; i++) |
|
775 { |
3482
|
776 int ii = idx_i.elem (i); |
3180
|
777 int fr = ii % lhs_nr; |
|
778 int fc = (ii - fr) / lhs_nr; |
|
779 lhs.elem (fr, fc) = scalar; |
|
780 } |
3177
|
781 } |
1988
|
782 else |
|
783 { |
|
784 (*current_liboctave_error_handler) |
|
785 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
786 |
|
787 retval = 0; |
|
788 } |
|
789 } |
|
790 // idx_vector::freeze() printed an error message for us. |
|
791 } |
|
792 else |
|
793 { |
|
794 (*current_liboctave_error_handler) |
|
795 ("single index only valid for row or column vector"); |
|
796 |
|
797 retval = 0; |
|
798 } |
|
799 } |
|
800 else |
|
801 { |
|
802 (*current_liboctave_error_handler) |
|
803 ("invalid number of indices for matrix expression"); |
|
804 |
|
805 retval = 0; |
|
806 } |
|
807 |
|
808 lhs.clear_index (); |
|
809 |
|
810 return retval; |
|
811 } |
|
812 |
|
813 /* |
|
814 ;;; Local Variables: *** |
|
815 ;;; mode: C++ *** |
|
816 ;;; End: *** |
|
817 */ |