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