1741
|
1 // tree-mat.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
|
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 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
|
32 #include <iostream.h> |
|
33 #include <strstream.h> |
|
34 |
|
35 #include "error.h" |
|
36 #include "oct-obj.h" |
|
37 #include "pt-const.h" |
|
38 #include "pt-exp.h" |
|
39 #include "pt-fvc.h" |
|
40 #include "pt-mat.h" |
|
41 #include "pt-misc.h" |
|
42 #include "pt-mvr.h" |
|
43 #include "user-prefs.h" |
|
44 |
|
45 // General matrices. This list type is much more work to handle than |
|
46 // constant matrices, but it allows us to construct matrices from |
|
47 // other matrices, variables, and functions. |
|
48 |
|
49 tree_matrix::~tree_matrix (void) |
|
50 { |
|
51 delete element; |
|
52 delete next; |
|
53 } |
|
54 |
|
55 int |
|
56 tree_matrix::is_matrix_constant (void) const |
|
57 { |
|
58 const tree_matrix *list = this; |
|
59 |
|
60 while (list) |
|
61 { |
|
62 tree_expression *elem = list->element; |
|
63 |
|
64 if (! elem->is_constant ()) |
|
65 return 0; |
|
66 |
|
67 list = list->next; |
|
68 } |
|
69 |
|
70 return 1; |
|
71 } |
|
72 |
|
73 tree_matrix * |
|
74 tree_matrix::chain (tree_expression *t, tree_matrix::dir d) |
|
75 { |
|
76 tree_matrix *tmp = new tree_matrix (t, d); |
|
77 tmp->next = this; |
|
78 return tmp; |
|
79 } |
|
80 |
|
81 tree_matrix * |
|
82 tree_matrix::reverse (void) |
|
83 { |
|
84 tree_matrix *list = this; |
|
85 tree_matrix *next; |
|
86 tree_matrix *prev = 0; |
|
87 |
|
88 while (list) |
|
89 { |
|
90 next = list->next; |
|
91 list->next = prev; |
|
92 prev = list; |
|
93 list = next; |
|
94 } |
|
95 return prev; |
|
96 } |
|
97 |
|
98 int |
|
99 tree_matrix::length (void) |
|
100 { |
|
101 tree_matrix *list = this; |
|
102 int len = 0; |
|
103 while (list) |
|
104 { |
|
105 len++; |
|
106 list = list->next; |
|
107 } |
|
108 return len; |
|
109 } |
|
110 |
|
111 tree_return_list * |
|
112 tree_matrix::to_return_list (void) |
|
113 { |
|
114 tree_return_list *retval = 0; |
|
115 |
|
116 tree_matrix *list; |
|
117 |
|
118 for (list = this; list; list = list->next) |
|
119 { |
|
120 tree_expression *elem = list->element; |
|
121 |
|
122 int is_id = elem->is_identifier (); |
|
123 |
|
124 int is_idx_expr = elem->is_index_expression (); |
|
125 |
|
126 if (is_id || is_idx_expr) |
|
127 { |
|
128 tree_index_expression *idx_expr; |
|
129 if (is_id) |
|
130 { |
|
131 tree_identifier *id = (tree_identifier *) elem; |
|
132 idx_expr = new tree_index_expression (id); |
|
133 } |
|
134 else |
|
135 idx_expr = (tree_index_expression *) elem; |
|
136 |
|
137 if (list == this) |
|
138 retval = new tree_return_list (idx_expr); |
|
139 else |
|
140 retval->append (idx_expr); |
|
141 } |
|
142 else |
|
143 { |
|
144 delete retval; |
|
145 retval = 0; |
|
146 break; |
|
147 } |
|
148 } |
|
149 |
|
150 return retval; |
|
151 } |
|
152 |
|
153 // Just about as ugly as it gets. |
|
154 |
|
155 struct const_matrix_list |
|
156 { |
|
157 tree_matrix::dir direction; |
|
158 tree_constant elem; |
|
159 int nr; |
|
160 int nc; |
|
161 }; |
|
162 |
|
163 // Less ugly than before, anyway. |
|
164 |
|
165 tree_constant |
|
166 tree_matrix::eval (int /* print */) |
|
167 { |
|
168 tree_constant retval; |
|
169 |
|
170 if (error_state) |
|
171 return retval; |
|
172 |
|
173 // Just count the elements without looking at them. |
|
174 |
|
175 int total_len = length (); |
|
176 |
|
177 // Easier to deal with this later instead of a tree_matrix |
|
178 // structure. |
|
179 |
|
180 const_matrix_list *list = new const_matrix_list [total_len]; |
|
181 |
|
182 // Stats we want to keep track of. |
|
183 |
|
184 int all_strings = 1; |
|
185 |
|
186 int found_complex = 0; |
|
187 |
|
188 int row_total = 0; |
|
189 int col_total = 0; |
|
190 |
|
191 int row_height = 0; |
|
192 |
|
193 int cols_this_row = 0; |
|
194 |
|
195 int first_row = 1; |
|
196 |
|
197 int empties_ok = user_pref.empty_list_elements_ok; |
|
198 |
|
199 tree_matrix *ptr = this; |
|
200 |
|
201 // Stuff for the result matrix or string. Declared here so that we |
|
202 // don't get warnings from gcc about the goto crossing the |
|
203 // initialization of these values. |
|
204 |
|
205 int put_row = 0; |
|
206 int put_col = 0; |
|
207 |
|
208 int prev_nr = 0; |
|
209 int prev_nc = 0; |
|
210 |
|
211 Matrix m; |
|
212 ComplexMatrix cm; |
|
213 charMatrix chm; |
|
214 |
|
215 // Eliminate empties and gather stats. |
|
216 |
|
217 int found_new_row_in_empties = 0; |
|
218 |
|
219 int len = 0; |
|
220 for (int i = 0; i < total_len; i++) |
|
221 { |
|
222 tree_expression *elem = ptr->element; |
|
223 if (! elem) |
|
224 { |
|
225 retval = tree_constant (Matrix ()); |
|
226 goto done; |
|
227 } |
|
228 |
|
229 tree_constant tmp = elem->eval (0); |
|
230 if (error_state || tmp.is_undefined ()) |
|
231 { |
|
232 retval = tree_constant (); |
|
233 goto done; |
|
234 } |
|
235 |
|
236 int nr = tmp.rows (); |
|
237 int nc = tmp.columns (); |
|
238 |
|
239 dir direct = ptr->direction; |
|
240 |
|
241 if (nr == 0 || nc == 0) |
|
242 { |
|
243 if (empties_ok < 0) |
|
244 warning ("empty matrix found in matrix list"); |
|
245 else if (empties_ok == 0) |
|
246 { |
|
247 ::error ("empty matrix found in matrix list"); |
|
248 retval = tree_constant (); |
|
249 goto done; |
|
250 } |
|
251 |
|
252 if (direct == md_down) |
|
253 found_new_row_in_empties = 1; |
|
254 |
|
255 goto next; |
|
256 } |
|
257 |
|
258 if (found_new_row_in_empties) |
|
259 { |
|
260 found_new_row_in_empties = 0; |
|
261 list[len].direction = md_down; |
|
262 } |
|
263 else |
|
264 list[len].direction = direct; |
|
265 |
|
266 list[len].elem = tmp; |
|
267 list[len].nr = nr; |
|
268 list[len].nc = nc; |
|
269 |
|
270 if (all_strings && ! tmp.is_string ()) |
|
271 all_strings = 0; |
|
272 |
|
273 if (! found_complex && tmp.is_complex_type ()) |
|
274 found_complex = 1; |
|
275 |
|
276 len++; |
|
277 |
|
278 next: |
|
279 |
|
280 ptr = ptr->next; |
|
281 } |
|
282 |
|
283 // if (all_strings) |
|
284 // cerr << "all strings\n"; |
|
285 |
|
286 // Compute size of result matrix, and check to see that the dimensions |
|
287 // of all the elements will match up properly. |
|
288 |
|
289 for (int i = 0; i < len; i++) |
|
290 { |
|
291 dir direct = list[i].direction; |
|
292 |
|
293 int nr = list[i].nr; |
|
294 int nc = list[i].nc; |
|
295 |
|
296 if (i == 0) |
|
297 { |
|
298 row_total = nr; |
|
299 col_total = nc; |
|
300 |
|
301 row_height = nr; |
|
302 cols_this_row = nc; |
|
303 } |
|
304 else |
|
305 { |
|
306 switch (direct) |
|
307 { |
|
308 case md_right: |
|
309 { |
|
310 if (nr != row_height) |
|
311 { |
|
312 ::error ("number of rows must match"); |
|
313 goto done; |
|
314 } |
|
315 else |
|
316 { |
|
317 cols_this_row += nc; |
|
318 |
|
319 if (first_row) |
|
320 col_total = cols_this_row; |
|
321 else if (all_strings && cols_this_row > col_total) |
|
322 col_total = cols_this_row; |
|
323 } |
|
324 } |
|
325 break; |
|
326 |
|
327 case md_down: |
|
328 { |
|
329 if (cols_this_row != col_total && ! all_strings) |
|
330 { |
|
331 ::error ("number of columns must match"); |
|
332 goto done; |
|
333 } |
|
334 first_row = 0; |
|
335 row_total += nr; |
|
336 row_height = nr; |
|
337 cols_this_row = nc; |
|
338 } |
|
339 break; |
|
340 |
|
341 default: |
|
342 panic_impossible (); |
|
343 break; |
|
344 } |
|
345 } |
|
346 } |
|
347 |
|
348 // Don't forget to check to see if the last element will fit. |
|
349 |
|
350 if (all_strings && cols_this_row > col_total) |
|
351 { |
|
352 col_total = cols_this_row; |
|
353 } |
|
354 else if (cols_this_row != col_total) |
|
355 { |
|
356 ::error ("number of columns must match"); |
|
357 goto done; |
|
358 } |
|
359 |
|
360 // Now, extract the values from the individual elements and insert |
|
361 // them in the result matrix. |
|
362 |
|
363 if (all_strings) |
|
364 chm.resize (row_total, col_total, 0); |
|
365 else if (found_complex) |
|
366 cm.resize (row_total, col_total, 0.0); |
|
367 else |
|
368 m.resize (row_total, col_total, 0.0); |
|
369 |
|
370 for (int i = 0; i < len; i++) |
|
371 { |
|
372 tree_constant tmp = list[i].elem; |
|
373 |
|
374 int nr = list[i].nr; |
|
375 int nc = list[i].nc; |
|
376 |
|
377 if (nr == 0 || nc == 0) |
|
378 continue; |
|
379 |
|
380 if (i == 0) |
|
381 { |
|
382 put_row = 0; |
|
383 put_col = 0; |
|
384 } |
|
385 else |
|
386 { |
|
387 switch (list[i].direction) |
|
388 { |
|
389 case md_right: |
|
390 put_col += prev_nc; |
|
391 break; |
|
392 |
|
393 case md_down: |
|
394 put_row += prev_nr; |
|
395 put_col = 0; |
|
396 break; |
|
397 |
|
398 default: |
|
399 panic_impossible (); |
|
400 break; |
|
401 } |
|
402 } |
|
403 |
|
404 if (found_complex) |
|
405 { |
|
406 if (tmp.is_real_scalar ()) |
|
407 { |
|
408 cm (put_row, put_col) = tmp.double_value (); |
|
409 } |
|
410 else if (tmp.is_real_matrix () || tmp.is_range ()) |
|
411 { |
|
412 cm.insert (tmp.matrix_value (), put_row, put_col); |
|
413 } |
|
414 else if (tmp.is_complex_scalar ()) |
|
415 { |
|
416 cm (put_row, put_col) = tmp.complex_value (); |
|
417 } |
|
418 else |
|
419 { |
|
420 ComplexMatrix cm_tmp = tmp.complex_matrix_value (); |
|
421 |
|
422 if (error_state) |
|
423 goto done; |
|
424 |
|
425 cm.insert (cm_tmp, put_row, put_col); |
|
426 } |
|
427 } |
|
428 else |
|
429 { |
|
430 if (tmp.is_real_scalar ()) |
|
431 { |
|
432 m (put_row, put_col) = tmp.double_value (); |
|
433 } |
|
434 else if (tmp.is_string () && all_strings) |
|
435 { |
|
436 charMatrix chm_tmp = tmp.all_strings (); |
|
437 |
|
438 if (error_state) |
|
439 goto done; |
|
440 |
|
441 chm.insert (chm_tmp, put_row, put_col); |
|
442 } |
|
443 else |
|
444 { |
|
445 Matrix m_tmp = tmp.matrix_value (); |
|
446 |
|
447 if (error_state) |
|
448 goto done; |
|
449 |
|
450 m.insert (m_tmp, put_row, put_col); |
|
451 } |
|
452 } |
|
453 |
|
454 prev_nr = nr; |
|
455 prev_nc = nc; |
|
456 } |
|
457 |
|
458 if (all_strings && chm.rows () > 0 && chm.cols () > 0) |
|
459 retval = tree_constant (chm, 1); |
|
460 else if (found_complex) |
|
461 retval = cm; |
|
462 else |
|
463 retval = m; |
|
464 |
|
465 done: |
|
466 delete [] list; |
|
467 |
|
468 return retval; |
|
469 } |
|
470 |
|
471 void |
|
472 tree_matrix::print_code (ostream& os) |
|
473 { |
|
474 print_code_indent (os); |
|
475 |
|
476 if (in_parens) |
|
477 os << "("; |
|
478 |
|
479 os << "["; |
|
480 |
|
481 tree_matrix *list = this; |
|
482 |
|
483 while (list) |
|
484 { |
|
485 list->element->print_code (os); |
|
486 |
|
487 list = list->next; |
|
488 |
|
489 if (list) |
|
490 { |
|
491 switch (list->direction) |
|
492 { |
|
493 case md_right: |
|
494 os << ", "; |
|
495 break; |
|
496 |
|
497 case md_down: |
|
498 os << "; "; |
|
499 break; |
|
500 |
|
501 default: |
|
502 break; |
|
503 } |
|
504 } |
|
505 } |
|
506 |
|
507 os << "]"; |
|
508 |
|
509 if (in_parens) |
|
510 os << ")"; |
|
511 } |
|
512 |
|
513 /* |
|
514 ;;; Local Variables: *** |
|
515 ;;; mode: C++ *** |
|
516 ;;; page-delimiter: "^/\\*" *** |
|
517 ;;; End: *** |
|
518 */ |