ov-base-mat.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1998-2012 John W. Eaton
00004 Copyright (C) 2009-2010 VZLU Prague
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #if !defined (octave_base_matrix_h)
00025 #define octave_base_matrix_h 1
00026 
00027 #include <cstdlib>
00028 
00029 #include <iosfwd>
00030 #include <string>
00031 
00032 #include "mx-base.h"
00033 #include "str-vec.h"
00034 #include "MatrixType.h"
00035 
00036 #include "error.h"
00037 #include "oct-obj.h"
00038 #include "ov-base.h"
00039 #include "ov-typeinfo.h"
00040 
00041 class tree_walker;
00042 
00043 // Real matrix values.
00044 
00045 template <class MT>
00046 class
00047 octave_base_matrix : public octave_base_value
00048 {
00049 public:
00050 
00051   octave_base_matrix (void)
00052     : octave_base_value (), matrix (), typ (), idx_cache () { }
00053 
00054   octave_base_matrix (const MT& m, const MatrixType& t = MatrixType ())
00055     : octave_base_value (), matrix (m),
00056       typ (t.is_known () ? new MatrixType(t) : 0), idx_cache ()
00057   {
00058     if (matrix.ndims () == 0)
00059       matrix.resize (dim_vector (0, 0));
00060   }
00061 
00062   octave_base_matrix (const octave_base_matrix& m)
00063     : octave_base_value (), matrix (m.matrix),
00064       typ (m.typ ? new MatrixType (*m.typ) : 0),
00065       idx_cache (m.idx_cache ? new idx_vector (*m.idx_cache) : 0)
00066     { }
00067 
00068   ~octave_base_matrix (void) { clear_cached_info (); }
00069 
00070   size_t byte_size (void) const { return matrix.byte_size (); }
00071 
00072   octave_value squeeze (void) const { return MT (matrix.squeeze ()); }
00073 
00074   octave_value full_value (void) const { return matrix; }
00075 
00076   void maybe_economize (void) { matrix.maybe_economize (); }
00077 
00078   octave_value subsref (const std::string& type,
00079                         const std::list<octave_value_list>& idx);
00080 
00081   octave_value_list subsref (const std::string& type,
00082                              const std::list<octave_value_list>& idx, int)
00083     { return subsref (type, idx); }
00084 
00085   octave_value subsasgn (const std::string& type,
00086                          const std::list<octave_value_list>& idx,
00087                          const octave_value& rhs);
00088 
00089   octave_value do_index_op (const octave_value_list& idx,
00090                             bool resize_ok = false);
00091 
00092   octave_value_list do_multi_index_op (int, const octave_value_list& idx)
00093     { return do_index_op (idx); }
00094 
00095   void assign (const octave_value_list& idx, const MT& rhs);
00096 
00097   void assign (const octave_value_list& idx, typename MT::element_type rhs);
00098 
00099   void delete_elements (const octave_value_list& idx);
00100 
00101   dim_vector dims (void) const { return matrix.dims (); }
00102 
00103   octave_idx_type numel (void) const { return matrix.numel (); }
00104 
00105   int ndims (void) const { return matrix.ndims (); }
00106 
00107   octave_idx_type nnz (void) const { return matrix.nnz (); }
00108 
00109   octave_value reshape (const dim_vector& new_dims) const
00110     { return MT (matrix.reshape (new_dims)); }
00111 
00112   octave_value permute (const Array<int>& vec, bool inv = false) const
00113     { return MT (matrix.permute (vec, inv)); }
00114 
00115   octave_value resize (const dim_vector& dv, bool fill = false) const;
00116 
00117   octave_value all (int dim = 0) const { return matrix.all (dim); }
00118   octave_value any (int dim = 0) const { return matrix.any (dim); }
00119 
00120   MatrixType matrix_type (void) const { return typ ? *typ : MatrixType (); }
00121   MatrixType matrix_type (const MatrixType& _typ) const;
00122 
00123   octave_value diag (octave_idx_type k = 0) const
00124     { return octave_value (matrix.diag (k)); }
00125 
00126   octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const
00127     { return octave_value (matrix.sort (dim, mode)); }
00128   octave_value sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0,
00129                      sortmode mode = ASCENDING) const
00130     { return octave_value (matrix.sort (sidx, dim, mode)); }
00131 
00132   sortmode is_sorted (sortmode mode = UNSORTED) const
00133     { return matrix.is_sorted (mode); }
00134 
00135   Array<octave_idx_type> sort_rows_idx (sortmode mode = ASCENDING) const
00136     { return matrix.sort_rows_idx (mode); }
00137 
00138   sortmode is_sorted_rows (sortmode mode = UNSORTED) const
00139     { return matrix.is_sorted_rows (mode); }
00140 
00141   bool is_matrix_type (void) const { return true; }
00142 
00143   bool is_numeric_type (void) const { return true; }
00144 
00145   bool is_defined (void) const { return true; }
00146 
00147   bool is_constant (void) const { return true; }
00148 
00149   bool is_true (void) const;
00150 
00151   bool print_as_scalar (void) const;
00152 
00153   void print (std::ostream& os, bool pr_as_read_syntax = false) const;
00154 
00155   void print_info (std::ostream& os, const std::string& prefix) const;
00156 
00157   MT& matrix_ref (void)
00158     {
00159       clear_cached_info ();
00160       return matrix;
00161     }
00162 
00163   const MT& matrix_ref (void) const
00164     {
00165       return matrix;
00166     }
00167 
00168   octave_value
00169   fast_elem_extract (octave_idx_type n) const;
00170 
00171   bool
00172   fast_elem_insert (octave_idx_type n, const octave_value& x);
00173 
00174 protected:
00175 
00176   MT matrix;
00177 
00178   idx_vector set_idx_cache (const idx_vector& idx) const
00179     {
00180       delete idx_cache;
00181       idx_cache = idx ? new idx_vector (idx) : 0;
00182       return idx;
00183     }
00184 
00185   void clear_cached_info (void) const
00186     {
00187       delete typ; typ = 0;
00188       delete idx_cache; idx_cache = 0;
00189     }
00190 
00191   mutable MatrixType *typ;
00192   mutable idx_vector *idx_cache;
00193 
00194 private:
00195 
00196   // No assignment.
00197 
00198   octave_base_matrix& operator = (const octave_base_matrix&);
00199 };
00200 
00201 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines