ov-flt-cx-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-cx-diag.h"
00030 #include "ov-base-diag.cc"
00031 #include "ov-flt-re-diag.h"
00032 #include "ov-flt-complex.h"
00033 #include "ov-flt-cx-mat.h"
00034 #include "ls-utils.h"
00035 
00036 template class octave_base_diag<FloatComplexDiagMatrix, FloatComplexMatrix>;
00037 
00038 DEFINE_OCTAVE_ALLOCATOR (octave_float_complex_diag_matrix);
00039 
00040 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_float_complex_diag_matrix,
00041                                      "float complex diagonal matrix", "single");
00042 
00043 static octave_base_value *
00044 default_numeric_conversion_function (const octave_base_value& a)
00045 {
00046   CAST_CONV_ARG (const octave_float_complex_diag_matrix&);
00047 
00048   return new octave_float_complex_matrix (v.float_complex_matrix_value ());
00049 }
00050 
00051 octave_base_value::type_conv_info
00052 octave_float_complex_diag_matrix::numeric_conversion_function (void) const
00053 {
00054   return octave_base_value::type_conv_info (default_numeric_conversion_function,
00055                                             octave_float_complex_matrix::static_type_id ());
00056 }
00057 
00058 octave_base_value *
00059 octave_float_complex_diag_matrix::try_narrowing_conversion (void)
00060 {
00061   octave_base_value *retval = 0;
00062 
00063   if (matrix.nelem () == 1)
00064     {
00065       retval = new octave_float_complex (matrix (0, 0));
00066       octave_base_value *rv2 = retval->try_narrowing_conversion ();
00067       if (rv2)
00068         {
00069           delete retval;
00070           retval = rv2;
00071         }
00072     }
00073   else if (matrix.all_elements_are_real ())
00074     {
00075       return new octave_float_diag_matrix (::real (matrix));
00076     }
00077 
00078   return retval;
00079 }
00080 
00081 DiagMatrix
00082 octave_float_complex_diag_matrix::diag_matrix_value (bool force_conversion) const
00083 {
00084   DiagMatrix retval;
00085 
00086   if (! force_conversion)
00087     gripe_implicit_conversion ("Octave:imag-to-real",
00088                                type_name (), "real matrix");
00089 
00090   retval = ::real (matrix);
00091 
00092   return retval;
00093 }
00094 
00095 FloatDiagMatrix
00096 octave_float_complex_diag_matrix::float_diag_matrix_value (bool force_conversion) const
00097 {
00098   DiagMatrix retval;
00099 
00100   if (! force_conversion)
00101     gripe_implicit_conversion ("Octave:imag-to-real",
00102                                type_name (), "real matrix");
00103 
00104   retval = ::real (matrix);
00105 
00106   return retval;
00107 }
00108 
00109 ComplexDiagMatrix
00110 octave_float_complex_diag_matrix::complex_diag_matrix_value (bool) const
00111 {
00112   return ComplexDiagMatrix (matrix);
00113 }
00114 
00115 FloatComplexDiagMatrix
00116 octave_float_complex_diag_matrix::float_complex_diag_matrix_value (bool) const
00117 {
00118   return matrix;
00119 }
00120 
00121 octave_value
00122 octave_float_complex_diag_matrix::map (unary_mapper_t umap) const
00123 {
00124   switch (umap)
00125     {
00126     case umap_abs:
00127       return matrix.abs ();
00128     case umap_real:
00129       return ::real (matrix);
00130     case umap_conj:
00131       return ::conj (matrix);
00132     case umap_imag:
00133       return ::imag (matrix);
00134     case umap_sqrt:
00135       {
00136         FloatComplexColumnVector tmp = matrix.diag ().map<FloatComplex> (std::sqrt);
00137         FloatComplexDiagMatrix retval (tmp);
00138         retval.resize (matrix.rows (), matrix.columns ());
00139         return retval;
00140       }
00141     default:
00142       return to_dense ().map (umap);
00143     }
00144 }
00145 
00146 
00147 bool
00148 octave_float_complex_diag_matrix::save_binary (std::ostream& os,
00149                                                bool& /* save_as_floats */)
00150 {
00151 
00152   int32_t r = matrix.rows (), c = matrix.cols ();
00153   os.write (reinterpret_cast<char *> (&r), 4);
00154   os.write (reinterpret_cast<char *> (&c), 4);
00155 
00156   FloatComplexMatrix m = FloatComplexMatrix (matrix.diag ());
00157   save_type st = LS_FLOAT;
00158   if (matrix.length () > 4096) // FIXME -- make this configurable.
00159     {
00160       float max_val, min_val;
00161       if (m.all_integers (max_val, min_val))
00162         st = get_save_type (max_val, min_val);
00163     }
00164 
00165   const FloatComplex *mtmp = m.data ();
00166   write_floats (os, reinterpret_cast<const float *> (mtmp), st, 2 * m.numel ());
00167 
00168   return true;
00169 }
00170 
00171 bool
00172 octave_float_complex_diag_matrix::load_binary (std::istream& is, bool swap,
00173                                  oct_mach_info::float_format fmt)
00174 {
00175   int32_t r, c;
00176   char tmp;
00177   if (! (is.read (reinterpret_cast<char *> (&r), 4)
00178          && is.read (reinterpret_cast<char *> (&c), 4)
00179          && is.read (reinterpret_cast<char *> (&tmp), 1)))
00180     return false;
00181   if (swap)
00182     {
00183       swap_bytes<4> (&r);
00184       swap_bytes<4> (&c);
00185     }
00186 
00187   FloatComplexDiagMatrix m (r, c);
00188   FloatComplex *re = m.fortran_vec ();
00189   octave_idx_type len = m.length ();
00190   read_floats (is, reinterpret_cast<float *> (re),
00191                static_cast<save_type> (tmp), 2 * len, swap, fmt);
00192   if (error_state || ! is)
00193     return false;
00194   matrix = m;
00195 
00196   return true;
00197 }
00198 
00199 bool
00200 octave_float_complex_diag_matrix::chk_valid_scalar (const octave_value& val,
00201                                                     FloatComplex& x) const
00202 {
00203   bool retval = val.is_complex_scalar () || val.is_real_scalar ();
00204   if (retval)
00205     x = val.float_complex_value ();
00206   return retval;
00207 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines