GNU Octave 7.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-2022 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
42template <typename ST>
44octave_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
70template <typename ST>
72octave_base_scalar<ST>::subsasgn (const std::string& type,
73 const std::list<octave_value_list>& idx,
74 const octave_value& rhs)
75{
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;
101 default:
103 }
104
105 return retval;
106}
107
108template <typename ST>
111{
112 static dim_vector dv (1, 1);
113 return dv;
114}
115
116template <typename ST>
118octave_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
123template <typename ST>
126{
127 return Array<ST> (dim_vector (1, 1), scalar).reshape (new_dims);
128}
129
130template <typename ST>
133{
134 return Array<ST> (dim_vector (1, 1), scalar).diag (k);
135}
136
137template <typename ST>
140{
141 return Array<ST> (dim_vector (1, 1), scalar).diag (m, n);
143
144template <typename ST>
145bool
151 return (scalar != ST ());
153
154template <typename ST>
155void
156octave_base_scalar<ST>::print (std::ostream& os, bool pr_as_read_syntax)
158 print_raw (os, pr_as_read_syntax);
159 newline (os);
160}
161
162template <typename ST>
163void
165 bool pr_as_read_syntax) const
166{
167 indent (os);
168 octave_print_internal (os, scalar, pr_as_read_syntax);
169}
170
171template <typename ST>
172bool
174 const std::string& name) const
175{
176 indent (os);
177 os << name << " = ";
178 return false;
179}
180
181template <typename ST>
182void
184{
185 std::ostringstream buf;
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
195template <typename ST>
198{
199 return make_format (scalar);
200}
201
202template <typename ST>
203std::string
206{
207 std::ostringstream buf;
208 octave_print_internal (buf, fmt, scalar);
209 return buf.str ();
210}
211
212template <typename ST>
215{
216 return (n == 0) ? octave_value (scalar) : octave_value ();
217}
218
219template <typename ST>
220bool
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}
OCTARRAY_API Array< T, Alloc > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Size of the specified dimension.
Definition: Array.cc:431
Array< T, Alloc > reshape(octave_idx_type nr, octave_idx_type nc) const
Size of the specified dimension.
Definition: Array.h:595
OCTARRAY_API Array< T, Alloc > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Definition: Array.cc:2521
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
OCTINTERP_API octave_value reshape(const dim_vector &new_dims) const
OCTINTERP_API octave_value permute(const Array< int > &, bool=false) const
OCTINTERP_API bool is_true(void) const
OCTINTERP_API octave_value fast_elem_extract(octave_idx_type n) const
OCTINTERP_API dim_vector dims(void) const
OCTINTERP_API octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
OCTINTERP_API octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
OCTINTERP_API octave_value diag(octave_idx_type k=0) const
OCTINTERP_API float_display_format get_edit_display_format(void) const
OCTINTERP_API void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
OCTINTERP_API std::string edit_display(const float_display_format &fmt, octave_idx_type i, octave_idx_type j) const
OCTINTERP_API bool fast_elem_insert_self(void *where, builtin_type_t btyp) const
OCTINTERP_API void print(std::ostream &os, bool pr_as_read_syntax=false)
OCTINTERP_API void short_disp(std::ostream &os) const
OCTINTERP_API bool print_name_tag(std::ostream &os, const std::string &name) const
OCTINTERP_API 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:980
#define panic_impossible()
Definition: error.h:411
QString name
bool isnan(bool)
Definition: lo-mappers.h:178
void err_nan_to_logical_conversion(void)
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:75
static bool scalar(const dim_vector &dims)
Definition: ov-struct.cc:679
void octave_print_internal(std::ostream &os, const float_display_format &fmt, bool d, bool pr_as_read_syntax)
Definition: pr-output.cc:1762
float_display_format make_format(const double &d)
Definition: pr-output.cc:525