1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 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
53 octave_complex : public octave_base_scalar<Complex>
54 {
55 public:
56 
octave_complex(void)57   octave_complex (void)
58     : octave_base_scalar<Complex> () { }
59 
octave_complex(const Complex & c)60   octave_complex (const Complex& c)
61     : octave_base_scalar<Complex> (c) { }
62 
octave_complex(const octave_complex & c)63   octave_complex (const octave_complex& c)
64     : octave_base_scalar<Complex> (c) { }
65 
66   ~octave_complex (void) = default;
67 
clone(void)68   octave_base_value * clone (void) 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.
empty_clone(void)74   octave_base_value * empty_clone (void) const
75   { return new octave_complex_matrix (); }
76 
77   type_conv_info numeric_demotion_function (void) const;
78 
79   octave_base_value * try_narrowing_conversion (void);
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   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 
builtin_type(void)94   builtin_type_t builtin_type (void) const { return btyp_complex; }
95 
is_complex_scalar(void)96   bool is_complex_scalar (void) const { return true; }
97 
iscomplex(void)98   bool iscomplex (void) const { return true; }
99 
is_double_type(void)100   bool is_double_type (void) const { return true; }
101 
isfloat(void)102   bool isfloat (void) const { return true; }
103 
104   double double_value (bool = false) const;
105 
106   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   Matrix matrix_value (bool = false) const;
115 
116   FloatMatrix float_matrix_value (bool = false) const;
117 
118   NDArray array_value (bool = false) const;
119 
120   FloatNDArray float_array_value (bool = false) const;
121 
122   SparseMatrix sparse_matrix_value (bool = false) const
123   { return SparseMatrix (matrix_value ()); }
124 
125   SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
126   { return SparseComplexMatrix (complex_matrix_value ()); }
127 
128   octave_value resize (const dim_vector& dv, bool fill = false) const;
129 
130   Complex complex_value (bool = false) const;
131 
132   FloatComplex float_complex_value (bool = false) const;
133 
134   ComplexMatrix complex_matrix_value (bool = false) const;
135 
136   FloatComplexMatrix float_complex_matrix_value (bool = false) const;
137 
138   ComplexNDArray complex_array_value (bool = false) const;
139 
140   FloatComplexNDArray float_complex_array_value (bool = false) const;
141 
142   bool bool_value (bool warn = false) const
143   {
144     if (octave::math::isnan (scalar))
145       octave::err_nan_to_logical_conversion ();
146     if (warn && scalar != 0.0 && scalar != 1.0)
147       warn_logical_conversion ();
148 
149     return scalar != 0.0;
150   }
151 
152   boolNDArray bool_array_value (bool warn = false) const
153   {
154     if (octave::math::isnan (scalar))
155       octave::err_nan_to_logical_conversion ();
156     if (warn && scalar != 0.0 && scalar != 1.0)
157       warn_logical_conversion ();
158 
159     return boolNDArray (dim_vector (1, 1), scalar != 0.0);
160   }
161 
162   octave_value as_double (void) const;
163   octave_value as_single (void) const;
164 
165   // We don't need to override both forms of the diag method.  The using
166   // declaration will avoid warnings about partially-overloaded virtual
167   // functions.
168   using octave_base_scalar<Complex>::diag;
169 
170   octave_value diag (octave_idx_type m, octave_idx_type n) const;
171 
increment(void)172   void increment (void) { scalar += 1.0; }
173 
decrement(void)174   void decrement (void) { scalar -= 1.0; }
175 
176   bool save_ascii (std::ostream& os);
177 
178   bool load_ascii (std::istream& is);
179 
180   bool save_binary (std::ostream& os, bool save_as_floats);
181 
182   bool load_binary (std::istream& is, bool swap,
183                     octave::mach_info::float_format fmt);
184 
185   bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
186 
187   bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
188 
write(octave::stream & os,int block_size,oct_data_conv::data_type output_type,int skip,octave::mach_info::float_format flt_fmt)189   int write (octave::stream& os, int block_size,
190              oct_data_conv::data_type output_type, int skip,
191              octave::mach_info::float_format flt_fmt) const
192   {
193     // Yes, for compatibility, we drop the imaginary part here.
194     return os.write (array_value (true), block_size, output_type,
195                      skip, flt_fmt);
196   }
197 
198   mxArray * as_mxArray (void) const;
199 
200   octave_value map (unary_mapper_t umap) const;
201 
202 private:
203 
204   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
205 };
206 
207 typedef octave_complex octave_complex_scalar;
208 
209 #endif
210