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