1
|
1 // tc-assign.cc -*- C++ -*- |
|
2 /* |
|
3 |
399
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
1
|
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, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
164
|
28 #include "idx-vector.h" |
1
|
29 #include "user-prefs.h" |
164
|
30 #include "tree-const.h" |
|
31 #include "utils.h" |
1
|
32 #include "gripes.h" |
164
|
33 #include "error.h" |
1
|
34 |
|
35 #include "tc-inlines.cc" |
|
36 |
767
|
37 // Top-level tree-constant function that handles assignments. Only |
|
38 // decide if the left-hand side is currently a scalar or a matrix and |
|
39 // hand off to other functions to do the real work. |
|
40 |
1
|
41 void |
|
42 tree_constant_rep::assign (tree_constant& rhs, tree_constant *args, int nargs) |
|
43 { |
|
44 tree_constant rhs_tmp = rhs.make_numeric (); |
|
45 |
427
|
46 // This is easier than actually handling assignments to strings. |
|
47 // An assignment to a range will normally require a conversion to a |
|
48 // vector since it will normally destroy the equally-spaced property |
|
49 // of the range elements. |
|
50 |
1
|
51 if (type_tag == string_constant || type_tag == range_constant) |
|
52 force_numeric (); |
|
53 |
|
54 switch (type_tag) |
|
55 { |
|
56 case complex_scalar_constant: |
|
57 case scalar_constant: |
|
58 case unknown_constant: |
|
59 do_scalar_assignment (rhs_tmp, args, nargs); |
|
60 break; |
|
61 case complex_matrix_constant: |
|
62 case matrix_constant: |
|
63 do_matrix_assignment (rhs_tmp, args, nargs); |
|
64 break; |
|
65 case string_constant: |
240
|
66 ::error ("invalid assignment to string type"); |
1
|
67 break; |
|
68 case range_constant: |
|
69 case magic_colon: |
|
70 default: |
|
71 panic_impossible (); |
|
72 break; |
|
73 } |
|
74 } |
|
75 |
767
|
76 // Assignments to scalars. If resize_on_range_error is true, |
|
77 // this can convert the left-hand size to a matrix. |
|
78 |
1
|
79 void |
|
80 tree_constant_rep::do_scalar_assignment (tree_constant& rhs, |
|
81 tree_constant *args, int nargs) |
|
82 { |
|
83 assert (type_tag == unknown_constant |
|
84 || type_tag == scalar_constant |
|
85 || type_tag == complex_scalar_constant); |
|
86 |
435
|
87 if ((rhs.is_scalar_type () || rhs.is_zero_by_zero) |
|
88 && valid_scalar_indices (args, nargs)) |
1
|
89 { |
435
|
90 if (rhs.is_zero_by_zero ()) |
|
91 { |
|
92 if (type_tag == complex_scalar_constant) |
|
93 delete complex_scalar; |
|
94 |
|
95 matrix = new Matrix (0, 0); |
|
96 type_tag = matrix_constant; |
|
97 } |
|
98 else if (type_tag == unknown_constant || type_tag == scalar_constant) |
1
|
99 { |
|
100 if (rhs.const_type () == scalar_constant) |
|
101 { |
|
102 scalar = rhs.double_value (); |
|
103 type_tag = scalar_constant; |
|
104 } |
|
105 else if (rhs.const_type () == complex_scalar_constant) |
|
106 { |
|
107 complex_scalar = new Complex (rhs.complex_value ()); |
|
108 type_tag = complex_scalar_constant; |
|
109 } |
|
110 else |
|
111 { |
240
|
112 ::error ("invalid assignment to scalar"); |
143
|
113 return; |
1
|
114 } |
|
115 } |
|
116 else |
|
117 { |
|
118 if (rhs.const_type () == scalar_constant) |
|
119 { |
435
|
120 delete complex_scalar; |
1
|
121 scalar = rhs.double_value (); |
|
122 type_tag = scalar_constant; |
|
123 } |
|
124 else if (rhs.const_type () == complex_scalar_constant) |
|
125 { |
|
126 *complex_scalar = rhs.complex_value (); |
|
127 type_tag = complex_scalar_constant; |
|
128 } |
|
129 else |
|
130 { |
240
|
131 ::error ("invalid assignment to scalar"); |
143
|
132 return; |
1
|
133 } |
|
134 } |
|
135 } |
|
136 else if (user_pref.resize_on_range_error) |
|
137 { |
143
|
138 tree_constant_rep::constant_type old_type_tag = type_tag; |
|
139 |
1
|
140 if (type_tag == complex_scalar_constant) |
|
141 { |
|
142 Complex *old_complex = complex_scalar; |
|
143 complex_matrix = new ComplexMatrix (1, 1, *complex_scalar); |
|
144 type_tag = complex_matrix_constant; |
|
145 delete old_complex; |
|
146 } |
|
147 else if (type_tag == scalar_constant) |
|
148 { |
|
149 matrix = new Matrix (1, 1, scalar); |
|
150 type_tag = matrix_constant; |
|
151 } |
143
|
152 |
|
153 // If there is an error, the call to do_matrix_assignment should not |
|
154 // destroy the current value. tree_constant_rep::eval(int) will take |
|
155 // care of converting single element matrices back to scalars. |
|
156 |
1
|
157 do_matrix_assignment (rhs, args, nargs); |
143
|
158 |
|
159 // I don't think there's any other way to revert back to unknown |
|
160 // constant types, so here it is. |
|
161 |
|
162 if (old_type_tag == unknown_constant && error_state) |
|
163 { |
|
164 if (type_tag == matrix_constant) |
|
165 delete matrix; |
|
166 else if (type_tag == complex_matrix_constant) |
|
167 delete complex_matrix; |
|
168 |
|
169 type_tag = unknown_constant; |
|
170 } |
1
|
171 } |
|
172 else if (nargs > 3 || nargs < 2) |
240
|
173 ::error ("invalid index expression for scalar type"); |
1
|
174 else |
240
|
175 ::error ("index invalid or out of range for scalar type"); |
1
|
176 } |
|
177 |
767
|
178 // Assignments to matrices (and vectors). |
|
179 // |
|
180 // For compatibility with Matlab, we allow assignment of an empty |
|
181 // matrix to an expression with empty indices to do nothing. |
|
182 |
1
|
183 void |
|
184 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, |
|
185 tree_constant *args, int nargs) |
|
186 { |
|
187 assert (type_tag == unknown_constant |
|
188 || type_tag == matrix_constant |
|
189 || type_tag == complex_matrix_constant); |
|
190 |
|
191 if (type_tag == matrix_constant && rhs.is_complex_type ()) |
|
192 { |
|
193 Matrix *old_matrix = matrix; |
|
194 complex_matrix = new ComplexMatrix (*matrix); |
|
195 type_tag = complex_matrix_constant; |
|
196 delete old_matrix; |
|
197 } |
|
198 else if (type_tag == unknown_constant) |
|
199 { |
|
200 if (rhs.is_complex_type ()) |
|
201 { |
|
202 complex_matrix = new ComplexMatrix (); |
|
203 type_tag = complex_matrix_constant; |
|
204 } |
|
205 else |
|
206 { |
|
207 matrix = new Matrix (); |
|
208 type_tag = matrix_constant; |
|
209 } |
|
210 } |
|
211 |
427
|
212 // The do_matrix_assignment functions can't handle empty matrices, so |
|
213 // don't let any pass through here. |
1
|
214 switch (nargs) |
|
215 { |
|
216 case 2: |
|
217 if (args == NULL_TREE_CONST) |
240
|
218 ::error ("matrix index is null"); |
1
|
219 else if (args[1].is_undefined ()) |
240
|
220 ::error ("matrix index is undefined"); |
1
|
221 else |
|
222 do_matrix_assignment (rhs, args[1]); |
|
223 break; |
|
224 case 3: |
|
225 if (args == NULL_TREE_CONST) |
240
|
226 ::error ("matrix indices are null"); |
1
|
227 else if (args[1].is_undefined ()) |
240
|
228 ::error ("first matrix index is undefined"); |
1
|
229 else if (args[2].is_undefined ()) |
240
|
230 ::error ("second matrix index is undefined"); |
427
|
231 else if (args[1].is_empty () || args[2].is_empty ()) |
|
232 { |
|
233 if (! rhs.is_empty ()) |
|
234 { |
|
235 ::error ("in assignment expression, a matrix index is empty"); |
|
236 ::error ("but hte right hand side is not an empty matrix"); |
|
237 } |
|
238 // XXX FIXME XXX -- to really be correct here, we should probably |
|
239 // check to see if the assignment conforms, but that seems like more |
|
240 // work than it's worth right now... |
|
241 } |
1
|
242 else |
|
243 do_matrix_assignment (rhs, args[1], args[2]); |
|
244 break; |
|
245 default: |
240
|
246 ::error ("too many indices for matrix expression"); |
1
|
247 break; |
|
248 } |
|
249 } |
|
250 |
767
|
251 // Matrix assignments indexed by a single value. |
|
252 |
1
|
253 void |
|
254 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, |
|
255 tree_constant& i_arg) |
|
256 { |
|
257 int nr = rows (); |
|
258 int nc = columns (); |
|
259 |
427
|
260 if (user_pref.do_fortran_indexing || nr <= 1 || nc <= 1) |
|
261 { |
|
262 if (i_arg.is_empty ()) |
|
263 { |
|
264 if (! rhs.is_empty ()) |
|
265 { |
|
266 ::error ("in assignment expression, matrix index is empty but"); |
|
267 ::error ("right hand side is not an empty matrix"); |
|
268 } |
|
269 // XXX FIXME XXX -- to really be correct here, we should probably |
|
270 // check to see if the assignment conforms, but that seems like more |
|
271 // work than it's worth right now... |
|
272 |
|
273 // The assignment functions can't handle empty matrices, so don't let |
|
274 // any pass through here. |
|
275 return; |
|
276 } |
|
277 |
435
|
278 // We can't handle the case of assigning to a vector first, since even |
|
279 // then, the two operations are not equivalent. For example, the |
|
280 // expression V(:) = M is handled differently depending on whether the |
|
281 // user specified do_fortran_indexing = "true". |
|
282 |
427
|
283 if (user_pref.do_fortran_indexing) |
|
284 fortran_style_matrix_assignment (rhs, i_arg); |
|
285 else if (nr <= 1 || nc <= 1) |
|
286 vector_assignment (rhs, i_arg); |
|
287 else |
|
288 panic_impossible (); |
|
289 } |
1
|
290 else |
240
|
291 ::error ("single index only valid for row or column vector"); |
1
|
292 } |
|
293 |
767
|
294 // Fortran-style assignments. Matrices are assumed to be stored in |
|
295 // column-major order and it is ok to use a single index for |
|
296 // multi-dimensional matrices. |
|
297 |
1
|
298 void |
|
299 tree_constant_rep::fortran_style_matrix_assignment (tree_constant& rhs, |
|
300 tree_constant& i_arg) |
|
301 { |
|
302 tree_constant tmp_i = i_arg.make_numeric_or_magic (); |
|
303 |
|
304 tree_constant_rep::constant_type itype = tmp_i.const_type (); |
|
305 |
|
306 int nr = rows (); |
|
307 int nc = columns (); |
|
308 |
|
309 int rhs_nr = rhs.rows (); |
|
310 int rhs_nc = rhs.columns (); |
|
311 |
|
312 switch (itype) |
|
313 { |
|
314 case complex_scalar_constant: |
|
315 case scalar_constant: |
|
316 { |
|
317 int i = NINT (tmp_i.double_value ()); |
435
|
318 int idx = i - 1; |
|
319 |
|
320 if (rhs_nr == 0 && rhs_nc == 0) |
|
321 { |
|
322 if (idx < nr * nc) |
|
323 { |
|
324 convert_to_row_or_column_vector (); |
|
325 |
|
326 nr = rows (); |
|
327 nc = columns (); |
|
328 |
|
329 if (nr == 1) |
|
330 delete_column (idx); |
|
331 else if (nc == 1) |
|
332 delete_row (idx); |
|
333 else |
|
334 panic_impossible (); |
|
335 } |
|
336 return; |
|
337 } |
|
338 |
|
339 if (index_check (idx, "") < 0) |
143
|
340 return; |
435
|
341 |
1
|
342 if (nr <= 1 || nc <= 1) |
191
|
343 { |
435
|
344 maybe_resize (idx); |
191
|
345 if (error_state) |
|
346 return; |
|
347 } |
435
|
348 else if (range_max_check (idx, nr * nc) < 0) |
143
|
349 return; |
1
|
350 |
|
351 nr = rows (); |
|
352 nc = columns (); |
|
353 |
|
354 if (! indexed_assign_conforms (1, 1, rhs_nr, rhs_nc)) |
|
355 { |
240
|
356 ::error ("for A(int) = X: X must be a scalar"); |
143
|
357 return; |
1
|
358 } |
|
359 int ii = fortran_row (i, nr) - 1; |
|
360 int jj = fortran_column (i, nr) - 1; |
|
361 do_matrix_assignment (rhs, ii, jj); |
|
362 } |
|
363 break; |
|
364 case complex_matrix_constant: |
|
365 case matrix_constant: |
|
366 { |
|
367 Matrix mi = tmp_i.matrix_value (); |
|
368 int len = nr * nc; |
|
369 idx_vector ii (mi, 1, "", len); // Always do fortran indexing here... |
191
|
370 if (! ii) |
|
371 return; |
|
372 |
435
|
373 if (rhs_nr == 0 && rhs_nc == 0) |
|
374 { |
|
375 ii.sort_uniq (); |
|
376 int num_to_delete = 0; |
|
377 for (int i = 0; i < ii.length (); i++) |
|
378 { |
|
379 if (ii.elem (i) < len) |
|
380 num_to_delete++; |
|
381 else |
|
382 break; |
|
383 } |
|
384 |
|
385 if (num_to_delete > 0) |
|
386 { |
|
387 if (num_to_delete != ii.length ()) |
|
388 ii.shorten (num_to_delete); |
|
389 |
|
390 convert_to_row_or_column_vector (); |
|
391 |
|
392 nr = rows (); |
|
393 nc = columns (); |
|
394 |
|
395 if (nr == 1) |
|
396 delete_columns (ii); |
|
397 else if (nc == 1) |
|
398 delete_rows (ii); |
|
399 else |
|
400 panic_impossible (); |
|
401 } |
|
402 return; |
|
403 } |
|
404 |
1
|
405 if (nr <= 1 || nc <= 1) |
191
|
406 { |
399
|
407 maybe_resize (ii.max ()); |
191
|
408 if (error_state) |
|
409 return; |
|
410 } |
399
|
411 else if (range_max_check (ii.max (), len) < 0) |
143
|
412 return; |
1
|
413 |
399
|
414 int ilen = ii.capacity (); |
|
415 |
|
416 if (ilen != rhs_nr * rhs_nc) |
1
|
417 { |
240
|
418 ::error ("A(matrix) = X: X and matrix must have the same number"); |
|
419 ::error ("of elements"); |
1
|
420 } |
399
|
421 else if (ilen == 1 && rhs.is_scalar_type ()) |
|
422 { |
|
423 int nr = rows (); |
|
424 int idx = ii.elem (0); |
|
425 int ii = fortran_row (idx + 1, nr) - 1; |
|
426 int jj = fortran_column (idx + 1, nr) - 1; |
|
427 |
|
428 if (rhs.const_type () == scalar_constant) |
|
429 matrix->elem (ii, jj) = rhs.double_value (); |
|
430 else if (rhs.const_type () == complex_scalar_constant) |
|
431 complex_matrix->elem (ii, jj) = rhs.complex_value (); |
|
432 else |
|
433 panic_impossible (); |
|
434 } |
|
435 else |
|
436 fortran_style_matrix_assignment (rhs, ii); |
1
|
437 } |
|
438 break; |
|
439 case string_constant: |
|
440 gripe_string_invalid (); |
|
441 break; |
|
442 case range_constant: |
|
443 gripe_range_invalid (); |
|
444 break; |
|
445 case magic_colon: |
435
|
446 // a(:) = [] is equivalent to a(:,:) = foo. |
|
447 if (rhs_nr == 0 && rhs_nc == 0) |
|
448 do_matrix_assignment (rhs, magic_colon, magic_colon); |
|
449 else |
|
450 fortran_style_matrix_assignment (rhs, magic_colon); |
1
|
451 break; |
|
452 default: |
|
453 panic_impossible (); |
|
454 break; |
|
455 } |
|
456 } |
|
457 |
767
|
458 // Fortran-style assignment for vector index. |
|
459 |
427
|
460 void |
|
461 tree_constant_rep::fortran_style_matrix_assignment (tree_constant& rhs, |
|
462 idx_vector& i) |
|
463 { |
|
464 assert (rhs.is_matrix_type ()); |
|
465 |
|
466 int ilen = i.capacity (); |
|
467 |
|
468 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
469 |
|
470 int len = rhs_nr * rhs_nc; |
|
471 |
|
472 if (len == ilen) |
|
473 { |
|
474 int nr = rows (); |
|
475 if (rhs.const_type () == matrix_constant) |
|
476 { |
|
477 double *cop_out = rhs_m.fortran_vec (); |
|
478 for (int k = 0; k < len; k++) |
|
479 { |
|
480 int ii = fortran_row (i.elem (k) + 1, nr) - 1; |
|
481 int jj = fortran_column (i.elem (k) + 1, nr) - 1; |
|
482 |
|
483 matrix->elem (ii, jj) = *cop_out++; |
|
484 } |
|
485 } |
|
486 else |
|
487 { |
|
488 Complex *cop_out = rhs_cm.fortran_vec (); |
|
489 for (int k = 0; k < len; k++) |
|
490 { |
|
491 int ii = fortran_row (i.elem (k) + 1, nr) - 1; |
|
492 int jj = fortran_column (i.elem (k) + 1, nr) - 1; |
|
493 |
|
494 complex_matrix->elem (ii, jj) = *cop_out++; |
|
495 } |
|
496 } |
|
497 } |
|
498 else |
|
499 ::error ("number of rows and columns must match for indexed assignment"); |
|
500 } |
|
501 |
767
|
502 // Fortran-style assignment for colon index. |
|
503 |
427
|
504 void |
|
505 tree_constant_rep::fortran_style_matrix_assignment |
|
506 (tree_constant& rhs, tree_constant_rep::constant_type mci) |
|
507 { |
|
508 assert (rhs.is_matrix_type () && mci == tree_constant_rep::magic_colon); |
|
509 |
|
510 int nr = rows (); |
|
511 int nc = columns (); |
|
512 |
|
513 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
514 |
|
515 int rhs_size = rhs_nr * rhs_nc; |
|
516 if (rhs_size == 0) |
|
517 { |
|
518 if (rhs.const_type () == matrix_constant) |
|
519 { |
|
520 delete matrix; |
|
521 matrix = new Matrix (0, 0); |
|
522 return; |
|
523 } |
|
524 else |
|
525 panic_impossible (); |
|
526 } |
|
527 else if (nr*nc != rhs_size) |
|
528 { |
|
529 ::error ("A(:) = X: X and A must have the same number of elements"); |
|
530 return; |
|
531 } |
|
532 |
|
533 if (rhs.const_type () == matrix_constant) |
|
534 { |
|
535 double *cop_out = rhs_m.fortran_vec (); |
|
536 for (int j = 0; j < nc; j++) |
|
537 for (int i = 0; i < nr; i++) |
|
538 matrix->elem (i, j) = *cop_out++; |
|
539 } |
|
540 else |
|
541 { |
|
542 Complex *cop_out = rhs_cm.fortran_vec (); |
|
543 for (int j = 0; j < nc; j++) |
|
544 for (int i = 0; i < nr; i++) |
|
545 complex_matrix->elem (i, j) = *cop_out++; |
|
546 } |
|
547 } |
|
548 |
767
|
549 // Assignments to vectors. Hand off to other functions once we know |
|
550 // what kind of index we have. For a colon, it is the same as |
|
551 // assignment to a matrix indexed by two colons. |
|
552 |
1
|
553 void |
|
554 tree_constant_rep::vector_assignment (tree_constant& rhs, tree_constant& i_arg) |
|
555 { |
|
556 int nr = rows (); |
|
557 int nc = columns (); |
|
558 |
|
559 assert ((nr == 1 || nc == 1 || (nr == 0 && nc == 0)) |
|
560 && ! user_pref.do_fortran_indexing); |
|
561 |
|
562 tree_constant tmp_i = i_arg.make_numeric_or_range_or_magic (); |
|
563 |
|
564 tree_constant_rep::constant_type itype = tmp_i.const_type (); |
|
565 |
|
566 switch (itype) |
|
567 { |
|
568 case complex_scalar_constant: |
|
569 case scalar_constant: |
|
570 { |
|
571 int i = tree_to_mat_idx (tmp_i.double_value ()); |
143
|
572 if (index_check (i, "") < 0) |
|
573 return; |
1
|
574 do_vector_assign (rhs, i); |
|
575 } |
|
576 break; |
|
577 case complex_matrix_constant: |
|
578 case matrix_constant: |
|
579 { |
|
580 Matrix mi = tmp_i.matrix_value (); |
|
581 int len = nr * nc; |
|
582 idx_vector iv (mi, user_pref.do_fortran_indexing, "", len); |
191
|
583 if (! iv) |
|
584 return; |
|
585 |
1
|
586 do_vector_assign (rhs, iv); |
|
587 } |
|
588 break; |
|
589 case string_constant: |
|
590 gripe_string_invalid (); |
|
591 break; |
|
592 case range_constant: |
|
593 { |
|
594 Range ri = tmp_i.range_value (); |
212
|
595 int len = nr * nc; |
|
596 if (len == 2 && is_zero_one (ri)) |
1
|
597 { |
|
598 do_vector_assign (rhs, 1); |
|
599 } |
212
|
600 else if (len == 2 && is_one_zero (ri)) |
|
601 { |
|
602 do_vector_assign (rhs, 0); |
|
603 } |
1
|
604 else |
|
605 { |
212
|
606 if (index_check (ri, "") < 0) |
143
|
607 return; |
212
|
608 do_vector_assign (rhs, ri); |
1
|
609 } |
|
610 } |
|
611 break; |
|
612 case magic_colon: |
|
613 { |
|
614 int rhs_nr = rhs.rows (); |
|
615 int rhs_nc = rhs.columns (); |
|
616 |
|
617 if (! indexed_assign_conforms (nr, nc, rhs_nr, rhs_nc)) |
|
618 { |
240
|
619 ::error ("A(:) = X: X and A must have the same dimensions"); |
143
|
620 return; |
1
|
621 } |
|
622 do_matrix_assignment (rhs, magic_colon, magic_colon); |
|
623 } |
|
624 break; |
|
625 default: |
|
626 panic_impossible (); |
|
627 break; |
|
628 } |
|
629 } |
|
630 |
767
|
631 // Check whether an indexed assignment to a vector is valid. |
|
632 |
1
|
633 void |
|
634 tree_constant_rep::check_vector_assign (int rhs_nr, int rhs_nc, |
164
|
635 int ilen, const char *rm) |
1
|
636 { |
|
637 int nr = rows (); |
|
638 int nc = columns (); |
|
639 |
206
|
640 if ((nr == 1 && nc == 1) || nr == 0 || nc == 0) // No orientation. |
1
|
641 { |
206
|
642 if (! (ilen == rhs_nr || ilen == rhs_nc)) |
|
643 { |
240
|
644 ::error ("A(%s) = X: X and %s must have the same number of elements", |
206
|
645 rm, rm); |
|
646 } |
1
|
647 } |
206
|
648 else if (nr == 1) // Preserve current row orientation. |
1
|
649 { |
|
650 if (! (rhs_nr == 1 && rhs_nc == ilen)) |
206
|
651 { |
240
|
652 ::error ("A(%s) = X: where A is a row vector, X must also be a", rm); |
|
653 ::error ("row vector with the same number of elements as %s", rm); |
206
|
654 } |
1
|
655 } |
206
|
656 else if (nc == 1) // Preserve current column orientation. |
1
|
657 { |
|
658 if (! (rhs_nc == 1 && rhs_nr == ilen)) |
206
|
659 { |
240
|
660 ::error ("A(%s) = X: where A is a column vector, X must also be", rm); |
|
661 ::error ("a column vector with the same number of elements as %s", rm); |
206
|
662 } |
1
|
663 } |
|
664 else |
|
665 panic_impossible (); |
|
666 } |
|
667 |
767
|
668 // Assignment to a vector with an integer index. |
|
669 |
1
|
670 void |
|
671 tree_constant_rep::do_vector_assign (tree_constant& rhs, int i) |
|
672 { |
|
673 int rhs_nr = rhs.rows (); |
|
674 int rhs_nc = rhs.columns (); |
|
675 |
208
|
676 if (indexed_assign_conforms (1, 1, rhs_nr, rhs_nc)) |
|
677 { |
|
678 maybe_resize (i); |
|
679 if (error_state) |
|
680 return; |
|
681 |
|
682 int nr = rows (); |
|
683 int nc = columns (); |
|
684 |
|
685 if (nr == 1) |
|
686 { |
|
687 REP_ELEM_ASSIGN (0, i, rhs.double_value (), rhs.complex_value (), |
|
688 rhs.is_real_type ()); |
|
689 } |
|
690 else if (nc == 1) |
|
691 { |
|
692 REP_ELEM_ASSIGN (i, 0, rhs.double_value (), rhs.complex_value (), |
|
693 rhs.is_real_type ()); |
|
694 } |
|
695 else |
|
696 panic_impossible (); |
|
697 } |
|
698 else if (rhs_nr == 0 && rhs_nc == 0) |
|
699 { |
|
700 int nr = rows (); |
|
701 int nc = columns (); |
|
702 |
|
703 int len = nr > nc ? nr : nc; |
|
704 |
|
705 if (i < 0 || i >= len) |
|
706 { |
240
|
707 ::error ("A(int) = []: index out of range"); |
208
|
708 return; |
|
709 } |
|
710 |
|
711 if (nr == 1) |
|
712 delete_column (i); |
|
713 else if (nc == 1) |
|
714 delete_row (i); |
|
715 else |
|
716 panic_impossible (); |
|
717 } |
|
718 else |
1
|
719 { |
240
|
720 ::error ("for A(int) = X: X must be a scalar"); |
143
|
721 return; |
1
|
722 } |
|
723 } |
|
724 |
767
|
725 // Assignment to a vector with a vector index. |
|
726 |
1
|
727 void |
|
728 tree_constant_rep::do_vector_assign (tree_constant& rhs, idx_vector& iv) |
|
729 { |
208
|
730 if (rhs.is_zero_by_zero ()) |
|
731 { |
|
732 int nr = rows (); |
|
733 int nc = columns (); |
1
|
734 |
208
|
735 int len = nr > nc ? nr : nc; |
1
|
736 |
208
|
737 if (iv.max () >= len) |
|
738 { |
240
|
739 ::error ("A(matrix) = []: index out of range"); |
208
|
740 return; |
|
741 } |
1
|
742 |
208
|
743 if (nr == 1) |
|
744 delete_columns (iv); |
|
745 else if (nc == 1) |
|
746 delete_rows (iv); |
|
747 else |
|
748 panic_impossible (); |
1
|
749 } |
399
|
750 else if (rhs.is_scalar_type ()) |
|
751 { |
|
752 int nr = rows (); |
|
753 int nc = columns (); |
|
754 |
|
755 if (iv.capacity () == 1) |
|
756 { |
|
757 int idx = iv.elem (0); |
|
758 |
|
759 if (nr == 1) |
|
760 { |
|
761 REP_ELEM_ASSIGN (0, idx, rhs.double_value (), |
|
762 rhs.complex_value (), rhs.is_real_type ()); |
|
763 } |
|
764 else if (nc == 1) |
|
765 { |
|
766 REP_ELEM_ASSIGN (idx, 0, rhs.double_value (), |
|
767 rhs.complex_value (), rhs.is_real_type ()); |
|
768 } |
|
769 else |
|
770 panic_impossible (); |
|
771 } |
|
772 else |
|
773 { |
|
774 if (nr == 1) |
|
775 { |
|
776 ::error ("A(matrix) = X: where A is a row vector, X must also be a"); |
|
777 ::error ("row vector with the same number of elements as matrix"); |
|
778 } |
|
779 else if (nc == 1) |
|
780 { |
|
781 ::error ("A(matrix) = X: where A is a column vector, X must also be a"); |
|
782 ::error ("column vector with the same number of elements as matrix"); |
|
783 } |
|
784 else |
|
785 panic_impossible (); |
|
786 } |
|
787 } |
|
788 else if (rhs.is_matrix_type ()) |
208
|
789 { |
|
790 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
791 |
|
792 int ilen = iv.capacity (); |
|
793 check_vector_assign (rhs_nr, rhs_nc, ilen, "matrix"); |
|
794 if (error_state) |
|
795 return; |
|
796 |
|
797 force_orient f_orient = no_orient; |
|
798 if (rhs_nr == 1 && rhs_nc != 1) |
|
799 f_orient = row_orient; |
|
800 else if (rhs_nc == 1 && rhs_nr != 1) |
|
801 f_orient = column_orient; |
|
802 |
|
803 maybe_resize (iv.max (), f_orient); |
|
804 if (error_state) |
|
805 return; |
|
806 |
|
807 int nr = rows (); |
|
808 int nc = columns (); |
|
809 |
|
810 if (nr == 1) |
|
811 { |
|
812 for (int i = 0; i < iv.capacity (); i++) |
|
813 REP_ELEM_ASSIGN (0, iv.elem (i), rhs_m.elem (0, i), |
|
814 rhs_cm.elem (0, i), rhs.is_real_type ()); |
|
815 } |
|
816 else if (nc == 1) |
|
817 { |
|
818 for (int i = 0; i < iv.capacity (); i++) |
|
819 REP_ELEM_ASSIGN (iv.elem (i), 0, rhs_m.elem (i, 0), |
|
820 rhs_cm.elem (i, 0), rhs.is_real_type ()); |
|
821 } |
|
822 else |
|
823 panic_impossible (); |
|
824 } |
399
|
825 else |
|
826 panic_impossible (); |
1
|
827 } |
|
828 |
767
|
829 // Assignment to a vector with a range index. |
|
830 |
1
|
831 void |
212
|
832 tree_constant_rep::do_vector_assign (tree_constant& rhs, Range& ri) |
1
|
833 { |
208
|
834 if (rhs.is_zero_by_zero ()) |
|
835 { |
|
836 int nr = rows (); |
|
837 int nc = columns (); |
1
|
838 |
208
|
839 int len = nr > nc ? nr : nc; |
1
|
840 |
208
|
841 int b = tree_to_mat_idx (ri.min ()); |
|
842 int l = tree_to_mat_idx (ri.max ()); |
|
843 if (b < 0 || l >= len) |
1
|
844 { |
240
|
845 ::error ("A(range) = []: index out of range"); |
208
|
846 return; |
1
|
847 } |
208
|
848 |
|
849 if (nr == 1) |
|
850 delete_columns (ri); |
|
851 else if (nc == 1) |
|
852 delete_rows (ri); |
|
853 else |
|
854 panic_impossible (); |
1
|
855 } |
399
|
856 else if (rhs.is_scalar_type ()) |
|
857 { |
|
858 int nr = rows (); |
|
859 int nc = columns (); |
|
860 |
|
861 if (nr == 1) |
|
862 { |
|
863 ::error ("A(range) = X: where A is a row vector, X must also be a"); |
|
864 ::error ("row vector with the same number of elements as range"); |
|
865 } |
|
866 else if (nc == 1) |
|
867 { |
|
868 ::error ("A(range) = X: where A is a column vector, X must also be a"); |
|
869 ::error ("column vector with the same number of elements as range"); |
|
870 } |
|
871 else |
|
872 panic_impossible (); |
|
873 } |
|
874 else if (rhs.is_matrix_type ()) |
208
|
875 { |
|
876 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
877 |
|
878 int ilen = ri.nelem (); |
|
879 check_vector_assign (rhs_nr, rhs_nc, ilen, "range"); |
|
880 if (error_state) |
|
881 return; |
|
882 |
|
883 force_orient f_orient = no_orient; |
|
884 if (rhs_nr == 1 && rhs_nc != 1) |
|
885 f_orient = row_orient; |
|
886 else if (rhs_nc == 1 && rhs_nr != 1) |
|
887 f_orient = column_orient; |
|
888 |
212
|
889 maybe_resize (tree_to_mat_idx (ri.max ()), f_orient); |
208
|
890 if (error_state) |
|
891 return; |
|
892 |
|
893 int nr = rows (); |
|
894 int nc = columns (); |
|
895 |
|
896 double b = ri.base (); |
|
897 double increment = ri.inc (); |
|
898 |
|
899 if (nr == 1) |
|
900 { |
|
901 for (int i = 0; i < ri.nelem (); i++) |
|
902 { |
|
903 double tmp = b + i * increment; |
|
904 int col = tree_to_mat_idx (tmp); |
|
905 REP_ELEM_ASSIGN (0, col, rhs_m.elem (0, i), rhs_cm.elem (0, i), |
|
906 rhs.is_real_type ()); |
|
907 } |
|
908 } |
|
909 else if (nc == 1) |
|
910 { |
|
911 for (int i = 0; i < ri.nelem (); i++) |
|
912 { |
|
913 double tmp = b + i * increment; |
|
914 int row = tree_to_mat_idx (tmp); |
|
915 REP_ELEM_ASSIGN (row, 0, rhs_m.elem (i, 0), rhs_cm.elem (i, 0), |
|
916 rhs.is_real_type ()); |
|
917 } |
|
918 } |
|
919 else |
|
920 panic_impossible (); |
|
921 } |
399
|
922 else |
|
923 panic_impossible (); |
1
|
924 } |
|
925 |
767
|
926 // Matrix assignment indexed by two values. This function determines |
|
927 // the type of the first arugment, checks as much as possible, and |
|
928 // then calls one of a set of functions to handle the specific cases: |
|
929 // |
|
930 // M (integer, arg2) = RHS (MA1) |
|
931 // M (vector, arg2) = RHS (MA2) |
|
932 // M (range, arg2) = RHS (MA3) |
|
933 // M (colon, arg2) = RHS (MA4) |
|
934 // |
|
935 // Each of those functions determines the type of the second argument |
|
936 // and calls another function to handle the real work of doing the |
|
937 // assignment. |
|
938 |
1
|
939 void |
|
940 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, |
|
941 tree_constant& i_arg, |
|
942 tree_constant& j_arg) |
|
943 { |
|
944 tree_constant tmp_i = i_arg.make_numeric_or_range_or_magic (); |
|
945 |
|
946 tree_constant_rep::constant_type itype = tmp_i.const_type (); |
|
947 |
|
948 switch (itype) |
|
949 { |
|
950 case complex_scalar_constant: |
|
951 case scalar_constant: |
|
952 { |
|
953 int i = tree_to_mat_idx (tmp_i.double_value ()); |
143
|
954 if (index_check (i, "row") < 0) |
|
955 return; |
1
|
956 do_matrix_assignment (rhs, i, j_arg); |
|
957 } |
|
958 break; |
|
959 case complex_matrix_constant: |
|
960 case matrix_constant: |
|
961 { |
|
962 Matrix mi = tmp_i.matrix_value (); |
|
963 idx_vector iv (mi, user_pref.do_fortran_indexing, "row", rows ()); |
191
|
964 if (! iv) |
|
965 return; |
|
966 |
1
|
967 do_matrix_assignment (rhs, iv, j_arg); |
|
968 } |
|
969 break; |
|
970 case string_constant: |
|
971 gripe_string_invalid (); |
|
972 break; |
|
973 case range_constant: |
|
974 { |
|
975 Range ri = tmp_i.range_value (); |
212
|
976 int nr = rows (); |
|
977 if (nr == 2 && is_zero_one (ri)) |
1
|
978 { |
|
979 do_matrix_assignment (rhs, 1, j_arg); |
|
980 } |
212
|
981 else if (nr == 2 && is_one_zero (ri)) |
|
982 { |
|
983 do_matrix_assignment (rhs, 0, j_arg); |
|
984 } |
1
|
985 else |
|
986 { |
212
|
987 if (index_check (ri, "row") < 0) |
143
|
988 return; |
212
|
989 do_matrix_assignment (rhs, ri, j_arg); |
1
|
990 } |
|
991 } |
|
992 break; |
|
993 case magic_colon: |
|
994 do_matrix_assignment (rhs, magic_colon, j_arg); |
|
995 break; |
|
996 default: |
|
997 panic_impossible (); |
|
998 break; |
|
999 } |
|
1000 } |
|
1001 |
767
|
1002 // -*- MA1 -*- |
1
|
1003 void |
|
1004 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, int i, |
|
1005 tree_constant& j_arg) |
|
1006 { |
|
1007 tree_constant tmp_j = j_arg.make_numeric_or_range_or_magic (); |
|
1008 |
|
1009 tree_constant_rep::constant_type jtype = tmp_j.const_type (); |
|
1010 |
|
1011 int rhs_nr = rhs.rows (); |
|
1012 int rhs_nc = rhs.columns (); |
|
1013 |
|
1014 switch (jtype) |
|
1015 { |
|
1016 case complex_scalar_constant: |
|
1017 case scalar_constant: |
|
1018 { |
|
1019 int j = tree_to_mat_idx (tmp_j.double_value ()); |
143
|
1020 if (index_check (j, "column") < 0) |
|
1021 return; |
1
|
1022 if (! indexed_assign_conforms (1, 1, rhs_nr, rhs_nc)) |
|
1023 { |
240
|
1024 ::error ("A(int,int) = X, X must be a scalar"); |
143
|
1025 return; |
1
|
1026 } |
|
1027 maybe_resize (i, j); |
191
|
1028 if (error_state) |
|
1029 return; |
|
1030 |
1
|
1031 do_matrix_assignment (rhs, i, j); |
|
1032 } |
|
1033 break; |
|
1034 case complex_matrix_constant: |
|
1035 case matrix_constant: |
|
1036 { |
|
1037 Matrix mj = tmp_j.matrix_value (); |
|
1038 idx_vector jv (mj, user_pref.do_fortran_indexing, "column", |
|
1039 columns ()); |
191
|
1040 if (! jv) |
|
1041 return; |
|
1042 |
1
|
1043 if (! indexed_assign_conforms (1, jv.capacity (), rhs_nr, rhs_nc)) |
|
1044 { |
240
|
1045 ::error ("A(int,matrix) = X: X must be a row vector with the same"); |
|
1046 ::error ("number of elements as matrix"); |
143
|
1047 return; |
1
|
1048 } |
|
1049 maybe_resize (i, jv.max ()); |
191
|
1050 if (error_state) |
|
1051 return; |
|
1052 |
1
|
1053 do_matrix_assignment (rhs, i, jv); |
|
1054 } |
|
1055 break; |
|
1056 case string_constant: |
|
1057 gripe_string_invalid (); |
|
1058 break; |
|
1059 case range_constant: |
|
1060 { |
|
1061 Range rj = tmp_j.range_value (); |
|
1062 if (! indexed_assign_conforms (1, rj.nelem (), rhs_nr, rhs_nc)) |
|
1063 { |
240
|
1064 ::error ("A(int,range) = X: X must be a row vector with the same"); |
|
1065 ::error ("number of elements as range"); |
143
|
1066 return; |
1
|
1067 } |
150
|
1068 |
212
|
1069 int nc = columns (); |
|
1070 if (nc == 2 && is_zero_one (rj) && rhs_nc == 1) |
1
|
1071 { |
|
1072 do_matrix_assignment (rhs, i, 1); |
|
1073 } |
212
|
1074 else if (nc == 2 && is_one_zero (rj) && rhs_nc == 1) |
|
1075 { |
|
1076 do_matrix_assignment (rhs, i, 0); |
|
1077 } |
1
|
1078 else |
|
1079 { |
212
|
1080 if (index_check (rj, "column") < 0) |
143
|
1081 return; |
212
|
1082 maybe_resize (i, tree_to_mat_idx (rj.max ())); |
191
|
1083 if (error_state) |
|
1084 return; |
|
1085 |
1
|
1086 do_matrix_assignment (rhs, i, rj); |
|
1087 } |
|
1088 } |
|
1089 break; |
|
1090 case magic_colon: |
|
1091 { |
|
1092 int nc = columns (); |
208
|
1093 int nr = rows (); |
|
1094 if (nc == 0 && nr == 0 && rhs_nr == 1) |
1
|
1095 { |
|
1096 if (rhs.is_complex_type ()) |
|
1097 { |
|
1098 complex_matrix = new ComplexMatrix (); |
|
1099 type_tag = complex_matrix_constant; |
|
1100 } |
|
1101 else |
|
1102 { |
|
1103 matrix = new Matrix (); |
|
1104 type_tag = matrix_constant; |
|
1105 } |
|
1106 maybe_resize (i, rhs_nc-1); |
191
|
1107 if (error_state) |
|
1108 return; |
1
|
1109 } |
|
1110 else if (indexed_assign_conforms (1, nc, rhs_nr, rhs_nc)) |
191
|
1111 { |
|
1112 maybe_resize (i, nc-1); |
|
1113 if (error_state) |
|
1114 return; |
|
1115 } |
208
|
1116 else if (rhs_nr == 0 && rhs_nc == 0) |
|
1117 { |
|
1118 if (i < 0 || i >= nr) |
|
1119 { |
240
|
1120 ::error ("A(int,:) = []: row index out of range"); |
208
|
1121 return; |
|
1122 } |
|
1123 } |
1
|
1124 else |
|
1125 { |
240
|
1126 ::error ("A(int,:) = X: X must be a row vector with the same"); |
|
1127 ::error ("number of columns as A"); |
143
|
1128 return; |
1
|
1129 } |
|
1130 |
|
1131 do_matrix_assignment (rhs, i, magic_colon); |
|
1132 } |
|
1133 break; |
|
1134 default: |
|
1135 panic_impossible (); |
|
1136 break; |
|
1137 } |
|
1138 } |
|
1139 |
767
|
1140 // -*- MA2 -*- |
1
|
1141 void |
|
1142 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, idx_vector& iv, |
|
1143 tree_constant& j_arg) |
|
1144 { |
|
1145 tree_constant tmp_j = j_arg.make_numeric_or_range_or_magic (); |
|
1146 |
|
1147 tree_constant_rep::constant_type jtype = tmp_j.const_type (); |
|
1148 |
|
1149 int rhs_nr = rhs.rows (); |
|
1150 int rhs_nc = rhs.columns (); |
|
1151 |
|
1152 switch (jtype) |
|
1153 { |
|
1154 case complex_scalar_constant: |
|
1155 case scalar_constant: |
|
1156 { |
|
1157 int j = tree_to_mat_idx (tmp_j.double_value ()); |
143
|
1158 if (index_check (j, "column") < 0) |
|
1159 return; |
1
|
1160 if (! indexed_assign_conforms (iv.capacity (), 1, rhs_nr, rhs_nc)) |
|
1161 { |
240
|
1162 ::error ("A(matrix,int) = X: X must be a column vector with the"); |
|
1163 ::error ("same number of elements as matrix"); |
143
|
1164 return; |
1
|
1165 } |
|
1166 maybe_resize (iv.max (), j); |
191
|
1167 if (error_state) |
|
1168 return; |
|
1169 |
1
|
1170 do_matrix_assignment (rhs, iv, j); |
|
1171 } |
|
1172 break; |
|
1173 case complex_matrix_constant: |
|
1174 case matrix_constant: |
|
1175 { |
|
1176 Matrix mj = tmp_j.matrix_value (); |
|
1177 idx_vector jv (mj, user_pref.do_fortran_indexing, "column", |
|
1178 columns ()); |
191
|
1179 if (! jv) |
|
1180 return; |
|
1181 |
1
|
1182 if (! indexed_assign_conforms (iv.capacity (), jv.capacity (), |
|
1183 rhs_nr, rhs_nc)) |
|
1184 { |
240
|
1185 ::error ("A(r_mat,c_mat) = X: the number of rows in X must match"); |
|
1186 ::error ("the number of elements in r_mat and the number of"); |
|
1187 ::error ("columns in X must match the number of elements in c_mat"); |
143
|
1188 return; |
1
|
1189 } |
|
1190 maybe_resize (iv.max (), jv.max ()); |
191
|
1191 if (error_state) |
|
1192 return; |
|
1193 |
1
|
1194 do_matrix_assignment (rhs, iv, jv); |
|
1195 } |
|
1196 break; |
|
1197 case string_constant: |
|
1198 gripe_string_invalid (); |
|
1199 break; |
|
1200 case range_constant: |
|
1201 { |
|
1202 Range rj = tmp_j.range_value (); |
|
1203 if (! indexed_assign_conforms (iv.capacity (), rj.nelem (), |
|
1204 rhs_nr, rhs_nc)) |
|
1205 { |
240
|
1206 ::error ("A(matrix,range) = X: the number of rows in X must match"); |
|
1207 ::error ("the number of elements in matrix and the number of"); |
|
1208 ::error ("columns in X must match the number of elements in range"); |
143
|
1209 return; |
1
|
1210 } |
150
|
1211 |
212
|
1212 int nc = columns (); |
|
1213 if (nc == 2 && is_zero_one (rj) && rhs_nc == 1) |
1
|
1214 { |
|
1215 do_matrix_assignment (rhs, iv, 1); |
|
1216 } |
212
|
1217 else if (nc == 2 && is_one_zero (rj) && rhs_nc == 1) |
|
1218 { |
|
1219 do_matrix_assignment (rhs, iv, 0); |
|
1220 } |
1
|
1221 else |
|
1222 { |
212
|
1223 if (index_check (rj, "column") < 0) |
143
|
1224 return; |
212
|
1225 maybe_resize (iv.max (), tree_to_mat_idx (rj.max ())); |
191
|
1226 if (error_state) |
|
1227 return; |
|
1228 |
1
|
1229 do_matrix_assignment (rhs, iv, rj); |
|
1230 } |
|
1231 } |
|
1232 break; |
|
1233 case magic_colon: |
|
1234 { |
|
1235 int nc = columns (); |
150
|
1236 int new_nc = nc; |
151
|
1237 if (nc == 0) |
150
|
1238 new_nc = rhs_nc; |
|
1239 |
208
|
1240 if (indexed_assign_conforms (iv.capacity (), new_nc, |
427
|
1241 rhs_nr, rhs_nc)) |
1
|
1242 { |
208
|
1243 maybe_resize (iv.max (), new_nc-1); |
|
1244 if (error_state) |
|
1245 return; |
|
1246 } |
|
1247 else if (rhs_nr == 0 && rhs_nc == 0) |
|
1248 { |
|
1249 if (iv.max () >= rows ()) |
|
1250 { |
240
|
1251 ::error ("A(matrix,:) = []: row index out of range"); |
208
|
1252 return; |
|
1253 } |
|
1254 } |
|
1255 else |
|
1256 { |
240
|
1257 ::error ("A(matrix,:) = X: the number of rows in X must match the"); |
|
1258 ::error ("number of elements in matrix, and the number of columns"); |
|
1259 ::error ("in X must match the number of columns in A"); |
143
|
1260 return; |
1
|
1261 } |
191
|
1262 |
1
|
1263 do_matrix_assignment (rhs, iv, magic_colon); |
|
1264 } |
|
1265 break; |
|
1266 default: |
|
1267 panic_impossible (); |
|
1268 break; |
|
1269 } |
|
1270 } |
|
1271 |
767
|
1272 // -*- MA3 -*- |
1
|
1273 void |
|
1274 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, |
212
|
1275 Range& ri, tree_constant& j_arg) |
1
|
1276 { |
|
1277 tree_constant tmp_j = j_arg.make_numeric_or_range_or_magic (); |
|
1278 |
|
1279 tree_constant_rep::constant_type jtype = tmp_j.const_type (); |
|
1280 |
|
1281 int rhs_nr = rhs.rows (); |
|
1282 int rhs_nc = rhs.columns (); |
|
1283 |
|
1284 switch (jtype) |
|
1285 { |
|
1286 case complex_scalar_constant: |
|
1287 case scalar_constant: |
|
1288 { |
|
1289 int j = tree_to_mat_idx (tmp_j.double_value ()); |
143
|
1290 if (index_check (j, "column") < 0) |
|
1291 return; |
1
|
1292 if (! indexed_assign_conforms (ri.nelem (), 1, rhs_nr, rhs_nc)) |
|
1293 { |
240
|
1294 ::error ("A(range,int) = X: X must be a column vector with the"); |
|
1295 ::error ("same number of elements as range"); |
143
|
1296 return; |
1
|
1297 } |
212
|
1298 maybe_resize (tree_to_mat_idx (ri.max ()), j); |
191
|
1299 if (error_state) |
|
1300 return; |
|
1301 |
1
|
1302 do_matrix_assignment (rhs, ri, j); |
|
1303 } |
|
1304 break; |
|
1305 case complex_matrix_constant: |
|
1306 case matrix_constant: |
|
1307 { |
|
1308 Matrix mj = tmp_j.matrix_value (); |
|
1309 idx_vector jv (mj, user_pref.do_fortran_indexing, "column", |
|
1310 columns ()); |
191
|
1311 if (! jv) |
|
1312 return; |
|
1313 |
1
|
1314 if (! indexed_assign_conforms (ri.nelem (), jv.capacity (), |
|
1315 rhs_nr, rhs_nc)) |
|
1316 { |
240
|
1317 ::error ("A(range,matrix) = X: the number of rows in X must match"); |
|
1318 ::error ("the number of elements in range and the number of"); |
|
1319 ::error ("columns in X must match the number of elements in matrix"); |
143
|
1320 return; |
1
|
1321 } |
212
|
1322 maybe_resize (tree_to_mat_idx (ri.max ()), jv.max ()); |
191
|
1323 if (error_state) |
|
1324 return; |
|
1325 |
1
|
1326 do_matrix_assignment (rhs, ri, jv); |
|
1327 } |
|
1328 break; |
|
1329 case string_constant: |
|
1330 gripe_string_invalid (); |
|
1331 break; |
|
1332 case range_constant: |
|
1333 { |
|
1334 Range rj = tmp_j.range_value (); |
|
1335 if (! indexed_assign_conforms (ri.nelem (), rj.nelem (), |
|
1336 rhs_nr, rhs_nc)) |
|
1337 { |
240
|
1338 ::error ("A(r_range,c_range) = X: the number of rows in X must"); |
|
1339 ::error ("match the number of elements in r_range and the number"); |
|
1340 ::error ("of columns in X must match the number of elements in"); |
|
1341 ::error ("c_range"); |
143
|
1342 return; |
1
|
1343 } |
150
|
1344 |
212
|
1345 int nc = columns (); |
|
1346 if (nc == 2 && is_zero_one (rj) && rhs_nc == 1) |
1
|
1347 { |
|
1348 do_matrix_assignment (rhs, ri, 1); |
|
1349 } |
212
|
1350 else if (nc == 2 && is_one_zero (rj) && rhs_nc == 1) |
|
1351 { |
|
1352 do_matrix_assignment (rhs, ri, 0); |
|
1353 } |
1
|
1354 else |
|
1355 { |
212
|
1356 if (index_check (rj, "column") < 0) |
143
|
1357 return; |
212
|
1358 |
|
1359 maybe_resize (tree_to_mat_idx (ri.max ()), |
|
1360 tree_to_mat_idx (rj.max ())); |
|
1361 |
191
|
1362 if (error_state) |
|
1363 return; |
|
1364 |
1
|
1365 do_matrix_assignment (rhs, ri, rj); |
|
1366 } |
|
1367 } |
|
1368 break; |
|
1369 case magic_colon: |
|
1370 { |
|
1371 int nc = columns (); |
150
|
1372 int new_nc = nc; |
151
|
1373 if (nc == 0) |
150
|
1374 new_nc = rhs_nc; |
|
1375 |
208
|
1376 if (indexed_assign_conforms (ri.nelem (), new_nc, rhs_nr, rhs_nc)) |
|
1377 { |
212
|
1378 maybe_resize (tree_to_mat_idx (ri.max ()), new_nc-1); |
208
|
1379 if (error_state) |
|
1380 return; |
|
1381 } |
|
1382 else if (rhs_nr == 0 && rhs_nc == 0) |
|
1383 { |
|
1384 int b = tree_to_mat_idx (ri.min ()); |
|
1385 int l = tree_to_mat_idx (ri.max ()); |
|
1386 if (b < 0 || l >= rows ()) |
|
1387 { |
240
|
1388 ::error ("A(range,:) = []: row index out of range"); |
208
|
1389 return; |
|
1390 } |
|
1391 } |
|
1392 else |
1
|
1393 { |
240
|
1394 ::error ("A(range,:) = X: the number of rows in X must match the"); |
|
1395 ::error ("number of elements in range, and the number of columns"); |
|
1396 ::error ("in X must match the number of columns in A"); |
143
|
1397 return; |
1
|
1398 } |
191
|
1399 |
1
|
1400 do_matrix_assignment (rhs, ri, magic_colon); |
|
1401 } |
|
1402 break; |
|
1403 default: |
|
1404 panic_impossible (); |
|
1405 break; |
|
1406 } |
|
1407 } |
|
1408 |
767
|
1409 // -*- MA4 -*- |
1
|
1410 void |
|
1411 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, |
|
1412 tree_constant_rep::constant_type i, |
|
1413 tree_constant& j_arg) |
|
1414 { |
|
1415 tree_constant tmp_j = j_arg.make_numeric_or_range_or_magic (); |
|
1416 |
|
1417 tree_constant_rep::constant_type jtype = tmp_j.const_type (); |
|
1418 |
|
1419 int rhs_nr = rhs.rows (); |
|
1420 int rhs_nc = rhs.columns (); |
|
1421 |
|
1422 switch (jtype) |
|
1423 { |
|
1424 case complex_scalar_constant: |
|
1425 case scalar_constant: |
|
1426 { |
|
1427 int j = tree_to_mat_idx (tmp_j.double_value ()); |
143
|
1428 if (index_check (j, "column") < 0) |
|
1429 return; |
1
|
1430 int nr = rows (); |
208
|
1431 int nc = columns (); |
|
1432 if (nr == 0 && nc == 0 && rhs_nc == 1) |
1
|
1433 { |
|
1434 if (rhs.is_complex_type ()) |
|
1435 { |
|
1436 complex_matrix = new ComplexMatrix (); |
|
1437 type_tag = complex_matrix_constant; |
|
1438 } |
|
1439 else |
|
1440 { |
|
1441 matrix = new Matrix (); |
|
1442 type_tag = matrix_constant; |
|
1443 } |
|
1444 maybe_resize (rhs_nr-1, j); |
191
|
1445 if (error_state) |
|
1446 return; |
1
|
1447 } |
|
1448 else if (indexed_assign_conforms (nr, 1, rhs_nr, rhs_nc)) |
191
|
1449 { |
|
1450 maybe_resize (nr-1, j); |
|
1451 if (error_state) |
|
1452 return; |
|
1453 } |
208
|
1454 else if (rhs_nr == 0 && rhs_nc == 0) |
|
1455 { |
|
1456 if (j < 0 || j >= nc) |
|
1457 { |
240
|
1458 ::error ("A(:,int) = []: column index out of range"); |
208
|
1459 return; |
|
1460 } |
|
1461 } |
1
|
1462 else |
|
1463 { |
240
|
1464 ::error ("A(:,int) = X: X must be a column vector with the same"); |
|
1465 ::error ("number of rows as A"); |
143
|
1466 return; |
1
|
1467 } |
|
1468 |
|
1469 do_matrix_assignment (rhs, magic_colon, j); |
|
1470 } |
|
1471 break; |
|
1472 case complex_matrix_constant: |
|
1473 case matrix_constant: |
|
1474 { |
|
1475 Matrix mj = tmp_j.matrix_value (); |
|
1476 idx_vector jv (mj, user_pref.do_fortran_indexing, "column", |
|
1477 columns ()); |
191
|
1478 if (! jv) |
|
1479 return; |
|
1480 |
1
|
1481 int nr = rows (); |
150
|
1482 int new_nr = nr; |
151
|
1483 if (nr == 0) |
150
|
1484 new_nr = rhs_nr; |
|
1485 |
432
|
1486 if (indexed_assign_conforms (new_nr, jv.capacity (), |
|
1487 rhs_nr, rhs_nc)) |
1
|
1488 { |
208
|
1489 maybe_resize (new_nr-1, jv.max ()); |
|
1490 if (error_state) |
|
1491 return; |
|
1492 } |
|
1493 else if (rhs_nr == 0 && rhs_nc == 0) |
|
1494 { |
|
1495 if (jv.max () >= columns ()) |
|
1496 { |
240
|
1497 ::error ("A(:,matrix) = []: column index out of range"); |
208
|
1498 return; |
|
1499 } |
|
1500 } |
|
1501 else |
|
1502 { |
240
|
1503 ::error ("A(:,matrix) = X: the number of rows in X must match the"); |
|
1504 ::error ("number of rows in A, and the number of columns in X must"); |
|
1505 ::error ("match the number of elements in matrix"); |
143
|
1506 return; |
1
|
1507 } |
191
|
1508 |
1
|
1509 do_matrix_assignment (rhs, magic_colon, jv); |
|
1510 } |
|
1511 break; |
|
1512 case string_constant: |
|
1513 gripe_string_invalid (); |
|
1514 break; |
|
1515 case range_constant: |
|
1516 { |
|
1517 Range rj = tmp_j.range_value (); |
|
1518 int nr = rows (); |
150
|
1519 int new_nr = nr; |
151
|
1520 if (nr == 0) |
150
|
1521 new_nr = rhs_nr; |
|
1522 |
208
|
1523 if (indexed_assign_conforms (new_nr, rj.nelem (), rhs_nr, rhs_nc)) |
|
1524 { |
212
|
1525 int nc = columns (); |
|
1526 if (nc == 2 && is_zero_one (rj) && rhs_nc == 1) |
208
|
1527 { |
|
1528 do_matrix_assignment (rhs, magic_colon, 1); |
|
1529 } |
212
|
1530 else if (nc == 2 && is_one_zero (rj) && rhs_nc == 1) |
|
1531 { |
|
1532 do_matrix_assignment (rhs, magic_colon, 0); |
|
1533 } |
208
|
1534 else |
|
1535 { |
212
|
1536 if (index_check (rj, "column") < 0) |
208
|
1537 return; |
212
|
1538 maybe_resize (new_nr-1, tree_to_mat_idx (rj.max ())); |
208
|
1539 if (error_state) |
|
1540 return; |
|
1541 } |
|
1542 } |
|
1543 else if (rhs_nr == 0 && rhs_nc == 0) |
|
1544 { |
|
1545 int b = tree_to_mat_idx (rj.min ()); |
|
1546 int l = tree_to_mat_idx (rj.max ()); |
|
1547 if (b < 0 || l >= columns ()) |
|
1548 { |
240
|
1549 ::error ("A(:,range) = []: column index out of range"); |
208
|
1550 return; |
|
1551 } |
|
1552 } |
|
1553 else |
1
|
1554 { |
240
|
1555 ::error ("A(:,range) = X: the number of rows in X must match the"); |
|
1556 ::error ("number of rows in A, and the number of columns in X"); |
|
1557 ::error ("must match the number of elements in range"); |
143
|
1558 return; |
1
|
1559 } |
150
|
1560 |
208
|
1561 do_matrix_assignment (rhs, magic_colon, rj); |
1
|
1562 } |
|
1563 break; |
|
1564 case magic_colon: |
|
1565 // a(:,:) = foo is equivalent to a = foo. |
|
1566 do_matrix_assignment (rhs, magic_colon, magic_colon); |
|
1567 break; |
|
1568 default: |
|
1569 panic_impossible (); |
|
1570 break; |
|
1571 } |
|
1572 } |
|
1573 |
767
|
1574 // Functions that actually handle assignment to a matrix using two |
|
1575 // index values. |
|
1576 // |
|
1577 // idx2 |
|
1578 // +---+---+----+----+ |
|
1579 // idx1 | i | v | r | c | |
|
1580 // ---------+---+---+----+----+ |
|
1581 // integer | 1 | 5 | 9 | 13 | |
|
1582 // ---------+---+---+----+----+ |
|
1583 // vector | 2 | 6 | 10 | 14 | |
|
1584 // ---------+---+---+----+----+ |
|
1585 // range | 3 | 7 | 11 | 15 | |
|
1586 // ---------+---+---+----+----+ |
|
1587 // colon | 4 | 8 | 12 | 16 | |
|
1588 // ---------+---+---+----+----+ |
427
|
1589 |
767
|
1590 // -*- 1 -*- |
1
|
1591 void |
|
1592 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, int i, int j) |
|
1593 { |
|
1594 REP_ELEM_ASSIGN (i, j, rhs.double_value (), rhs.complex_value (), |
|
1595 rhs.is_real_type ()); |
|
1596 } |
|
1597 |
767
|
1598 // -*- 2 -*- |
1
|
1599 void |
|
1600 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, int i, |
|
1601 idx_vector& jv) |
|
1602 { |
|
1603 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
399
|
1604 |
1
|
1605 for (int j = 0; j < jv.capacity (); j++) |
|
1606 REP_ELEM_ASSIGN (i, jv.elem (j), rhs_m.elem (0, j), |
|
1607 rhs_cm.elem (0, j), rhs.is_real_type ()); |
|
1608 } |
|
1609 |
767
|
1610 // -*- 3 -*- |
1
|
1611 void |
|
1612 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, int i, Range& rj) |
|
1613 { |
|
1614 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
1615 |
|
1616 double b = rj.base (); |
|
1617 double increment = rj.inc (); |
|
1618 |
|
1619 for (int j = 0; j < rj.nelem (); j++) |
|
1620 { |
|
1621 double tmp = b + j * increment; |
|
1622 int col = tree_to_mat_idx (tmp); |
|
1623 REP_ELEM_ASSIGN (i, col, rhs_m.elem (0, j), rhs_cm.elem (0, j), |
|
1624 rhs.is_real_type ()); |
|
1625 } |
|
1626 } |
|
1627 |
767
|
1628 // -*- 4 -*- |
1
|
1629 void |
|
1630 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, int i, |
|
1631 tree_constant_rep::constant_type mcj) |
|
1632 { |
|
1633 assert (mcj == magic_colon); |
|
1634 |
|
1635 int nc = columns (); |
|
1636 |
208
|
1637 if (rhs.is_zero_by_zero ()) |
|
1638 { |
|
1639 delete_row (i); |
|
1640 } |
|
1641 else if (rhs.is_matrix_type ()) |
1
|
1642 { |
|
1643 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
1644 |
|
1645 for (int j = 0; j < nc; j++) |
|
1646 REP_ELEM_ASSIGN (i, j, rhs_m.elem (0, j), rhs_cm.elem (0, j), |
|
1647 rhs.is_real_type ()); |
|
1648 } |
399
|
1649 else if (rhs.is_scalar_type () && nc == 1) |
1
|
1650 { |
|
1651 REP_ELEM_ASSIGN (i, 0, rhs.double_value (), |
|
1652 rhs.complex_value (), rhs.is_real_type ()); |
|
1653 } |
|
1654 else |
|
1655 panic_impossible (); |
|
1656 } |
|
1657 |
767
|
1658 // -*- 5 -*- |
1
|
1659 void |
|
1660 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, |
|
1661 idx_vector& iv, int j) |
|
1662 { |
|
1663 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
1664 |
|
1665 for (int i = 0; i < iv.capacity (); i++) |
|
1666 { |
|
1667 int row = iv.elem (i); |
|
1668 REP_ELEM_ASSIGN (row, j, rhs_m.elem (i, 0), |
|
1669 rhs_cm.elem (i, 0), rhs.is_real_type ()); |
|
1670 } |
|
1671 } |
|
1672 |
767
|
1673 // -*- 6 -*- |
1
|
1674 void |
|
1675 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, |
|
1676 idx_vector& iv, idx_vector& jv) |
|
1677 { |
|
1678 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
1679 |
|
1680 for (int i = 0; i < iv.capacity (); i++) |
|
1681 { |
|
1682 int row = iv.elem (i); |
|
1683 for (int j = 0; j < jv.capacity (); j++) |
|
1684 { |
|
1685 int col = jv.elem (j); |
|
1686 REP_ELEM_ASSIGN (row, col, rhs_m.elem (i, j), |
|
1687 rhs_cm.elem (i, j), rhs.is_real_type ()); |
|
1688 } |
|
1689 } |
|
1690 } |
|
1691 |
767
|
1692 // -*- 7 -*- |
1
|
1693 void |
|
1694 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, |
|
1695 idx_vector& iv, Range& rj) |
|
1696 { |
|
1697 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
1698 |
|
1699 double b = rj.base (); |
|
1700 double increment = rj.inc (); |
|
1701 |
|
1702 for (int i = 0; i < iv.capacity (); i++) |
|
1703 { |
|
1704 int row = iv.elem (i); |
|
1705 for (int j = 0; j < rj.nelem (); j++) |
|
1706 { |
|
1707 double tmp = b + j * increment; |
|
1708 int col = tree_to_mat_idx (tmp); |
|
1709 REP_ELEM_ASSIGN (row, col, rhs_m.elem (i, j), |
|
1710 rhs_cm.elem (i, j), rhs.is_real_type ()); |
|
1711 } |
|
1712 } |
|
1713 } |
|
1714 |
767
|
1715 // -*- 8 -*- |
1
|
1716 void |
|
1717 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, idx_vector& iv, |
|
1718 tree_constant_rep::constant_type mcj) |
|
1719 { |
|
1720 assert (mcj == magic_colon); |
|
1721 |
208
|
1722 if (rhs.is_zero_by_zero ()) |
|
1723 { |
|
1724 delete_rows (iv); |
|
1725 } |
|
1726 else |
1
|
1727 { |
208
|
1728 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
1729 |
|
1730 int nc = columns (); |
|
1731 |
|
1732 for (int j = 0; j < nc; j++) |
1
|
1733 { |
208
|
1734 for (int i = 0; i < iv.capacity (); i++) |
|
1735 { |
|
1736 int row = iv.elem (i); |
|
1737 REP_ELEM_ASSIGN (row, j, rhs_m.elem (i, j), |
|
1738 rhs_cm.elem (i, j), rhs.is_real_type ()); |
|
1739 } |
1
|
1740 } |
|
1741 } |
|
1742 } |
|
1743 |
767
|
1744 // -*- 9 -*- |
1
|
1745 void |
|
1746 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, Range& ri, int j) |
|
1747 { |
|
1748 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
1749 |
|
1750 double b = ri.base (); |
|
1751 double increment = ri.inc (); |
|
1752 |
|
1753 for (int i = 0; i < ri.nelem (); i++) |
|
1754 { |
|
1755 double tmp = b + i * increment; |
|
1756 int row = tree_to_mat_idx (tmp); |
|
1757 REP_ELEM_ASSIGN (row, j, rhs_m.elem (i, 0), |
|
1758 rhs_cm.elem (i, 0), rhs.is_real_type ()); |
|
1759 } |
|
1760 } |
|
1761 |
767
|
1762 // -*- 10 -*- |
1
|
1763 void |
|
1764 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, Range& ri, |
|
1765 idx_vector& jv) |
|
1766 { |
|
1767 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
1768 |
|
1769 double b = ri.base (); |
|
1770 double increment = ri.inc (); |
|
1771 |
|
1772 for (int j = 0; j < jv.capacity (); j++) |
|
1773 { |
|
1774 int col = jv.elem (j); |
|
1775 for (int i = 0; i < ri.nelem (); i++) |
|
1776 { |
|
1777 double tmp = b + i * increment; |
|
1778 int row = tree_to_mat_idx (tmp); |
|
1779 REP_ELEM_ASSIGN (row, col, rhs_m.elem (i, j), |
|
1780 rhs_m.elem (i, j), rhs.is_real_type ()); |
|
1781 } |
|
1782 } |
|
1783 } |
|
1784 |
767
|
1785 // -*- 11 -*- |
1
|
1786 void |
|
1787 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, Range& ri, |
|
1788 Range& rj) |
|
1789 { |
|
1790 double ib = ri.base (); |
|
1791 double iinc = ri.inc (); |
|
1792 double jb = rj.base (); |
|
1793 double jinc = rj.inc (); |
|
1794 |
|
1795 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
1796 |
|
1797 for (int i = 0; i < ri.nelem (); i++) |
|
1798 { |
|
1799 double itmp = ib + i * iinc; |
|
1800 int row = tree_to_mat_idx (itmp); |
|
1801 for (int j = 0; j < rj.nelem (); j++) |
|
1802 { |
|
1803 double jtmp = jb + j * jinc; |
|
1804 int col = tree_to_mat_idx (jtmp); |
|
1805 REP_ELEM_ASSIGN (row, col, rhs_m.elem (i, j), |
|
1806 rhs_cm.elem (i, j), rhs.is_real_type ()); |
|
1807 } |
|
1808 } |
|
1809 } |
|
1810 |
767
|
1811 // -*- 12 -*- |
1
|
1812 void |
|
1813 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, Range& ri, |
|
1814 tree_constant_rep::constant_type mcj) |
|
1815 { |
|
1816 assert (mcj == magic_colon); |
|
1817 |
208
|
1818 if (rhs.is_zero_by_zero ()) |
|
1819 { |
|
1820 delete_rows (ri); |
|
1821 } |
|
1822 else |
|
1823 { |
|
1824 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
1
|
1825 |
208
|
1826 double ib = ri.base (); |
|
1827 double iinc = ri.inc (); |
1
|
1828 |
208
|
1829 int nc = columns (); |
|
1830 |
|
1831 for (int i = 0; i < ri.nelem (); i++) |
|
1832 { |
|
1833 double itmp = ib + i * iinc; |
|
1834 int row = tree_to_mat_idx (itmp); |
|
1835 for (int j = 0; j < nc; j++) |
|
1836 REP_ELEM_ASSIGN (row, j, rhs_m.elem (i, j), |
|
1837 rhs_cm.elem (i, j), rhs.is_real_type ()); |
|
1838 } |
1
|
1839 } |
|
1840 } |
|
1841 |
767
|
1842 // -*- 13 -*- |
1
|
1843 void |
|
1844 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, |
|
1845 tree_constant_rep::constant_type mci, |
|
1846 int j) |
|
1847 { |
|
1848 assert (mci == magic_colon); |
|
1849 |
|
1850 int nr = rows (); |
|
1851 |
208
|
1852 if (rhs.is_zero_by_zero ()) |
|
1853 { |
|
1854 delete_column (j); |
|
1855 } |
|
1856 else if (rhs.is_matrix_type ()) |
1
|
1857 { |
|
1858 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
1859 |
|
1860 for (int i = 0; i < nr; i++) |
|
1861 REP_ELEM_ASSIGN (i, j, rhs_m.elem (i, 0), |
|
1862 rhs_cm.elem (i, 0), rhs.is_real_type ()); |
|
1863 } |
399
|
1864 else if (rhs.is_scalar_type () && nr == 1) |
1
|
1865 { |
|
1866 REP_ELEM_ASSIGN (0, j, rhs.double_value (), |
|
1867 rhs.complex_value (), rhs.is_real_type ()); |
|
1868 } |
|
1869 else |
|
1870 panic_impossible (); |
|
1871 } |
|
1872 |
767
|
1873 // -*- 14 -*- |
1
|
1874 void |
|
1875 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, |
|
1876 tree_constant_rep::constant_type mci, |
|
1877 idx_vector& jv) |
|
1878 { |
|
1879 assert (mci == magic_colon); |
|
1880 |
208
|
1881 if (rhs.is_zero_by_zero ()) |
|
1882 { |
|
1883 delete_columns (jv); |
|
1884 } |
|
1885 else |
1
|
1886 { |
208
|
1887 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
|
1888 |
|
1889 int nr = rows (); |
|
1890 |
|
1891 for (int i = 0; i < nr; i++) |
1
|
1892 { |
208
|
1893 for (int j = 0; j < jv.capacity (); j++) |
|
1894 { |
|
1895 int col = jv.elem (j); |
|
1896 REP_ELEM_ASSIGN (i, col, rhs_m.elem (i, j), |
|
1897 rhs_cm.elem (i, j), rhs.is_real_type ()); |
|
1898 } |
1
|
1899 } |
|
1900 } |
|
1901 } |
|
1902 |
767
|
1903 // -*- 15 -*- |
1
|
1904 void |
|
1905 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, |
|
1906 tree_constant_rep::constant_type mci, |
|
1907 Range& rj) |
|
1908 { |
|
1909 assert (mci == magic_colon); |
|
1910 |
208
|
1911 if (rhs.is_zero_by_zero ()) |
|
1912 { |
|
1913 delete_columns (rj); |
|
1914 } |
|
1915 else |
|
1916 { |
|
1917 REP_RHS_MATRIX (rhs, rhs_m, rhs_cm, rhs_nr, rhs_nc); |
1
|
1918 |
208
|
1919 int nr = rows (); |
1
|
1920 |
208
|
1921 double jb = rj.base (); |
|
1922 double jinc = rj.inc (); |
|
1923 |
|
1924 for (int j = 0; j < rj.nelem (); j++) |
1
|
1925 { |
208
|
1926 double jtmp = jb + j * jinc; |
|
1927 int col = tree_to_mat_idx (jtmp); |
|
1928 for (int i = 0; i < nr; i++) |
|
1929 { |
|
1930 REP_ELEM_ASSIGN (i, col, rhs_m.elem (i, j), |
|
1931 rhs_cm.elem (i, j), rhs.is_real_type ()); |
|
1932 } |
1
|
1933 } |
|
1934 } |
|
1935 } |
|
1936 |
767
|
1937 // -*- 16 -*- |
1
|
1938 void |
|
1939 tree_constant_rep::do_matrix_assignment (tree_constant& rhs, |
|
1940 tree_constant_rep::constant_type mci, |
|
1941 tree_constant_rep::constant_type mcj) |
|
1942 { |
|
1943 assert (mci == magic_colon && mcj == magic_colon); |
|
1944 |
|
1945 switch (type_tag) |
|
1946 { |
|
1947 case scalar_constant: |
|
1948 break; |
|
1949 case matrix_constant: |
|
1950 delete matrix; |
|
1951 break; |
|
1952 case complex_scalar_constant: |
|
1953 delete complex_scalar; |
|
1954 break; |
|
1955 case complex_matrix_constant: |
|
1956 delete complex_matrix; |
|
1957 break; |
|
1958 case string_constant: |
|
1959 delete [] string; |
|
1960 break; |
|
1961 case range_constant: |
|
1962 delete range; |
|
1963 break; |
|
1964 case magic_colon: |
|
1965 default: |
|
1966 panic_impossible (); |
|
1967 break; |
|
1968 } |
|
1969 |
|
1970 type_tag = rhs.const_type (); |
|
1971 |
|
1972 switch (type_tag) |
|
1973 { |
|
1974 case scalar_constant: |
|
1975 scalar = rhs.double_value (); |
|
1976 break; |
|
1977 case matrix_constant: |
|
1978 matrix = new Matrix (rhs.matrix_value ()); |
|
1979 break; |
|
1980 case string_constant: |
|
1981 string = strsave (rhs.string_value ()); |
|
1982 break; |
|
1983 case complex_matrix_constant: |
|
1984 complex_matrix = new ComplexMatrix (rhs.complex_matrix_value ()); |
|
1985 break; |
|
1986 case complex_scalar_constant: |
|
1987 complex_scalar = new Complex (rhs.complex_value ()); |
|
1988 break; |
|
1989 case range_constant: |
|
1990 range = new Range (rhs.range_value ()); |
|
1991 break; |
|
1992 case magic_colon: |
|
1993 default: |
|
1994 panic_impossible (); |
|
1995 break; |
|
1996 } |
|
1997 } |
|
1998 |
767
|
1999 // Functions for deleting rows or columns of a matrix. These are used |
|
2000 // to handle statements like |
|
2001 // |
|
2002 // M (i, j) = [] |
|
2003 |
208
|
2004 void |
|
2005 tree_constant_rep::delete_row (int idx) |
|
2006 { |
|
2007 if (type_tag == matrix_constant) |
|
2008 { |
|
2009 int nr = matrix->rows (); |
|
2010 int nc = matrix->columns (); |
|
2011 Matrix *new_matrix = new Matrix (nr-1, nc); |
|
2012 int ii = 0; |
|
2013 for (int i = 0; i < nr; i++) |
|
2014 { |
|
2015 if (i != idx) |
|
2016 { |
|
2017 for (int j = 0; j < nc; j++) |
|
2018 new_matrix->elem (ii, j) = matrix->elem (i, j); |
|
2019 ii++; |
|
2020 } |
|
2021 } |
|
2022 delete matrix; |
|
2023 matrix = new_matrix; |
|
2024 } |
|
2025 else if (type_tag == complex_matrix_constant) |
|
2026 { |
|
2027 int nr = complex_matrix->rows (); |
|
2028 int nc = complex_matrix->columns (); |
|
2029 ComplexMatrix *new_matrix = new ComplexMatrix (nr-1, nc); |
|
2030 int ii = 0; |
|
2031 for (int i = 0; i < nr; i++) |
|
2032 { |
|
2033 if (i != idx) |
|
2034 { |
|
2035 for (int j = 0; j < nc; j++) |
|
2036 new_matrix->elem (ii, j) = complex_matrix->elem (i, j); |
|
2037 ii++; |
|
2038 } |
|
2039 } |
|
2040 delete complex_matrix; |
|
2041 complex_matrix = new_matrix; |
|
2042 } |
|
2043 else |
|
2044 panic_impossible (); |
|
2045 } |
|
2046 |
|
2047 void |
|
2048 tree_constant_rep::delete_rows (idx_vector& iv) |
|
2049 { |
416
|
2050 iv.sort_uniq (); |
208
|
2051 int num_to_delete = iv.length (); |
|
2052 |
435
|
2053 int nr = rows (); |
|
2054 int nc = columns (); |
|
2055 |
|
2056 // If deleting all rows of a column vector, make result 0x0. |
|
2057 if (nc == 1 && num_to_delete == nr) |
|
2058 nc = 0; |
|
2059 |
208
|
2060 if (type_tag == matrix_constant) |
|
2061 { |
|
2062 Matrix *new_matrix = new Matrix (nr-num_to_delete, nc); |
|
2063 if (nr > num_to_delete) |
|
2064 { |
|
2065 int ii = 0; |
|
2066 int idx = 0; |
|
2067 for (int i = 0; i < nr; i++) |
|
2068 { |
|
2069 if (i == iv.elem (idx)) |
|
2070 idx++; |
|
2071 else |
|
2072 { |
|
2073 for (int j = 0; j < nc; j++) |
|
2074 new_matrix->elem (ii, j) = matrix->elem (i, j); |
|
2075 ii++; |
|
2076 } |
|
2077 } |
|
2078 } |
|
2079 delete matrix; |
|
2080 matrix = new_matrix; |
|
2081 } |
|
2082 else if (type_tag == complex_matrix_constant) |
|
2083 { |
|
2084 ComplexMatrix *new_matrix = new ComplexMatrix (nr-num_to_delete, nc); |
|
2085 if (nr > num_to_delete) |
|
2086 { |
|
2087 int ii = 0; |
|
2088 int idx = 0; |
|
2089 for (int i = 0; i < nr; i++) |
|
2090 { |
|
2091 if (i == iv.elem (idx)) |
|
2092 idx++; |
|
2093 else |
|
2094 { |
|
2095 for (int j = 0; j < nc; j++) |
|
2096 new_matrix->elem (ii, j) = complex_matrix->elem (i, j); |
|
2097 ii++; |
|
2098 } |
|
2099 } |
|
2100 } |
|
2101 delete complex_matrix; |
|
2102 complex_matrix = new_matrix; |
|
2103 } |
|
2104 else |
|
2105 panic_impossible (); |
|
2106 } |
|
2107 |
|
2108 void |
|
2109 tree_constant_rep::delete_rows (Range& ri) |
|
2110 { |
|
2111 ri.sort (); |
|
2112 int num_to_delete = ri.nelem (); |
|
2113 |
435
|
2114 int nr = rows (); |
|
2115 int nc = columns (); |
|
2116 |
|
2117 // If deleting all rows of a column vector, make result 0x0. |
|
2118 if (nc == 1 && num_to_delete == nr) |
|
2119 nc = 0; |
|
2120 |
208
|
2121 double ib = ri.base (); |
|
2122 double iinc = ri.inc (); |
|
2123 |
|
2124 int max_idx = tree_to_mat_idx (ri.max ()); |
|
2125 |
|
2126 if (type_tag == matrix_constant) |
|
2127 { |
|
2128 Matrix *new_matrix = new Matrix (nr-num_to_delete, nc); |
|
2129 if (nr > num_to_delete) |
|
2130 { |
|
2131 int ii = 0; |
|
2132 int idx = 0; |
|
2133 for (int i = 0; i < nr; i++) |
|
2134 { |
|
2135 double itmp = ib + idx * iinc; |
|
2136 int row = tree_to_mat_idx (itmp); |
|
2137 |
|
2138 if (i == row && row <= max_idx) |
|
2139 idx++; |
|
2140 else |
|
2141 { |
|
2142 for (int j = 0; j < nc; j++) |
|
2143 new_matrix->elem (ii, j) = matrix->elem (i, j); |
|
2144 ii++; |
|
2145 } |
|
2146 } |
|
2147 } |
|
2148 delete matrix; |
|
2149 matrix = new_matrix; |
|
2150 } |
|
2151 else if (type_tag == complex_matrix_constant) |
|
2152 { |
|
2153 ComplexMatrix *new_matrix = new ComplexMatrix (nr-num_to_delete, nc); |
|
2154 if (nr > num_to_delete) |
|
2155 { |
|
2156 int ii = 0; |
|
2157 int idx = 0; |
|
2158 for (int i = 0; i < nr; i++) |
|
2159 { |
|
2160 double itmp = ib + idx * iinc; |
|
2161 int row = tree_to_mat_idx (itmp); |
|
2162 |
|
2163 if (i == row && row <= max_idx) |
|
2164 idx++; |
|
2165 else |
|
2166 { |
|
2167 for (int j = 0; j < nc; j++) |
|
2168 new_matrix->elem (ii, j) = complex_matrix->elem (i, j); |
|
2169 ii++; |
|
2170 } |
|
2171 } |
|
2172 } |
|
2173 delete complex_matrix; |
|
2174 complex_matrix = new_matrix; |
|
2175 } |
|
2176 else |
|
2177 panic_impossible (); |
|
2178 } |
|
2179 |
|
2180 void |
|
2181 tree_constant_rep::delete_column (int idx) |
|
2182 { |
|
2183 if (type_tag == matrix_constant) |
|
2184 { |
|
2185 int nr = matrix->rows (); |
|
2186 int nc = matrix->columns (); |
|
2187 Matrix *new_matrix = new Matrix (nr, nc-1); |
|
2188 int jj = 0; |
|
2189 for (int j = 0; j < nc; j++) |
|
2190 { |
|
2191 if (j != idx) |
|
2192 { |
|
2193 for (int i = 0; i < nr; i++) |
|
2194 new_matrix->elem (i, jj) = matrix->elem (i, j); |
|
2195 jj++; |
|
2196 } |
|
2197 } |
|
2198 delete matrix; |
|
2199 matrix = new_matrix; |
|
2200 } |
|
2201 else if (type_tag == complex_matrix_constant) |
|
2202 { |
|
2203 int nr = complex_matrix->rows (); |
|
2204 int nc = complex_matrix->columns (); |
|
2205 ComplexMatrix *new_matrix = new ComplexMatrix (nr, nc-1); |
|
2206 int jj = 0; |
|
2207 for (int j = 0; j < nc; j++) |
|
2208 { |
|
2209 if (j != idx) |
|
2210 { |
|
2211 for (int i = 0; i < nr; i++) |
|
2212 new_matrix->elem (i, jj) = complex_matrix->elem (i, j); |
|
2213 jj++; |
|
2214 } |
|
2215 } |
|
2216 delete complex_matrix; |
|
2217 complex_matrix = new_matrix; |
|
2218 } |
|
2219 else |
|
2220 panic_impossible (); |
|
2221 } |
|
2222 |
|
2223 void |
|
2224 tree_constant_rep::delete_columns (idx_vector& jv) |
|
2225 { |
416
|
2226 jv.sort_uniq (); |
208
|
2227 int num_to_delete = jv.length (); |
|
2228 |
435
|
2229 int nr = rows (); |
|
2230 int nc = columns (); |
|
2231 |
|
2232 // If deleting all columns of a row vector, make result 0x0. |
|
2233 if (nr == 1 && num_to_delete == nc) |
|
2234 nr = 0; |
|
2235 |
208
|
2236 if (type_tag == matrix_constant) |
|
2237 { |
|
2238 Matrix *new_matrix = new Matrix (nr, nc-num_to_delete); |
|
2239 if (nc > num_to_delete) |
|
2240 { |
|
2241 int jj = 0; |
|
2242 int idx = 0; |
|
2243 for (int j = 0; j < nc; j++) |
|
2244 { |
|
2245 if (j == jv.elem (idx)) |
|
2246 idx++; |
|
2247 else |
|
2248 { |
|
2249 for (int i = 0; i < nr; i++) |
|
2250 new_matrix->elem (i, jj) = matrix->elem (i, j); |
|
2251 jj++; |
|
2252 } |
|
2253 } |
|
2254 } |
|
2255 delete matrix; |
|
2256 matrix = new_matrix; |
|
2257 } |
|
2258 else if (type_tag == complex_matrix_constant) |
|
2259 { |
|
2260 ComplexMatrix *new_matrix = new ComplexMatrix (nr, nc-num_to_delete); |
|
2261 if (nc > num_to_delete) |
|
2262 { |
|
2263 int jj = 0; |
|
2264 int idx = 0; |
|
2265 for (int j = 0; j < nc; j++) |
|
2266 { |
|
2267 if (j == jv.elem (idx)) |
|
2268 idx++; |
|
2269 else |
|
2270 { |
|
2271 for (int i = 0; i < nr; i++) |
|
2272 new_matrix->elem (i, jj) = complex_matrix->elem (i, j); |
|
2273 jj++; |
|
2274 } |
|
2275 } |
|
2276 } |
|
2277 delete complex_matrix; |
|
2278 complex_matrix = new_matrix; |
|
2279 } |
|
2280 else |
|
2281 panic_impossible (); |
|
2282 } |
|
2283 |
|
2284 void |
|
2285 tree_constant_rep::delete_columns (Range& rj) |
|
2286 { |
|
2287 rj.sort (); |
|
2288 int num_to_delete = rj.nelem (); |
|
2289 |
435
|
2290 int nr = rows (); |
|
2291 int nc = columns (); |
|
2292 |
|
2293 // If deleting all columns of a row vector, make result 0x0. |
|
2294 if (nr == 1 && num_to_delete == nc) |
|
2295 nr = 0; |
|
2296 |
208
|
2297 double jb = rj.base (); |
|
2298 double jinc = rj.inc (); |
|
2299 |
|
2300 int max_idx = tree_to_mat_idx (rj.max ()); |
|
2301 |
|
2302 if (type_tag == matrix_constant) |
|
2303 { |
|
2304 Matrix *new_matrix = new Matrix (nr, nc-num_to_delete); |
|
2305 if (nc > num_to_delete) |
|
2306 { |
|
2307 int jj = 0; |
|
2308 int idx = 0; |
|
2309 for (int j = 0; j < nc; j++) |
|
2310 { |
|
2311 double jtmp = jb + idx * jinc; |
|
2312 int col = tree_to_mat_idx (jtmp); |
|
2313 |
|
2314 if (j == col && col <= max_idx) |
|
2315 idx++; |
|
2316 else |
|
2317 { |
|
2318 for (int i = 0; i < nr; i++) |
|
2319 new_matrix->elem (i, jj) = matrix->elem (i, j); |
|
2320 jj++; |
|
2321 } |
|
2322 } |
|
2323 } |
|
2324 delete matrix; |
|
2325 matrix = new_matrix; |
|
2326 } |
|
2327 else if (type_tag == complex_matrix_constant) |
|
2328 { |
|
2329 ComplexMatrix *new_matrix = new ComplexMatrix (nr, nc-num_to_delete); |
|
2330 if (nc > num_to_delete) |
|
2331 { |
|
2332 int jj = 0; |
|
2333 int idx = 0; |
|
2334 for (int j = 0; j < nc; j++) |
|
2335 { |
|
2336 double jtmp = jb + idx * jinc; |
|
2337 int col = tree_to_mat_idx (jtmp); |
|
2338 |
|
2339 if (j == col && col <= max_idx) |
|
2340 idx++; |
|
2341 else |
|
2342 { |
|
2343 for (int i = 0; i < nr; i++) |
|
2344 new_matrix->elem (i, jj) = complex_matrix->elem (i, j); |
|
2345 jj++; |
|
2346 } |
|
2347 } |
|
2348 } |
|
2349 delete complex_matrix; |
|
2350 complex_matrix = new_matrix; |
|
2351 } |
|
2352 else |
|
2353 panic_impossible (); |
|
2354 } |
|
2355 |
1
|
2356 /* |
|
2357 ;;; Local Variables: *** |
|
2358 ;;; mode: C++ *** |
|
2359 ;;; page-delimiter: "^/\\*" *** |
|
2360 ;;; End: *** |
|
2361 */ |