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