GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-base-scalar.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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:
64 error ("unexpected: index not '(', '{', or '.' in octave_base_scalar<T>::subsref - please report this bug");
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;
100
101 default:
102 error ("unexpected: index not '(', '{', or '.' in octave_base_scalar<T>::subsasgn - please report this bug");
103 }
104
105 return retval;
106}
107
108template <typename ST>
111{
112 static dim_vector dv (1, 1);
113 return dv;
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);
142}
143
144template <typename ST>
145bool
147{
148 if (octave::math::isnan (scalar))
149 octave::err_nan_to_logical_conversion ();
150
151 return (scalar != ST ());
152}
153
154template <typename ST>
155void
156octave_base_scalar<ST>::print (std::ostream& os, bool pr_as_read_syntax)
157{
158 print_raw (os, pr_as_read_syntax);
159 newline (os);
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}
171template <typename ST>
172bool
174 const std::string& name) const
176 indent (os);
177 os << name << " = ";
178 return false;
179}
180
181template <typename ST>
182void
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
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}
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
Array< T, Alloc > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Size of the specified dimension.
Array< T, Alloc > reshape(octave_idx_type nr, octave_idx_type nc) const
Size of the specified dimension.
Definition Array.h:636
Array< T, Alloc > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
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)
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
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:1003
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)
float_display_format make_format(const double &d)
Definition pr-output.cc:525