ov-flt-re-diag.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2008-2012 Jaroslav Hajek
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 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include "byte-swap.h"
00028 
00029 #include "ov-flt-re-diag.h"
00030 #include "ov-base-diag.cc"
00031 #include "ov-float.h"
00032 #include "ov-flt-re-mat.h"
00033 #include "ls-utils.h"
00034 
00035 template class octave_base_diag<FloatDiagMatrix, FloatMatrix>;
00036 
00037 DEFINE_OCTAVE_ALLOCATOR (octave_float_diag_matrix);
00038 
00039 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_float_diag_matrix,
00040                                      "float diagonal matrix", "single");
00041 
00042 static octave_base_value *
00043 default_numeric_conversion_function (const octave_base_value& a)
00044 {
00045   CAST_CONV_ARG (const octave_float_diag_matrix&);
00046 
00047   return new octave_float_matrix (v.float_matrix_value ());
00048 }
00049 
00050 octave_base_value::type_conv_info
00051 octave_float_diag_matrix::numeric_conversion_function (void) const
00052 {
00053   return octave_base_value::type_conv_info (default_numeric_conversion_function,
00054                                             octave_float_matrix::static_type_id ());
00055 }
00056 
00057 octave_base_value *
00058 octave_float_diag_matrix::try_narrowing_conversion (void)
00059 {
00060   octave_base_value *retval = 0;
00061 
00062   if (matrix.nelem () == 1)
00063     retval = new octave_float_scalar (matrix (0, 0));
00064 
00065   return retval;
00066 }
00067 
00068 DiagMatrix
00069 octave_float_diag_matrix::diag_matrix_value (bool) const
00070 {
00071   return DiagMatrix (matrix);
00072 }
00073 
00074 FloatDiagMatrix
00075 octave_float_diag_matrix::float_diag_matrix_value (bool) const
00076 {
00077   return matrix;
00078 }
00079 
00080 ComplexDiagMatrix
00081 octave_float_diag_matrix::complex_diag_matrix_value (bool) const
00082 {
00083   return ComplexDiagMatrix (matrix);
00084 }
00085 
00086 FloatComplexDiagMatrix
00087 octave_float_diag_matrix::float_complex_diag_matrix_value (bool) const
00088 {
00089   return FloatComplexDiagMatrix (matrix);
00090 }
00091 
00092 octave_value
00093 octave_float_diag_matrix::map (unary_mapper_t umap) const
00094 {
00095   switch (umap)
00096     {
00097     case umap_abs:
00098       return matrix.abs ();
00099     case umap_real:
00100     case umap_conj:
00101       return matrix;
00102     case umap_imag:
00103       return DiagMatrix (matrix.rows (), matrix.cols (), 0.0);
00104     case umap_sqrt:
00105       {
00106         FloatComplexColumnVector tmp = matrix.diag ().map<FloatComplex> (rc_sqrt);
00107         FloatComplexDiagMatrix retval (tmp);
00108         retval.resize (matrix.rows (), matrix.columns ());
00109         return retval;
00110       }
00111     default:
00112       return to_dense ().map (umap);
00113     }
00114 }
00115 
00116 bool
00117 octave_float_diag_matrix::save_binary (std::ostream& os,
00118                                        bool& /* save_as_floats*/)
00119 {
00120 
00121   int32_t r = matrix.rows (), c = matrix.cols ();
00122   os.write (reinterpret_cast<char *> (&r), 4);
00123   os.write (reinterpret_cast<char *> (&c), 4);
00124 
00125   FloatMatrix m = FloatMatrix (matrix.diag ());
00126   save_type st = LS_FLOAT;
00127   if (matrix.length () > 8192) // FIXME -- make this configurable.
00128     {
00129       float max_val, min_val;
00130       if (m.all_integers (max_val, min_val))
00131         st = get_save_type (max_val, min_val);
00132     }
00133 
00134   const float *mtmp = m.data ();
00135   write_floats (os, mtmp, st, m.numel ());
00136 
00137   return true;
00138 }
00139 
00140 bool
00141 octave_float_diag_matrix::load_binary (std::istream& is, bool swap,
00142                                  oct_mach_info::float_format fmt)
00143 {
00144   int32_t r, c;
00145   char tmp;
00146   if (! (is.read (reinterpret_cast<char *> (&r), 4)
00147          && is.read (reinterpret_cast<char *> (&c), 4)
00148          && is.read (reinterpret_cast<char *> (&tmp), 1)))
00149     return false;
00150   if (swap)
00151     {
00152       swap_bytes<4> (&r);
00153       swap_bytes<4> (&c);
00154     }
00155 
00156   FloatDiagMatrix m (r, c);
00157   float *re = m.fortran_vec ();
00158   octave_idx_type len = m.length ();
00159   read_floats (is, re, static_cast<save_type> (tmp), len, swap, fmt);
00160   if (error_state || ! is)
00161     return false;
00162   matrix = m;
00163 
00164   return true;
00165 }
00166 
00167 bool
00168 octave_float_diag_matrix::chk_valid_scalar (const octave_value& val,
00169                                             float& x) const
00170 {
00171   bool retval = val.is_real_scalar ();
00172   if (retval)
00173     x = val.float_value ();
00174   return retval;
00175 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines