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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1741
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
1741
|
29 |
5502
|
30 #include "quit.h" |
|
31 |
2172
|
32 #include "defun.h" |
1741
|
33 #include "error.h" |
|
34 #include "oct-obj.h" |
2982
|
35 #include "pt-arg-list.h" |
3770
|
36 #include "pt-bp.h" |
1741
|
37 #include "pt-exp.h" |
|
38 #include "pt-mat.h" |
2124
|
39 #include "pt-walk.h" |
2201
|
40 #include "utils.h" |
2371
|
41 #include "ov.h" |
|
42 #include "variables.h" |
1741
|
43 |
5630
|
44 #include "ov-re-sparse.h" |
|
45 #include "ov-cx-sparse.h" |
|
46 |
4460
|
47 // If TRUE, print a warning message for empty elements in a matrix list. |
|
48 static bool Vwarn_empty_list_elements; |
2172
|
49 |
2254
|
50 // The character to fill with when creating string arrays. |
3836
|
51 char Vstring_fill_char = ' '; |
2254
|
52 |
5280
|
53 // Warn if concatenating double and single quoted strings. |
5391
|
54 char Vwarn_string_concat = false; |
5280
|
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 |
4219
|
68 tm_row_const_rep : public octave_base_list<octave_value> |
1827
|
69 { |
|
70 public: |
|
71 |
|
72 tm_row_const_rep (void) |
5514
|
73 : count (1), dv (0, 0), all_str (false), |
5280
|
74 all_sq_str (false), all_dq_str (false), |
5502
|
75 some_str (false), all_real (false), all_cmplx (false), |
5630
|
76 all_mt (true), any_sparse (false), |
|
77 class_nm (octave_base_value::static_class_name ()), ok (false) |
5533
|
78 { } |
1827
|
79 |
2971
|
80 tm_row_const_rep (const tree_argument_list& row) |
5514
|
81 : count (1), dv (0, 0), all_str (false), all_sq_str (false), |
5502
|
82 some_str (false), all_real (false), all_cmplx (false), |
5630
|
83 all_mt (true), any_sparse (false), |
|
84 class_nm (octave_base_value::static_class_name ()), ok (false) |
3110
|
85 { init (row); } |
1827
|
86 |
|
87 ~tm_row_const_rep (void) { } |
|
88 |
|
89 int count; |
|
90 |
4765
|
91 dim_vector dv; |
1741
|
92 |
1827
|
93 bool all_str; |
5280
|
94 bool all_sq_str; |
|
95 bool all_dq_str; |
3110
|
96 bool some_str; |
5502
|
97 bool all_real; |
|
98 bool all_cmplx; |
2602
|
99 bool all_mt; |
5630
|
100 bool any_sparse; |
1827
|
101 |
5533
|
102 std::string class_nm; |
|
103 |
1827
|
104 bool ok; |
|
105 |
4501
|
106 bool do_init_element (tree_expression *, const octave_value&, bool&); |
|
107 |
2971
|
108 void init (const tree_argument_list&); |
1827
|
109 |
|
110 private: |
|
111 |
|
112 tm_row_const_rep (const tm_row_const_rep&); |
|
113 |
2971
|
114 tm_row_const_rep& operator = (const tm_row_const_rep&); |
2419
|
115 |
3661
|
116 void eval_error (const char *msg, int l, int c, |
|
117 int x = -1, int y = -1) const; |
2419
|
118 |
|
119 void eval_warning (const char *msg, int l, int c) const; |
1827
|
120 }; |
|
121 |
|
122 public: |
|
123 |
4219
|
124 typedef tm_row_const_rep::iterator iterator; |
|
125 typedef tm_row_const_rep::const_iterator const_iterator; |
|
126 |
2990
|
127 tm_row_const (void) |
|
128 : rep (0) { } |
1827
|
129 |
2971
|
130 tm_row_const (const tree_argument_list& row) |
|
131 : rep (new tm_row_const_rep (row)) { } |
1827
|
132 |
2990
|
133 tm_row_const (const tm_row_const& x) |
|
134 : rep (x.rep) |
|
135 { |
|
136 if (rep) |
|
137 rep->count++; |
|
138 } |
1827
|
139 |
|
140 tm_row_const& operator = (const tm_row_const& x) |
2990
|
141 { |
|
142 if (this != &x && rep != x.rep) |
|
143 { |
|
144 if (rep && --rep->count == 0) |
|
145 delete rep; |
1741
|
146 |
2990
|
147 rep = x.rep; |
1827
|
148 |
2990
|
149 if (rep) |
|
150 rep->count++; |
|
151 } |
1741
|
152 |
2990
|
153 return *this; |
|
154 } |
1827
|
155 |
|
156 ~tm_row_const (void) |
2990
|
157 { |
|
158 if (rep && --rep->count == 0) |
|
159 delete rep; |
|
160 } |
1741
|
161 |
5514
|
162 octave_idx_type rows (void) { return rep->dv(0); } |
|
163 octave_idx_type cols (void) { return rep->dv(1); } |
4765
|
164 |
|
165 dim_vector dims (void) { return rep->dv; } |
1827
|
166 |
2868
|
167 bool all_strings_p (void) const { return rep->all_str; } |
5280
|
168 bool all_sq_strings_p (void) const { return rep->all_sq_str; } |
|
169 bool all_dq_strings_p (void) const { return rep->all_dq_str; } |
3110
|
170 bool some_strings_p (void) const { return rep->some_str; } |
5502
|
171 bool all_real_p (void) const { return rep->all_real; } |
|
172 bool all_complex_p (void) const { return rep->all_cmplx; } |
2868
|
173 bool all_empty_p (void) const { return rep->all_mt; } |
5630
|
174 bool any_sparse_p (void) const { return rep->any_sparse; } |
1827
|
175 |
5533
|
176 std::string class_name (void) const { return rep->class_nm; } |
|
177 |
4219
|
178 operator bool () const { return (rep && rep->ok); } |
1827
|
179 |
4219
|
180 iterator begin (void) { return rep->begin (); } |
|
181 const_iterator begin (void) const { return rep->begin (); } |
|
182 |
|
183 iterator end (void) { return rep->end (); } |
|
184 const_iterator end (void) const { return rep->end (); } |
1827
|
185 |
|
186 private: |
|
187 |
|
188 tm_row_const_rep *rep; |
|
189 }; |
|
190 |
5533
|
191 static std::string |
|
192 get_concat_class (const std::string& c1, const std::string& c2) |
|
193 { |
|
194 std::string retval = octave_base_value::static_class_name (); |
|
195 |
|
196 if (c1 == c2) |
|
197 retval = c1; |
|
198 else |
|
199 { |
|
200 bool c1_is_int = (c1 == "int8" || c1 == "uint8" |
|
201 || c1 == "int16" || c1 == "uint16" |
|
202 || c1 == "int32" || c1 == "uint32" |
|
203 || c1 == "int64" || c1 == "uint64"); |
|
204 bool c2_is_int = (c2 == "int8" || c2 == "uint8" |
|
205 || c2 == "int16" || c2 == "uint16" |
|
206 || c2 == "int32" || c2 == "uint32" |
|
207 || c2 == "int64" || c2 == "uint64"); |
|
208 |
|
209 bool c1_is_char = (c1 == "char"); |
|
210 bool c2_is_char = (c2 == "char"); |
|
211 |
|
212 bool c1_is_double = (c1 == "double"); |
|
213 bool c2_is_double = (c2 == "double"); |
|
214 |
|
215 bool c1_is_single = (c1 == "single"); |
|
216 bool c2_is_single = (c2 == "single"); |
|
217 |
|
218 bool c1_is_logical = (c1 == "logical"); |
|
219 bool c2_is_logical = (c2 == "logical"); |
|
220 |
|
221 bool c1_is_built_in_type |
|
222 = (c1_is_int || c1_is_char || c1_is_double || c1_is_single |
|
223 || c1_is_logical); |
|
224 |
|
225 bool c2_is_built_in_type |
|
226 = (c2_is_int || c2_is_char || c2_is_double || c2_is_single |
|
227 || c2_is_logical); |
|
228 |
|
229 // Order is important here... |
|
230 |
|
231 if (c1_is_char && c2_is_built_in_type) |
|
232 retval = c1; |
|
233 else if (c2_is_char && c1_is_built_in_type) |
|
234 retval = c2; |
|
235 else if (c1_is_int && c2_is_built_in_type) |
|
236 retval = c1; |
|
237 else if (c2_is_int && c1_is_built_in_type) |
|
238 retval = c2; |
|
239 else if (c1_is_single && c2_is_built_in_type) |
|
240 retval = c1; |
|
241 else if (c2_is_single && c1_is_built_in_type) |
|
242 retval = c2; |
|
243 else if (c1_is_double && c2_is_built_in_type) |
|
244 retval = c1; |
|
245 else if (c2_is_double && c1_is_built_in_type) |
|
246 retval = c2; |
|
247 else if (c1_is_logical && c2_is_logical) |
|
248 retval = c1; |
|
249 } |
|
250 |
|
251 return retval; |
|
252 } |
|
253 |
4501
|
254 bool |
|
255 tm_row_const::tm_row_const_rep::do_init_element (tree_expression *elt, |
|
256 const octave_value& val, |
|
257 bool& first_elem) |
|
258 { |
5275
|
259 octave_idx_type this_elt_nr = val.rows (); |
|
260 octave_idx_type this_elt_nc = val.columns (); |
4501
|
261 |
5533
|
262 std::string this_elt_class_nm = val.class_name (); |
|
263 |
4765
|
264 dim_vector this_elt_dv = val.dims (); |
|
265 |
5502
|
266 if (! this_elt_dv.all_zero ()) |
4501
|
267 { |
|
268 all_mt = false; |
|
269 |
|
270 if (first_elem) |
|
271 { |
|
272 first_elem = false; |
|
273 |
5533
|
274 class_nm = this_elt_class_nm; |
|
275 |
4765
|
276 dv.resize (this_elt_dv.length ()); |
|
277 for (int i = 2; i < dv.length (); i++) |
|
278 dv.elem (i) = this_elt_dv.elem (i); |
|
279 |
|
280 dv.elem (0) = this_elt_nr; |
|
281 |
|
282 dv.elem (1) = 0; |
4501
|
283 } |
4765
|
284 else |
4501
|
285 { |
5533
|
286 class_nm = get_concat_class (class_nm, this_elt_class_nm); |
|
287 |
5045
|
288 int len = (this_elt_dv.length () < dv.length () |
|
289 ? this_elt_dv.length () : dv.length ()); |
|
290 |
4765
|
291 if (this_elt_nr != dv (0)) |
|
292 { |
|
293 eval_error ("number of rows must match", |
|
294 elt->line (), elt->column (), this_elt_nr, dv (0)); |
|
295 return false; |
|
296 } |
5045
|
297 for (int i = 2; i < len; i++) |
4765
|
298 { |
|
299 if (this_elt_dv (i) != dv (i)) |
|
300 { |
|
301 eval_error ("dimensions mismatch", elt->line (), elt->column (), this_elt_dv (i), dv (i)); |
|
302 return false; |
|
303 } |
|
304 } |
5045
|
305 |
|
306 if (this_elt_dv.length () > len) |
|
307 for (int i = len; i < this_elt_dv.length (); i++) |
|
308 if (this_elt_dv (i) != 1) |
|
309 { |
|
310 eval_error ("dimensions mismatch", elt->line (), elt->column (), this_elt_dv (i), 1); |
|
311 return false; |
|
312 } |
|
313 |
|
314 if (dv.length () > len) |
|
315 for (int i = len; i < dv.length (); i++) |
|
316 if (dv (i) != 1) |
|
317 { |
|
318 eval_error ("dimensions mismatch", elt->line (), elt->column (), 1, dv (i)); |
|
319 return false; |
|
320 } |
4501
|
321 } |
4765
|
322 dv.elem (1) = dv.elem (1) + this_elt_nc; |
4501
|
323 |
|
324 } |
|
325 else if (Vwarn_empty_list_elements) |
|
326 eval_warning ("empty matrix found in matrix list", |
|
327 elt->line (), elt->column ()); |
|
328 |
4915
|
329 append (val); |
|
330 |
4501
|
331 if (all_str && ! val.is_string ()) |
|
332 all_str = false; |
|
333 |
5280
|
334 if (all_sq_str && ! val.is_sq_string ()) |
|
335 all_sq_str = false; |
|
336 |
|
337 if (all_dq_str && ! val.is_dq_string ()) |
|
338 all_dq_str = false; |
|
339 |
4501
|
340 if (! some_str && val.is_string ()) |
|
341 some_str = true; |
|
342 |
5502
|
343 if (all_real && ! val.is_real_type ()) |
|
344 all_real = false; |
|
345 |
|
346 if (all_cmplx && ! (val.is_complex_type () || val.is_real_type ())) |
|
347 all_cmplx = false; |
4501
|
348 |
5631
|
349 if (!any_sparse && val.is_sparse_type ()) |
5630
|
350 any_sparse = true; |
|
351 |
4501
|
352 return true; |
|
353 } |
|
354 |
1827
|
355 void |
2971
|
356 tm_row_const::tm_row_const_rep::init (const tree_argument_list& row) |
1827
|
357 { |
|
358 all_str = true; |
5280
|
359 all_sq_str = true; |
|
360 all_dq_str = true; |
5502
|
361 all_real = true; |
|
362 all_cmplx = true; |
5630
|
363 any_sparse = false; |
1827
|
364 |
|
365 bool first_elem = true; |
|
366 |
4219
|
367 for (tree_argument_list::const_iterator p = row.begin (); |
|
368 p != row.end (); |
|
369 p++) |
1827
|
370 { |
5502
|
371 OCTAVE_QUIT; |
|
372 |
4219
|
373 tree_expression *elt = *p; |
1827
|
374 |
2971
|
375 octave_value tmp = elt->rvalue (); |
1827
|
376 |
|
377 if (error_state || tmp.is_undefined ()) |
|
378 break; |
|
379 else |
|
380 { |
4501
|
381 if (tmp.is_cs_list ()) |
1827
|
382 { |
4587
|
383 octave_value_list tlst = tmp.list_value (); |
2602
|
384 |
5275
|
385 for (octave_idx_type i = 0; i < tlst.length (); i++) |
1827
|
386 { |
5502
|
387 OCTAVE_QUIT; |
|
388 |
4587
|
389 if (! do_init_element (elt, tlst(i), first_elem)) |
4501
|
390 goto done; |
1827
|
391 } |
|
392 } |
4501
|
393 else |
|
394 { |
|
395 if (! do_init_element (elt, tmp, first_elem)) |
|
396 goto done; |
|
397 } |
1827
|
398 } |
|
399 } |
|
400 |
4501
|
401 done: |
|
402 |
1827
|
403 ok = ! error_state; |
1741
|
404 } |
|
405 |
2419
|
406 void |
|
407 tm_row_const::tm_row_const_rep::eval_error (const char *msg, int l, |
3661
|
408 int c, int x, int y) const |
2419
|
409 { |
|
410 if (l == -1 && c == -1) |
3661
|
411 { |
|
412 if (x == -1 || y == -1) |
|
413 ::error ("%s", msg); |
|
414 else |
|
415 ::error ("%s (%d != %d)", msg, x, y); |
|
416 } |
2419
|
417 else |
3661
|
418 { |
|
419 if (x == -1 || y == -1) |
|
420 ::error ("%s near line %d, column %d", msg, l, c); |
|
421 else |
|
422 ::error ("%s (%d != %d) near line %d, column %d", msg, x, y, l, c); |
|
423 } |
2419
|
424 } |
|
425 |
|
426 void |
|
427 tm_row_const::tm_row_const_rep::eval_warning (const char *msg, int l, |
|
428 int c) const |
|
429 { |
|
430 if (l == -1 && c == -1) |
|
431 ::warning ("%s", msg); |
|
432 else |
|
433 ::warning ("%s near line %d, column %d", msg, l, c); |
|
434 } |
|
435 |
1827
|
436 class |
4219
|
437 tm_const : public octave_base_list<tm_row_const> |
1827
|
438 { |
|
439 public: |
|
440 |
|
441 tm_const (const tree_matrix& tm) |
5514
|
442 : dv (0, 0), all_str (false), all_sq_str (false), all_dq_str (false), |
5502
|
443 some_str (false), all_real (false), all_cmplx (false), |
5630
|
444 all_mt (true), any_sparse (false), |
|
445 class_nm (octave_base_value::static_class_name ()), ok (false) |
5533
|
446 { init (tm); } |
1827
|
447 |
|
448 ~tm_const (void) { } |
|
449 |
5514
|
450 octave_idx_type rows (void) const { return dv.elem (0); } |
|
451 octave_idx_type cols (void) const { return dv.elem (1); } |
4765
|
452 |
|
453 dim_vector dims (void) const { return dv; } |
1827
|
454 |
2868
|
455 bool all_strings_p (void) const { return all_str; } |
5280
|
456 bool all_sq_strings_p (void) const { return all_sq_str; } |
|
457 bool all_dq_strings_p (void) const { return all_dq_str; } |
3110
|
458 bool some_strings_p (void) const { return some_str; } |
5502
|
459 bool all_real_p (void) const { return all_real; } |
|
460 bool all_complex_p (void) const { return all_cmplx; } |
2868
|
461 bool all_empty_p (void) const { return all_mt; } |
5630
|
462 bool any_sparse_p (void) const { return any_sparse; } |
1827
|
463 |
5533
|
464 std::string class_name (void) const { return class_nm; } |
|
465 |
3145
|
466 operator bool () const { return ok; } |
1827
|
467 |
|
468 private: |
|
469 |
4765
|
470 dim_vector dv; |
1827
|
471 |
|
472 bool all_str; |
5280
|
473 bool all_sq_str; |
|
474 bool all_dq_str; |
3110
|
475 bool some_str; |
5502
|
476 bool all_real; |
|
477 bool all_cmplx; |
2602
|
478 bool all_mt; |
5630
|
479 bool any_sparse; |
1827
|
480 |
5533
|
481 std::string class_nm; |
|
482 |
1827
|
483 bool ok; |
|
484 |
|
485 tm_const (void); |
|
486 |
|
487 tm_const (const tm_const&); |
|
488 |
|
489 tm_const& operator = (const tm_const&); |
|
490 |
|
491 void init (const tree_matrix& tm); |
|
492 }; |
|
493 |
|
494 void |
|
495 tm_const::init (const tree_matrix& tm) |
1741
|
496 { |
1827
|
497 all_str = true; |
5280
|
498 all_sq_str = true; |
|
499 all_dq_str = true; |
5502
|
500 all_real = true; |
|
501 all_cmplx = true; |
5630
|
502 any_sparse = false; |
1827
|
503 |
|
504 bool first_elem = true; |
|
505 |
|
506 // Just eval and figure out if what we have is complex or all |
|
507 // strings. We can't check columns until we know that this is a |
|
508 // numeric matrix -- collections of strings can have elements of |
|
509 // different lengths. |
|
510 |
4219
|
511 for (tree_matrix::const_iterator p = tm.begin (); p != tm.end (); p++) |
1827
|
512 { |
5502
|
513 OCTAVE_QUIT; |
|
514 |
4219
|
515 tree_argument_list *elt = *p; |
1827
|
516 |
|
517 tm_row_const tmp (*elt); |
|
518 |
|
519 if (tmp) |
|
520 { |
2868
|
521 if (all_str && ! tmp.all_strings_p ()) |
1827
|
522 all_str = false; |
|
523 |
5280
|
524 if (all_sq_str && ! tmp.all_sq_strings_p ()) |
|
525 all_sq_str = false; |
|
526 |
|
527 if (all_dq_str && ! tmp.all_dq_strings_p ()) |
|
528 all_dq_str = false; |
|
529 |
3110
|
530 if (! some_str && tmp.some_strings_p ()) |
|
531 some_str = true; |
|
532 |
5502
|
533 if (all_real && ! tmp.all_real_p ()) |
|
534 all_real = false; |
|
535 |
|
536 if (all_cmplx && ! tmp.all_complex_p ()) |
|
537 all_cmplx = false; |
1827
|
538 |
2868
|
539 if (all_mt && ! tmp.all_empty_p ()) |
2602
|
540 all_mt = false; |
|
541 |
5630
|
542 if (!any_sparse && tmp.any_sparse_p ()) |
|
543 any_sparse = true; |
|
544 |
1827
|
545 append (tmp); |
|
546 } |
|
547 else |
|
548 break; |
|
549 } |
|
550 |
|
551 if (! error_state) |
|
552 { |
4219
|
553 for (iterator p = begin (); p != end (); p++) |
1827
|
554 { |
5502
|
555 OCTAVE_QUIT; |
|
556 |
4219
|
557 tm_row_const elt = *p; |
1827
|
558 |
5275
|
559 octave_idx_type this_elt_nr = elt.rows (); |
|
560 octave_idx_type this_elt_nc = elt.cols (); |
1827
|
561 |
5533
|
562 std::string this_elt_class_nm = elt.class_name (); |
|
563 |
4765
|
564 dim_vector this_elt_dv = elt.dims (); |
|
565 |
|
566 if (!this_elt_dv.all_zero ()) |
1827
|
567 { |
2602
|
568 all_mt = false; |
|
569 |
1827
|
570 if (first_elem) |
|
571 { |
|
572 first_elem = false; |
|
573 |
5533
|
574 class_nm = this_elt_class_nm; |
|
575 |
4765
|
576 dv.resize (this_elt_dv.length ()); |
|
577 for (int i = 2; i < dv.length (); i++) |
|
578 dv.elem (i) = this_elt_dv.elem (i); |
|
579 |
|
580 dv.elem (0) = 0; |
|
581 |
|
582 dv.elem (1) = this_elt_nc; |
1827
|
583 } |
|
584 else if (all_str) |
|
585 { |
5533
|
586 class_nm = get_concat_class (class_nm, this_elt_class_nm); |
|
587 |
4765
|
588 if (this_elt_nc > cols ()) |
|
589 dv.elem (1) = this_elt_nc; |
1827
|
590 } |
4765
|
591 else |
1827
|
592 { |
5533
|
593 class_nm = get_concat_class (class_nm, this_elt_class_nm); |
|
594 |
4765
|
595 bool get_out = false; |
5045
|
596 int len = (this_elt_dv.length () < dv.length () |
|
597 ? this_elt_dv.length () : dv.length ()); |
4765
|
598 |
5045
|
599 for (int i = 1; i < len; i++) |
4765
|
600 { |
|
601 if (i == 1 && this_elt_nc != dv (1)) |
|
602 { |
|
603 ::error ("number of columns must match (%d != %d)", |
|
604 this_elt_nc, dv (1)); |
|
605 get_out = true; |
|
606 break; |
|
607 } |
|
608 else if (this_elt_dv (i) != dv (i)) |
|
609 { |
|
610 ::error ("dimensions mismatch (dim = %i, %d != %d)", i+1, this_elt_dv (i), dv (i)); |
|
611 get_out = true; |
|
612 break; |
|
613 } |
|
614 } |
|
615 |
5045
|
616 if (this_elt_dv.length () > len) |
|
617 for (int i = len; i < this_elt_dv.length (); i++) |
|
618 if (this_elt_dv (i) != 1) |
|
619 { |
|
620 ::error ("dimensions mismatch (dim = %i, %d != %d)", i+1, this_elt_dv (i), 1); |
|
621 get_out = true; |
|
622 break; |
|
623 } |
|
624 |
|
625 if (dv.length () > len) |
|
626 for (int i = len; i < dv.length (); i++) |
|
627 if (dv (i) != 1) |
|
628 { |
|
629 ::error ("dimensions mismatch (dim = %i, %d != %d)", i+1, 1, dv(i)); |
|
630 get_out = true; |
|
631 break; |
|
632 } |
|
633 |
4765
|
634 if (get_out) |
|
635 break; |
1827
|
636 } |
4765
|
637 dv.elem (0) = dv.elem (0) + this_elt_nr; |
1827
|
638 } |
4460
|
639 else if (Vwarn_empty_list_elements) |
|
640 warning ("empty matrix found in matrix list"); |
1827
|
641 } |
|
642 } |
|
643 |
|
644 ok = ! error_state; |
1741
|
645 } |
|
646 |
2990
|
647 tree_matrix::~tree_matrix (void) |
|
648 { |
4219
|
649 while (! empty ()) |
2990
|
650 { |
4219
|
651 iterator p = begin (); |
|
652 delete *p; |
|
653 erase (p); |
2990
|
654 } |
|
655 } |
|
656 |
1827
|
657 bool |
4267
|
658 tree_matrix::has_magic_end (void) const |
|
659 { |
|
660 for (const_iterator p = begin (); p != end (); p++) |
|
661 { |
5502
|
662 OCTAVE_QUIT; |
|
663 |
4267
|
664 tree_argument_list *elt = *p; |
|
665 |
|
666 if (elt && elt->has_magic_end ()) |
|
667 return true; |
|
668 } |
|
669 |
|
670 return false; |
|
671 } |
|
672 |
|
673 bool |
2529
|
674 tree_matrix::all_elements_are_constant (void) const |
1827
|
675 { |
4219
|
676 for (const_iterator p = begin (); p != end (); p++) |
1827
|
677 { |
5502
|
678 OCTAVE_QUIT; |
|
679 |
4219
|
680 tree_argument_list *elt = *p; |
1827
|
681 |
2529
|
682 if (! elt->all_elements_are_constant ()) |
1827
|
683 return false; |
|
684 } |
|
685 |
|
686 return true; |
|
687 } |
|
688 |
2971
|
689 octave_value_list |
|
690 tree_matrix::rvalue (int nargout) |
|
691 { |
|
692 octave_value_list retval; |
|
693 |
3770
|
694 MAYBE_DO_BREAKPOINT; |
|
695 |
2971
|
696 if (nargout > 1) |
|
697 error ("invalid number of output arguments for matrix list"); |
|
698 else |
|
699 retval = rvalue (); |
|
700 |
|
701 return retval; |
|
702 } |
|
703 |
5280
|
704 static void |
|
705 maybe_warn_string_concat (bool all_dq_strings_p, bool all_sq_strings_p) |
|
706 { |
|
707 if (Vwarn_string_concat && ! (all_dq_strings_p || all_sq_strings_p)) |
|
708 ::warning ("concatenation of different character string types may have unintended consequences"); |
|
709 } |
|
710 |
5502
|
711 #define SINGLE_TYPE_CONCAT(TYPE, EXTRACTOR) \ |
|
712 do \ |
|
713 { \ |
|
714 int dv_len = dv.length (); \ |
|
715 Array<octave_idx_type> ra_idx (dv_len > 1 ? dv_len : 2, 0); \ |
|
716 \ |
|
717 for (tm_const::iterator p = tmp.begin (); p != tmp.end (); p++) \ |
|
718 { \ |
|
719 OCTAVE_QUIT; \ |
|
720 \ |
|
721 tm_row_const row = *p; \ |
|
722 \ |
|
723 for (tm_row_const::iterator q = row.begin (); \ |
|
724 q != row.end (); \ |
|
725 q++) \ |
|
726 { \ |
|
727 OCTAVE_QUIT; \ |
|
728 \ |
|
729 TYPE ra = q->EXTRACTOR (); \ |
|
730 \ |
|
731 if (! error_state) \ |
|
732 { \ |
|
733 result.insert (ra, ra_idx); \ |
|
734 \ |
|
735 if (! error_state) \ |
|
736 ra_idx(1) += ra.columns (); \ |
|
737 else \ |
|
738 goto done; \ |
|
739 } \ |
|
740 else \ |
|
741 goto done; \ |
|
742 } \ |
|
743 \ |
|
744 ra_idx(0) += row.rows (); \ |
|
745 ra_idx(1) = 0; \ |
|
746 } \ |
|
747 } \ |
|
748 while (0) |
|
749 |
5533
|
750 #define DO_SINGLE_TYPE_CONCAT(TYPE, EXTRACTOR) \ |
|
751 do \ |
|
752 { \ |
|
753 TYPE result (dv); \ |
|
754 \ |
|
755 SINGLE_TYPE_CONCAT(TYPE, EXTRACTOR); \ |
|
756 \ |
|
757 retval = result; \ |
|
758 } \ |
|
759 while (0) |
|
760 |
2086
|
761 octave_value |
2971
|
762 tree_matrix::rvalue (void) |
1741
|
763 { |
2086
|
764 octave_value retval; |
1741
|
765 |
4915
|
766 bool all_strings_p = false; |
5280
|
767 bool all_sq_strings_p = false; |
|
768 bool all_dq_strings_p = false; |
4915
|
769 bool all_empty_p = false; |
5502
|
770 bool all_real_p = false; |
|
771 bool all_complex_p = false; |
5630
|
772 bool any_sparse_p = false; |
4915
|
773 bool frc_str_conv = false; |
1741
|
774 |
4915
|
775 tm_const tmp (*this); |
3110
|
776 |
1827
|
777 if (tmp) |
|
778 { |
4765
|
779 dim_vector dv = tmp.dims (); |
4915
|
780 all_strings_p = tmp.all_strings_p (); |
5280
|
781 all_sq_strings_p = tmp.all_sq_strings_p (); |
|
782 all_dq_strings_p = tmp.all_dq_strings_p (); |
4915
|
783 all_empty_p = tmp.all_empty_p (); |
5502
|
784 all_real_p = tmp.all_real_p (); |
|
785 all_complex_p = tmp.all_complex_p (); |
5630
|
786 any_sparse_p = tmp.any_sparse_p (); |
4915
|
787 frc_str_conv = tmp.some_strings_p (); |
1741
|
788 |
5502
|
789 // Try to speed up the common cases. |
4915
|
790 |
5533
|
791 std::string result_type = tmp.class_name (); |
|
792 |
|
793 if (result_type == "double") |
|
794 { |
|
795 if (all_real_p) |
|
796 DO_SINGLE_TYPE_CONCAT (NDArray, array_value); |
|
797 else |
|
798 DO_SINGLE_TYPE_CONCAT (ComplexNDArray, complex_array_value); |
|
799 } |
|
800 #if 0 |
|
801 else if (result_type == "single") |
|
802 #endif |
|
803 else if (result_type == "char") |
5280
|
804 { |
|
805 char type = all_sq_strings_p ? '\'' : '"'; |
|
806 |
|
807 maybe_warn_string_concat (all_dq_strings_p, all_sq_strings_p); |
|
808 |
5502
|
809 charNDArray result (dv, Vstring_fill_char); |
|
810 |
|
811 SINGLE_TYPE_CONCAT (charNDArray, char_array_value); |
|
812 |
|
813 retval = octave_value (result, true, type); |
|
814 } |
5533
|
815 else if (result_type == "logical") |
|
816 DO_SINGLE_TYPE_CONCAT (boolNDArray, bool_array_value); |
|
817 else if (result_type == "int8") |
|
818 DO_SINGLE_TYPE_CONCAT (int8NDArray, int8_array_value); |
|
819 else if (result_type == "int16") |
|
820 DO_SINGLE_TYPE_CONCAT (int16NDArray, int16_array_value); |
|
821 else if (result_type == "int32") |
|
822 DO_SINGLE_TYPE_CONCAT (int32NDArray, int32_array_value); |
|
823 else if (result_type == "int64") |
|
824 DO_SINGLE_TYPE_CONCAT (int64NDArray, int64_array_value); |
|
825 else if (result_type == "uint8") |
|
826 DO_SINGLE_TYPE_CONCAT (uint8NDArray, uint8_array_value); |
|
827 else if (result_type == "uint16") |
|
828 DO_SINGLE_TYPE_CONCAT (uint16NDArray, uint16_array_value); |
|
829 else if (result_type == "uint32") |
|
830 DO_SINGLE_TYPE_CONCAT (uint32NDArray, uint32_array_value); |
|
831 else if (result_type == "uint64") |
|
832 DO_SINGLE_TYPE_CONCAT (uint64NDArray, uint64_array_value); |
4915
|
833 else |
|
834 { |
5502
|
835 // The line below might seem crazy, since we take a copy of |
|
836 // the first argument, resize it to be empty and then resize |
|
837 // it to be full. This is done since it means that there is |
|
838 // no recopying of data, as would happen if we used a single |
|
839 // resize. It should be noted that resize operation is also |
|
840 // significantly slower than the do_cat_op function, so it |
|
841 // makes sense to have an empty matrix and copy all data. |
|
842 // |
|
843 // We might also start with a empty octave_value using |
|
844 // |
|
845 // ctmp = octave_value_typeinfo::lookup_type |
|
846 // (tmp.begin() -> begin() -> type_name()); |
|
847 // |
|
848 // and then directly resize. However, for some types there |
|
849 // might be some additional setup needed, and so this should |
|
850 // be avoided. |
|
851 |
|
852 octave_value ctmp; |
|
853 |
5164
|
854 // Find the first non-empty object |
5502
|
855 |
5630
|
856 if (any_sparse_p) |
5164
|
857 { |
5630
|
858 // Start with sparse matrix to avoid issues memory issues |
|
859 // with things like [ones(1,4),sprandn(1e8,4,1e-4)] |
|
860 if (all_real_p) |
|
861 ctmp = octave_sparse_matrix ().resize (dv); |
|
862 else |
|
863 ctmp = octave_sparse_complex_matrix ().resize (dv); |
|
864 } |
|
865 else |
|
866 { |
|
867 for (tm_const::iterator p = tmp.begin (); p != tmp.end (); p++) |
5164
|
868 { |
5502
|
869 OCTAVE_QUIT; |
|
870 |
5630
|
871 tm_row_const row = *p; |
5502
|
872 |
5630
|
873 for (tm_row_const::iterator q = row.begin (); |
|
874 q != row.end (); q++) |
|
875 { |
|
876 OCTAVE_QUIT; |
|
877 |
|
878 ctmp = *q; |
5164
|
879 |
5630
|
880 if (! ctmp.all_zero_dims ()) |
|
881 goto found_non_empty; |
|
882 } |
|
883 } |
5164
|
884 |
5630
|
885 ctmp = (*(tmp.begin() -> begin())); |
|
886 |
|
887 found_non_empty: |
5502
|
888 |
5630
|
889 if (! all_empty_p) |
|
890 ctmp = ctmp.resize (dim_vector (0,0)).resize (dv); |
|
891 } |
4915
|
892 |
5502
|
893 if (! error_state) |
1827
|
894 { |
5502
|
895 // Now, extract the values from the individual elements and |
|
896 // insert them in the result matrix. |
|
897 |
|
898 int dv_len = dv.length (); |
|
899 Array<int> ra_idx (dv_len > 1 ? dv_len : 2, 0); |
|
900 |
|
901 for (tm_const::iterator p = tmp.begin (); p != tmp.end (); p++) |
|
902 { |
|
903 OCTAVE_QUIT; |
|
904 |
|
905 tm_row_const row = *p; |
|
906 |
|
907 for (tm_row_const::iterator q = row.begin (); |
|
908 q != row.end (); |
|
909 q++) |
|
910 { |
|
911 OCTAVE_QUIT; |
1741
|
912 |
5502
|
913 octave_value elt = *q; |
|
914 |
|
915 ctmp = do_cat_op (ctmp, elt, ra_idx); |
|
916 |
|
917 if (error_state) |
|
918 goto done; |
|
919 |
|
920 ra_idx (1) += elt.columns (); |
|
921 } |
|
922 |
|
923 ra_idx (0) += row.rows (); |
|
924 ra_idx (1) = 0; |
|
925 } |
|
926 |
|
927 retval = ctmp; |
|
928 |
|
929 if (frc_str_conv && ! retval.is_string ()) |
|
930 retval = retval.convert_to_str (); |
1741
|
931 } |
|
932 } |
|
933 } |
|
934 |
1827
|
935 done: |
1741
|
936 return retval; |
|
937 } |
|
938 |
|
939 void |
2124
|
940 tree_matrix::accept (tree_walker& tw) |
1741
|
941 { |
2124
|
942 tw.visit_matrix (*this); |
1741
|
943 } |
|
944 |
2172
|
945 static int |
4460
|
946 warn_empty_list_elements (void) |
2172
|
947 { |
4460
|
948 Vwarn_empty_list_elements = check_preference ("warn_empty_list_elements"); |
2172
|
949 |
|
950 return 0; |
|
951 } |
|
952 |
2254
|
953 static int |
5280
|
954 warn_string_concat (void) |
|
955 { |
|
956 Vwarn_string_concat = check_preference ("warn_string_concat"); |
|
957 |
|
958 return 0; |
|
959 } |
|
960 |
|
961 static int |
2254
|
962 string_fill_char (void) |
|
963 { |
|
964 int status = 0; |
|
965 |
3523
|
966 std::string s = builtin_string_variable ("string_fill_char"); |
2254
|
967 |
|
968 switch (s.length ()) |
|
969 { |
|
970 case 1: |
|
971 Vstring_fill_char = s[0]; |
|
972 break; |
|
973 |
|
974 case 0: |
|
975 Vstring_fill_char = '\0'; |
|
976 break; |
|
977 |
|
978 default: |
|
979 warning ("string_fill_char must be a single character"); |
|
980 status = -1; |
|
981 break; |
|
982 } |
|
983 |
|
984 return status; |
|
985 } |
|
986 |
2172
|
987 void |
|
988 symbols_of_pt_mat (void) |
|
989 { |
3258
|
990 DEFVAR (string_fill_char, " ", string_fill_char, |
3361
|
991 "-*- texinfo -*-\n\ |
|
992 @defvr {Built-in Variable} string_fill_char\n\ |
|
993 The value of this variable is used to pad all strings in a string matrix\n\ |
|
994 to the same length. It should be a single character. The default value\n\ |
|
995 is @code{\" \"} (a single space). For example,\n\ |
|
996 \n\ |
|
997 @example\n\ |
|
998 @group\n\ |
|
999 string_fill_char = \"X\";\n\ |
|
1000 [ \"these\"; \"are\"; \"strings\" ]\n\ |
|
1001 @result{} \"theseXX\"\n\ |
|
1002 \"areXXXX\"\n\ |
|
1003 \"strings\"\n\ |
|
1004 @end group\n\ |
|
1005 @end example\n\ |
3363
|
1006 @end defvr"); |
4460
|
1007 |
|
1008 DEFVAR (warn_empty_list_elements, false, warn_empty_list_elements, |
|
1009 "-*- texinfo -*-\n\ |
|
1010 @defvr {Built-in Variable} warn_empty_list_elements\n\ |
|
1011 If the value of @code{warn_empty_list_elements} is nonzero, print a\n\ |
|
1012 warning when an empty matrix is found in a matrix list. For example,\n\ |
|
1013 \n\ |
|
1014 @example\n\ |
|
1015 a = [1, [], 3, [], 5]\n\ |
|
1016 @end example\n\ |
|
1017 \n\ |
|
1018 @noindent\n\ |
|
1019 The default value is 0.\n\ |
|
1020 @end defvr"); |
|
1021 |
5391
|
1022 DEFVAR (warn_string_concat, false, warn_string_concat, |
5280
|
1023 "-*- texinfo -*-\n\ |
|
1024 @defvr {Built-in Variable} warn_string_concat\n\ |
|
1025 If the value of @code{warn_string_concat} is nonzero, print a\n\ |
|
1026 warning when concatenating a mixture of double and single quoted strings.\n\ |
|
1027 The default value is 1.\n\ |
|
1028 @end defvr"); |
|
1029 |
2172
|
1030 } |
|
1031 |
1741
|
1032 /* |
|
1033 ;;; Local Variables: *** |
|
1034 ;;; mode: C++ *** |
|
1035 ;;; End: *** |
|
1036 */ |