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