GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-complex.h
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#if ! defined (octave_ov_complex_h)
27#define octave_ov_complex_h 1
28
29#include "octave-config.h"
30
31#include <cstdlib>
32
33#include <iosfwd>
34#include <string>
35
36#include "mx-base.h"
37#include "str-vec.h"
38
39#include "errwarn.h"
40#include "error.h"
41#include "ov-base.h"
42#include "ov-cx-mat.h"
43#include "ov-base-scalar.h"
44#include "ov-typeinfo.h"
45
47
48// Complex scalar values.
49
50extern template class OCTINTERP_EXTERN_TEMPLATE_API octave_base_scalar<Complex>;
51
52class OCTINTERP_API octave_complex : public octave_base_scalar<Complex>
53{
54public:
55
58
61
64
65 ~octave_complex () = default;
66
67 octave_base_value * clone () const { return new octave_complex (*this); }
68
69 // We return an octave_complex_matrix object here instead of an
70 // octave_complex object so that in expressions like A(2,2,2) = 2
71 // (for A previously undefined), A will be empty instead of a 1x1
72 // object.
74 { return new octave_complex_matrix (); }
75
76 type_conv_info numeric_demotion_function () const;
77
78 octave_base_value * try_narrowing_conversion ();
79
80 octave_value do_index_op (const octave_value_list& idx,
81 bool resize_ok = false);
82
83 // Use this to give a more specific error message.
84 octave::idx_vector index_vector (bool /* require_integers */ = false) const;
85
86 octave_value any (int = 0) const
87 {
88 return (scalar != Complex (0, 0)
89 && ! (octave::math::isnan (scalar.real ())
90 || octave::math::isnan (scalar.imag ())));
91 }
92
94
95 bool is_complex_scalar () const { return true; }
96
97 bool iscomplex () const { return true; }
98
99 bool is_double_type () const { return true; }
100
101 bool isfloat () const { return true; }
102
103 OCTINTERP_API double double_value (bool = false) const;
104
105 OCTINTERP_API float float_value (bool = false) const;
106
107 double scalar_value (bool frc_str_conv = false) const
108 { return double_value (frc_str_conv); }
109
110 float float_scalar_value (bool frc_str_conv = false) const
111 { return float_value (frc_str_conv); }
112
113 OCTINTERP_API Matrix matrix_value (bool = false) const;
114
115 OCTINTERP_API FloatMatrix float_matrix_value (bool = false) const;
116
117 OCTINTERP_API NDArray array_value (bool = false) const;
118
119 OCTINTERP_API FloatNDArray float_array_value (bool = false) const;
120
122 { return SparseMatrix (matrix_value ()); }
123
125 { return SparseComplexMatrix (complex_matrix_value ()); }
126
127 OCTINTERP_API octave_value
128 resize (const dim_vector& dv, bool fill = false) const;
129
130 OCTINTERP_API Complex complex_value (bool = false) const;
131
132 OCTINTERP_API FloatComplex float_complex_value (bool = false) const;
133
134 OCTINTERP_API ComplexMatrix complex_matrix_value (bool = false) const;
135
136 OCTINTERP_API FloatComplexMatrix
137 float_complex_matrix_value (bool = false) const;
138
139 OCTINTERP_API ComplexNDArray complex_array_value (bool = false) const;
140
141 OCTINTERP_API FloatComplexNDArray
142 float_complex_array_value (bool = false) const;
143
144 bool bool_value (bool warn = false) const
145 {
146 if (octave::math::isnan (scalar))
147 octave::err_nan_to_logical_conversion ();
148 if (warn && scalar != 0.0 && scalar != 1.0)
150
151 return scalar != 0.0;
152 }
153
154 boolNDArray bool_array_value (bool warn = false) const
155 {
156 if (octave::math::isnan (scalar))
157 octave::err_nan_to_logical_conversion ();
158 if (warn && scalar != 0.0 && scalar != 1.0)
160
161 return boolNDArray (dim_vector (1, 1), scalar != 0.0);
162 }
163
164 OCTINTERP_API octave_value as_double () const;
165 OCTINTERP_API octave_value as_single () const;
166
167 // We don't need to override both forms of the diag method. The using
168 // declaration will avoid warnings about partially-overloaded virtual
169 // functions.
170 using octave_base_scalar<Complex>::diag;
171
172 OCTINTERP_API octave_value diag (octave_idx_type m, octave_idx_type n) const;
173
174 void increment () { scalar += 1.0; }
175
176 void decrement () { scalar -= 1.0; }
177
178 OCTINTERP_API bool save_ascii (std::ostream& os);
179
180 OCTINTERP_API bool load_ascii (std::istream& is);
181
182 OCTINTERP_API bool save_binary (std::ostream& os, bool save_as_floats);
183
184 OCTINTERP_API bool
185 load_binary (std::istream& is, bool swap,
186 octave::mach_info::float_format fmt);
187
188 OCTINTERP_API bool
189 save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
190
191 OCTINTERP_API bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
192
193 int write (octave::stream& os, int block_size,
194 oct_data_conv::data_type output_type, int skip,
195 octave::mach_info::float_format flt_fmt) const
196 {
197 // Yes, for compatibility, we drop the imaginary part here.
198 return os.write (array_value (true), block_size, output_type,
199 skip, flt_fmt);
200 }
201
202 OCTINTERP_API mxArray * as_mxArray (bool interleaved) const;
203
204 OCTINTERP_API octave_value map (unary_mapper_t umap) const;
205
206private:
207
209};
210
212
213#endif
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
octave_complex(const Complex &c)
Definition ov-complex.h:59
float float_scalar_value(bool frc_str_conv=false) const
Definition ov-complex.h:110
SparseMatrix sparse_matrix_value(bool=false) const
Definition ov-complex.h:121
bool iscomplex() const
Definition ov-complex.h:97
octave_base_value * empty_clone() const
Definition ov-complex.h:73
octave_complex(const octave_complex &c)
Definition ov-complex.h:62
~octave_complex()=default
bool is_double_type() const
Definition ov-complex.h:99
bool isfloat() const
Definition ov-complex.h:101
octave_base_value * clone() const
Definition ov-complex.h:67
builtin_type_t builtin_type() const
Definition ov-complex.h:93
boolNDArray bool_array_value(bool warn=false) const
Definition ov-complex.h:154
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition ov-complex.h:124
double scalar_value(bool frc_str_conv=false) const
Definition ov-complex.h:107
octave_value any(int=0) const
Definition ov-complex.h:86
int write(octave::stream &os, int block_size, oct_data_conv::data_type output_type, int skip, octave::mach_info::float_format flt_fmt) const
Definition ov-complex.h:193
bool is_complex_scalar() const
Definition ov-complex.h:95
bool bool_value(bool warn=false) const
Definition ov-complex.h:144
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Definition ovl.h:115
Array< octave_value > array_value() const
Definition ovl.h:88
void warn_logical_conversion()
Definition errwarn.cc:365
std::complex< double > Complex
Definition oct-cmplx.h:33
std::complex< float > FloatComplex
Definition oct-cmplx.h:34
int64_t octave_hdf5_id
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA_API(API)
Definition ov-base.h:185
builtin_type_t
Definition ov-base.h:83
@ btyp_complex
Definition ov-base.h:86
octave_complex octave_complex_scalar
Definition ov-complex.h:211