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