ov-flt-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_float_complex_h)
00024 #define octave_float_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-flt-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_float_complex : public octave_base_scalar<FloatComplex>
00052 {
00053 public:
00054 
00055   octave_float_complex (void)
00056     : octave_base_scalar<FloatComplex> () { }
00057 
00058   octave_float_complex (const FloatComplex& c)
00059     : octave_base_scalar<FloatComplex> (c) { }
00060 
00061   octave_float_complex (const octave_float_complex& c)
00062     : octave_base_scalar<FloatComplex> (c) { }
00063 
00064   ~octave_float_complex (void) { }
00065 
00066   octave_base_value *clone (void) const { return new octave_float_complex (*this); }
00067 
00068   // We return an octave_float_complex_matrix object here instead of an
00069   // octave_float_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_float_complex_matrix (); }
00074 
00075   octave_base_value *try_narrowing_conversion (void);
00076 
00077   octave_value do_index_op (const octave_value_list& idx,
00078                             bool resize_ok = false);
00079 
00080   octave_value any (int = 0) const
00081     {
00082       return (scalar != FloatComplex (0, 0)
00083               && ! (lo_ieee_isnan (std::real (scalar))
00084                     || lo_ieee_isnan (std::imag (scalar))));
00085     }
00086 
00087   builtin_type_t builtin_type (void) const { return btyp_float_complex; }
00088 
00089   bool is_complex_scalar (void) const { return true; }
00090 
00091   bool is_complex_type (void) const { return true; }
00092 
00093   bool is_single_type (void) const { return true; }
00094 
00095   bool is_float_type (void) const { return true; }
00096 
00097   double double_value (bool = false) const;
00098 
00099   float float_value (bool = false) const;
00100 
00101   double scalar_value (bool frc_str_conv = false) const
00102     { return double_value (frc_str_conv); }
00103 
00104   float float_scalar_value (bool frc_str_conv = false) const
00105     { return float_value (frc_str_conv); }
00106 
00107   Matrix matrix_value (bool = false) const;
00108 
00109   FloatMatrix float_matrix_value (bool = false) const;
00110 
00111   NDArray array_value (bool = false) const;
00112 
00113   FloatNDArray float_array_value (bool = false) const;
00114 
00115   SparseMatrix sparse_matrix_value (bool = false) const
00116     { return SparseMatrix (matrix_value ()); }
00117 
00118   SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
00119     { return SparseComplexMatrix (complex_matrix_value ()); }
00120 
00121   octave_value resize (const dim_vector& dv, bool fill = false) const;
00122 
00123   Complex complex_value (bool = false) const;
00124 
00125   FloatComplex float_complex_value (bool = false) const;
00126 
00127   ComplexMatrix complex_matrix_value (bool = false) const;
00128 
00129   FloatComplexMatrix float_complex_matrix_value (bool = false) const;
00130 
00131   ComplexNDArray complex_array_value (bool = false) const;
00132 
00133   FloatComplexNDArray float_complex_array_value (bool = false) const;
00134 
00135   bool bool_value (bool warn = false) const
00136   {
00137     if (xisnan (scalar))
00138       gripe_nan_to_logical_conversion ();
00139     else if (warn && scalar != 0.0f && scalar != 1.0f)
00140       gripe_logical_conversion ();
00141 
00142     return scalar != 0.0f;
00143   }
00144 
00145   boolNDArray bool_array_value (bool warn = false) const
00146   {
00147     if (xisnan (scalar))
00148       gripe_nan_to_logical_conversion ();
00149     else if (warn && scalar != 0.0f && scalar != 1.0f)
00150       gripe_logical_conversion ();
00151 
00152     return boolNDArray (dim_vector (1, 1), scalar != 1.0f);
00153   }
00154 
00155   void increment (void) { scalar += 1.0; }
00156 
00157   void decrement (void) { scalar -= 1.0; }
00158 
00159   bool save_ascii (std::ostream& os);
00160 
00161   bool load_ascii (std::istream& is);
00162 
00163   bool save_binary (std::ostream& os, bool& save_as_floats);
00164 
00165   bool load_binary (std::istream& is, bool swap,
00166                     oct_mach_info::float_format fmt);
00167 
00168 #if defined (HAVE_HDF5)
00169   bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
00170 
00171   bool load_hdf5 (hid_t loc_id, const char *name);
00172 #endif
00173 
00174   int write (octave_stream& os, int block_size,
00175              oct_data_conv::data_type output_type, int skip,
00176              oct_mach_info::float_format flt_fmt) const
00177     {
00178       // Yes, for compatibility, we drop the imaginary part here.
00179       return os.write (array_value (true), block_size, output_type,
00180                        skip, flt_fmt);
00181     }
00182 
00183   mxArray *as_mxArray (void) const;
00184 
00185   octave_value map (unary_mapper_t umap) const;
00186 
00187 private:
00188 
00189   DECLARE_OCTAVE_ALLOCATOR
00190 
00191   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
00192 };
00193 
00194 typedef octave_float_complex octave_float_complex_scalar;
00195 
00196 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines