GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-complex.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_ov_complex_h)
24 #define octave_ov_complex_h 1
25 
26 #include <cstdlib>
27 
28 #include <iosfwd>
29 #include <string>
30 
31 #include "lo-ieee.h"
32 #include "mx-base.h"
33 #include "oct-alloc.h"
34 #include "str-vec.h"
35 
36 #include "gripes.h"
37 #include "error.h"
38 #include "ov-base.h"
39 #include "ov-cx-mat.h"
40 #include "ov-base-scalar.h"
41 #include "ov-typeinfo.h"
42 
43 class octave_value_list;
44 
45 class tree_walker;
46 
47 // Complex scalar values.
48 
49 class
52 {
53 public:
54 
56  : octave_base_scalar<Complex> () { }
57 
59  : octave_base_scalar<Complex> (c) { }
60 
62  : octave_base_scalar<Complex> (c) { }
63 
64  ~octave_complex (void) { }
65 
66  octave_base_value *clone (void) const { return new octave_complex (*this); }
67 
68  // We return an octave_complex_matrix object here instead of an
69  // octave_complex object so that in expressions like A(2,2,2) = 2
70  // (for A previously undefined), A will be empty instead of a 1x1
71  // object.
72  octave_base_value *empty_clone (void) const
73  { return new octave_complex_matrix (); }
74 
75  type_conv_info numeric_demotion_function (void) const;
76 
77  octave_base_value *try_narrowing_conversion (void);
78 
79  octave_value do_index_op (const octave_value_list& idx,
80  bool resize_ok = false);
81 
82  // Use this to give a more specific error message
83  idx_vector index_vector (void) const
84  {
85  error ("attempted to use a complex scalar as an index\n"
86  " (forgot to initialize i or j?)");
87  return idx_vector ();
88  }
89 
90  octave_value any (int = 0) const
91  {
92  return (scalar != Complex (0, 0)
95  }
96 
97  builtin_type_t builtin_type (void) const { return btyp_complex; }
98 
99  bool is_complex_scalar (void) const { return true; }
100 
101  bool is_complex_type (void) const { return true; }
102 
103  bool is_double_type (void) const { return true; }
104 
105  bool is_float_type (void) const { return true; }
106 
107  double double_value (bool = false) const;
108 
109  float float_value (bool = false) const;
110 
111  double scalar_value (bool frc_str_conv = false) const
112  { return double_value (frc_str_conv); }
113 
114  float float_scalar_value (bool frc_str_conv = false) const
115  { return float_value (frc_str_conv); }
116 
117  Matrix matrix_value (bool = false) const;
118 
119  FloatMatrix float_matrix_value (bool = false) const;
120 
121  NDArray array_value (bool = false) const;
122 
123  FloatNDArray float_array_value (bool = false) const;
124 
125  SparseMatrix sparse_matrix_value (bool = false) const
126  { return SparseMatrix (matrix_value ()); }
127 
128  SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
129  { return SparseComplexMatrix (complex_matrix_value ()); }
130 
131  octave_value resize (const dim_vector& dv, bool fill = false) const;
132 
133  Complex complex_value (bool = false) const;
134 
135  FloatComplex float_complex_value (bool = false) const;
136 
137  ComplexMatrix complex_matrix_value (bool = false) const;
138 
139  FloatComplexMatrix float_complex_matrix_value (bool = false) const;
140 
141  ComplexNDArray complex_array_value (bool = false) const;
142 
143  FloatComplexNDArray float_complex_array_value (bool = false) const;
144 
145  bool bool_value (bool warn = false) const
146  {
147  if (xisnan (scalar))
149  else 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 (xisnan (scalar))
159  else if (warn && scalar != 0.0 && scalar != 1.0)
161 
162  return boolNDArray (dim_vector (1, 1), scalar != 0.0);
163  }
164 
165  octave_value diag (octave_idx_type m, octave_idx_type n) const;
166 
167  void increment (void) { scalar += 1.0; }
168 
169  void decrement (void) { scalar -= 1.0; }
170 
171  bool save_ascii (std::ostream& os);
172 
173  bool load_ascii (std::istream& is);
174 
175  bool save_binary (std::ostream& os, bool& save_as_floats);
176 
177  bool load_binary (std::istream& is, bool swap,
179 
180 #if defined (HAVE_HDF5)
181  bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
182 
183  bool load_hdf5 (hid_t loc_id, const char *name);
184 #endif
185 
186  int write (octave_stream& os, int block_size,
187  oct_data_conv::data_type output_type, int skip,
188  oct_mach_info::float_format flt_fmt) const
189  {
190  // Yes, for compatibility, we drop the imaginary part here.
191  return os.write (array_value (true), block_size, output_type,
192  skip, flt_fmt);
193  }
194 
195  mxArray *as_mxArray (void) const;
196 
197  octave_value map (unary_mapper_t umap) const;
198 
199 private:
200 
202 
204 };
205 
207 
208 #endif