ov-complex.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_complex_h)
00024 #define octave_complex_h 1
00025 
00026 #include <cstdlib>
00027 
00028 #include <iosfwd>
00029 #include <string>
00030 
00031 #include "lo-ieee.h"
00032 #include "mx-base.h"
00033 #include "oct-alloc.h"
00034 #include "str-vec.h"
00035 
00036 #include "gripes.h"
00037 #include "error.h"
00038 #include "ov-base.h"
00039 #include "ov-cx-mat.h"
00040 #include "ov-base-scalar.h"
00041 #include "ov-typeinfo.h"
00042 
00043 class octave_value_list;
00044 
00045 class tree_walker;
00046 
00047 // Complex scalar values.
00048 
00049 class
00050 OCTINTERP_API
00051 octave_complex : public octave_base_scalar<Complex>
00052 {
00053 public:
00054 
00055   octave_complex (void)
00056     : octave_base_scalar<Complex> () { }
00057 
00058   octave_complex (const Complex& c)
00059     : octave_base_scalar<Complex> (c) { }
00060 
00061   octave_complex (const octave_complex& c)
00062     : octave_base_scalar<Complex> (c) { }
00063 
00064   ~octave_complex (void) { }
00065 
00066   octave_base_value *clone (void) const { return new octave_complex (*this); }
00067 
00068   // We return an octave_complex_matrix object here instead of an
00069   // octave_complex object so that in expressions like A(2,2,2) = 2
00070   // (for A previously undefined), A will be empty instead of a 1x1
00071   // object.
00072   octave_base_value *empty_clone (void) const
00073     { return new octave_complex_matrix (); }
00074 
00075   type_conv_info numeric_demotion_function (void) const;
00076 
00077   octave_base_value *try_narrowing_conversion (void);
00078 
00079   octave_value do_index_op (const octave_value_list& idx,
00080                             bool resize_ok = false);
00081 
00082   // Use this to give a more specific error message
00083   idx_vector index_vector (void) const
00084   {
00085     error (
00086            "attempted to use a complex scalar as an index\n"
00087            "       (forgot to initialize i or j?)");
00088     return idx_vector ();
00089   }
00090 
00091   octave_value any (int = 0) const
00092     {
00093       return (scalar != Complex (0, 0)
00094               && ! (lo_ieee_isnan (std::real (scalar))
00095                     || lo_ieee_isnan (std::imag (scalar))));
00096     }
00097 
00098   builtin_type_t builtin_type (void) const { return btyp_complex; }
00099 
00100   bool is_complex_scalar (void) const { return true; }
00101 
00102   bool is_complex_type (void) const { return true; }
00103 
00104   bool is_double_type (void) const { return true; }
00105 
00106   bool is_float_type (void) const { return true; }
00107 
00108   double double_value (bool = false) const;
00109 
00110   float float_value (bool = false) const;
00111 
00112   double scalar_value (bool frc_str_conv = false) const
00113     { return double_value (frc_str_conv); }
00114 
00115   float float_scalar_value (bool frc_str_conv = false) const
00116     { return float_value (frc_str_conv); }
00117 
00118   Matrix matrix_value (bool = false) const;
00119 
00120   FloatMatrix float_matrix_value (bool = false) const;
00121 
00122   NDArray array_value (bool = false) const;
00123 
00124   FloatNDArray float_array_value (bool = false) const;
00125 
00126   SparseMatrix sparse_matrix_value (bool = false) const
00127     { return SparseMatrix (matrix_value ()); }
00128 
00129   SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
00130     { return SparseComplexMatrix (complex_matrix_value ()); }
00131 
00132   octave_value resize (const dim_vector& dv, bool fill = false) const;
00133 
00134   Complex complex_value (bool = false) const;
00135 
00136   FloatComplex float_complex_value (bool = false) const;
00137 
00138   ComplexMatrix complex_matrix_value (bool = false) const;
00139 
00140   FloatComplexMatrix float_complex_matrix_value (bool = false) const;
00141 
00142   ComplexNDArray complex_array_value (bool = false) const;
00143 
00144   FloatComplexNDArray float_complex_array_value (bool = false) const;
00145 
00146   bool bool_value (bool warn = false) const
00147   {
00148     if (xisnan (scalar))
00149       gripe_nan_to_logical_conversion ();
00150     else if (warn && scalar != 0.0 && scalar != 1.0)
00151       gripe_logical_conversion ();
00152 
00153     return scalar != 0.0;
00154   }
00155 
00156   boolNDArray bool_array_value (bool warn = false) const
00157   {
00158     if (xisnan (scalar))
00159       gripe_nan_to_logical_conversion ();
00160     else if (warn && scalar != 0.0 && scalar != 1.0)
00161       gripe_logical_conversion ();
00162 
00163     return boolNDArray (dim_vector (1, 1), scalar != 0.0);
00164   }
00165 
00166   void increment (void) { scalar += 1.0; }
00167 
00168   void decrement (void) { scalar -= 1.0; }
00169 
00170   bool save_ascii (std::ostream& os);
00171 
00172   bool load_ascii (std::istream& is);
00173 
00174   bool save_binary (std::ostream& os, bool& save_as_floats);
00175 
00176   bool load_binary (std::istream& is, bool swap,
00177                     oct_mach_info::float_format fmt);
00178 
00179 #if defined (HAVE_HDF5)
00180   bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
00181 
00182   bool load_hdf5 (hid_t loc_id, const char *name);
00183 #endif
00184 
00185   int write (octave_stream& os, int block_size,
00186              oct_data_conv::data_type output_type, int skip,
00187              oct_mach_info::float_format flt_fmt) const
00188     {
00189       // Yes, for compatibility, we drop the imaginary part here.
00190       return os.write (array_value (true), block_size, output_type,
00191                        skip, flt_fmt);
00192     }
00193 
00194   mxArray *as_mxArray (void) const;
00195 
00196   octave_value map (unary_mapper_t umap) const;
00197 
00198 private:
00199 
00200   DECLARE_OCTAVE_ALLOCATOR
00201 
00202   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
00203 };
00204 
00205 typedef octave_complex octave_complex_scalar;
00206 
00207 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines