1741
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 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 |
2172
|
34 #include "defun.h" |
1741
|
35 #include "error.h" |
|
36 #include "oct-obj.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" |
2124
|
42 #include "pt-walk.h" |
2201
|
43 #include "utils.h" |
2371
|
44 #include "ov.h" |
|
45 #include "variables.h" |
1741
|
46 |
2172
|
47 // Are empty elements in a matrix list ok? For example, is the empty |
|
48 // matrix in an expression like `[[], 1]' ok? A positive value means |
|
49 // yes. A negative value means yes, but print a warning message. |
|
50 // Zero means it should be considered an error. |
|
51 static int Vempty_list_elements_ok; |
|
52 |
2254
|
53 // The character to fill with when creating string arrays. |
|
54 static char Vstring_fill_char; |
|
55 |
1741
|
56 // General matrices. This list type is much more work to handle than |
|
57 // constant matrices, but it allows us to construct matrices from |
|
58 // other matrices, variables, and functions. |
|
59 |
1827
|
60 // But first, some internal classes that make our job much easier. |
1741
|
61 |
1827
|
62 class |
|
63 tm_row_const |
1741
|
64 { |
1827
|
65 private: |
|
66 |
|
67 class |
2086
|
68 tm_row_const_rep : public SLList<octave_value> |
1827
|
69 { |
|
70 public: |
|
71 |
|
72 tm_row_const_rep (void) |
2086
|
73 : SLList<octave_value> (), count (1), nr (0), nc (0), |
2602
|
74 all_str (false), is_cmplx (false), all_mt (true), ok (false) { } |
1827
|
75 |
|
76 tm_row_const_rep (const tree_matrix_row& mr) |
2086
|
77 : SLList<octave_value> (), count (1), nr (0), nc (0), |
2602
|
78 all_str (false), is_cmplx (false), all_mt (true), ok (false) |
1827
|
79 { init (mr); } |
|
80 |
|
81 ~tm_row_const_rep (void) { } |
|
82 |
|
83 int count; |
|
84 |
|
85 int nr; |
|
86 int nc; |
1741
|
87 |
1827
|
88 bool all_str; |
|
89 bool is_cmplx; |
2602
|
90 bool all_mt; |
1827
|
91 |
|
92 bool ok; |
|
93 |
|
94 void init (const tree_matrix_row&); |
|
95 |
|
96 private: |
|
97 |
|
98 tm_row_const_rep (const tm_row_const_rep&); |
|
99 |
|
100 tm_row_const_rep& operator = |
|
101 (const tm_row_const_rep&); |
2419
|
102 |
|
103 |
|
104 void eval_error (const char *msg, int l, int c) const; |
|
105 |
|
106 void eval_warning (const char *msg, int l, int c) const; |
1827
|
107 }; |
|
108 |
|
109 public: |
|
110 |
|
111 tm_row_const (void) : rep (0) { } |
|
112 |
|
113 tm_row_const (const tree_matrix_row& mr) |
|
114 : rep (new tm_row_const_rep (mr)) { } |
|
115 |
|
116 tm_row_const (const tm_row_const& x) : rep (x.rep) |
1741
|
117 { |
1827
|
118 if (rep) |
|
119 rep->count++; |
|
120 } |
|
121 |
|
122 tm_row_const& operator = (const tm_row_const& x) |
|
123 { |
|
124 if (this != &x && rep != x.rep) |
|
125 { |
|
126 if (rep && --rep->count == 0) |
|
127 delete rep; |
1741
|
128 |
1827
|
129 rep = x.rep; |
|
130 |
|
131 if (rep) |
|
132 rep->count++; |
|
133 } |
1741
|
134 |
1827
|
135 return *this; |
|
136 } |
|
137 |
|
138 ~tm_row_const (void) |
|
139 { |
|
140 if (rep && --rep->count == 0) |
|
141 delete rep; |
1741
|
142 } |
|
143 |
1827
|
144 int rows (void) { return rep->nr; } |
|
145 int cols (void) { return rep->nc; } |
|
146 |
2868
|
147 bool all_strings_p (void) const { return rep->all_str; } |
|
148 bool complex_p (void) const { return rep->is_cmplx; } |
|
149 bool all_empty_p (void) const { return rep->all_mt; } |
1827
|
150 |
2086
|
151 octave_value& operator () (Pix p) { return rep->operator () (p); } |
1827
|
152 |
2086
|
153 const octave_value& operator () (Pix p) const |
1827
|
154 { return rep->operator () (p); } |
|
155 |
|
156 Pix first (void) const { return rep->first (); } |
|
157 void next (Pix& p) const { rep->next (p); } |
|
158 |
|
159 operator void* () const |
|
160 { |
2800
|
161 return (rep && rep->ok) |
|
162 ? static_cast<void *> (-1) : static_cast<void *> (0); |
1827
|
163 } |
|
164 |
|
165 private: |
|
166 |
|
167 tm_row_const_rep *rep; |
|
168 }; |
|
169 |
|
170 void |
|
171 tm_row_const::tm_row_const_rep::init (const tree_matrix_row& mr) |
|
172 { |
|
173 all_str = true; |
|
174 |
|
175 bool first_elem = true; |
|
176 |
|
177 for (Pix p = mr.first (); p != 0; mr.next (p)) |
|
178 { |
|
179 tree_expression *elt = mr (p); |
|
180 |
2859
|
181 octave_value tmp = elt->eval (); |
1827
|
182 |
|
183 if (error_state || tmp.is_undefined ()) |
|
184 break; |
|
185 else |
|
186 { |
|
187 int this_elt_nr = tmp.rows (); |
|
188 int this_elt_nc = tmp.columns (); |
|
189 |
|
190 if (this_elt_nr == 0 || this_elt_nc == 0) |
|
191 { |
2172
|
192 if (Vempty_list_elements_ok < 0) |
2419
|
193 eval_warning ("empty matrix found in matrix list", |
|
194 elt->line (), elt->column ()); |
2172
|
195 else if (Vempty_list_elements_ok == 0) |
1827
|
196 { |
2419
|
197 eval_error ("empty matrix found in matrix list", |
|
198 elt->line (), elt->column ()); |
1827
|
199 break; |
|
200 } |
|
201 } |
|
202 else |
|
203 { |
2602
|
204 all_mt = false; |
|
205 |
1827
|
206 if (first_elem) |
|
207 { |
|
208 first_elem = false; |
|
209 |
|
210 nr = this_elt_nr; |
|
211 } |
|
212 else if (this_elt_nr != nr) |
|
213 { |
2419
|
214 eval_error ("number of rows must match", |
|
215 elt->line (), elt->column ()); |
1827
|
216 break; |
|
217 } |
|
218 |
|
219 nc += this_elt_nc; |
|
220 |
|
221 append (tmp); |
|
222 } |
|
223 |
|
224 if (all_str && ! tmp.is_string ()) |
|
225 all_str = false; |
|
226 |
|
227 if (! is_cmplx && tmp.is_complex_type ()) |
|
228 is_cmplx = true; |
|
229 } |
|
230 } |
|
231 |
|
232 ok = ! error_state; |
1741
|
233 } |
|
234 |
2419
|
235 void |
|
236 tm_row_const::tm_row_const_rep::eval_error (const char *msg, int l, |
|
237 int c) const |
|
238 { |
|
239 if (l == -1 && c == -1) |
|
240 ::error ("%s", msg); |
|
241 else |
|
242 ::error ("%s near line %d, column %d", msg, l, c); |
|
243 } |
|
244 |
|
245 void |
|
246 tm_row_const::tm_row_const_rep::eval_warning (const char *msg, int l, |
|
247 int c) const |
|
248 { |
|
249 if (l == -1 && c == -1) |
|
250 ::warning ("%s", msg); |
|
251 else |
|
252 ::warning ("%s near line %d, column %d", msg, l, c); |
|
253 } |
|
254 |
2580
|
255 #include "SLList.h" |
|
256 #include "SLList.cc" |
|
257 |
1827
|
258 template class SLNode<tm_row_const>; |
|
259 template class SLList<tm_row_const>; |
|
260 |
|
261 class |
|
262 tm_const : public SLList<tm_row_const> |
|
263 { |
|
264 public: |
|
265 |
|
266 tm_const (const tree_matrix& tm) |
|
267 : SLList<tm_row_const> (), nr (0), nc (0), all_str (false), |
2602
|
268 is_cmplx (false), all_mt (true), ok (false) |
1827
|
269 { init (tm); } |
|
270 |
|
271 ~tm_const (void) { } |
|
272 |
|
273 int rows (void) const { return nr; } |
|
274 int cols (void) const { return nc; } |
|
275 |
2868
|
276 bool all_strings_p (void) const { return all_str; } |
|
277 bool complex_p (void) const { return is_cmplx; } |
|
278 bool all_empty_p (void) const { return all_mt; } |
1827
|
279 |
2800
|
280 operator void* () const |
|
281 { return ok ? static_cast<void *> (-1) : static_cast<void *> (0); } |
1827
|
282 |
|
283 private: |
|
284 |
|
285 int nr; |
|
286 int nc; |
|
287 |
|
288 bool all_str; |
|
289 bool is_cmplx; |
2602
|
290 bool all_mt; |
1827
|
291 |
|
292 bool ok; |
|
293 |
|
294 tm_const (void); |
|
295 |
|
296 tm_const (const tm_const&); |
|
297 |
|
298 tm_const& operator = (const tm_const&); |
|
299 |
|
300 void init (const tree_matrix& tm); |
|
301 }; |
|
302 |
|
303 void |
|
304 tm_const::init (const tree_matrix& tm) |
1741
|
305 { |
1827
|
306 all_str = true; |
|
307 |
|
308 bool first_elem = true; |
|
309 |
|
310 // Just eval and figure out if what we have is complex or all |
|
311 // strings. We can't check columns until we know that this is a |
|
312 // numeric matrix -- collections of strings can have elements of |
|
313 // different lengths. |
|
314 |
|
315 for (Pix p = tm.first (); p != 0; tm.next (p)) |
|
316 { |
|
317 tree_matrix_row *elt = tm (p); |
|
318 |
|
319 tm_row_const tmp (*elt); |
|
320 |
|
321 if (tmp) |
|
322 { |
2868
|
323 if (all_str && ! tmp.all_strings_p ()) |
1827
|
324 all_str = false; |
|
325 |
2868
|
326 if (! is_cmplx && tmp.complex_p ()) |
1827
|
327 is_cmplx = true; |
|
328 |
2868
|
329 if (all_mt && ! tmp.all_empty_p ()) |
2602
|
330 all_mt = false; |
|
331 |
1827
|
332 append (tmp); |
|
333 } |
|
334 else |
|
335 break; |
|
336 } |
|
337 |
|
338 if (! error_state) |
|
339 { |
|
340 for (Pix p = first (); p != 0; next (p)) |
|
341 { |
|
342 tm_row_const elt = this->operator () (p); |
|
343 |
|
344 int this_elt_nr = elt.rows (); |
|
345 int this_elt_nc = elt.cols (); |
|
346 |
|
347 if (this_elt_nr == 0 || this_elt_nc == 0) |
|
348 { |
2172
|
349 if (Vempty_list_elements_ok < 0) |
1827
|
350 warning ("empty matrix found in matrix list"); |
2172
|
351 else if (Vempty_list_elements_ok == 0) |
1827
|
352 { |
|
353 ::error ("empty matrix found in matrix list"); |
|
354 break; |
|
355 } |
|
356 } |
|
357 else |
|
358 { |
2602
|
359 all_mt = false; |
|
360 |
1827
|
361 if (first_elem) |
|
362 { |
|
363 first_elem = false; |
|
364 |
|
365 nc = this_elt_nc; |
|
366 } |
|
367 else if (all_str) |
|
368 { |
|
369 if (this_elt_nc > nc) |
|
370 nc = this_elt_nc; |
|
371 } |
|
372 else if (this_elt_nc != nc) |
|
373 { |
|
374 ::error ("number of columns must match"); |
|
375 break; |
|
376 } |
|
377 |
|
378 nr += this_elt_nr; |
|
379 } |
|
380 } |
|
381 } |
|
382 |
|
383 ok = ! error_state; |
1741
|
384 } |
|
385 |
1827
|
386 bool |
2529
|
387 tree_matrix_row::all_elements_are_constant (void) const |
1741
|
388 { |
1827
|
389 for (Pix p = first (); p != 0; next (p)) |
|
390 { |
|
391 tree_expression *elt = this->operator () (p); |
1741
|
392 |
1827
|
393 if (! elt->is_constant ()) |
|
394 return false; |
1741
|
395 } |
|
396 |
1827
|
397 return true; |
1741
|
398 } |
|
399 |
|
400 tree_return_list * |
1827
|
401 tree_matrix_row::to_return_list (void) |
1741
|
402 { |
|
403 tree_return_list *retval = 0; |
|
404 |
1827
|
405 bool first_elem = true; |
1741
|
406 |
1827
|
407 for (Pix p = first (); p != 0; next (p)) |
1741
|
408 { |
1827
|
409 tree_expression *elt = this->operator () (p); |
1741
|
410 |
1827
|
411 bool is_id = elt->is_identifier (); |
1741
|
412 |
1827
|
413 bool is_idx_expr = elt->is_index_expression (); |
1741
|
414 |
|
415 if (is_id || is_idx_expr) |
|
416 { |
|
417 tree_index_expression *idx_expr; |
1827
|
418 |
1741
|
419 if (is_id) |
|
420 { |
2800
|
421 tree_identifier *id = static_cast<tree_identifier *> (elt); |
1741
|
422 idx_expr = new tree_index_expression (id); |
|
423 } |
|
424 else |
2800
|
425 idx_expr = static_cast<tree_index_expression *> (elt); |
1741
|
426 |
1827
|
427 if (first_elem) |
|
428 { |
|
429 first_elem = false; |
|
430 |
|
431 retval = new tree_return_list (idx_expr); |
|
432 } |
1741
|
433 else |
|
434 retval->append (idx_expr); |
|
435 } |
|
436 else |
|
437 { |
|
438 delete retval; |
|
439 retval = 0; |
|
440 break; |
|
441 } |
|
442 } |
|
443 |
|
444 return retval; |
|
445 } |
|
446 |
1827
|
447 void |
2124
|
448 tree_matrix_row::accept (tree_walker& tw) |
1741
|
449 { |
2124
|
450 tw.visit_matrix_row (*this); |
1827
|
451 } |
1741
|
452 |
1827
|
453 bool |
2529
|
454 tree_matrix::all_elements_are_constant (void) const |
1827
|
455 { |
|
456 for (Pix p = first (); p != 0; next (p)) |
|
457 { |
|
458 tree_matrix_row *elt = this->operator () (p); |
|
459 |
2529
|
460 if (! elt->all_elements_are_constant ()) |
1827
|
461 return false; |
|
462 } |
|
463 |
|
464 return true; |
|
465 } |
|
466 |
|
467 // Just about as ugly as it gets. |
1741
|
468 // Less ugly than before, anyway. |
1827
|
469 // Looking better all the time. |
1741
|
470 |
2086
|
471 octave_value |
1827
|
472 tree_matrix::eval (bool /* print */) |
1741
|
473 { |
2086
|
474 octave_value retval; |
1741
|
475 |
1827
|
476 tm_const tmp (*this); |
1741
|
477 |
2868
|
478 bool all_strings_p = false; |
|
479 bool all_empty_p = false; |
2602
|
480 |
1827
|
481 if (tmp) |
|
482 { |
|
483 int nr = tmp.rows (); |
|
484 int nc = tmp.cols (); |
1741
|
485 |
1827
|
486 Matrix m; |
|
487 ComplexMatrix cm; |
|
488 charMatrix chm; |
1741
|
489 |
1827
|
490 // Now, extract the values from the individual elements and |
|
491 // insert them in the result matrix. |
1741
|
492 |
2868
|
493 bool found_complex = tmp.complex_p (); |
1741
|
494 |
2868
|
495 all_strings_p = tmp.all_strings_p (); |
|
496 all_empty_p = tmp.all_empty_p (); |
2602
|
497 |
2868
|
498 if (all_strings_p) |
2254
|
499 chm.resize (nr, nc, Vstring_fill_char); |
1827
|
500 else if (found_complex) |
|
501 cm.resize (nr, nc, 0.0); |
|
502 else |
|
503 m.resize (nr, nc, 0.0); |
1741
|
504 |
1827
|
505 int put_row = 0; |
1741
|
506 |
1827
|
507 for (Pix p = tmp.first (); p != 0; tmp.next (p)) |
|
508 { |
|
509 int put_col = 0; |
1741
|
510 |
1827
|
511 tm_row_const row = tmp (p); |
1741
|
512 |
1827
|
513 for (Pix q = row.first (); q != 0; row.next (q)) |
|
514 { |
2086
|
515 octave_value elt = row (q); |
1741
|
516 |
1827
|
517 if (found_complex) |
|
518 { |
|
519 if (elt.is_real_scalar ()) |
|
520 cm (put_row, put_col) = elt.double_value (); |
|
521 else if (elt.is_real_matrix () || elt.is_range ()) |
|
522 cm.insert (elt.matrix_value (), put_row, put_col); |
|
523 else if (elt.is_complex_scalar ()) |
|
524 cm (put_row, put_col) = elt.complex_value (); |
|
525 else |
|
526 { |
|
527 ComplexMatrix cm_elt = elt.complex_matrix_value (); |
1741
|
528 |
1827
|
529 if (error_state) |
|
530 goto done; |
1741
|
531 |
1827
|
532 cm.insert (cm_elt, put_row, put_col); |
|
533 } |
|
534 } |
|
535 else |
|
536 { |
|
537 if (elt.is_real_scalar ()) |
|
538 m (put_row, put_col) = elt.double_value (); |
2868
|
539 else if (elt.is_string () && all_strings_p) |
1827
|
540 { |
2495
|
541 charMatrix chm_elt = elt.char_matrix_value (); |
1741
|
542 |
1827
|
543 if (error_state) |
|
544 goto done; |
1741
|
545 |
1827
|
546 chm.insert (chm_elt, put_row, put_col); |
|
547 } |
|
548 else |
|
549 { |
|
550 Matrix m_elt = elt.matrix_value (); |
|
551 |
|
552 if (error_state) |
|
553 goto done; |
1741
|
554 |
1827
|
555 m.insert (m_elt, put_row, put_col); |
|
556 } |
|
557 } |
|
558 |
2868
|
559 if (all_strings_p && chm.rows () > 0 && chm.cols () > 0) |
2086
|
560 retval = octave_value (chm, true); |
1827
|
561 else if (found_complex) |
|
562 retval = cm; |
|
563 else |
|
564 retval = m; |
|
565 |
|
566 put_col += elt.columns (); |
1741
|
567 } |
|
568 |
1827
|
569 put_row += row.rows (); |
1741
|
570 } |
|
571 } |
|
572 |
1827
|
573 done: |
1741
|
574 |
2868
|
575 if (! error_state && retval.is_undefined () && all_empty_p) |
2602
|
576 { |
2868
|
577 if (all_strings_p) |
2602
|
578 retval = ""; |
|
579 else |
|
580 retval = Matrix (); |
|
581 } |
|
582 |
1741
|
583 return retval; |
|
584 } |
|
585 |
|
586 void |
2124
|
587 tree_matrix::accept (tree_walker& tw) |
1741
|
588 { |
2124
|
589 tw.visit_matrix (*this); |
1741
|
590 } |
|
591 |
2172
|
592 static int |
|
593 empty_list_elements_ok (void) |
|
594 { |
|
595 Vempty_list_elements_ok = check_preference ("empty_list_elements_ok"); |
|
596 |
|
597 return 0; |
|
598 } |
|
599 |
2254
|
600 static int |
|
601 string_fill_char (void) |
|
602 { |
|
603 int status = 0; |
|
604 |
|
605 string s = builtin_string_variable ("string_fill_char"); |
|
606 |
|
607 switch (s.length ()) |
|
608 { |
|
609 case 1: |
|
610 Vstring_fill_char = s[0]; |
|
611 break; |
|
612 |
|
613 case 0: |
|
614 Vstring_fill_char = '\0'; |
|
615 break; |
|
616 |
|
617 default: |
|
618 warning ("string_fill_char must be a single character"); |
|
619 status = -1; |
|
620 break; |
|
621 } |
|
622 |
|
623 return status; |
|
624 } |
|
625 |
2172
|
626 void |
|
627 symbols_of_pt_mat (void) |
|
628 { |
|
629 DEFVAR (empty_list_elements_ok, "warn", 0, empty_list_elements_ok, |
|
630 "ignore the empty element in expressions like `a = [[], 1]'"); |
2254
|
631 |
|
632 DEFVAR (string_fill_char, " ", 0, string_fill_char, |
|
633 "the character to fill with when creating string arrays."); |
2172
|
634 } |
|
635 |
1741
|
636 /* |
|
637 ;;; Local Variables: *** |
|
638 ;;; mode: C++ *** |
|
639 ;;; End: *** |
|
640 */ |