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