GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-base-mat.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1998-2013 John W. Eaton
4 Copyright (C) 2009-2010 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if !defined (octave_ov_base_mat_h)
25 #define octave_ov_base_mat_h 1
26 
27 #include <cstdlib>
28 
29 #include <iosfwd>
30 #include <string>
31 
32 #include "mx-base.h"
33 #include "str-vec.h"
34 #include "MatrixType.h"
35 
36 #include "error.h"
37 #include "oct-obj.h"
38 #include "ov-base.h"
39 #include "ov-typeinfo.h"
40 
41 class tree_walker;
42 
43 // Real matrix values.
44 
45 template <class MT>
46 class
48 {
49 public:
50 
52  : octave_base_value (), matrix (), typ (), idx_cache () { }
53 
54  octave_base_matrix (const MT& m, const MatrixType& t = MatrixType ())
55  : octave_base_value (), matrix (m),
56  typ (t.is_known () ? new MatrixType (t) : 0), idx_cache ()
57  {
58  if (matrix.ndims () == 0)
59  matrix.resize (dim_vector (0, 0));
60  }
61 
63  : octave_base_value (), matrix (m.matrix),
64  typ (m.typ ? new MatrixType (*m.typ) : 0),
65  idx_cache (m.idx_cache ? new idx_vector (*m.idx_cache) : 0)
66  { }
67 
68  ~octave_base_matrix (void) { clear_cached_info (); }
69 
70  size_t byte_size (void) const { return matrix.byte_size (); }
71 
72  octave_value squeeze (void) const { return MT (matrix.squeeze ()); }
73 
74  octave_value full_value (void) const { return matrix; }
75 
76  void maybe_economize (void) { matrix.maybe_economize (); }
77 
78  octave_value subsref (const std::string& type,
79  const std::list<octave_value_list>& idx);
80 
81  octave_value_list subsref (const std::string& type,
82  const std::list<octave_value_list>& idx, int)
83  { return subsref (type, idx); }
84 
85  octave_value subsasgn (const std::string& type,
86  const std::list<octave_value_list>& idx,
87  const octave_value& rhs);
88 
89  octave_value do_index_op (const octave_value_list& idx,
90  bool resize_ok = false);
91 
92  octave_value_list do_multi_index_op (int, const octave_value_list& idx)
93  { return do_index_op (idx); }
94 
95  void assign (const octave_value_list& idx, const MT& rhs);
96 
97  void assign (const octave_value_list& idx, typename MT::element_type rhs);
98 
99  void delete_elements (const octave_value_list& idx);
100 
101  dim_vector dims (void) const { return matrix.dims (); }
102 
103  octave_idx_type numel (void) const { return matrix.numel (); }
104 
105  int ndims (void) const { return matrix.ndims (); }
106 
107  octave_idx_type nnz (void) const { return matrix.nnz (); }
108 
109  octave_value reshape (const dim_vector& new_dims) const
110  { return MT (matrix.reshape (new_dims)); }
111 
112  octave_value permute (const Array<int>& vec, bool inv = false) const
113  { return MT (matrix.permute (vec, inv)); }
114 
115  octave_value resize (const dim_vector& dv, bool fill = false) const;
116 
117  octave_value all (int dim = 0) const { return matrix.all (dim); }
118  octave_value any (int dim = 0) const { return matrix.any (dim); }
119 
120  MatrixType matrix_type (void) const { return typ ? *typ : MatrixType (); }
121  MatrixType matrix_type (const MatrixType& _typ) const;
122 
123  octave_value diag (octave_idx_type k = 0) const
124  { return octave_value (matrix.diag (k)); }
125 
127  { return octave_value (matrix.diag (m, n)); }
128 
129  octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const
130  { return octave_value (matrix.sort (dim, mode)); }
132  sortmode mode = ASCENDING) const
133  { return octave_value (matrix.sort (sidx, dim, mode)); }
134 
135  sortmode is_sorted (sortmode mode = UNSORTED) const
136  { return matrix.is_sorted (mode); }
137 
138  Array<octave_idx_type> sort_rows_idx (sortmode mode = ASCENDING) const
139  { return matrix.sort_rows_idx (mode); }
140 
141  sortmode is_sorted_rows (sortmode mode = UNSORTED) const
142  { return matrix.is_sorted_rows (mode); }
143 
144  bool is_matrix_type (void) const { return true; }
145 
146  bool is_numeric_type (void) const { return true; }
147 
148  bool is_defined (void) const { return true; }
149 
150  bool is_constant (void) const { return true; }
151 
152  bool is_true (void) const;
153 
154  bool print_as_scalar (void) const;
155 
156  void print (std::ostream& os, bool pr_as_read_syntax = false) const;
157 
158  void print_info (std::ostream& os, const std::string& prefix) const;
159 
160  void short_disp (std::ostream& os) const;
161 
162  MT& matrix_ref (void)
163  {
164  clear_cached_info ();
165  return matrix;
166  }
167 
168  const MT& matrix_ref (void) const
169  {
170  return matrix;
171  }
172 
174  fast_elem_extract (octave_idx_type n) const;
175 
176  bool
177  fast_elem_insert (octave_idx_type n, const octave_value& x);
178 
179 protected:
180 
181  MT matrix;
182 
183  idx_vector set_idx_cache (const idx_vector& idx) const
184  {
185  delete idx_cache;
186  idx_cache = idx ? new idx_vector (idx) : 0;
187  return idx;
188  }
189 
190  void clear_cached_info (void) const
191  {
192  delete typ; typ = 0;
193  delete idx_cache; idx_cache = 0;
194  }
195 
196  mutable MatrixType *typ;
198 
199 private:
200 
201  // No assignment.
202 
204 };
205 
206 #endif