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