2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
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 "Array-flags.h" |
|
32 |
|
33 #include "ov.h" |
|
34 #include "ov-base.h" |
2825
|
35 #include "ov-bool.h" |
|
36 #include "ov-bool-mat.h" |
2376
|
37 #include "ov-scalar.h" |
|
38 #include "ov-re-mat.h" |
|
39 #include "ov-complex.h" |
|
40 #include "ov-cx-mat.h" |
|
41 #include "ov-ch-mat.h" |
|
42 #include "ov-str-mat.h" |
|
43 #include "ov-range.h" |
|
44 #include "ov-struct.h" |
|
45 #include "ov-colon.h" |
|
46 #include "ov-va-args.h" |
|
47 #include "ov-typeinfo.h" |
|
48 |
|
49 #include "defun.h" |
|
50 #include "gripes.h" |
|
51 #include "pager.h" |
|
52 #include "pr-output.h" |
|
53 #include "utils.h" |
|
54 #include "variables.h" |
|
55 |
2477
|
56 // We are likely to have a lot of octave_value objects to allocate, so |
|
57 // make the grow_size large. |
|
58 octave_allocator |
|
59 octave_value::allocator (sizeof (octave_value), 1024); |
|
60 |
2376
|
61 // If TRUE, allow assignments like |
|
62 // |
|
63 // octave> A(1) = 3; A(2) = 5 |
|
64 // |
|
65 // for A already defined and a matrix type. |
|
66 bool Vdo_fortran_indexing; |
|
67 |
|
68 // Should we allow things like: |
|
69 // |
|
70 // octave> 'abc' + 0 |
|
71 // 97 98 99 |
|
72 // |
|
73 // to happen? A positive value means yes. A negative value means |
|
74 // yes, but print a warning message. Zero means it should be |
|
75 // considered an error. |
|
76 int Vimplicit_str_to_num_ok; |
|
77 |
|
78 // Should we allow silent conversion of complex to real when a real |
|
79 // type is what we're really looking for? A positive value means yes. |
|
80 // A negative value means yes, but print a warning message. Zero |
|
81 // means it should be considered an error. |
|
82 int Vok_to_lose_imaginary_part; |
|
83 |
|
84 // If TRUE, create column vectors when doing assignments like: |
|
85 // |
|
86 // octave> A(1) = 3; A(2) = 5 |
|
87 // |
|
88 // (for A undefined). Only matters when resize_on_range_error is also |
|
89 // TRUE. |
|
90 bool Vprefer_column_vectors; |
|
91 |
|
92 // If TRUE, print the name along with the value. |
|
93 bool Vprint_answer_id_name; |
|
94 |
|
95 // Should operations on empty matrices return empty matrices or an |
|
96 // error? A positive value means yes. A negative value means yes, |
|
97 // but print a warning message. Zero means it should be considered an |
|
98 // error. |
|
99 int Vpropagate_empty_matrices; |
|
100 |
|
101 // If TRUE, resize matrices when performing and indexed assignment and |
|
102 // the indices are outside the current bounds. |
|
103 bool Vresize_on_range_error; |
|
104 |
|
105 // How many levels of structure elements should we print? |
|
106 int Vstruct_levels_to_print; |
|
107 |
|
108 // Allow divide by zero errors to be suppressed. |
|
109 bool Vwarn_divide_by_zero; |
|
110 |
|
111 // Indentation level for structures. |
|
112 int struct_indent = 0; |
|
113 |
|
114 // XXX FIXME XXX |
|
115 void |
|
116 increment_struct_indent (void) |
|
117 { |
|
118 struct_indent += 2; |
|
119 } |
|
120 |
|
121 void |
|
122 decrement_struct_indent (void) |
|
123 { |
|
124 struct_indent -= 2; |
|
125 } |
|
126 |
|
127 // Octave's value type. |
|
128 |
|
129 string |
|
130 octave_value::binary_op_as_string (binary_op op) |
|
131 { |
|
132 string retval; |
|
133 |
|
134 switch (op) |
|
135 { |
|
136 case add: |
|
137 retval = "+"; |
|
138 break; |
|
139 |
|
140 case sub: |
|
141 retval = "-"; |
|
142 break; |
|
143 |
|
144 case mul: |
|
145 retval = "*"; |
|
146 break; |
|
147 |
|
148 case div: |
|
149 retval = "/"; |
|
150 break; |
|
151 |
|
152 case pow: |
|
153 retval = "^"; |
|
154 break; |
|
155 |
|
156 case ldiv: |
|
157 retval = "\\"; |
|
158 break; |
|
159 |
|
160 case lt: |
|
161 retval = "<"; |
|
162 break; |
|
163 |
|
164 case le: |
|
165 retval = "<="; |
|
166 break; |
|
167 |
|
168 case eq: |
|
169 retval = "=="; |
|
170 break; |
|
171 |
|
172 case ge: |
|
173 retval = ">="; |
|
174 break; |
|
175 |
|
176 case gt: |
|
177 retval = ">"; |
|
178 break; |
|
179 |
|
180 case ne: |
|
181 retval = "!="; |
|
182 break; |
|
183 |
|
184 case el_mul: |
|
185 retval = ".*"; |
|
186 break; |
|
187 |
|
188 case el_div: |
|
189 retval = "./"; |
|
190 break; |
|
191 |
|
192 case el_pow: |
|
193 retval = ".^"; |
|
194 break; |
|
195 |
|
196 case el_ldiv: |
|
197 retval = ".\\"; |
|
198 break; |
|
199 |
|
200 case el_and: |
|
201 retval = "&"; |
|
202 break; |
|
203 |
|
204 case el_or: |
|
205 retval = "|"; |
|
206 break; |
|
207 |
|
208 case struct_ref: |
|
209 retval = "."; |
|
210 break; |
|
211 |
|
212 default: |
|
213 retval = "<unknown>"; |
|
214 } |
|
215 |
|
216 return retval; |
|
217 } |
|
218 |
|
219 octave_value::octave_value (void) |
2825
|
220 : rep (new octave_base_value ()) |
|
221 { |
|
222 rep->count = 1; |
|
223 } |
2376
|
224 |
|
225 octave_value::octave_value (double d) |
2825
|
226 : rep (new octave_scalar (d)) |
|
227 { |
|
228 rep->count = 1; |
|
229 } |
2376
|
230 |
|
231 octave_value::octave_value (const Matrix& m) |
2423
|
232 : rep (new octave_matrix (m)) |
|
233 { |
|
234 rep->count = 1; |
|
235 maybe_mutate (); |
|
236 } |
2376
|
237 |
|
238 octave_value::octave_value (const DiagMatrix& d) |
2423
|
239 : rep (new octave_matrix (d)) |
|
240 { |
|
241 rep->count = 1; |
|
242 maybe_mutate (); |
|
243 } |
2376
|
244 |
|
245 octave_value::octave_value (const RowVector& v, int pcv) |
2423
|
246 : rep (new octave_matrix (v, pcv)) |
|
247 { |
|
248 rep->count = 1; |
|
249 maybe_mutate (); |
|
250 } |
2376
|
251 |
|
252 octave_value::octave_value (const ColumnVector& v, int pcv) |
2423
|
253 : rep (new octave_matrix (v, pcv)) |
|
254 { |
|
255 rep->count = 1; |
|
256 maybe_mutate (); |
|
257 } |
2376
|
258 |
|
259 octave_value::octave_value (const Complex& C) |
2423
|
260 : rep (new octave_complex (C)) |
|
261 { |
|
262 rep->count = 1; |
|
263 maybe_mutate (); |
|
264 } |
2376
|
265 |
|
266 octave_value::octave_value (const ComplexMatrix& m) |
2423
|
267 : rep (new octave_complex_matrix (m)) |
|
268 { |
|
269 rep->count = 1; |
|
270 maybe_mutate (); |
|
271 } |
2376
|
272 |
|
273 octave_value::octave_value (const ComplexDiagMatrix& d) |
2423
|
274 : rep (new octave_complex_matrix (d)) |
|
275 { |
|
276 rep->count = 1; |
|
277 maybe_mutate (); |
|
278 } |
2376
|
279 |
|
280 octave_value::octave_value (const ComplexRowVector& v, int pcv) |
2423
|
281 : rep (new octave_complex_matrix (v, pcv)) |
|
282 { |
|
283 rep->count = 1; |
|
284 maybe_mutate (); |
|
285 } |
2376
|
286 |
|
287 octave_value::octave_value (const ComplexColumnVector& v, int pcv) |
2423
|
288 : rep (new octave_complex_matrix (v, pcv)) |
|
289 { |
|
290 rep->count = 1; |
|
291 maybe_mutate (); |
|
292 } |
2376
|
293 |
2825
|
294 octave_value::octave_value (bool b) |
|
295 : rep (new octave_bool (b)) |
|
296 { |
|
297 rep->count = 1; |
|
298 } |
|
299 |
|
300 octave_value::octave_value (const boolMatrix& bm) |
|
301 : rep (new octave_bool_matrix (bm)) |
|
302 { |
|
303 rep->count = 1; |
|
304 maybe_mutate (); |
|
305 } |
|
306 |
2376
|
307 octave_value::octave_value (const char *s) |
2423
|
308 : rep (new octave_char_matrix_str (s)) |
|
309 { |
|
310 rep->count = 1; |
|
311 maybe_mutate (); |
|
312 } |
2376
|
313 |
|
314 octave_value::octave_value (const string& s) |
2423
|
315 : rep (new octave_char_matrix_str (s)) |
|
316 { |
|
317 rep->count = 1; |
|
318 maybe_mutate (); |
|
319 } |
2376
|
320 |
|
321 octave_value::octave_value (const string_vector& s) |
2423
|
322 : rep (new octave_char_matrix_str (s)) |
|
323 { |
|
324 rep->count = 1; |
|
325 maybe_mutate (); |
|
326 } |
2376
|
327 |
|
328 octave_value::octave_value (const charMatrix& chm, bool is_string) |
2409
|
329 : rep (0) |
|
330 { |
|
331 if (is_string) |
|
332 rep = new octave_char_matrix_str (chm); |
|
333 else |
|
334 rep = new octave_char_matrix (chm); |
2376
|
335 |
2409
|
336 rep->count = 1; |
2423
|
337 maybe_mutate (); |
2409
|
338 } |
2376
|
339 |
|
340 octave_value::octave_value (double base, double limit, double inc) |
2423
|
341 : rep (new octave_range (base, limit, inc)) |
|
342 { |
|
343 rep->count = 1; |
|
344 maybe_mutate (); |
|
345 } |
2376
|
346 |
|
347 octave_value::octave_value (const Range& r) |
2423
|
348 : rep (new octave_range (r)) |
|
349 { |
|
350 rep->count = 1; |
|
351 maybe_mutate (); |
|
352 } |
2376
|
353 |
|
354 octave_value::octave_value (const Octave_map& m) |
2825
|
355 : rep (new octave_struct (m)) |
|
356 { |
|
357 rep->count = 1; |
|
358 } |
2376
|
359 |
|
360 octave_value::octave_value (octave_value::magic_colon) |
2825
|
361 : rep (new octave_magic_colon ()) |
|
362 { |
|
363 rep->count = 1; |
|
364 } |
2376
|
365 |
|
366 octave_value::octave_value (octave_value::all_va_args) |
2825
|
367 : rep (new octave_all_va_args ()) |
|
368 { |
|
369 rep->count = 1; |
|
370 } |
2376
|
371 |
|
372 octave_value::octave_value (octave_value *new_rep) |
2825
|
373 : rep (new_rep) |
|
374 { |
|
375 rep->count = 1; |
|
376 } |
2376
|
377 |
|
378 octave_value::~octave_value (void) |
|
379 { |
|
380 #if defined (MDEBUG) |
|
381 cerr << "~octave_value: rep: " << rep |
|
382 << " rep->count: " << rep->count << "\n"; |
|
383 #endif |
|
384 |
|
385 if (rep && --rep->count == 0) |
|
386 { |
|
387 delete rep; |
|
388 rep = 0; |
|
389 } |
|
390 } |
|
391 |
2409
|
392 void |
|
393 octave_value::maybe_mutate (void) |
|
394 { |
2410
|
395 octave_value *tmp = rep->try_narrowing_conversion (); |
2409
|
396 |
|
397 if (tmp && tmp != rep) |
|
398 { |
|
399 if (--rep->count == 0) |
|
400 delete rep; |
|
401 |
|
402 rep = tmp; |
|
403 rep->count = 1; |
|
404 } |
|
405 } |
|
406 |
2376
|
407 static void |
|
408 gripe_no_conversion (const string& tn1, const string& tn2) |
|
409 { |
|
410 error ("no suitable conversion found for assignment of %s to indexed %s", |
|
411 tn2.c_str (), tn1.c_str ()); |
|
412 } |
|
413 |
2409
|
414 octave_value& |
|
415 octave_value::assign (const octave_value_list& idx, const octave_value& rhs) |
|
416 { |
|
417 make_unique (); |
2376
|
418 |
2409
|
419 bool assignment_ok = try_assignment (idx, rhs); |
|
420 |
|
421 if (! (error_state || assignment_ok)) |
|
422 { |
|
423 assignment_ok = try_assignment_with_conversion (idx, rhs); |
|
424 |
|
425 if (! (error_state || assignment_ok)) |
2376
|
426 gripe_no_conversion (type_name (), rhs.type_name ()); |
|
427 } |
|
428 |
2409
|
429 if (! error_state) |
|
430 maybe_mutate (); |
|
431 |
2376
|
432 return *this; |
|
433 } |
|
434 |
|
435 Octave_map |
|
436 octave_value::map_value (void) const |
|
437 { |
|
438 return rep->map_value (); |
|
439 } |
|
440 |
|
441 ColumnVector |
|
442 octave_value::vector_value (bool force_string_conv, |
|
443 bool force_vector_conversion) const |
|
444 { |
|
445 ColumnVector retval; |
|
446 |
|
447 Matrix m = matrix_value (force_string_conv); |
|
448 |
|
449 if (error_state) |
|
450 return retval; |
|
451 |
|
452 int nr = m.rows (); |
|
453 int nc = m.columns (); |
|
454 |
|
455 if (nr == 1) |
|
456 { |
|
457 retval.resize (nc); |
|
458 for (int i = 0; i < nc; i++) |
|
459 retval (i) = m (0, i); |
|
460 } |
|
461 else if (nc == 1) |
|
462 { |
|
463 retval.resize (nr); |
|
464 for (int i = 0; i < nr; i++) |
|
465 retval (i) = m (i, 0); |
|
466 } |
|
467 else if (nr > 0 && nc > 0 |
|
468 && (Vdo_fortran_indexing || force_vector_conversion)) |
|
469 { |
|
470 retval.resize (nr * nc); |
|
471 int k = 0; |
|
472 for (int j = 0; j < nc; j++) |
|
473 for (int i = 0; i < nr; i++) |
|
474 retval (k++) = m (i, j); |
|
475 } |
|
476 else |
|
477 { |
|
478 string tn = type_name (); |
|
479 gripe_invalid_conversion (tn.c_str (), "real vector"); |
|
480 } |
|
481 |
|
482 return retval; |
|
483 } |
|
484 |
|
485 ComplexColumnVector |
|
486 octave_value::complex_vector_value (bool force_string_conv, |
|
487 bool force_vector_conversion) const |
|
488 { |
|
489 ComplexColumnVector retval; |
|
490 |
|
491 ComplexMatrix m = complex_matrix_value (force_string_conv); |
|
492 |
|
493 if (error_state) |
|
494 return retval; |
|
495 |
|
496 int nr = m.rows (); |
|
497 int nc = m.columns (); |
|
498 |
|
499 if (nr == 1) |
|
500 { |
|
501 retval.resize (nc); |
|
502 for (int i = 0; i < nc; i++) |
|
503 retval (i) = m (0, i); |
|
504 } |
|
505 else if (nc == 1) |
|
506 { |
|
507 retval.resize (nr); |
|
508 for (int i = 0; i < nr; i++) |
|
509 retval (i) = m (i, 0); |
|
510 } |
|
511 else if (nr > 0 && nc > 0 |
|
512 && (Vdo_fortran_indexing || force_vector_conversion)) |
|
513 { |
|
514 retval.resize (nr * nc); |
|
515 int k = 0; |
|
516 for (int j = 0; j < nc; j++) |
|
517 for (int i = 0; i < nr; i++) |
|
518 retval (k++) = m (i, j); |
|
519 } |
|
520 else |
|
521 { |
|
522 string tn = type_name (); |
|
523 gripe_invalid_conversion (tn.c_str (), "complex vector"); |
|
524 } |
|
525 |
|
526 return retval; |
|
527 } |
|
528 |
|
529 void |
2466
|
530 octave_value::print (bool pr_as_read_syntax) |
2376
|
531 { |
2466
|
532 print (octave_stdout, pr_as_read_syntax); |
2376
|
533 } |
|
534 |
|
535 void |
|
536 octave_value::print_with_name (const string& name, bool print_padding) |
|
537 { |
|
538 print_with_name (octave_stdout, name, print_padding); |
|
539 } |
|
540 |
|
541 void |
|
542 octave_value::print_with_name (ostream& output_buf, const string& name, |
|
543 bool print_padding) |
|
544 { |
|
545 bool pad_after = false; |
|
546 |
|
547 if (Vprint_answer_id_name) |
|
548 { |
|
549 if (print_as_scalar ()) |
|
550 output_buf << name << " = "; |
2475
|
551 else if (is_map ()) |
2376
|
552 { |
|
553 pad_after = true; |
|
554 output_buf << name << " ="; |
|
555 } |
|
556 else |
|
557 { |
|
558 pad_after = true; |
|
559 output_buf << name << " =\n\n"; |
|
560 } |
|
561 } |
|
562 |
|
563 print (output_buf); |
|
564 |
|
565 if (print_padding && pad_after) |
|
566 output_buf << "\n"; |
|
567 } |
|
568 |
|
569 bool |
|
570 octave_value::print_as_scalar (void) |
|
571 { |
|
572 int nr = rows (); |
|
573 int nc = columns (); |
|
574 |
|
575 return (is_scalar_type () |
|
576 || (is_string () && nr <= 1) |
|
577 || (is_matrix_type () |
|
578 && ((nr == 1 && nc == 1) |
|
579 || nr == 0 |
|
580 || nc == 0))); |
|
581 } |
|
582 |
|
583 static void |
2413
|
584 gripe_indexed_assignment (const string& tn1, const string& tn2) |
|
585 { |
|
586 error ("assignment of %s to indexed %s not implemented", |
|
587 tn2.c_str (), tn1.c_str ()); |
|
588 } |
|
589 |
|
590 static void |
|
591 gripe_conversion_failed (const string& tn1, const string& tn2) |
|
592 { |
|
593 error ("type conversion for assignment of %s to indexed %s failed", |
|
594 tn2.c_str (), tn1.c_str ()); |
|
595 } |
|
596 |
|
597 bool |
|
598 octave_value::convert_and_assign (const octave_value_list& idx, |
|
599 const octave_value& rhs) |
|
600 { |
|
601 bool assignment_ok = false; |
|
602 |
|
603 int t_lhs = type_id (); |
|
604 int t_rhs = rhs.type_id (); |
|
605 |
|
606 int t_result |
|
607 = octave_value_typeinfo::lookup_pref_assign_conv (t_lhs, t_rhs); |
|
608 |
|
609 if (t_result >= 0) |
|
610 { |
2427
|
611 type_conv_fcn cf |
2413
|
612 = octave_value_typeinfo::lookup_widening_op (t_lhs, t_result); |
|
613 |
|
614 if (cf) |
|
615 { |
|
616 octave_value *tmp = cf (*rep); |
|
617 |
|
618 if (tmp) |
|
619 { |
2589
|
620 octave_value *old_rep = rep; |
|
621 rep = tmp; |
|
622 rep->count = 1; |
|
623 |
|
624 assignment_ok = try_assignment (idx, rhs); |
|
625 |
|
626 if (! assignment_ok && old_rep) |
2413
|
627 { |
|
628 if (--rep->count == 0) |
|
629 delete rep; |
|
630 |
2589
|
631 rep = old_rep; |
|
632 old_rep = 0; |
2413
|
633 } |
|
634 |
2589
|
635 if (old_rep && --old_rep->count == 0) |
|
636 delete old_rep; |
2413
|
637 } |
|
638 else |
|
639 gripe_conversion_failed (type_name (), rhs.type_name ()); |
|
640 } |
|
641 else |
|
642 gripe_indexed_assignment (type_name (), rhs.type_name ()); |
|
643 } |
|
644 |
|
645 return (assignment_ok && ! error_state); |
|
646 } |
|
647 |
|
648 bool |
|
649 octave_value::try_assignment_with_conversion (const octave_value_list& idx, |
|
650 const octave_value& rhs) |
|
651 { |
|
652 bool assignment_ok = convert_and_assign (idx, rhs); |
|
653 |
|
654 if (! (error_state || assignment_ok)) |
|
655 { |
|
656 octave_value tmp_rhs; |
2427
|
657 type_conv_fcn cf_rhs = rhs.numeric_conversion_function (); |
2413
|
658 |
|
659 if (cf_rhs) |
|
660 tmp_rhs = octave_value (cf_rhs (*rhs.rep)); |
|
661 else |
|
662 tmp_rhs = rhs; |
|
663 |
|
664 octave_value *old_rep = 0; |
2427
|
665 type_conv_fcn cf_this = numeric_conversion_function (); |
2413
|
666 |
|
667 if (cf_this) |
|
668 { |
|
669 old_rep = rep; |
|
670 rep = cf_this (*rep); |
|
671 rep->count = 1; |
|
672 } |
|
673 |
|
674 if (cf_this || cf_rhs) |
|
675 { |
|
676 assignment_ok = try_assignment (idx, tmp_rhs); |
|
677 |
|
678 if (! (error_state || assignment_ok)) |
|
679 assignment_ok = convert_and_assign (idx, tmp_rhs); |
|
680 } |
|
681 |
|
682 if (! assignment_ok && old_rep) |
|
683 { |
|
684 if (--rep->count == 0) |
|
685 delete rep; |
|
686 |
|
687 rep = old_rep; |
|
688 old_rep = 0; |
|
689 } |
|
690 |
|
691 if (old_rep && --old_rep->count == 0) |
|
692 delete old_rep; |
|
693 } |
|
694 |
|
695 return (assignment_ok && ! error_state); |
|
696 } |
|
697 |
|
698 bool |
|
699 octave_value::try_assignment (const octave_value_list& idx, |
|
700 const octave_value& rhs) |
|
701 { |
|
702 bool retval = false; |
|
703 |
|
704 int t_lhs = type_id (); |
|
705 int t_rhs = rhs.type_id (); |
|
706 |
2427
|
707 assign_op_fcn f = octave_value_typeinfo::lookup_assign_op (t_lhs, t_rhs); |
2413
|
708 |
|
709 if (f) |
|
710 { |
|
711 f (*rep, idx, *(rhs.rep)); |
|
712 |
|
713 retval = (! error_state); |
|
714 } |
|
715 |
|
716 return retval; |
|
717 } |
|
718 |
|
719 static void |
2376
|
720 gripe_binary_op (const string& on, const string& tn1, const string& tn2) |
|
721 { |
|
722 error ("binary operator %s not implemented for %s by %s operations", |
|
723 on.c_str (), tn1.c_str (), tn2.c_str ()); |
|
724 } |
|
725 |
|
726 octave_value |
|
727 do_binary_op (octave_value::binary_op op, const octave_value& v1, |
|
728 const octave_value& v2) |
|
729 { |
|
730 octave_value retval; |
|
731 |
|
732 int t1 = v1.type_id (); |
|
733 int t2 = v2.type_id (); |
|
734 |
2427
|
735 binary_op_fcn f = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
2376
|
736 |
|
737 if (f) |
|
738 retval = f (*v1.rep, *v2.rep); |
|
739 else |
|
740 { |
|
741 octave_value tv1; |
2427
|
742 type_conv_fcn cf1 = v1.numeric_conversion_function (); |
2376
|
743 |
|
744 if (cf1) |
|
745 { |
|
746 tv1 = octave_value (cf1 (*v1.rep)); |
|
747 t1 = tv1.type_id (); |
|
748 } |
|
749 else |
|
750 tv1 = v1; |
|
751 |
|
752 octave_value tv2; |
2427
|
753 type_conv_fcn cf2 = v2.numeric_conversion_function (); |
2376
|
754 |
|
755 if (cf2) |
|
756 { |
|
757 tv2 = octave_value (cf2 (*v2.rep)); |
|
758 t2 = tv2.type_id (); |
|
759 } |
|
760 else |
|
761 tv2 = v2; |
|
762 |
|
763 if (cf1 || cf2) |
|
764 { |
2427
|
765 binary_op_fcn f |
2376
|
766 = octave_value_typeinfo::lookup_binary_op (op, t1, t2); |
|
767 |
|
768 if (f) |
|
769 retval = f (*tv1.rep, *tv2.rep); |
|
770 else |
|
771 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
772 v1.type_name (), v2.type_name ()); |
|
773 } |
|
774 else |
|
775 gripe_binary_op (octave_value::binary_op_as_string (op), |
|
776 v1.type_name (), v2.type_name ()); |
|
777 } |
|
778 |
|
779 return retval; |
|
780 } |
|
781 |
|
782 void |
|
783 install_types (void) |
|
784 { |
|
785 octave_base_value::register_type (); |
|
786 octave_scalar::register_type (); |
|
787 octave_complex::register_type (); |
|
788 octave_matrix::register_type (); |
|
789 octave_complex_matrix::register_type (); |
|
790 octave_range::register_type (); |
2825
|
791 octave_bool::register_type (); |
|
792 octave_bool_matrix::register_type (); |
2376
|
793 octave_char_matrix::register_type (); |
|
794 octave_char_matrix_str::register_type (); |
|
795 octave_struct::register_type (); |
|
796 octave_all_va_args::register_type (); |
|
797 octave_magic_colon::register_type (); |
|
798 } |
|
799 |
|
800 static int |
|
801 do_fortran_indexing (void) |
|
802 { |
|
803 Vdo_fortran_indexing = check_preference ("do_fortran_indexing"); |
|
804 |
|
805 liboctave_dfi_flag = Vdo_fortran_indexing; |
|
806 |
|
807 return 0; |
|
808 } |
|
809 |
|
810 static int |
|
811 implicit_str_to_num_ok (void) |
|
812 { |
|
813 Vimplicit_str_to_num_ok = check_preference ("implicit_str_to_num_ok"); |
|
814 |
|
815 return 0; |
|
816 } |
|
817 |
|
818 static int |
|
819 ok_to_lose_imaginary_part (void) |
|
820 { |
|
821 Vok_to_lose_imaginary_part = check_preference ("ok_to_lose_imaginary_part"); |
|
822 |
|
823 return 0; |
|
824 } |
|
825 |
|
826 static int |
|
827 prefer_column_vectors (void) |
|
828 { |
|
829 Vprefer_column_vectors |
|
830 = check_preference ("prefer_column_vectors"); |
|
831 |
|
832 liboctave_pcv_flag = Vprefer_column_vectors; |
|
833 |
|
834 return 0; |
|
835 } |
|
836 |
|
837 static int |
|
838 print_answer_id_name (void) |
|
839 { |
|
840 Vprint_answer_id_name = check_preference ("print_answer_id_name"); |
|
841 |
|
842 return 0; |
|
843 } |
|
844 |
|
845 static int |
|
846 propagate_empty_matrices (void) |
|
847 { |
|
848 Vpropagate_empty_matrices = check_preference ("propagate_empty_matrices"); |
|
849 |
|
850 return 0; |
|
851 } |
|
852 |
|
853 static int |
|
854 resize_on_range_error (void) |
|
855 { |
|
856 Vresize_on_range_error = check_preference ("resize_on_range_error"); |
|
857 |
|
858 liboctave_rre_flag = Vresize_on_range_error; |
|
859 |
|
860 return 0; |
|
861 } |
|
862 |
|
863 static int |
|
864 struct_levels_to_print (void) |
|
865 { |
|
866 double val; |
|
867 if (builtin_real_scalar_variable ("struct_levels_to_print", val) |
|
868 && ! xisnan (val)) |
|
869 { |
|
870 int ival = NINT (val); |
2800
|
871 if (ival >= 0 && ival == val) |
2376
|
872 { |
|
873 Vstruct_levels_to_print = ival; |
|
874 return 0; |
|
875 } |
|
876 } |
|
877 gripe_invalid_value_specified ("struct_levels_to_print"); |
|
878 return -1; |
|
879 } |
|
880 |
|
881 static int |
|
882 warn_divide_by_zero (void) |
|
883 { |
|
884 Vwarn_divide_by_zero = check_preference ("warn_divide_by_zero"); |
|
885 |
|
886 return 0; |
|
887 } |
|
888 |
|
889 void |
|
890 symbols_of_value (void) |
|
891 { |
|
892 DEFVAR (do_fortran_indexing, 0.0, 0, do_fortran_indexing, |
|
893 "allow single indices for matrices"); |
|
894 |
|
895 DEFVAR (implicit_str_to_num_ok, 0.0, 0, implicit_str_to_num_ok, |
|
896 "allow implicit string to number conversion"); |
|
897 |
|
898 DEFVAR (ok_to_lose_imaginary_part, "warn", 0, ok_to_lose_imaginary_part, |
|
899 "silently convert from complex to real by dropping imaginary part"); |
|
900 |
|
901 DEFVAR (prefer_column_vectors, 1.0, 0, prefer_column_vectors, |
|
902 "prefer column/row vectors"); |
|
903 |
|
904 DEFVAR (print_answer_id_name, 1.0, 0, print_answer_id_name, |
|
905 "set output style to print `var_name = ...'"); |
|
906 |
|
907 DEFVAR (propagate_empty_matrices, 1.0, 0, propagate_empty_matrices, |
|
908 "operations on empty matrices return an empty matrix, not an error"); |
|
909 |
|
910 DEFVAR (resize_on_range_error, 1.0, 0, resize_on_range_error, |
|
911 "enlarge matrices on assignment"); |
|
912 |
|
913 DEFVAR (struct_levels_to_print, 2.0, 0, struct_levels_to_print, |
|
914 "number of levels of structure elements to print"); |
|
915 |
|
916 DEFVAR (warn_divide_by_zero, 1.0, 0, warn_divide_by_zero, |
|
917 "If TRUE, warn about division by zero"); |
|
918 } |
|
919 |
|
920 /* |
|
921 ;;; Local Variables: *** |
|
922 ;;; mode: C++ *** |
|
923 ;;; End: *** |
|
924 */ |