GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-base-mat.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1998-2018 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
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License 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 <https://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 "octave-config.h"
28 
29 #include <cstdlib>
30 
31 #include <iosfwd>
32 #include <string>
33 
34 #include "mx-base.h"
35 #include "str-vec.h"
36 #include "MatrixType.h"
37 
38 #include "error.h"
39 #include "ovl.h"
40 #include "ov-base.h"
41 #include "ov-typeinfo.h"
42 
43 // Real matrix values.
44 
45 template <typename 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) : nullptr), idx_cache ()
57  {
58  if (matrix.ndims () == 0)
59  matrix.resize (dim_vector (0, 0));
60  }
61 
64  typ (m.typ ? new MatrixType (*m.typ) : nullptr),
65  idx_cache (m.idx_cache ? new idx_vector (*m.idx_cache) : nullptr)
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 
79  const std::list<octave_value_list>& idx);
80 
82  const std::list<octave_value_list>& idx, int)
83  { return subsref (type, idx); }
84 
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  // FIXME: should we import the functions from the base class and
93  // overload them here, or should we use a different name so we don't
94  // have to do this? Without the using declaration or a name change,
95  // the base class functions will be hidden. That may be OK, but it
96  // can also cause some confusion.
98 
99  void assign (const octave_value_list& idx, const MT& rhs);
100 
101  void assign (const octave_value_list& idx, typename MT::element_type rhs);
102 
103  void delete_elements (const octave_value_list& idx);
104 
105  dim_vector dims (void) const { return matrix.dims (); }
106 
107  octave_idx_type numel (void) const { return matrix.numel (); }
108 
109  int ndims (void) const { return matrix.ndims (); }
110 
111  octave_idx_type nnz (void) const { return matrix.nnz (); }
112 
113  octave_value reshape (const dim_vector& new_dims) const
114  { return MT (matrix.reshape (new_dims)); }
115 
116  octave_value permute (const Array<int>& vec, bool inv = false) const
117  { return MT (matrix.permute (vec, inv)); }
118 
119  octave_value resize (const dim_vector& dv, bool fill = false) const;
120 
121  octave_value all (int dim = 0) const { return matrix.all (dim); }
122  octave_value any (int dim = 0) const { return matrix.any (dim); }
123 
124  MatrixType matrix_type (void) const { return typ ? *typ : MatrixType (); }
125  MatrixType matrix_type (const MatrixType& _typ) const;
126 
128  { return octave_value (matrix.diag (k)); }
129 
131  { return octave_value (matrix.diag (m, n)); }
132 
134  { return octave_value (matrix.sort (dim, mode)); }
136  sortmode mode = ASCENDING) const
137  { return octave_value (matrix.sort (sidx, dim, mode)); }
138 
140  { return matrix.issorted (mode); }
141 
143  { return matrix.sort_rows_idx (mode); }
144 
146  { return matrix.is_sorted_rows (mode); }
147 
148  bool is_matrix_type (void) const { return true; }
149 
150  bool isnumeric (void) const { return true; }
151 
152  bool is_defined (void) const { return true; }
153 
154  bool is_constant (void) const { return true; }
155 
156  bool is_true (void) const;
157 
158  bool print_as_scalar (void) const;
159 
160  void print (std::ostream& os, bool pr_as_read_syntax = false);
161 
162  void print_info (std::ostream& os, const std::string& prefix) const;
163 
164  void short_disp (std::ostream& os) const;
165 
167 
168  std::string edit_display (const float_display_format& fmt,
170 
171  MT& matrix_ref (void)
172  {
173  clear_cached_info ();
174  return matrix;
175  }
176 
177  const MT& matrix_ref (void) const
178  {
179  return matrix;
180  }
181 
183  fast_elem_extract (octave_idx_type n) const;
184 
185  bool
186  fast_elem_insert (octave_idx_type n, const octave_value& x);
187 
188 protected:
189 
190  MT matrix;
191 
193  {
194  delete idx_cache;
195  idx_cache = (idx ? new idx_vector (idx) : nullptr);
196  return idx;
197  }
198 
199  void clear_cached_info (void) const
200  {
201  delete typ; typ = nullptr;
202  delete idx_cache; idx_cache = nullptr;
203  }
204 
205  mutable MatrixType *typ;
207 
208 private:
209 
210  // No assignment.
211 
212  octave_base_matrix& operator = (const octave_base_matrix&);
213 };
214 
215 #endif
octave_idx_type nnz(void) const
Definition: ov-base-mat.h:111
octave_value any(int dim=0) const
Definition: ov-base-mat.h:122
characters Given a string matrix
Definition: hex2num.cc:155
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-base-mat.h:116
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-base-mat.h:130
sortmode
Definition: oct-sort.h:105
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6348
idx subsref(val, idx) esult
Definition: ov.cc:3065
void maybe_economize(void)
Definition: ov-base-mat.h:76
octave_value all(int dim=0) const
Definition: ov-base-mat.h:121
virtual void assign(const std::string &, const octave_value &)
Definition: ov-base.h:300
for large enough k
Definition: lu.cc:617
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition: ov-base-mat.h:81
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:997
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:133
~octave_base_matrix(void)
Definition: ov-base-mat.h:68
bool isnumeric(void) const
Definition: ov-base-mat.h:150
octave_base_matrix(const octave_base_matrix &m)
Definition: ov-base-mat.h:62
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-base-mat.h:145
octave_value squeeze(void) const
Definition: ov-base-mat.h:72
bool is_constant(void) const
Definition: ov-base-mat.h:154
bool is_matrix_type(void) const
Definition: ov-base-mat.h:148
MatrixType * typ
Definition: ov-base-mat.h:205
octave_value diag(octave_idx_type k=0) const
Definition: ov-base-mat.h:127
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-base-mat.h:139
octave_value full_value(void) const
Definition: ov-base-mat.h:74
bool is_true(const std::string &s)
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-base-mat.h:113
idx type
Definition: ov.cc:3114
octave_base_matrix(const MT &m, const MatrixType &t=MatrixType())
Definition: ov-base-mat.h:54
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
int ndims(void) const
Definition: ov-base-mat.h:109
MT & matrix_ref(void)
Definition: ov-base-mat.h:171
size_t byte_size(void) const
Definition: ov-base-mat.h:70
idx_vector * idx_cache
Definition: ov-base-mat.h:206
void clear_cached_info(void) const
Definition: ov-base-mat.h:199
dim_vector dims(void) const
Definition: ov-base-mat.h:105
MatrixType matrix_type(void) const
Definition: ov-base-mat.h:124
idx subsasgn(val, idx, 0) esult
Definition: ov.cc:3114
octave_idx_type numel(void) const
Definition: ov-base-mat.h:107
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:142
idx_vector set_idx_cache(const idx_vector &idx) const
Definition: ov-base-mat.h:192
for i
Definition: data.cc:5264
bool is_defined(void) const
Definition: ov-base-mat.h:152
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
octave_base_matrix(void)
Definition: ov-base-mat.h:51
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
dim_vector dv
Definition: sub2ind.cc:263
octave::stream os
Definition: file-io.cc:627
octave_value sort(Array< octave_idx_type > &sidx, octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:135
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE * x
const MT & matrix_ref(void) const
Definition: ov-base-mat.h:177
static float_display_format get_edit_display_format(const octave_value &val)