ov-float.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_h)
00024 #define octave_float_h 1
00025 
00026 #include <cstdlib>
00027 
00028 #include <iosfwd>
00029 #include <string>
00030 
00031 #include "lo-ieee.h"
00032 #include "lo-mappers.h"
00033 #include "lo-utils.h"
00034 #include "mx-base.h"
00035 #include "oct-alloc.h"
00036 #include "str-vec.h"
00037 
00038 #include "gripes.h"
00039 #include "ov-base.h"
00040 #include "ov-re-mat.h"
00041 #include "ov-flt-re-mat.h"
00042 #include "ov-base-scalar.h"
00043 #include "ov-typeinfo.h"
00044 
00045 class octave_value_list;
00046 
00047 class tree_walker;
00048 
00049 // Real scalar values.
00050 
00051 class
00052 OCTINTERP_API
00053 octave_float_scalar : public octave_base_scalar<float>
00054 {
00055 public:
00056 
00057   octave_float_scalar (void)
00058     : octave_base_scalar<float> (0.0) { }
00059 
00060   octave_float_scalar (float d)
00061     : octave_base_scalar<float> (d) { }
00062 
00063   octave_float_scalar (const octave_float_scalar& s)
00064     : octave_base_scalar<float> (s) { }
00065 
00066   ~octave_float_scalar (void) { }
00067 
00068   octave_base_value *clone (void) const { return new octave_float_scalar (*this); }
00069 
00070   // We return an octave_matrix here instead of an octave_float_scalar so
00071   // that in expressions like A(2,2,2) = 2 (for A previously
00072   // undefined), A will be empty instead of a 1x1 object.
00073   octave_base_value *empty_clone (void) const { return new octave_float_matrix (); }
00074 
00075   octave_value do_index_op (const octave_value_list& idx,
00076                             bool resize_ok = false);
00077 
00078   idx_vector index_vector (void) const { return idx_vector (scalar); }
00079 
00080   octave_value any (int = 0) const
00081     { return (scalar != 0 && ! lo_ieee_isnan (scalar)); }
00082 
00083   builtin_type_t builtin_type (void) const { return btyp_float; }
00084 
00085   bool is_real_scalar (void) const { return true; }
00086 
00087   bool is_real_type (void) const { return true; }
00088 
00089   bool is_single_type (void) const { return true; }
00090 
00091   bool is_float_type (void) const { return true; }
00092 
00093   int8NDArray
00094   int8_array_value (void) const
00095     { return int8NDArray (dim_vector (1, 1), scalar); }
00096 
00097   int16NDArray
00098   int16_array_value (void) const
00099     { return int16NDArray (dim_vector (1, 1), scalar); }
00100 
00101   int32NDArray
00102   int32_array_value (void) const
00103     { return int32NDArray (dim_vector (1, 1), scalar); }
00104 
00105   int64NDArray
00106   int64_array_value (void) const
00107     { return int64NDArray (dim_vector (1, 1), scalar); }
00108 
00109   uint8NDArray
00110   uint8_array_value (void) const
00111     { return uint8NDArray (dim_vector (1, 1), scalar); }
00112 
00113   uint16NDArray
00114   uint16_array_value (void) const
00115     { return uint16NDArray (dim_vector (1, 1), scalar); }
00116 
00117   uint32NDArray
00118   uint32_array_value (void) const
00119     { return uint32NDArray (dim_vector (1, 1), scalar); }
00120 
00121   uint64NDArray
00122   uint64_array_value (void) const
00123     { return uint64NDArray (dim_vector (1, 1), scalar); }
00124 
00125 #define DEFINE_INT_SCALAR_VALUE(TYPE) \
00126   octave_ ## TYPE \
00127   TYPE ## _scalar_value (void) const \
00128     { return octave_ ## TYPE (scalar); }
00129 
00130   DEFINE_INT_SCALAR_VALUE (int8)
00131   DEFINE_INT_SCALAR_VALUE (int16)
00132   DEFINE_INT_SCALAR_VALUE (int32)
00133   DEFINE_INT_SCALAR_VALUE (int64)
00134   DEFINE_INT_SCALAR_VALUE (uint8)
00135   DEFINE_INT_SCALAR_VALUE (uint16)
00136   DEFINE_INT_SCALAR_VALUE (uint32)
00137   DEFINE_INT_SCALAR_VALUE (uint64)
00138 
00139 #undef DEFINE_INT_SCALAR_VALUE
00140 
00141   double double_value (bool = false) const { return static_cast<double> (scalar); }
00142 
00143   float float_value (bool = false) const { return scalar; }
00144 
00145   double scalar_value (bool = false) const { return static_cast<double> (scalar); }
00146 
00147   float float_scalar_value (bool = false) const { return scalar; }
00148 
00149   Matrix matrix_value (bool = false) const
00150     { return Matrix (1, 1, scalar); }
00151 
00152   FloatMatrix float_matrix_value (bool = false) const
00153     { return FloatMatrix (1, 1, scalar); }
00154 
00155   NDArray array_value (bool = false) const
00156     { return NDArray (dim_vector (1, 1), scalar); }
00157 
00158   FloatNDArray float_array_value (bool = false) const
00159     { return FloatNDArray (dim_vector (1, 1), scalar); }
00160 
00161   SparseMatrix sparse_matrix_value (bool = false) const
00162     { return SparseMatrix (Matrix (1, 1, scalar)); }
00163 
00164   // FIXME Need SparseComplexMatrix (Matrix) constructor!!!
00165   SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
00166     { return SparseComplexMatrix (sparse_matrix_value ()); }
00167 
00168   octave_value resize (const dim_vector& dv, bool fill = false) const;
00169 
00170   Complex complex_value (bool = false) const { return scalar; }
00171 
00172   FloatComplex float_complex_value (bool = false) const { return scalar; }
00173 
00174   ComplexMatrix complex_matrix_value (bool = false) const
00175     { return  ComplexMatrix (1, 1, Complex (scalar)); }
00176 
00177   FloatComplexMatrix float_complex_matrix_value (bool = false) const
00178     { return  FloatComplexMatrix (1, 1, FloatComplex (scalar)); }
00179 
00180   ComplexNDArray complex_array_value (bool = false) const
00181     { return ComplexNDArray (dim_vector (1, 1), Complex (scalar)); }
00182 
00183   FloatComplexNDArray float_complex_array_value (bool = false) const
00184     { return FloatComplexNDArray (dim_vector (1, 1), FloatComplex (scalar)); }
00185 
00186   charNDArray
00187   char_array_value (bool = false) const
00188   {
00189     charNDArray retval (dim_vector (1, 1));
00190     retval(0) = static_cast<char> (scalar);
00191     return retval;
00192   }
00193 
00194   bool bool_value (bool warn = false) const
00195   {
00196     if (xisnan (scalar))
00197       gripe_nan_to_logical_conversion ();
00198     else if (warn && scalar != 0 && scalar != 1)
00199       gripe_logical_conversion ();
00200 
00201     return scalar;
00202   }
00203 
00204   boolNDArray bool_array_value (bool warn = false) const
00205   {
00206     if (xisnan (scalar))
00207       gripe_nan_to_logical_conversion ();
00208     else if (warn && scalar != 0 && scalar != 1)
00209       gripe_logical_conversion ();
00210 
00211     return boolNDArray (dim_vector (1, 1), scalar);
00212   }
00213 
00214   octave_value convert_to_str_internal (bool pad, bool force, char type) const;
00215 
00216   void increment (void) { ++scalar; }
00217 
00218   void decrement (void) { --scalar; }
00219 
00220   bool save_ascii (std::ostream& os);
00221 
00222   bool load_ascii (std::istream& is);
00223 
00224   bool save_binary (std::ostream& os, bool& save_as_floats);
00225 
00226   bool load_binary (std::istream& is, bool swap,
00227                     oct_mach_info::float_format fmt);
00228 
00229 #if defined (HAVE_HDF5)
00230   bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
00231 
00232   bool load_hdf5 (hid_t loc_id, const char *name);
00233 #endif
00234 
00235   int write (octave_stream& os, int block_size,
00236              oct_data_conv::data_type output_type, int skip,
00237              oct_mach_info::float_format flt_fmt) const
00238     {
00239       return os.write (array_value (), block_size, output_type,
00240                        skip, flt_fmt);
00241     }
00242 
00243   mxArray *as_mxArray (void) const;
00244 
00245   octave_value map (unary_mapper_t umap) const;
00246 
00247   bool fast_elem_insert_self (void *where, builtin_type_t btyp) const;
00248 
00249 private:
00250 
00251   DECLARE_OCTAVE_ALLOCATOR
00252 
00253   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
00254 };
00255 
00256 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines