1742
|
1 // pt-mat.cc -*- C++ -*- |
1741
|
2 /* |
|
3 |
1827
|
4 Copyright (C) 1996 John W. Eaton |
1741
|
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 |
1827
|
49 // But first, some internal classes that make our job much easier. |
1741
|
50 |
1827
|
51 class |
|
52 tm_row_const |
1741
|
53 { |
1827
|
54 private: |
|
55 |
|
56 class |
|
57 tm_row_const_rep : public SLList<tree_constant> |
|
58 { |
|
59 public: |
|
60 |
|
61 tm_row_const_rep (void) |
|
62 : SLList<tree_constant> (), count (1), nr (0), nc (0), |
|
63 all_str (false), is_cmplx (false), ok (false) { } |
|
64 |
|
65 tm_row_const_rep (const tree_matrix_row& mr) |
|
66 : SLList<tree_constant> (), count (1), nr (0), nc (0), |
|
67 all_str (false), is_cmplx (false), ok (false) |
|
68 { init (mr); } |
|
69 |
|
70 ~tm_row_const_rep (void) { } |
|
71 |
|
72 int count; |
|
73 |
|
74 int nr; |
|
75 int nc; |
1741
|
76 |
1827
|
77 bool all_str; |
|
78 bool is_cmplx; |
|
79 |
|
80 bool ok; |
|
81 |
|
82 void init (const tree_matrix_row&); |
|
83 |
|
84 private: |
|
85 |
|
86 tm_row_const_rep (const tm_row_const_rep&); |
|
87 |
|
88 tm_row_const_rep& operator = |
|
89 (const tm_row_const_rep&); |
|
90 }; |
|
91 |
|
92 public: |
|
93 |
|
94 tm_row_const (void) : rep (0) { } |
|
95 |
|
96 tm_row_const (const tree_matrix_row& mr) |
|
97 : rep (new tm_row_const_rep (mr)) { } |
|
98 |
|
99 tm_row_const (const tm_row_const& x) : rep (x.rep) |
1741
|
100 { |
1827
|
101 if (rep) |
|
102 rep->count++; |
|
103 } |
|
104 |
|
105 tm_row_const& operator = (const tm_row_const& x) |
|
106 { |
|
107 if (this != &x && rep != x.rep) |
|
108 { |
|
109 if (rep && --rep->count == 0) |
|
110 delete rep; |
1741
|
111 |
1827
|
112 rep = x.rep; |
|
113 |
|
114 if (rep) |
|
115 rep->count++; |
|
116 } |
1741
|
117 |
1827
|
118 return *this; |
|
119 } |
|
120 |
|
121 ~tm_row_const (void) |
|
122 { |
|
123 if (rep && --rep->count == 0) |
|
124 delete rep; |
1741
|
125 } |
|
126 |
1827
|
127 int rows (void) { return rep->nr; } |
|
128 int cols (void) { return rep->nc; } |
|
129 |
|
130 bool all_strings (void) const { return rep->all_str; } |
|
131 bool is_complex (void) const { return rep->is_cmplx; } |
|
132 |
|
133 tree_constant& operator () (Pix p) { return rep->operator () (p); } |
|
134 |
|
135 const tree_constant& operator () (Pix p) const |
|
136 { return rep->operator () (p); } |
|
137 |
|
138 Pix first (void) const { return rep->first (); } |
|
139 void next (Pix& p) const { rep->next (p); } |
|
140 |
|
141 operator void* () const |
|
142 { |
|
143 return (rep && rep->ok) ? (void *) -1 : (void *) 0; |
|
144 } |
|
145 |
|
146 private: |
|
147 |
|
148 tm_row_const_rep *rep; |
|
149 }; |
|
150 |
|
151 void |
|
152 tm_row_const::tm_row_const_rep::init (const tree_matrix_row& mr) |
|
153 { |
|
154 all_str = true; |
|
155 |
|
156 int empties_ok = user_pref.empty_list_elements_ok; |
|
157 |
|
158 bool first_elem = true; |
|
159 |
|
160 for (Pix p = mr.first (); p != 0; mr.next (p)) |
|
161 { |
|
162 tree_expression *elt = mr (p); |
|
163 |
|
164 tree_constant tmp = elt->eval (false); |
|
165 |
|
166 if (error_state || tmp.is_undefined ()) |
|
167 break; |
|
168 else |
|
169 { |
|
170 int this_elt_nr = tmp.rows (); |
|
171 int this_elt_nc = tmp.columns (); |
|
172 |
|
173 if (this_elt_nr == 0 || this_elt_nc == 0) |
|
174 { |
|
175 if (empties_ok < 0) |
|
176 warning ("empty matrix found in matrix list"); |
|
177 else if (empties_ok == 0) |
|
178 { |
|
179 ::error ("empty matrix found in matrix list"); |
|
180 break; |
|
181 } |
|
182 } |
|
183 else |
|
184 { |
|
185 if (first_elem) |
|
186 { |
|
187 first_elem = false; |
|
188 |
|
189 nr = this_elt_nr; |
|
190 } |
|
191 else if (this_elt_nr != nr) |
|
192 { |
|
193 ::error ("number of rows must match"); |
|
194 break; |
|
195 } |
|
196 |
|
197 nc += this_elt_nc; |
|
198 |
|
199 append (tmp); |
|
200 } |
|
201 |
|
202 if (all_str && ! tmp.is_string ()) |
|
203 all_str = false; |
|
204 |
|
205 if (! is_cmplx && tmp.is_complex_type ()) |
|
206 is_cmplx = true; |
|
207 } |
|
208 } |
|
209 |
|
210 ok = ! error_state; |
1741
|
211 } |
|
212 |
1827
|
213 template class SLNode<tm_row_const>; |
|
214 template class SLList<tm_row_const>; |
|
215 |
|
216 class |
|
217 tm_const : public SLList<tm_row_const> |
|
218 { |
|
219 public: |
|
220 |
|
221 tm_const (const tree_matrix& tm) |
|
222 : SLList<tm_row_const> (), nr (0), nc (0), all_str (false), |
|
223 is_cmplx (false), ok (false) |
|
224 { init (tm); } |
|
225 |
|
226 ~tm_const (void) { } |
|
227 |
|
228 int rows (void) const { return nr; } |
|
229 int cols (void) const { return nc; } |
|
230 |
|
231 bool all_strings (void) const { return all_str; } |
|
232 bool is_complex (void) const { return is_cmplx; } |
|
233 |
|
234 operator void* () const { return ok ? (void *) -1 : (void *) 0; } |
|
235 |
|
236 private: |
|
237 |
|
238 int nr; |
|
239 int nc; |
|
240 |
|
241 bool all_str; |
|
242 bool is_cmplx; |
|
243 |
|
244 bool ok; |
|
245 |
|
246 tm_const (void); |
|
247 |
|
248 tm_const (const tm_const&); |
|
249 |
|
250 tm_const& operator = (const tm_const&); |
|
251 |
|
252 void init (const tree_matrix& tm); |
|
253 }; |
|
254 |
|
255 void |
|
256 tm_const::init (const tree_matrix& tm) |
1741
|
257 { |
1827
|
258 all_str = true; |
|
259 |
|
260 int empties_ok = user_pref.empty_list_elements_ok; |
|
261 |
|
262 bool first_elem = true; |
|
263 |
|
264 // Just eval and figure out if what we have is complex or all |
|
265 // strings. We can't check columns until we know that this is a |
|
266 // numeric matrix -- collections of strings can have elements of |
|
267 // different lengths. |
|
268 |
|
269 for (Pix p = tm.first (); p != 0; tm.next (p)) |
|
270 { |
|
271 tree_matrix_row *elt = tm (p); |
|
272 |
|
273 tm_row_const tmp (*elt); |
|
274 |
|
275 if (tmp) |
|
276 { |
|
277 if (all_str && ! tmp.all_strings ()) |
|
278 all_str = false; |
|
279 |
|
280 if (! is_cmplx && tmp.is_complex ()) |
|
281 is_cmplx = true; |
|
282 |
|
283 append (tmp); |
|
284 } |
|
285 else |
|
286 break; |
|
287 } |
|
288 |
|
289 if (! error_state) |
|
290 { |
|
291 for (Pix p = first (); p != 0; next (p)) |
|
292 { |
|
293 tm_row_const elt = this->operator () (p); |
|
294 |
|
295 int this_elt_nr = elt.rows (); |
|
296 int this_elt_nc = elt.cols (); |
|
297 |
|
298 if (this_elt_nr == 0 || this_elt_nc == 0) |
|
299 { |
|
300 if (empties_ok < 0) |
|
301 warning ("empty matrix found in matrix list"); |
|
302 else if (empties_ok == 0) |
|
303 { |
|
304 ::error ("empty matrix found in matrix list"); |
|
305 break; |
|
306 } |
|
307 } |
|
308 else |
|
309 { |
|
310 if (first_elem) |
|
311 { |
|
312 first_elem = false; |
|
313 |
|
314 nc = this_elt_nc; |
|
315 } |
|
316 else if (all_str) |
|
317 { |
|
318 if (this_elt_nc > nc) |
|
319 nc = this_elt_nc; |
|
320 } |
|
321 else if (this_elt_nc != nc) |
|
322 { |
|
323 ::error ("number of columns must match"); |
|
324 break; |
|
325 } |
|
326 |
|
327 nr += this_elt_nr; |
|
328 } |
|
329 } |
|
330 } |
|
331 |
|
332 ok = ! error_state; |
1741
|
333 } |
|
334 |
1827
|
335 bool |
|
336 tree_matrix_row::is_matrix_constant (void) const |
1741
|
337 { |
1827
|
338 for (Pix p = first (); p != 0; next (p)) |
|
339 { |
|
340 tree_expression *elt = this->operator () (p); |
1741
|
341 |
1827
|
342 if (! elt->is_constant ()) |
|
343 return false; |
1741
|
344 } |
|
345 |
1827
|
346 return true; |
1741
|
347 } |
|
348 |
|
349 tree_return_list * |
1827
|
350 tree_matrix_row::to_return_list (void) |
1741
|
351 { |
|
352 tree_return_list *retval = 0; |
|
353 |
1827
|
354 bool first_elem = true; |
1741
|
355 |
1827
|
356 for (Pix p = first (); p != 0; next (p)) |
1741
|
357 { |
1827
|
358 tree_expression *elt = this->operator () (p); |
1741
|
359 |
1827
|
360 bool is_id = elt->is_identifier (); |
1741
|
361 |
1827
|
362 bool is_idx_expr = elt->is_index_expression (); |
1741
|
363 |
|
364 if (is_id || is_idx_expr) |
|
365 { |
|
366 tree_index_expression *idx_expr; |
1827
|
367 |
1741
|
368 if (is_id) |
|
369 { |
1827
|
370 tree_identifier *id = (tree_identifier *) elt; |
1741
|
371 idx_expr = new tree_index_expression (id); |
|
372 } |
|
373 else |
1827
|
374 idx_expr = (tree_index_expression *) elt; |
1741
|
375 |
1827
|
376 if (first_elem) |
|
377 { |
|
378 first_elem = false; |
|
379 |
|
380 retval = new tree_return_list (idx_expr); |
|
381 } |
1741
|
382 else |
|
383 retval->append (idx_expr); |
|
384 } |
|
385 else |
|
386 { |
|
387 delete retval; |
|
388 retval = 0; |
|
389 break; |
|
390 } |
|
391 } |
|
392 |
|
393 return retval; |
|
394 } |
|
395 |
1827
|
396 void |
|
397 tree_matrix_row::print_code (ostream& os) |
1741
|
398 { |
1827
|
399 Pix p = first (); |
|
400 |
|
401 while (p) |
|
402 { |
|
403 tree_expression *elt = this->operator () (p); |
|
404 |
|
405 next (p); |
|
406 |
|
407 if (elt) |
|
408 { |
|
409 elt->print_code (os); |
|
410 |
|
411 if (p) |
|
412 os << ", "; |
|
413 } |
|
414 } |
|
415 } |
1741
|
416 |
1827
|
417 bool |
|
418 tree_matrix::is_matrix_constant (void) const |
|
419 { |
|
420 for (Pix p = first (); p != 0; next (p)) |
|
421 { |
|
422 tree_matrix_row *elt = this->operator () (p); |
|
423 |
|
424 if (! elt->is_matrix_constant ()) |
|
425 return false; |
|
426 } |
|
427 |
|
428 return true; |
|
429 } |
|
430 |
|
431 // Just about as ugly as it gets. |
1741
|
432 // Less ugly than before, anyway. |
1827
|
433 // Looking better all the time. |
1741
|
434 |
|
435 tree_constant |
1827
|
436 tree_matrix::eval (bool /* print */) |
1741
|
437 { |
|
438 tree_constant retval; |
|
439 |
1827
|
440 tm_const tmp (*this); |
1741
|
441 |
1827
|
442 if (tmp) |
|
443 { |
|
444 int nr = tmp.rows (); |
|
445 int nc = tmp.cols (); |
1741
|
446 |
1827
|
447 Matrix m; |
|
448 ComplexMatrix cm; |
|
449 charMatrix chm; |
1741
|
450 |
1827
|
451 // Now, extract the values from the individual elements and |
|
452 // insert them in the result matrix. |
1741
|
453 |
1827
|
454 bool all_strings = tmp.all_strings (); |
|
455 bool found_complex = tmp.is_complex (); |
1741
|
456 |
1827
|
457 if (all_strings) |
|
458 chm.resize (nr, nc, 0); |
|
459 else if (found_complex) |
|
460 cm.resize (nr, nc, 0.0); |
|
461 else |
|
462 m.resize (nr, nc, 0.0); |
1741
|
463 |
1827
|
464 int put_row = 0; |
1741
|
465 |
1827
|
466 for (Pix p = tmp.first (); p != 0; tmp.next (p)) |
|
467 { |
|
468 int put_col = 0; |
1741
|
469 |
1827
|
470 tm_row_const row = tmp (p); |
1741
|
471 |
1827
|
472 for (Pix q = row.first (); q != 0; row.next (q)) |
|
473 { |
|
474 tree_constant elt = row (q); |
1741
|
475 |
1827
|
476 if (found_complex) |
|
477 { |
|
478 if (elt.is_real_scalar ()) |
|
479 cm (put_row, put_col) = elt.double_value (); |
|
480 else if (elt.is_real_matrix () || elt.is_range ()) |
|
481 cm.insert (elt.matrix_value (), put_row, put_col); |
|
482 else if (elt.is_complex_scalar ()) |
|
483 cm (put_row, put_col) = elt.complex_value (); |
|
484 else |
|
485 { |
|
486 ComplexMatrix cm_elt = elt.complex_matrix_value (); |
1741
|
487 |
1827
|
488 if (error_state) |
|
489 goto done; |
1741
|
490 |
1827
|
491 cm.insert (cm_elt, put_row, put_col); |
|
492 } |
|
493 } |
|
494 else |
|
495 { |
|
496 if (elt.is_real_scalar ()) |
|
497 m (put_row, put_col) = elt.double_value (); |
|
498 else if (elt.is_string () && all_strings) |
|
499 { |
|
500 charMatrix chm_elt = elt.all_strings (); |
1741
|
501 |
1827
|
502 if (error_state) |
|
503 goto done; |
1741
|
504 |
1827
|
505 chm.insert (chm_elt, put_row, put_col); |
|
506 } |
|
507 else |
|
508 { |
|
509 Matrix m_elt = elt.matrix_value (); |
|
510 |
|
511 if (error_state) |
|
512 goto done; |
1741
|
513 |
1827
|
514 m.insert (m_elt, put_row, put_col); |
|
515 } |
|
516 } |
|
517 |
|
518 if (all_strings && chm.rows () > 0 && chm.cols () > 0) |
|
519 retval = tree_constant (chm, true); |
|
520 else if (found_complex) |
|
521 retval = cm; |
|
522 else |
|
523 retval = m; |
|
524 |
|
525 put_col += elt.columns (); |
1741
|
526 } |
|
527 |
1827
|
528 put_row += row.rows (); |
1741
|
529 } |
|
530 } |
|
531 |
1827
|
532 done: |
1741
|
533 |
|
534 return retval; |
|
535 } |
|
536 |
|
537 void |
|
538 tree_matrix::print_code (ostream& os) |
|
539 { |
|
540 print_code_indent (os); |
|
541 |
|
542 if (in_parens) |
|
543 os << "("; |
|
544 |
|
545 os << "["; |
|
546 |
1827
|
547 Pix p = first (); |
1741
|
548 |
1827
|
549 while (p) |
1741
|
550 { |
1827
|
551 tree_matrix_row *elt = this->operator () (p); |
1741
|
552 |
1827
|
553 next (p); |
1741
|
554 |
1827
|
555 if (elt) |
1741
|
556 { |
1827
|
557 elt->print_code (os); |
1741
|
558 |
1827
|
559 if (p) |
|
560 os << "; "; |
1741
|
561 } |
|
562 } |
|
563 |
|
564 os << "]"; |
|
565 |
|
566 if (in_parens) |
|
567 os << ")"; |
|
568 } |
|
569 |
|
570 /* |
|
571 ;;; Local Variables: *** |
|
572 ;;; mode: C++ *** |
|
573 ;;; page-delimiter: "^/\\*" *** |
|
574 ;;; End: *** |
|
575 */ |