GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-base-scalar.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 // This file should not include config.h. It is only included in other
27 // C++ source files that should have included config.h before including
28 // this file.
29 
30 #include <ostream>
31 #include <sstream>
32 
33 #include "oct-inttypes-fwd.h"
34 
35 #include "ovl.h"
36 #include "ov-base.h"
37 #include "ov-cx-mat.h"
38 #include "ov-re-mat.h"
39 #include "ov-base-scalar.h"
40 #include "pr-output.h"
41 
42 template <typename ST>
44 octave_base_scalar<ST>::subsref (const std::string& type,
45  const std::list<octave_value_list>& idx)
46 {
47  octave_value retval;
48 
49  switch (type[0])
50  {
51  case '(':
52  retval = do_index_op (idx.front ());
53  break;
54 
55  case '{':
56  case '.':
57  {
58  std::string nm = type_name ();
59  error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
60  }
61  break;
62 
63  default:
65  }
66 
67  return retval.next_subsref (type, idx);
68 }
69 
70 template <typename ST>
72 octave_base_scalar<ST>::subsasgn (const std::string& type,
73  const std::list<octave_value_list>& idx,
74  const octave_value& rhs)
75 {
76  octave_value retval;
77 
78  switch (type[0])
79  {
80  case '(':
81  {
82  if (type.length () != 1)
83  {
84  std::string nm = type_name ();
85  error ("in indexed assignment of %s, last rhs index must be ()",
86  nm.c_str ());
87  }
88 
89  retval = numeric_assign (type, idx, rhs);
90  }
91  break;
92 
93  case '{':
94  case '.':
95  {
96  std::string nm = type_name ();
97  error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
98  }
99  break;
100 
101  default:
102  panic_impossible ();
103  }
104 
105  return retval;
106 }
107 
108 template <typename ST>
111 {
112  static dim_vector dv (1, 1);
113  return dv;
114 }
115 
116 template <typename ST>
118 octave_base_scalar<ST>::permute (const Array<int>& vec, bool inv) const
119 {
120  return Array<ST> (dim_vector (1, 1), scalar).permute (vec, inv);
121 }
122 
123 template <typename ST>
126 {
127  return Array<ST> (dim_vector (1, 1), scalar).reshape (new_dims);
128 }
129 
130 template <typename ST>
133 {
134  return Array<ST> (dim_vector (1, 1), scalar).diag (k);
135 }
136 
137 template <typename ST>
140 {
141  return Array<ST> (dim_vector (1, 1), scalar).diag (m, n);
142 }
143 
144 template <typename ST>
145 bool
147 {
148  if (octave::math::isnan (scalar))
150 
151  return (scalar != ST ());
152 }
153 
154 template <typename ST>
155 void
156 octave_base_scalar<ST>::print (std::ostream& os, bool pr_as_read_syntax)
157 {
158  print_raw (os, pr_as_read_syntax);
159  newline (os);
160 }
161 
162 template <typename ST>
163 void
165  bool pr_as_read_syntax) const
166 {
167  indent (os);
168  octave_print_internal (os, scalar, pr_as_read_syntax);
169 }
170 
171 template <typename ST>
172 bool
174  const std::string& name) const
175 {
176  indent (os);
177  os << name << " = ";
178  return false;
179 }
180 
181 template <typename ST>
182 void
183 octave_base_scalar<ST>::short_disp (std::ostream& os) const
184 {
185  std::ostringstream buf;
186  octave_print_internal (buf, scalar);
187  std::string tmp = buf.str ();
188  std::size_t pos = tmp.find_first_not_of (' ');
189  if (pos != std::string::npos)
190  os << tmp.substr (pos);
191  else if (! tmp.empty ())
192  os << tmp[0];
193 }
194 
195 template <typename ST>
198 {
199  return make_format (scalar);
200 }
201 
202 template <typename ST>
203 std::string
206 {
207  std::ostringstream buf;
208  octave_print_internal (buf, fmt, scalar);
209  return buf.str ();
210 }
211 
212 template <typename ST>
215 {
216  return (n == 0) ? octave_value (scalar) : octave_value ();
217 }
218 
219 template <typename ST>
220 bool
222  builtin_type_t btyp) const
223 {
224 
225  // Don't use builtin_type () here to avoid an extra VM call.
226  if (btyp == class_to_btyp<ST>::btyp)
227  {
228  *(reinterpret_cast<ST *> (where)) = scalar;
229  return true;
230  }
231  else
232  return false;
233 }
Array< T, Alloc > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Size of the specified dimension.
Definition: Array-base.cc:450
Array< T, Alloc > reshape(octave_idx_type nr, octave_idx_type nc) const
Size of the specified dimension.
Definition: Array.h:635
Array< T, Alloc > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Definition: Array-base.cc:2541
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:248
dim_vector dims() const
float_display_format get_edit_display_format() const
octave_value reshape(const dim_vector &new_dims) const
octave_value permute(const Array< int > &, bool=false) const
octave_value fast_elem_extract(octave_idx_type n) const
octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
bool is_true() const
octave_value diag(octave_idx_type k=0) const
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
std::string edit_display(const float_display_format &fmt, octave_idx_type i, octave_idx_type j) const
bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
void print(std::ostream &os, bool pr_as_read_syntax=false)
void short_disp(std::ostream &os) 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, std::size_t skip=1)
void() error(const char *fmt,...)
Definition: error.cc:988
#define panic_impossible()
Definition: error.h:503
void err_nan_to_logical_conversion()
bool isnan(bool)
Definition: lo-mappers.h:178
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
builtin_type_t
Definition: ov-base.h:83
void octave_print_internal(std::ostream &os, const float_display_format &fmt, bool d, bool pr_as_read_syntax)
Definition: pr-output.cc:1761
float_display_format make_format(const double &d)
Definition: pr-output.cc:525