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 |
|
164 operator void* () const |
2990
|
165 { |
|
166 return (rep && rep->ok) |
|
167 ? static_cast<void *> (-1) : static_cast<void *> (0); |
|
168 } |
1827
|
169 |
|
170 private: |
|
171 |
|
172 tm_row_const_rep *rep; |
|
173 }; |
|
174 |
|
175 void |
2971
|
176 tm_row_const::tm_row_const_rep::init (const tree_argument_list& row) |
1827
|
177 { |
|
178 all_str = true; |
|
179 |
|
180 bool first_elem = true; |
|
181 |
2971
|
182 for (Pix p = row.first (); p != 0; row.next (p)) |
1827
|
183 { |
2971
|
184 tree_expression *elt = row (p); |
1827
|
185 |
2971
|
186 octave_value tmp = elt->rvalue (); |
1827
|
187 |
|
188 if (error_state || tmp.is_undefined ()) |
|
189 break; |
|
190 else |
|
191 { |
|
192 int this_elt_nr = tmp.rows (); |
|
193 int this_elt_nc = tmp.columns (); |
|
194 |
|
195 if (this_elt_nr == 0 || this_elt_nc == 0) |
|
196 { |
2172
|
197 if (Vempty_list_elements_ok < 0) |
2419
|
198 eval_warning ("empty matrix found in matrix list", |
|
199 elt->line (), elt->column ()); |
2172
|
200 else if (Vempty_list_elements_ok == 0) |
1827
|
201 { |
2419
|
202 eval_error ("empty matrix found in matrix list", |
|
203 elt->line (), elt->column ()); |
1827
|
204 break; |
|
205 } |
|
206 } |
|
207 else |
|
208 { |
2602
|
209 all_mt = false; |
|
210 |
1827
|
211 if (first_elem) |
|
212 { |
|
213 first_elem = false; |
|
214 |
|
215 nr = this_elt_nr; |
|
216 } |
|
217 else if (this_elt_nr != nr) |
|
218 { |
2419
|
219 eval_error ("number of rows must match", |
|
220 elt->line (), elt->column ()); |
1827
|
221 break; |
|
222 } |
|
223 |
|
224 nc += this_elt_nc; |
|
225 |
|
226 append (tmp); |
|
227 } |
|
228 |
|
229 if (all_str && ! tmp.is_string ()) |
|
230 all_str = false; |
|
231 |
3110
|
232 if (! some_str && tmp.is_string ()) |
|
233 some_str = true; |
|
234 |
1827
|
235 if (! is_cmplx && tmp.is_complex_type ()) |
|
236 is_cmplx = true; |
|
237 } |
|
238 } |
|
239 |
|
240 ok = ! error_state; |
1741
|
241 } |
|
242 |
2419
|
243 void |
|
244 tm_row_const::tm_row_const_rep::eval_error (const char *msg, int l, |
|
245 int c) const |
|
246 { |
|
247 if (l == -1 && c == -1) |
|
248 ::error ("%s", msg); |
|
249 else |
|
250 ::error ("%s near line %d, column %d", msg, l, c); |
|
251 } |
|
252 |
|
253 void |
|
254 tm_row_const::tm_row_const_rep::eval_warning (const char *msg, int l, |
|
255 int c) const |
|
256 { |
|
257 if (l == -1 && c == -1) |
|
258 ::warning ("%s", msg); |
|
259 else |
|
260 ::warning ("%s near line %d, column %d", msg, l, c); |
|
261 } |
|
262 |
2580
|
263 #include "SLList.h" |
|
264 #include "SLList.cc" |
|
265 |
1827
|
266 template class SLNode<tm_row_const>; |
|
267 template class SLList<tm_row_const>; |
|
268 |
|
269 class |
|
270 tm_const : public SLList<tm_row_const> |
|
271 { |
|
272 public: |
|
273 |
|
274 tm_const (const tree_matrix& tm) |
3110
|
275 : SLList<tm_row_const> (), nr (0), nc (0), |
|
276 all_str (false), some_str (false), is_cmplx (false), |
|
277 all_mt (true), ok (false) |
1827
|
278 { init (tm); } |
|
279 |
|
280 ~tm_const (void) { } |
|
281 |
|
282 int rows (void) const { return nr; } |
|
283 int cols (void) const { return nc; } |
|
284 |
2868
|
285 bool all_strings_p (void) const { return all_str; } |
3110
|
286 bool some_strings_p (void) const { return some_str; } |
2868
|
287 bool complex_p (void) const { return is_cmplx; } |
|
288 bool all_empty_p (void) const { return all_mt; } |
1827
|
289 |
2800
|
290 operator void* () const |
|
291 { return ok ? static_cast<void *> (-1) : static_cast<void *> (0); } |
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 |
|
326 for (Pix p = tm.first (); p != 0; tm.next (p)) |
|
327 { |
2971
|
328 tree_argument_list *elt = tm (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 { |
|
354 for (Pix p = first (); p != 0; next (p)) |
|
355 { |
|
356 tm_row_const elt = this->operator () (p); |
|
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 { |
|
388 ::error ("number of columns must match"); |
|
389 break; |
|
390 } |
|
391 |
|
392 nr += this_elt_nr; |
|
393 } |
|
394 } |
|
395 } |
|
396 |
|
397 ok = ! error_state; |
1741
|
398 } |
|
399 |
2990
|
400 tree_matrix::~tree_matrix (void) |
|
401 { |
|
402 while (! empty ()) |
|
403 { |
|
404 tree_argument_list *t = remove_front (); |
|
405 delete t; |
|
406 } |
|
407 } |
|
408 |
1827
|
409 bool |
2529
|
410 tree_matrix::all_elements_are_constant (void) const |
1827
|
411 { |
|
412 for (Pix p = first (); p != 0; next (p)) |
|
413 { |
2971
|
414 tree_argument_list *elt = this->operator () (p); |
1827
|
415 |
2529
|
416 if (! elt->all_elements_are_constant ()) |
1827
|
417 return false; |
|
418 } |
|
419 |
|
420 return true; |
|
421 } |
|
422 |
|
423 // Just about as ugly as it gets. |
1741
|
424 // Less ugly than before, anyway. |
1827
|
425 // Looking better all the time. |
1741
|
426 |
2971
|
427 octave_value_list |
|
428 tree_matrix::rvalue (int nargout) |
|
429 { |
|
430 octave_value_list retval; |
|
431 |
|
432 if (nargout > 1) |
|
433 error ("invalid number of output arguments for matrix list"); |
|
434 else |
|
435 retval = rvalue (); |
|
436 |
|
437 return retval; |
|
438 } |
|
439 |
2086
|
440 octave_value |
2971
|
441 tree_matrix::rvalue (void) |
1741
|
442 { |
2086
|
443 octave_value retval; |
1741
|
444 |
1827
|
445 tm_const tmp (*this); |
1741
|
446 |
2868
|
447 bool all_strings_p = false; |
3110
|
448 bool some_strings_p = false; |
2868
|
449 bool all_empty_p = false; |
2602
|
450 |
3110
|
451 bool frc_str_conv = false; |
|
452 |
1827
|
453 if (tmp) |
|
454 { |
|
455 int nr = tmp.rows (); |
|
456 int nc = tmp.cols (); |
1741
|
457 |
1827
|
458 Matrix m; |
|
459 ComplexMatrix cm; |
|
460 charMatrix chm; |
1741
|
461 |
1827
|
462 // Now, extract the values from the individual elements and |
|
463 // insert them in the result matrix. |
1741
|
464 |
2868
|
465 bool found_complex = tmp.complex_p (); |
1741
|
466 |
2868
|
467 all_strings_p = tmp.all_strings_p (); |
3110
|
468 some_strings_p = tmp.some_strings_p (); |
2868
|
469 all_empty_p = tmp.all_empty_p (); |
2602
|
470 |
3110
|
471 frc_str_conv = Vimplicit_num_to_str_ok && some_strings_p; |
|
472 |
2868
|
473 if (all_strings_p) |
2254
|
474 chm.resize (nr, nc, Vstring_fill_char); |
1827
|
475 else if (found_complex) |
|
476 cm.resize (nr, nc, 0.0); |
|
477 else |
|
478 m.resize (nr, nc, 0.0); |
1741
|
479 |
1827
|
480 int put_row = 0; |
1741
|
481 |
1827
|
482 for (Pix p = tmp.first (); p != 0; tmp.next (p)) |
|
483 { |
|
484 int put_col = 0; |
1741
|
485 |
1827
|
486 tm_row_const row = tmp (p); |
1741
|
487 |
1827
|
488 for (Pix q = row.first (); q != 0; row.next (q)) |
|
489 { |
2086
|
490 octave_value elt = row (q); |
1741
|
491 |
1827
|
492 if (found_complex) |
|
493 { |
|
494 if (elt.is_real_scalar ()) |
|
495 cm (put_row, put_col) = elt.double_value (); |
|
496 else if (elt.is_real_matrix () || elt.is_range ()) |
|
497 cm.insert (elt.matrix_value (), put_row, put_col); |
|
498 else if (elt.is_complex_scalar ()) |
|
499 cm (put_row, put_col) = elt.complex_value (); |
|
500 else |
|
501 { |
|
502 ComplexMatrix cm_elt = elt.complex_matrix_value (); |
1741
|
503 |
1827
|
504 if (error_state) |
|
505 goto done; |
1741
|
506 |
1827
|
507 cm.insert (cm_elt, put_row, put_col); |
|
508 } |
|
509 } |
|
510 else |
|
511 { |
|
512 if (elt.is_real_scalar ()) |
|
513 m (put_row, put_col) = elt.double_value (); |
2868
|
514 else if (elt.is_string () && all_strings_p) |
1827
|
515 { |
2495
|
516 charMatrix chm_elt = elt.char_matrix_value (); |
1741
|
517 |
1827
|
518 if (error_state) |
|
519 goto done; |
1741
|
520 |
1827
|
521 chm.insert (chm_elt, put_row, put_col); |
|
522 } |
|
523 else |
|
524 { |
3110
|
525 Matrix m_elt = elt.matrix_value (frc_str_conv); |
1827
|
526 |
|
527 if (error_state) |
|
528 goto done; |
1741
|
529 |
1827
|
530 m.insert (m_elt, put_row, put_col); |
|
531 } |
|
532 } |
|
533 |
2868
|
534 if (all_strings_p && chm.rows () > 0 && chm.cols () > 0) |
2086
|
535 retval = octave_value (chm, true); |
1827
|
536 else if (found_complex) |
|
537 retval = cm; |
|
538 else |
|
539 retval = m; |
|
540 |
|
541 put_col += elt.columns (); |
1741
|
542 } |
|
543 |
1827
|
544 put_row += row.rows (); |
1741
|
545 } |
|
546 } |
|
547 |
1827
|
548 done: |
1741
|
549 |
3110
|
550 if (! error_state) |
2602
|
551 { |
3110
|
552 if (retval.is_undefined () && all_empty_p) |
|
553 { |
|
554 if (all_strings_p) |
|
555 retval = ""; |
|
556 else |
|
557 retval = Matrix (); |
|
558 } |
|
559 else if (frc_str_conv && ! retval.is_string ()) |
|
560 retval = retval.convert_to_str (); |
2602
|
561 } |
|
562 |
1741
|
563 return retval; |
|
564 } |
|
565 |
|
566 void |
2124
|
567 tree_matrix::accept (tree_walker& tw) |
1741
|
568 { |
2124
|
569 tw.visit_matrix (*this); |
1741
|
570 } |
|
571 |
2172
|
572 static int |
|
573 empty_list_elements_ok (void) |
|
574 { |
|
575 Vempty_list_elements_ok = check_preference ("empty_list_elements_ok"); |
|
576 |
|
577 return 0; |
|
578 } |
|
579 |
2254
|
580 static int |
3110
|
581 implicit_num_to_str_ok (void) |
|
582 { |
|
583 Vimplicit_num_to_str_ok = check_preference ("implicit_num_to_str_ok"); |
|
584 |
|
585 return 0; |
|
586 } |
|
587 |
|
588 static int |
2254
|
589 string_fill_char (void) |
|
590 { |
|
591 int status = 0; |
|
592 |
|
593 string s = builtin_string_variable ("string_fill_char"); |
|
594 |
|
595 switch (s.length ()) |
|
596 { |
|
597 case 1: |
|
598 Vstring_fill_char = s[0]; |
|
599 break; |
|
600 |
|
601 case 0: |
|
602 Vstring_fill_char = '\0'; |
|
603 break; |
|
604 |
|
605 default: |
|
606 warning ("string_fill_char must be a single character"); |
|
607 status = -1; |
|
608 break; |
|
609 } |
|
610 |
|
611 return status; |
|
612 } |
|
613 |
2172
|
614 void |
|
615 symbols_of_pt_mat (void) |
|
616 { |
|
617 DEFVAR (empty_list_elements_ok, "warn", 0, empty_list_elements_ok, |
|
618 "ignore the empty element in expressions like `a = [[], 1]'"); |
2254
|
619 |
3110
|
620 DEFVAR (implicit_num_to_str_ok, 0.0, 0, implicit_num_to_str_ok, |
|
621 "make the result of things like `[97, 98, 99, \"123\"]' be a string"); |
|
622 |
2254
|
623 DEFVAR (string_fill_char, " ", 0, string_fill_char, |
|
624 "the character to fill with when creating string arrays."); |
2172
|
625 } |
|
626 |
1741
|
627 /* |
|
628 ;;; Local Variables: *** |
|
629 ;;; mode: C++ *** |
|
630 ;;; End: *** |
|
631 */ |