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