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