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