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