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-scalar.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <iostream>
28 
29 #include "oct-obj.h"
30 #include "ov-base.h"
31 #include "ov-cx-mat.h"
32 #include "ov-re-mat.h"
33 #include "ov-base-scalar.h"
34 #include "pr-output.h"
35 
36 template <class ST>
39  const std::list<octave_value_list>& idx)
40 {
41  octave_value retval;
42 
43  switch (type[0])
44  {
45  case '(':
46  retval = do_index_op (idx.front ());
47  break;
48 
49  case '{':
50  case '.':
51  {
52  std::string nm = type_name ();
53  error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
54  }
55  break;
56 
57  default:
59  }
60 
61  return retval.next_subsref (type, idx);
62 }
63 
64 template <class ST>
67  const std::list<octave_value_list>& idx,
68  const octave_value& rhs)
69 {
70  octave_value retval;
71 
72  switch (type[0])
73  {
74  case '(':
75  {
76  if (type.length () == 1)
77  retval = numeric_assign (type, idx, rhs);
78  else
79  {
80  std::string nm = type_name ();
81  error ("in indexed assignment of %s, last rhs index must be ()",
82  nm.c_str ());
83  }
84  }
85  break;
86 
87  case '{':
88  case '.':
89  {
90  std::string nm = type_name ();
91  error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
92  }
93  break;
94 
95  default:
97  }
98 
99  return retval;
100 }
101 
102 template <class ST>
104 octave_base_scalar<ST>::permute (const Array<int>& vec, bool inv) const
105 {
106  return Array<ST> (dim_vector (1, 1), scalar).permute (vec, inv);
107 }
108 
109 template <class ST>
112 {
113  return Array<ST> (dim_vector (1, 1), scalar).reshape (new_dims);
114 }
115 
116 template <class ST>
119 {
120  return Array<ST> (dim_vector (1, 1), scalar).diag (k);
121 }
122 
123 template <class ST>
126 {
127  return Array<ST> (dim_vector (1, 1), scalar).diag (m, n);
128 }
129 
130 template <class ST>
131 bool
133 {
134  bool retval = false;
135 
136  if (xisnan (scalar))
138  else
139  retval = (scalar != ST ());
140 
141  return retval;
142 }
143 
144 template <class ST>
145 void
146 octave_base_scalar<ST>::print (std::ostream& os, bool pr_as_read_syntax) const
147 {
148  print_raw (os, pr_as_read_syntax);
149  newline (os);
150 }
151 
152 template <class ST>
153 void
155  bool pr_as_read_syntax) const
156 {
157  indent (os);
158  octave_print_internal (os, scalar, pr_as_read_syntax);
159 }
160 
161 template <class ST>
162 bool
164  const std::string& name) const
165 {
166  indent (os);
167  os << name << " = ";
168  return false;
169 }
170 
171 template <class ST>
172 void
173 octave_base_scalar<ST>::short_disp (std::ostream& os) const
174 {
175  std::ostringstream buf;
177  std::string tmp = buf.str ();
178  size_t pos = tmp.find_first_not_of (" ");
179  os << tmp.substr (pos);
180 }
181 
182 template <class ST>
183 bool
185  builtin_type_t btyp) const
186 {
187 
188  // Don't use builtin_type () here to avoid an extra VM call.
189  if (btyp == class_to_btyp<ST>::btyp)
190  {
191  *(reinterpret_cast<ST *>(where)) = scalar;
192  return true;
193  }
194  else
195  return false;
196 }