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 { |
|
87 int result_is_column_vector = (nc == 1); |
|
88 |
|
89 if (liboctave_dfi_flag && idx.is_colon ()) |
|
90 result_is_column_vector = 1; |
|
91 |
|
92 Array<T> tmp = Array<T>::index (idx); |
|
93 |
|
94 int len = tmp.length (); |
|
95 |
|
96 if (len == 0) |
|
97 retval = Array2<T> (0, 0); |
|
98 else |
|
99 { |
|
100 if (result_is_column_vector) |
|
101 retval = Array2<T> (tmp, len, 1); |
|
102 else |
|
103 retval = Array2<T> (tmp, 1, len); |
|
104 } |
|
105 } |
|
106 else if (liboctave_dfi_flag) |
|
107 { |
|
108 // This code is only for indexing matrices. The vector |
|
109 // cases are handled above. |
|
110 |
2830
|
111 idx.freeze (nr * nc, "matrix"); |
2382
|
112 |
|
113 if (idx) |
|
114 { |
|
115 int result_nr = idx.orig_rows (); |
|
116 int result_nc = idx.orig_columns (); |
|
117 |
|
118 if (idx.is_colon ()) |
|
119 { |
|
120 result_nr = nr * nc; |
3050
|
121 result_nc = result_nr ? 1 : 0; |
2382
|
122 } |
|
123 else if (idx.one_zero_only ()) |
|
124 { |
|
125 result_nr = idx.ones_count (); |
|
126 result_nc = (result_nr > 0 ? 1 : 0); |
|
127 } |
|
128 |
|
129 retval.resize (result_nr, result_nc); |
|
130 |
|
131 int k = 0; |
|
132 for (int j = 0; j < result_nc; j++) |
|
133 { |
|
134 for (int i = 0; i < result_nr; i++) |
|
135 { |
|
136 int ii = idx.elem (k++); |
|
137 int fr = ii % nr; |
|
138 int fc = (ii - fr) / nr; |
|
139 retval.elem (i, j) = elem (fr, fc); |
|
140 } |
|
141 } |
|
142 } |
|
143 // idx_vector::freeze() printed an error message for us. |
|
144 } |
|
145 else |
|
146 (*current_liboctave_error_handler) |
|
147 ("single index only valid for row or column vector"); |
|
148 |
|
149 return retval; |
|
150 } |
|
151 |
|
152 template <class T> |
|
153 Array2<T> |
|
154 Array2<T>::index (idx_vector& idx_i, idx_vector& idx_j) const |
|
155 { |
|
156 Array2<T> retval; |
|
157 |
|
158 int nr = d1; |
|
159 int nc = d2; |
|
160 |
2830
|
161 int n = idx_i.freeze (nr, "row"); |
|
162 int m = idx_j.freeze (nc, "column"); |
2382
|
163 |
|
164 if (idx_i && idx_j) |
|
165 { |
2663
|
166 if (idx_i.orig_empty () || idx_j.orig_empty ()) |
2382
|
167 { |
2663
|
168 retval.resize (n, m); |
|
169 } |
|
170 else if (n == 0) |
|
171 { |
2673
|
172 if (m == 0) |
2382
|
173 retval.resize (0, 0); |
2663
|
174 else if (idx_j.is_colon_equiv (nc, 1)) |
|
175 retval.resize (0, nc); |
2382
|
176 else |
2663
|
177 (*current_liboctave_error_handler) ("invalid row index = 0"); |
2382
|
178 } |
|
179 else if (m == 0) |
|
180 { |
2663
|
181 if (n == 0) |
2382
|
182 retval.resize (0, 0); |
2663
|
183 else if (idx_i.is_colon_equiv (nr, 1)) |
|
184 retval.resize (nr, 0); |
2382
|
185 else |
2663
|
186 (*current_liboctave_error_handler) ("invalid column index = 0"); |
2382
|
187 } |
|
188 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) |
|
189 { |
|
190 retval = *this; |
|
191 } |
|
192 else |
|
193 { |
|
194 retval.resize (n, m); |
|
195 |
|
196 for (int j = 0; j < m; j++) |
|
197 { |
|
198 int jj = idx_j.elem (j); |
|
199 for (int i = 0; i < n; i++) |
|
200 { |
|
201 int ii = idx_i.elem (i); |
|
202 retval.elem (i, j) = elem (ii, jj); |
|
203 } |
|
204 } |
|
205 } |
|
206 } |
|
207 |
|
208 // idx_vector::freeze() printed an error message for us. |
|
209 |
|
210 return retval; |
|
211 } |
|
212 |
|
213 template <class T> |
1988
|
214 void |
|
215 Array2<T>::maybe_delete_elements (idx_vector& idx_i, idx_vector& idx_j) |
|
216 { |
|
217 int nr = d1; |
|
218 int nc = d2; |
|
219 |
|
220 if (nr == 0 && nc == 0) |
|
221 return; |
|
222 |
|
223 if (idx_i.is_colon_equiv (nr, 1)) |
|
224 { |
|
225 if (idx_j.is_colon_equiv (nc, 1)) |
|
226 resize (0, 0); |
|
227 else |
|
228 { |
3079
|
229 idx_j.sort (true); |
|
230 |
1988
|
231 int num_to_delete = idx_j.length (nc); |
|
232 |
|
233 if (num_to_delete != 0) |
|
234 { |
|
235 if (nr == 1 && num_to_delete == nc) |
|
236 resize (0, 0); |
|
237 else |
|
238 { |
2245
|
239 int new_nc = nc; |
|
240 |
|
241 int idx = 0; |
|
242 |
|
243 for (int j = 0; j < nc; j++) |
|
244 if (j == idx_j.elem (idx)) |
|
245 { |
|
246 idx++; |
|
247 new_nc--; |
2917
|
248 |
|
249 if (idx == num_to_delete) |
|
250 break; |
2245
|
251 } |
|
252 |
1988
|
253 if (new_nc > 0) |
|
254 { |
|
255 T *new_data = new T [nr * new_nc]; |
|
256 |
|
257 int jj = 0; |
2245
|
258 idx = 0; |
1988
|
259 for (int j = 0; j < nc; j++) |
|
260 { |
2917
|
261 if (idx < num_to_delete && j == idx_j.elem (idx)) |
1988
|
262 idx++; |
|
263 else |
|
264 { |
|
265 for (int i = 0; i < nr; i++) |
|
266 new_data[nr*jj+i] = elem (i, j); |
|
267 jj++; |
|
268 } |
|
269 } |
|
270 |
|
271 if (--rep->count <= 0) |
|
272 delete rep; |
|
273 |
|
274 rep = new ArrayRep (new_data, nr * new_nc); |
|
275 |
|
276 d2 = new_nc; |
|
277 |
|
278 set_max_indices (2); |
|
279 } |
|
280 else |
|
281 (*current_liboctave_error_handler) |
|
282 ("A(idx) = []: index out of range"); |
|
283 } |
|
284 } |
|
285 } |
|
286 } |
2714
|
287 else if (idx_j.is_colon_equiv (nc, 1)) |
1988
|
288 { |
2714
|
289 if (idx_i.is_colon_equiv (nr, 1)) |
1988
|
290 resize (0, 0); |
|
291 else |
|
292 { |
3079
|
293 idx_i.sort (true); |
|
294 |
1988
|
295 int num_to_delete = idx_i.length (nr); |
|
296 |
|
297 if (num_to_delete != 0) |
|
298 { |
|
299 if (nc == 1 && num_to_delete == nr) |
|
300 resize (0, 0); |
|
301 else |
|
302 { |
2245
|
303 int new_nr = nr; |
|
304 |
|
305 int idx = 0; |
|
306 |
|
307 for (int i = 0; i < nr; i++) |
|
308 if (i == idx_i.elem (idx)) |
|
309 { |
|
310 idx++; |
|
311 new_nr--; |
2917
|
312 |
|
313 if (idx == num_to_delete) |
|
314 break; |
2245
|
315 } |
|
316 |
1988
|
317 if (new_nr > 0) |
|
318 { |
|
319 T *new_data = new T [new_nr * nc]; |
|
320 |
|
321 int ii = 0; |
2245
|
322 idx = 0; |
1988
|
323 for (int i = 0; i < nr; i++) |
|
324 { |
2917
|
325 if (idx < num_to_delete && i == idx_i.elem (idx)) |
1988
|
326 idx++; |
|
327 else |
|
328 { |
|
329 for (int j = 0; j < nc; j++) |
|
330 new_data[new_nr*j+ii] = elem (i, j); |
|
331 ii++; |
|
332 } |
|
333 } |
|
334 |
|
335 if (--rep->count <= 0) |
|
336 delete rep; |
|
337 |
|
338 rep = new ArrayRep (new_data, new_nr * nc); |
|
339 |
|
340 d1 = new_nr; |
|
341 |
|
342 set_max_indices (2); |
|
343 } |
|
344 else |
|
345 (*current_liboctave_error_handler) |
|
346 ("A(idx) = []: index out of range"); |
|
347 } |
|
348 } |
|
349 } |
|
350 } |
|
351 } |
|
352 |
2589
|
353 #define MAYBE_RESIZE_LHS \ |
|
354 do \ |
|
355 { \ |
|
356 if (liboctave_rre_flag) \ |
|
357 { \ |
|
358 int max_row_idx = idx_i_is_colon ? rhs_nr : idx_i.max () + 1; \ |
|
359 int max_col_idx = idx_j_is_colon ? rhs_nc : idx_j.max () + 1; \ |
|
360 \ |
|
361 int new_nr = max_row_idx > lhs_nr ? max_row_idx : lhs_nr; \ |
|
362 int new_nc = max_col_idx > lhs_nc ? max_col_idx : lhs_nc; \ |
|
363 \ |
|
364 lhs.resize (new_nr, new_nc, 0.0); \ |
|
365 } \ |
|
366 } \ |
|
367 while (0) |
|
368 |
1988
|
369 template <class LT, class RT> |
|
370 int |
|
371 assign (Array2<LT>& lhs, const Array2<RT>& rhs) |
|
372 { |
|
373 int retval = 1; |
|
374 |
|
375 int n_idx = lhs.index_count (); |
|
376 |
|
377 int lhs_nr = lhs.rows (); |
|
378 int lhs_nc = lhs.cols (); |
|
379 |
|
380 int rhs_nr = rhs.rows (); |
|
381 int rhs_nc = rhs.cols (); |
|
382 |
|
383 if (n_idx == 2) |
|
384 { |
|
385 idx_vector *tmp = lhs.get_idx (); |
|
386 |
|
387 idx_vector idx_i = tmp[0]; |
|
388 idx_vector idx_j = tmp[1]; |
|
389 |
2830
|
390 int n = idx_i.freeze (lhs_nr, "row", liboctave_rre_flag); |
1988
|
391 |
2830
|
392 int m = idx_j.freeze (lhs_nc, "column", liboctave_rre_flag); |
1988
|
393 |
|
394 int idx_i_is_colon = idx_i.is_colon (); |
|
395 int idx_j_is_colon = idx_j.is_colon (); |
|
396 |
|
397 if (idx_i_is_colon) |
2575
|
398 n = lhs_nr > 0 ? lhs_nr : rhs_nr; |
1988
|
399 |
|
400 if (idx_j_is_colon) |
2575
|
401 m = lhs_nc > 0 ? lhs_nc : rhs_nc; |
1988
|
402 |
|
403 if (idx_i && idx_j) |
|
404 { |
|
405 if (rhs_nr == 0 && rhs_nc == 0) |
|
406 { |
|
407 lhs.maybe_delete_elements (idx_i, idx_j); |
|
408 } |
|
409 else |
|
410 { |
2570
|
411 if (rhs_nr == 1 && rhs_nc == 1 && n > 0 && m > 0) |
1988
|
412 { |
2589
|
413 MAYBE_RESIZE_LHS; |
|
414 |
1988
|
415 RT scalar = rhs.elem (0, 0); |
|
416 |
|
417 for (int j = 0; j < m; j++) |
|
418 { |
|
419 int jj = idx_j.elem (j); |
|
420 for (int i = 0; i < n; i++) |
|
421 { |
|
422 int ii = idx_i.elem (i); |
|
423 lhs.elem (ii, jj) = scalar; |
|
424 } |
|
425 } |
|
426 } |
2570
|
427 else if (n == rhs_nr && m == rhs_nc) |
|
428 { |
2589
|
429 MAYBE_RESIZE_LHS; |
|
430 |
2570
|
431 for (int j = 0; j < m; j++) |
|
432 { |
|
433 int jj = idx_j.elem (j); |
|
434 for (int i = 0; i < n; i++) |
|
435 { |
|
436 int ii = idx_i.elem (i); |
|
437 lhs.elem (ii, jj) = rhs.elem (i, j); |
|
438 } |
|
439 } |
|
440 } |
1988
|
441 else |
|
442 { |
|
443 (*current_liboctave_error_handler) |
|
444 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
445 (*current_liboctave_error_handler) |
|
446 ("match the number of rows in X and the number of elements in J must"); |
|
447 (*current_liboctave_error_handler) |
|
448 ("match the number of columns in X"); |
|
449 |
|
450 retval = 0; |
|
451 } |
|
452 } |
|
453 } |
|
454 // idx_vector::freeze() printed an error message for us. |
|
455 } |
|
456 else if (n_idx == 1) |
|
457 { |
2112
|
458 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
459 |
|
460 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
1988
|
461 { |
|
462 idx_vector *tmp = lhs.get_idx (); |
|
463 |
|
464 idx_vector idx = tmp[0]; |
|
465 |
|
466 int lhs_len = lhs.length (); |
|
467 |
2830
|
468 int n = idx.freeze (lhs_len, 0, liboctave_rre_flag); |
1988
|
469 |
|
470 if (idx) |
|
471 { |
|
472 if (rhs_nr == 0 && rhs_nc == 0) |
|
473 { |
|
474 if (n != 0 && (lhs_nr != 0 || lhs_nc != 0)) |
|
475 { |
|
476 idx_vector tmp (':'); |
|
477 lhs.maybe_delete_elements (idx, tmp); |
|
478 } |
|
479 } |
2112
|
480 else if (! liboctave_dfi_flag && lhs_is_empty |
|
481 && idx.is_colon () |
|
482 && ! (rhs_nr == 1 || rhs_nc == 1)) |
|
483 { |
|
484 (*current_liboctave_error_handler) |
|
485 ("A(:) = X: X must be a vector"); |
|
486 } |
1988
|
487 else |
|
488 { |
|
489 if (assign ((Array<LT>&) lhs, (Array<RT>&) rhs)) |
|
490 { |
|
491 int len = lhs.length (); |
|
492 |
|
493 if (len > 0) |
|
494 { |
|
495 int idx_nr = idx.orig_rows (); |
|
496 int idx_nc = idx.orig_columns (); |
|
497 |
2112
|
498 // lhs_is_empty now means that lhs was |
3130
|
499 // *originally* empty, and lhs_len is the |
|
500 // *original* length of the lhs. |
2112
|
501 |
1988
|
502 if (liboctave_dfi_flag |
3130
|
503 || (idx_nr == 1 && idx_nc == 1) |
|
504 || (rhs_nr == 1 && rhs_nc == 1 && lhs_len == 1)) |
1988
|
505 { |
|
506 if (liboctave_pcv_flag) |
|
507 { |
|
508 lhs.d1 = lhs.length (); |
|
509 lhs.d2 = 1; |
|
510 } |
|
511 else |
|
512 { |
|
513 lhs.d1 = 1; |
|
514 lhs.d2 = lhs.length (); |
|
515 } |
|
516 } |
2112
|
517 else if (lhs_is_empty && idx.is_colon ()) |
|
518 { |
|
519 lhs.d1 = rhs.d1; |
|
520 lhs.d2 = rhs.d2; |
|
521 } |
3130
|
522 else if (rhs_nr == 1 |
|
523 && (idx_nr == 1 || lhs_len == 1)) |
1988
|
524 { |
|
525 lhs.d1 = 1; |
|
526 lhs.d2 = lhs.length (); |
|
527 } |
3130
|
528 else if (rhs_nc == 1 |
|
529 && (idx_nc == 1 || lhs_len == 1)) |
1988
|
530 { |
|
531 lhs.d1 = lhs.length (); |
|
532 lhs.d2 = 1; |
|
533 } |
|
534 else |
|
535 (*current_liboctave_error_handler) |
|
536 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
537 } |
|
538 else |
|
539 { |
|
540 lhs.d1 = 0; |
|
541 lhs.d2 = 0; |
|
542 } |
|
543 } |
|
544 else |
|
545 retval = 0; |
|
546 } |
|
547 } |
|
548 // idx_vector::freeze() printed an error message for us. |
|
549 } |
|
550 else if (lhs_nr == 1) |
|
551 { |
|
552 idx_vector *tmp = lhs.get_idx (); |
|
553 |
|
554 idx_vector idx = tmp[0]; |
|
555 |
2830
|
556 idx.freeze (lhs_nc, "vector", liboctave_rre_flag); |
1988
|
557 |
|
558 if (idx) |
|
559 { |
|
560 if (rhs_nr == 0 && rhs_nc == 0) |
|
561 { |
|
562 idx_vector tmp (':'); |
|
563 lhs.maybe_delete_elements (tmp, idx); |
|
564 } |
|
565 else |
|
566 { |
|
567 if (assign ((Array<LT>&) lhs, (Array<RT>&) rhs)) |
|
568 lhs.d2 = lhs.length (); |
|
569 else |
|
570 retval = 0; |
|
571 } |
|
572 } |
|
573 // idx_vector::freeze() printed an error message for us. |
|
574 } |
|
575 else if (lhs_nc == 1) |
|
576 { |
|
577 idx_vector *tmp = lhs.get_idx (); |
|
578 |
|
579 idx_vector idx = tmp[0]; |
|
580 |
2830
|
581 idx.freeze (lhs_nr, "vector", liboctave_rre_flag); |
1988
|
582 |
|
583 if (idx) |
|
584 { |
|
585 if (rhs_nr == 0 && rhs_nc == 0) |
|
586 { |
|
587 idx_vector tmp (':'); |
|
588 lhs.maybe_delete_elements (idx, tmp); |
|
589 } |
|
590 else |
|
591 { |
|
592 if (assign ((Array<LT>&) lhs, (Array<RT>&) rhs)) |
|
593 lhs.d1 = lhs.length (); |
|
594 else |
|
595 retval = 0; |
|
596 } |
|
597 } |
|
598 // idx_vector::freeze() printed an error message for us. |
|
599 } |
|
600 else if (liboctave_dfi_flag) |
|
601 { |
|
602 idx_vector *tmp = lhs.get_idx (); |
|
603 idx_vector idx = tmp[0]; |
|
604 |
2830
|
605 int len = idx.freeze (lhs_nr * lhs_nc, "matrix"); |
1988
|
606 |
|
607 if (idx) |
|
608 { |
|
609 if (len == rhs_nr * rhs_nc) |
|
610 { |
|
611 int k = 0; |
|
612 for (int j = 0; j < rhs_nc; j++) |
|
613 { |
|
614 for (int i = 0; i < rhs_nr; i++) |
|
615 { |
|
616 int ii = idx.elem (k++); |
|
617 int fr = ii % lhs_nr; |
|
618 int fc = (ii - fr) / lhs_nr; |
|
619 lhs.elem (fr, fc) = rhs.elem (i, j); |
|
620 } |
|
621 } |
|
622 } |
|
623 else |
|
624 { |
|
625 (*current_liboctave_error_handler) |
|
626 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
627 |
|
628 retval = 0; |
|
629 } |
|
630 } |
|
631 // idx_vector::freeze() printed an error message for us. |
|
632 } |
|
633 else |
|
634 { |
|
635 (*current_liboctave_error_handler) |
|
636 ("single index only valid for row or column vector"); |
|
637 |
|
638 retval = 0; |
|
639 } |
|
640 } |
|
641 else |
|
642 { |
|
643 (*current_liboctave_error_handler) |
|
644 ("invalid number of indices for matrix expression"); |
|
645 |
|
646 retval = 0; |
|
647 } |
|
648 |
|
649 lhs.clear_index (); |
|
650 |
|
651 return retval; |
|
652 } |
|
653 |
|
654 /* |
|
655 ;;; Local Variables: *** |
|
656 ;;; mode: C++ *** |
|
657 ;;; End: *** |
|
658 */ |