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