GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-base.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 John W. Eaton
4 Copyright (C) 2009-2010 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <iostream>
29 #include <limits>
30 
31 #include "lo-ieee.h"
32 #include "lo-mappers.h"
33 
34 #include "defun.h"
35 #include "gripes.h"
36 #include "mxarray.h"
37 #include "oct-map.h"
38 #include "oct-obj.h"
39 #include "oct-lvalue.h"
40 #include "oct-stream.h"
41 #include "ops.h"
42 #include "ov-base.h"
43 #include "ov-cell.h"
44 #include "ov-ch-mat.h"
45 #include "ov-complex.h"
46 #include "ov-cx-mat.h"
47 #include "ov-range.h"
48 #include "ov-re-mat.h"
49 #include "ov-scalar.h"
50 #include "ov-str-mat.h"
51 #include "ov-fcn-handle.h"
52 #include "parse.h"
53 #include "pr-output.h"
54 #include "utils.h"
55 #include "variables.h"
56 
58 {
60 
61  if (x == btyp_bool)
62  x = btyp_double;
63  if (y == btyp_bool)
64  y = btyp_double;
65 
66  if (x <= btyp_float_complex && y <= btyp_float_complex)
67  retval = static_cast<builtin_type_t> (x | y);
68  else if (x <= btyp_uint64 && y <= btyp_float)
69  retval = x;
70  else if (x <= btyp_float && y <= btyp_uint64)
71  retval = y;
72  else if ((x >= btyp_int8 && x <= btyp_int64
73  && y >= btyp_int8 && y <= btyp_int64)
74  || (x >= btyp_uint8 && x <= btyp_uint64
75  && y >= btyp_uint8 && y <= btyp_uint64))
76  retval = (x > y) ? x : y;
77 
78  return retval;
79 }
80 
82 {
83  "double", "single", "double", "single",
84  "int8", "int16", "int32", "int64",
85  "uint8", "uint16", "uint32", "uint64",
86  "logical", "char",
87  "struct", "cell", "function_handle"
88 };
89 
92 {
93  static string_vector retval;
94 
95  if (retval.is_empty ())
96  {
97  int n = btyp_num_types - 2;
98  retval = string_vector (n);
99  int j = 0;
100  for (int i = 0; i < btyp_num_types; i++)
101  {
102  builtin_type_t ityp = static_cast<builtin_type_t> (i);
103  if (ityp != btyp_complex && ityp != btyp_float_complex)
104  retval(j++) = btyp_class_name[i];
105  }
106  }
107 
108  return retval;
109 }
110 
112  "<unknown type>", "unknown");
113 
114 // TRUE means to perform automatic sparse to real mutation if there
115 // is memory to be saved
116 bool Vsparse_auto_mutate = false;
117 
120 {
121  return resize (dim_vector ()).clone ();
122 }
123 
126 {
127  std::string nm = type_name ();
128  error ("squeeze: invalid operation for %s type", nm.c_str ());
129  return octave_value ();
130 }
131 
134 {
135  gripe_wrong_type_arg ("full: invalid operation for %s type", type_name ());
136  return octave_value ();
137 }
138 
139 Matrix
141 {
142  const dim_vector dv = dims ();
143  Matrix mdv (1, dv.length ());
144  for (octave_idx_type i = 0; i < dv.length (); i++)
145  mdv(i) = dv(i);
146  return mdv;
147 }
148 
151 {
152  return dims_to_numel (dims (), idx);
153 }
154 
156 octave_base_value::subsref (const std::string&,
157  const std::list<octave_value_list>&)
158 {
159  std::string nm = type_name ();
160  error ("can't perform indexing operations for %s type", nm.c_str ());
161  return octave_value ();
162 }
163 
165 octave_base_value::subsref (const std::string&,
166  const std::list<octave_value_list>&, int)
167 {
168  std::string nm = type_name ();
169  error ("can't perform indexing operations for %s type", nm.c_str ());
170  return octave_value ();
171 }
172 
174 octave_base_value::subsref (const std::string& type,
175  const std::list<octave_value_list>& idx,
176  bool /* auto_add */)
177 {
178  // This way we may get a more meaningful error message.
179  return subsref (type, idx);
180 }
181 
183 octave_base_value::subsref (const std::string& type,
184  const std::list<octave_value_list>& idx,
185  int nargout,
186  const std::list<octave_lvalue> *)
187 {
188  // Fall back to call without passing lvalue list.
189  return subsref (type, idx, nargout);
190 }
191 
194 {
195  std::string nm = type_name ();
196  error ("can't perform indexing operations for %s type", nm.c_str ());
197  return octave_value ();
198 }
199 
202 {
203  std::string nm = type_name ();
204  error ("can't perform indexing operations for %s type", nm.c_str ());
205  return octave_value ();
206 }
207 
210  const std::list<octave_lvalue> *)
211 {
212  // Fall back.
213  return do_multi_index_op (nargout, idx);
214 }
215 
218 {
219  std::string nm = type_name ();
220  error ("%s type invalid as index value", nm.c_str ());
221  return idx_vector ();
222 }
223 
225 octave_base_value::subsasgn (const std::string& type,
226  const std::list<octave_value_list>& idx,
227  const octave_value& rhs)
228 {
229  octave_value retval;
230 
231  if (is_defined ())
232  {
233  if (is_numeric_type ())
234  {
235  switch (type[0])
236  {
237  case '(':
238  {
239  if (type.length () == 1)
240  retval = numeric_assign (type, idx, rhs);
241  else if (is_empty ())
242  {
243  // Allow conversion of empty matrix to some other
244  // type in cases like
245  //
246  // x = []; x(i).f = rhs
247 
248  octave_value tmp = octave_value::empty_conv (type, rhs);
249 
250  retval = tmp.subsasgn (type, idx, rhs);
251  }
252  else
253  {
254  std::string nm = type_name ();
255  error ("in indexed assignment of %s, last rhs index must be ()",
256  nm.c_str ());
257  }
258  }
259  break;
260 
261  case '{':
262  case '.':
263  {
264  std::string nm = type_name ();
265  error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
266  }
267  break;
268 
269  default:
270  panic_impossible ();
271  }
272  }
273  else
274  {
275  std::string nm = type_name ();
276  error ("can't perform indexed assignment for %s type", nm.c_str ());
277  }
278  }
279  else
280  {
281  // Create new object of appropriate type for given index and rhs
282  // types and then call undef_subsasgn for that object.
283 
284  octave_value tmp = octave_value::empty_conv (type, rhs);
285 
286  retval = tmp.undef_subsasgn (type, idx, rhs);
287  }
288 
289  return retval;
290 }
291 
294  const std::list<octave_value_list>& idx,
295  const octave_value& rhs)
296 {
297  // In most cases, undef_subsasgn is handled the sams as subsasgn. One
298  // exception is octave_class objects.
299 
300  return subsasgn (type, idx, rhs);
301 }
302 
305 {
306  gripe_wrong_type_arg ("octave_base_value::nnz ()", type_name ());
307  return -1;
308 }
309 
312 {
313  return numel ();
314 }
315 
318 {
319  gripe_wrong_type_arg ("octave_base_value::nfields ()", type_name ());
320  return -1;
321 }
322 
325 {
326  gripe_wrong_type_arg ("octave_base_value::reshape ()", type_name ());
327  return octave_value ();
328 }
329 
332 {
333  gripe_wrong_type_arg ("octave_base_value::permute ()", type_name ());
334  return octave_value ();
335 }
336 
339 {
340  gripe_wrong_type_arg ("octave_base_value::resize ()", type_name ());
341  return octave_value ();
342 }
343 
346 {
347  gripe_wrong_type_arg ("octave_base_value::matrix_type ()", type_name ());
348  return MatrixType ();
349 }
350 
353 {
354  gripe_wrong_type_arg ("octave_base_value::matrix_type ()", type_name ());
355  return MatrixType ();
356 }
357 
360 {
361  return 0.0;
362 }
363 
366 {
367  return 0.0;
368 }
369 
371 octave_base_value::convert_to_str (bool pad, bool force, char type) const
372 {
373  octave_value retval = convert_to_str_internal (pad, force, type);
374 
375  if (! force && is_numeric_type ())
376  gripe_implicit_conversion ("Octave:num-to-str",
377  type_name (), retval.type_name ());
378 
379  return retval;
380 }
381 
384 {
385  gripe_wrong_type_arg ("octave_base_value::convert_to_str_internal ()",
386  type_name ());
387  return octave_value ();
388 }
389 
390 void
392 {
394  ("octave_base_value::convert_to_row_or_column_vector ()",
395  type_name ());
396 }
397 
398 void
399 octave_base_value::print (std::ostream&, bool) const
400 {
401  gripe_wrong_type_arg ("octave_base_value::print ()", type_name ());
402 }
403 
404 void
405 octave_base_value::print_raw (std::ostream&, bool) const
406 {
407  gripe_wrong_type_arg ("octave_base_value::print_raw ()", type_name ());
408 }
409 
410 bool
412  const std::string& name) const
413 {
414  bool retval = false;
415 
416  indent (os);
417 
418  if (print_as_scalar ())
419  os << name << " = ";
420  else
421  {
422  os << name << " =";
423  newline (os);
424  if (! Vcompact_format)
425  newline (os);
426 
427  retval = true;
428  }
429 
430  return retval;
431 }
432 
433 void
434 octave_base_value::print_with_name (std::ostream& output_buf,
435  const std::string& name,
436  bool print_padding)
437 {
438  bool pad_after = print_name_tag (output_buf, name);
439 
440  print (output_buf);
441 
442  if (print_padding && pad_after && ! Vcompact_format)
443  newline (output_buf);
444 }
445 
446 void
448  const std::string& /* prefix */) const
449 {
450  os << "no info for type: " << type_name () << "\n";
451 }
452 
453 #define INT_CONV_METHOD(T, F) \
454  T \
455  octave_base_value::F ## _value (bool require_int, bool frc_str_conv) const \
456  { \
457  T retval = 0; \
458  \
459  double d = double_value (frc_str_conv); \
460  \
461  if (! error_state) \
462  { \
463  if (require_int && D_NINT (d) != d) \
464  error_with_cfn ("conversion of %g to " #T " value failed", d); \
465  else if (d < std::numeric_limits<T>::min ()) \
466  retval = std::numeric_limits<T>::min (); \
467  else if (d > std::numeric_limits<T>::max ()) \
468  retval = std::numeric_limits<T>::max (); \
469  else \
470  retval = static_cast<T> (::fix (d)); \
471  } \
472  else \
473  gripe_wrong_type_arg ("octave_base_value::" #F "_value ()", \
474  type_name ()); \
475  \
476  return retval; \
477  }
478 
479 INT_CONV_METHOD (short int, short)
480 INT_CONV_METHOD (unsigned short int, ushort)
481 
482 INT_CONV_METHOD (int, int)
483 INT_CONV_METHOD (unsigned int, uint)
484 
485 INT_CONV_METHOD (long int, long)
486 INT_CONV_METHOD (unsigned long int, ulong)
487 
488 INT_CONV_METHOD (int64_t, int64)
489 INT_CONV_METHOD (uint64_t, uint64)
490 
491 int
492 octave_base_value::nint_value (bool frc_str_conv) const
493 {
494  int retval = 0;
495 
496  double d = double_value (frc_str_conv);
497 
498  if (! error_state)
499  {
500  if (xisnan (d))
501  {
502  error ("conversion of NaN to integer value failed");
503  return retval;
504  }
505 
506  retval = static_cast<int> (::fix (d));
507  }
508  else
509  gripe_wrong_type_arg ("octave_base_value::nint_value ()", type_name ());
510 
511  return retval;
512 }
513 
514 double
516 {
517  double retval = lo_ieee_nan_value ();
518  gripe_wrong_type_arg ("octave_base_value::double_value ()", type_name ());
519  return retval;
520 }
521 
522 float
524 {
525  float retval = lo_ieee_float_nan_value ();
526  gripe_wrong_type_arg ("octave_base_value::float_value ()", type_name ());
527  return retval;
528 }
529 
530 Cell
532 {
533  Cell retval;
534  gripe_wrong_type_arg ("octave_base_value::cell_value()", type_name ());
535  return retval;
536 }
537 
538 Matrix
540 {
541  Matrix retval;
542  gripe_wrong_type_arg ("octave_base_value::matrix_value()", type_name ());
543  return retval;
544 }
545 
548 {
549  FloatMatrix retval;
550  gripe_wrong_type_arg ("octave_base_value::float_matrix_value()",
551  type_name ());
552  return retval;
553 }
554 
555 NDArray
557 {
558  FloatNDArray retval;
559  gripe_wrong_type_arg ("octave_base_value::array_value()", type_name ());
560  return retval;
561 }
562 
565 {
566  FloatNDArray retval;
567  gripe_wrong_type_arg ("octave_base_value::float_array_value()", type_name ());
568  return retval;
569 }
570 
571 Complex
573 {
574  double tmp = lo_ieee_nan_value ();
575  Complex retval (tmp, tmp);
576  gripe_wrong_type_arg ("octave_base_value::complex_value()", type_name ());
577  return retval;
578 }
579 
582 {
583  float tmp = lo_ieee_float_nan_value ();
584  FloatComplex retval (tmp, tmp);
585  gripe_wrong_type_arg ("octave_base_value::float_complex_value()",
586  type_name ());
587  return retval;
588 }
589 
592 {
593  ComplexMatrix retval;
594  gripe_wrong_type_arg ("octave_base_value::complex_matrix_value()",
595  type_name ());
596  return retval;
597 }
598 
601 {
602  FloatComplexMatrix retval;
603  gripe_wrong_type_arg ("octave_base_value::float_complex_matrix_value()",
604  type_name ());
605  return retval;
606 }
607 
610 {
611  ComplexNDArray retval;
612  gripe_wrong_type_arg ("octave_base_value::complex_array_value()",
613  type_name ());
614  return retval;
615 }
616 
619 {
620  FloatComplexNDArray retval;
621  gripe_wrong_type_arg ("octave_base_value::float_complex_array_value()",
622  type_name ());
623  return retval;
624 }
625 
626 bool
628 {
629  bool retval = false;
630  gripe_wrong_type_arg ("octave_base_value::bool_value()", type_name ());
631  return retval;
632 }
633 
636 {
637  boolMatrix retval;
638  gripe_wrong_type_arg ("octave_base_value::bool_matrix_value()",
639  type_name ());
640  return retval;
641 }
642 
645 {
646  boolNDArray retval;
647  gripe_wrong_type_arg ("octave_base_value::bool_array_value()",
648  type_name ());
649  return retval;
650 }
651 
654 {
655  charMatrix retval;
656 
657  octave_value tmp = convert_to_str (false, force);
658 
659  if (! error_state)
660  retval = tmp.char_matrix_value ();
661 
662  return retval;
663 }
664 
667 {
668  charNDArray retval;
669  gripe_wrong_type_arg ("octave_base_value::char_array_value()",
670  type_name ());
671  return retval;
672 }
673 
676 {
677  SparseMatrix retval;
678  gripe_wrong_type_arg ("octave_base_value::sparse_matrix_value()",
679  type_name ());
680  return retval;
681 }
682 
685 {
686  SparseComplexMatrix retval;
687  gripe_wrong_type_arg ("octave_base_value::sparse_complex_matrix_value()",
688  type_name ());
689  return retval;
690 }
691 
694 {
695  SparseBoolMatrix retval;
696  gripe_wrong_type_arg ("octave_base_value::sparse_bool_matrix_value()",
697  type_name ());
698  return retval;
699 }
700 
703 {
704  DiagMatrix retval;
705  gripe_wrong_type_arg ("octave_base_value::diag_matrix_value()", type_name ());
706  return retval;
707 }
708 
711 {
712  FloatDiagMatrix retval;
713  gripe_wrong_type_arg ("octave_base_value::float_diag_matrix_value()",
714  type_name ());
715  return retval;
716 }
717 
720 {
721  ComplexDiagMatrix retval;
722  gripe_wrong_type_arg ("octave_base_value::complex_diag_matrix_value()",
723  type_name ());
724  return retval;
725 }
726 
729 {
730  FloatComplexDiagMatrix retval;
731  gripe_wrong_type_arg ("octave_base_value::float_complex_diag_matrix_value()",
732  type_name ());
733  return retval;
734 }
735 
738 {
739  PermMatrix retval;
740  gripe_wrong_type_arg ("octave_base_value::perm_matrix_value()", type_name ());
741  return retval;
742 }
743 
746 {
747  octave_int8 retval;
748  gripe_wrong_type_arg ("octave_base_value::int8_scalar_value()",
749  type_name ());
750  return retval;
751 }
752 
755 {
756  octave_int16 retval;
757  gripe_wrong_type_arg ("octave_base_value::int16_scalar_value()",
758  type_name ());
759  return retval;
760 }
761 
764 {
765  octave_int32 retval;
766  gripe_wrong_type_arg ("octave_base_value::int32_scalar_value()",
767  type_name ());
768  return retval;
769 }
770 
773 {
774  octave_int64 retval;
775  gripe_wrong_type_arg ("octave_base_value::int64_scalar_value()",
776  type_name ());
777  return retval;
778 }
779 
782 {
783  octave_uint8 retval;
784  gripe_wrong_type_arg ("octave_base_value::uint8_scalar_value()",
785  type_name ());
786  return retval;
787 }
788 
791 {
792  octave_uint16 retval;
793  gripe_wrong_type_arg ("octave_base_value::uint16_scalar_value()",
794  type_name ());
795  return retval;
796 }
797 
800 {
801  octave_uint32 retval;
802  gripe_wrong_type_arg ("octave_base_value::uint32_scalar_value()",
803  type_name ());
804  return retval;
805 }
806 
809 {
810  octave_uint64 retval;
811  gripe_wrong_type_arg ("octave_base_value::uint64_scalar_value()",
812  type_name ());
813  return retval;
814 }
815 
818 {
819  int8NDArray retval;
820  gripe_wrong_type_arg ("octave_base_value::int8_array_value()",
821  type_name ());
822  return retval;
823 }
824 
827 {
828  int16NDArray retval;
829  gripe_wrong_type_arg ("octave_base_value::int16_array_value()",
830  type_name ());
831  return retval;
832 }
833 
836 {
837  int32NDArray retval;
838  gripe_wrong_type_arg ("octave_base_value::int32_array_value()",
839  type_name ());
840  return retval;
841 }
842 
845 {
846  int64NDArray retval;
847  gripe_wrong_type_arg ("octave_base_value::int64_array_value()",
848  type_name ());
849  return retval;
850 }
851 
854 {
855  uint8NDArray retval;
856  gripe_wrong_type_arg ("octave_base_value::uint8_array_value()",
857  type_name ());
858  return retval;
859 }
860 
863 {
864  uint16NDArray retval;
865  gripe_wrong_type_arg ("octave_base_value::uint16_array_value()",
866  type_name ());
867  return retval;
868 }
869 
872 {
873  uint32NDArray retval;
874  gripe_wrong_type_arg ("octave_base_value::uint32_array_value()",
875  type_name ());
876  return retval;
877 }
878 
881 {
882  uint64NDArray retval;
883  gripe_wrong_type_arg ("octave_base_value::uint64_array_value()",
884  type_name ());
885  return retval;
886 }
887 
890 {
891  string_vector retval;
892 
893  octave_value tmp = convert_to_str (pad, true);
894 
895  if (! error_state)
896  retval = tmp.all_strings ();
897 
898  return retval;
899 }
900 
901 std::string
903 {
904  std::string retval;
905 
906  octave_value tmp = convert_to_str (force);
907 
908  if (! error_state)
909  retval = tmp.string_value ();
910 
911  return retval;
912 }
913 
916 {
917  Array<std::string> retval;
918  gripe_wrong_type_arg ("octave_base_value::cellstry_value()",
919  type_name ());
920  return retval;
921 }
922 
923 Range
925 {
926  Range retval;
927  gripe_wrong_type_arg ("octave_base_value::range_value()", type_name ());
928  return retval;
929 }
930 
933 {
934  octave_map retval;
935  gripe_wrong_type_arg ("octave_base_value::map_value()", type_name ());
936  return retval;
937 }
938 
941 {
942  octave_map tmp = map_value ();
943 
944  if (tmp.numel () == 1)
945  return tmp.checkelem (0);
946  else
947  {
948  if (! error_state)
949  error ("invalid conversion of multi-dimensional struct to scalar struct");
950 
951  return octave_scalar_map ();
952  }
953 }
954 
957 {
958  string_vector retval;
959  gripe_wrong_type_arg ("octave_base_value::map_keys()", type_name ());
960  return retval;
961 }
962 
963 size_t
965 {
966  size_t retval = 0;
967  gripe_wrong_type_arg ("octave_base_value::nparents()", type_name ());
968  return retval;
969 }
970 
971 std::list<std::string>
973 {
974  std::list<std::string> retval;
975  gripe_wrong_type_arg ("octave_base_value::parent_class_name_list()",
976  type_name ());
977  return retval;
978 }
979 
982 {
983  string_vector retval;
984  gripe_wrong_type_arg ("octave_base_value::parent_class_names()",
985  type_name ());
986  return retval;
987 }
988 
991 {
992  octave_function *retval = 0;
993 
994  if (! silent)
995  gripe_wrong_type_arg ("octave_base_value::function_value()",
996  type_name ());
997  return retval;
998 }
999 
1002 {
1003  octave_user_function *retval = 0;
1004 
1005  if (! silent)
1006  gripe_wrong_type_arg ("octave_base_value::user_function_value()",
1007  type_name ());
1008  return retval;
1009 }
1010 
1013 {
1014  octave_user_script *retval = 0;
1015 
1016  if (! silent)
1017  gripe_wrong_type_arg ("octave_base_value::user_script_value()",
1018  type_name ());
1019  return retval;
1020 }
1021 
1024 {
1025  octave_user_code *retval = 0;
1026 
1027  if (! silent)
1028  gripe_wrong_type_arg ("octave_base_value::user_code_value()",
1029  type_name ());
1030  return retval;
1031 }
1032 
1035 {
1036  octave_fcn_handle *retval = 0;
1037 
1038  if (! silent)
1039  gripe_wrong_type_arg ("octave_base_value::fcn_handle_value()",
1040  type_name ());
1041  return retval;
1042 }
1043 
1046 {
1047  octave_fcn_inline *retval = 0;
1048 
1049  if (! silent)
1050  gripe_wrong_type_arg ("octave_base_value::fcn_inline_value()",
1051  type_name ());
1052  return retval;
1053 }
1054 
1057 {
1058  octave_value_list retval;
1059  gripe_wrong_type_arg ("octave_base_value::list_value()", type_name ());
1060  return retval;
1061 }
1062 
1063 bool
1065 {
1066  gripe_wrong_type_arg ("octave_base_value::save_ascii()", type_name ());
1067  return false;
1068 }
1069 
1070 bool
1072 {
1073  gripe_wrong_type_arg ("octave_base_value::load_ascii()", type_name ());
1074  return false;
1075 }
1076 
1077 bool
1078 octave_base_value::save_binary (std::ostream&, bool&)
1079 {
1080  gripe_wrong_type_arg ("octave_base_value::save_binary()", type_name ());
1081  return false;
1082 }
1083 
1084 bool
1085 octave_base_value::load_binary (std::istream&, bool,
1087 {
1088  gripe_wrong_type_arg ("octave_base_value::load_binary()", type_name ());
1089  return false;
1090 }
1091 
1092 #if defined (HAVE_HDF5)
1093 
1094 bool
1095 octave_base_value::save_hdf5 (hid_t, const char *, bool)
1096 {
1097  gripe_wrong_type_arg ("octave_base_value::save_binary()", type_name ());
1098 
1099  return false;
1100 }
1101 
1102 bool
1103 octave_base_value::load_hdf5 (hid_t, const char *)
1104 {
1105  gripe_wrong_type_arg ("octave_base_value::load_binary()", type_name ());
1106 
1107  return false;
1108 }
1109 
1110 #endif
1111 
1112 int
1114  int, oct_mach_info::float_format) const
1115 {
1116  gripe_wrong_type_arg ("octave_base_value::write()", type_name ());
1117 
1118  return false;
1119 }
1120 
1121 mxArray *
1123 {
1124  return 0;
1125 }
1126 
1129 {
1130  gripe_wrong_type_arg ("octave_base_value::diag ()", type_name ());
1131 
1132  return octave_value ();
1133 }
1134 
1137 {
1138  gripe_wrong_type_arg ("octave_base_value::diag ()", type_name ());
1139 
1140  return octave_value ();
1141 }
1142 
1145 {
1146  gripe_wrong_type_arg ("octave_base_value::sort ()", type_name ());
1147 
1148  return octave_value ();
1149 }
1150 
1153  octave_idx_type, sortmode) const
1154 {
1155  gripe_wrong_type_arg ("octave_base_value::sort ()", type_name ());
1156 
1157  return octave_value ();
1158 }
1159 
1160 sortmode
1162 {
1163  gripe_wrong_type_arg ("octave_base_value::is_sorted ()", type_name ());
1164 
1165  return UNSORTED;
1166 }
1167 
1170 {
1171  gripe_wrong_type_arg ("octave_base_value::sort_rows_idx ()", type_name ());
1172 
1173  return Array<octave_idx_type> ();
1174 }
1175 
1176 sortmode
1178 {
1179  gripe_wrong_type_arg ("octave_base_value::is_sorted_rows ()", type_name ());
1180 
1181  return UNSORTED;
1182 }
1183 
1184 
1185 const char *
1187 {
1188  static const char *names[num_unary_mappers] =
1189  {
1190  "abs",
1191  "acos",
1192  "acosh",
1193  "angle",
1194  "arg",
1195  "asin",
1196  "asinh",
1197  "atan",
1198  "atanh",
1199  "cbrt",
1200  "ceil",
1201  "conj",
1202  "cos",
1203  "cosh",
1204  "erf",
1205  "erfinv",
1206  "erfcinv",
1207  "erfc",
1208  "erfcx",
1209  "erfi",
1210  "dawson",
1211  "exp",
1212  "expm1",
1213  "finite",
1214  "fix",
1215  "floor",
1216  "gamma",
1217  "imag",
1218  "isinf",
1219  "isna",
1220  "isnan",
1221  "lgamma",
1222  "log",
1223  "log2",
1224  "log10",
1225  "log1p",
1226  "real",
1227  "round",
1228  "roundb",
1229  "signum",
1230  "sin",
1231  "sinh",
1232  "sqrt",
1233  "tan",
1234  "tanh",
1235  "isalnum",
1236  "isalpha",
1237  "isascii",
1238  "iscntrl",
1239  "isdigit",
1240  "isgraph",
1241  "islower",
1242  "isprint",
1243  "ispunct",
1244  "isspace",
1245  "isupper",
1246  "isxdigit",
1247  "signbit",
1248  "toascii",
1249  "tolower",
1250  "toupper"
1251  };
1252 
1253  if (umap < 0 || umap >= num_unary_mappers)
1254  return "unknown";
1255  else
1256  return names[umap];
1257 }
1258 
1261 {
1262  error ("%s: not defined for %s", get_umap_name (umap), type_name ().c_str ());
1263  return octave_value ();
1264 }
1265 
1266 void
1268 {
1269  gripe_wrong_type_arg ("octave_base_value::lock ()", type_name ());
1270 }
1271 
1272 void
1274 {
1275  gripe_wrong_type_arg ("octave_base_value::unlock ()", type_name ());
1276 }
1277 
1278 void
1279 octave_base_value::dump (std::ostream& os) const
1280 {
1281  dim_vector dv = this->dims ();
1282 
1283  os << "class: " << this->class_name ()
1284  << " type: " << this->type_name ()
1285  << " dims: " << dv.str ();
1286 }
1287 
1288 static void
1289 gripe_indexed_assignment (const std::string& tn1, const std::string& tn2)
1290 {
1291  error ("assignment of '%s' to indexed '%s' not implemented",
1292  tn2.c_str (), tn1.c_str ());
1293 }
1294 
1295 static void
1296 gripe_assign_conversion_failed (const std::string& tn1,
1297  const std::string& tn2)
1298 {
1299  error ("type conversion for assignment of '%s' to indexed '%s' failed",
1300  tn2.c_str (), tn1.c_str ());
1301 }
1302 
1303 static void
1304 gripe_no_conversion (const std::string& on, const std::string& tn1,
1305  const std::string& tn2)
1306 {
1307  error ("operator %s: no conversion for assignment of '%s' to indexed '%s'",
1308  on.c_str (), tn2.c_str (), tn1.c_str ());
1309 }
1310 
1313  const std::list<octave_value_list>& idx,
1314  const octave_value& rhs)
1315 {
1316  octave_value retval;
1317 
1318  if (idx.front ().empty ())
1319  {
1320  error ("missing index in indexed assignment");
1321  return retval;
1322  }
1323 
1324  int t_lhs = type_id ();
1325  int t_rhs = rhs.type_id ();
1326 
1329  t_lhs, t_rhs);
1330 
1331  bool done = false;
1332 
1333  if (f)
1334  {
1335  f (*this, idx.front (), rhs.get_rep ());
1336 
1337  done = (! error_state);
1338  }
1339 
1340  if (done)
1341  {
1342  count++;
1343  retval = octave_value (this);
1344  }
1345  else
1346  {
1347  int t_result
1349 
1350  if (t_result >= 0)
1351  {
1353  = octave_value_typeinfo::lookup_widening_op (t_lhs, t_result);
1354 
1355  if (cf)
1356  {
1357  octave_base_value *tmp = cf (*this);
1358 
1359  if (tmp)
1360  {
1361  octave_value val (tmp);
1362 
1363  retval = val.subsasgn (type, idx, rhs);
1364 
1365  done = (! error_state);
1366  }
1367  else
1369  rhs.type_name ());
1370  }
1371  else
1373  }
1374 
1375  if (! (done || error_state))
1376  {
1377  octave_value tmp_rhs;
1378 
1380  = rhs.numeric_conversion_function ();
1381 
1384 
1385  // Try biased (one-sided) conversions first.
1386  if (cf_rhs.type_id () >= 0
1388  t_lhs,
1389  cf_rhs.type_id ())
1391  cf_rhs.type_id ()) >= 0))
1392  cf_this = 0;
1393  else if (cf_this.type_id () >= 0
1395  cf_this.type_id (), t_rhs)
1397  t_rhs) >= 0))
1398  cf_rhs = 0;
1399 
1400  if (cf_rhs)
1401  {
1402  octave_base_value *tmp = cf_rhs (rhs.get_rep ());
1403 
1404  if (tmp)
1405  tmp_rhs = octave_value (tmp);
1406  else
1407  {
1409  rhs.type_name ());
1410  return octave_value ();
1411  }
1412  }
1413  else
1414  tmp_rhs = rhs;
1415 
1416  count++;
1417  octave_value tmp_lhs = octave_value (this);
1418 
1419  if (cf_this)
1420  {
1421  octave_base_value *tmp = cf_this (*this);
1422 
1423  if (tmp)
1424  tmp_lhs = octave_value (tmp);
1425  else
1426  {
1428  rhs.type_name ());
1429  return octave_value ();
1430  }
1431  }
1432 
1433  if (cf_this || cf_rhs)
1434  {
1435  retval = tmp_lhs.subsasgn (type, idx, tmp_rhs);
1436 
1437  done = (! error_state);
1438  }
1439  else
1442  type_name (), rhs.type_name ());
1443  }
1444  }
1445 
1446  // The assignment may have converted to a type that is wider than
1447  // necessary.
1448 
1449  retval.maybe_mutate ();
1450 
1451  return retval;
1452 }
1453 
1454 // Current indentation.
1456 
1457 // TRUE means we are at the beginning of a line.
1459 
1460 // Each print() function should call this before printing anything.
1461 //
1462 // This doesn't need to be fast, but isn't there a better way?
1463 
1464 void
1465 octave_base_value::indent (std::ostream& os) const
1466 {
1467  assert (curr_print_indent_level >= 0);
1468 
1469  if (beginning_of_line)
1470  {
1471  // FIXME: do we need this?
1472  // os << prefix;
1473 
1474  for (int i = 0; i < curr_print_indent_level; i++)
1475  os << " ";
1476 
1477  beginning_of_line = false;
1478  }
1479 }
1480 
1481 // All print() functions should use this to print new lines.
1482 
1483 void
1484 octave_base_value::newline (std::ostream& os) const
1485 {
1486  os << "\n";
1487 
1488  beginning_of_line = true;
1489 }
1490 
1491 // For ressetting print state.
1492 
1493 void
1495 {
1496  beginning_of_line = true;
1498 }
1499 
1500 
1503 {
1504  return octave_value ();
1505 }
1506 
1507 bool
1509 {
1510  return false;
1511 }
1512 
1513 bool
1515 {
1516  return false;
1517 }
1518 
1519 CONVDECLX (matrix_conv)
1520 {
1521  return new octave_matrix ();
1522 }
1523 
1524 CONVDECLX (complex_matrix_conv)
1525 {
1526  return new octave_complex_matrix ();
1527 }
1528 
1529 CONVDECLX (string_conv)
1530 {
1531  return new octave_char_matrix_str ();
1532 }
1533 
1534 CONVDECLX (cell_conv)
1535 {
1536  return new octave_cell ();
1537 }
1538 
1539 void
1541 {
1551 
1554  complex_matrix_conv);
1557 }
1558 
1559 DEFUN (sparse_auto_mutate, args, nargout,
1560  "-*- texinfo -*-\n\
1561 @deftypefn {Built-in Function} {@var{val} =} sparse_auto_mutate ()\n\
1562 @deftypefnx {Built-in Function} {@var{old_val} =} sparse_auto_mutate (@var{new_val})\n\
1563 @deftypefnx {Built-in Function} {} sparse_auto_mutate (@var{new_val}, \"local\")\n\
1564 Query or set the internal variable that controls whether Octave will\n\
1565 automatically mutate sparse matrices to full matrices to save memory.\n\
1566 For example:\n\
1567 \n\
1568 @example\n\
1569 @group\n\
1570 s = speye (3);\n\
1571 sparse_auto_mutate (false);\n\
1572 s(:, 1) = 1;\n\
1573 typeinfo (s)\n\
1574 @result{} sparse matrix\n\
1575 sparse_auto_mutate (true);\n\
1576 s(1, :) = 1;\n\
1577 typeinfo (s)\n\
1578 @result{} matrix\n\
1579 @end group\n\
1580 @end example\n\
1581 \n\
1582 When called from inside a function with the @qcode{\"local\"} option, the\n\
1583 variable is changed locally for the function and any subroutines it calls. \n\
1584 The original variable value is restored when exiting the function.\n\
1585 @end deftypefn")
1586 {
1587  return SET_INTERNAL_VARIABLE (sparse_auto_mutate);
1588 }
1589 
1590 /*
1591 %!test
1592 %! s = speye (3);
1593 %! sparse_auto_mutate (false);
1594 %! s(:, 1) = 1;
1595 %! assert (typeinfo (s), "sparse matrix");
1596 %! sparse_auto_mutate (true);
1597 %! s(1, :) = 1;
1598 %! assert (typeinfo (s), "matrix");
1599 %! sparse_auto_mutate (false);
1600 */