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