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