GNU Octave  4.0.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-2015 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)
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  if (pos != std::string::npos)
180  os << tmp.substr (pos);
181  else if (! tmp.empty ())
182  os << tmp[0];
183 }
184 
185 template <class ST>
188 {
189  return (n == 0) ? octave_value (scalar) : octave_value ();
190 }
191 
192 template <class ST>
193 bool
195  builtin_type_t btyp) const
196 {
197 
198  // Don't use builtin_type () here to avoid an extra VM call.
199  if (btyp == class_to_btyp<ST>::btyp)
200  {
201  *(reinterpret_cast<ST *>(where)) = scalar;
202  return true;
203  }
204  else
205  return false;
206 }
void short_disp(std::ostream &os) const
bool xisnan(double x)
Definition: lo-mappers.cc:144
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
octave_value diag(octave_idx_type k=0) const
octave_value permute(const Array< int > &, bool=false) const
void error(const char *fmt,...)
Definition: error.cc:476
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
builtin_type_t
Definition: ov-base.h:59
void print(std::ostream &os, bool pr_as_read_syntax=false)
octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
void gripe_nan_to_logical_conversion(void)
octave_value fast_elem_extract(octave_idx_type n) const
#define panic_impossible()
Definition: error.h:33
octave_value reshape(const dim_vector &new_dims) const
bool is_true(void) const
void octave_print_internal(std::ostream &, char, bool)
Definition: pr-output.cc:1715
bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
bool print_name_tag(std::ostream &os, const std::string &name) const
octave_value next_subsref(const std::string &type, const std::list< octave_value_list > &idx, size_t skip=1)
Definition: ov.cc:1317
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
static bool scalar(const dim_vector &dims)
Definition: ov-struct.cc:736