GNU Octave  8.1.0
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-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_ov_base_mat_h)
27 #define octave_ov_base_mat_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstdlib>
32 
33 #include <iosfwd>
34 #include <string>
35 
36 #include "mx-base.h"
37 #include "str-vec.h"
38 #include "MatrixType.h"
39 
40 #include "error.h"
41 #include "ovl.h"
42 #include "ov-base.h"
43 #include "ov-typeinfo.h"
44 
45 // Real matrix values.
46 
47 template <typename MT>
48 class
49 OCTINTERP_API
51 {
52 public:
53 
54  typedef MT object_type;
55 
57  : octave_base_value (), m_matrix (), m_typ (), m_idx_cache () { }
58 
59  octave_base_matrix (const MT& m, const MatrixType& t = MatrixType ())
60  : octave_base_value (), m_matrix (m),
61  m_typ (t.is_known () ? new MatrixType (t) : nullptr), m_idx_cache ()
62  {
63  if (m_matrix.ndims () == 0)
64  m_matrix.resize (dim_vector (0, 0));
65  }
66 
68  : octave_base_value (), m_matrix (m.m_matrix),
69  m_typ (m.m_typ ? new MatrixType (*m.m_typ) : nullptr),
70  m_idx_cache (m.m_idx_cache ? new octave::idx_vector (*m.m_idx_cache)
71  : nullptr)
72  { }
73 
74  ~octave_base_matrix (void) { clear_cached_info (); }
75 
76  std::size_t byte_size (void) const { return m_matrix.byte_size (); }
77 
78  octave_value squeeze (void) const { return MT (m_matrix.squeeze ()); }
79 
80  octave_value full_value (void) const { return m_matrix; }
81 
82  void maybe_economize (void) { m_matrix.maybe_economize (); }
83 
84  // We don't need to override all three forms of subsref. The using
85  // declaration will avoid warnings about partially-overloaded virtual
86  // functions.
88 
89  OCTINTERP_API octave_value
90  subsref (const std::string& type, const std::list<octave_value_list>& idx);
91 
92  octave_value_list subsref (const std::string& type,
93  const std::list<octave_value_list>& idx, int)
94  { return subsref (type, idx); }
95 
96  OCTINTERP_API octave_value
97  subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
98  const octave_value& rhs);
99 
100  OCTINTERP_API octave_value
101  do_index_op (const octave_value_list& idx, bool resize_ok = false);
102 
103  // FIXME: should we import the functions from the base class and
104  // overload them here, or should we use a different name so we don't
105  // have to do this? Without the using declaration or a name change,
106  // the base class functions will be hidden. That may be OK, but it
107  // can also cause some confusion.
109 
110  OCTINTERP_API void assign (const octave_value_list& idx, const MT& rhs);
111 
112  OCTINTERP_API void
113  assign (const octave_value_list& idx, typename MT::element_type rhs);
114 
115  OCTINTERP_API void delete_elements (const octave_value_list& idx);
116 
117  dim_vector dims (void) const { return m_matrix.dims (); }
118 
119  octave_idx_type numel (void) const { return m_matrix.numel (); }
120 
121  int ndims (void) const { return m_matrix.ndims (); }
122 
123  octave_idx_type nnz (void) const { return m_matrix.nnz (); }
124 
125  octave_value reshape (const dim_vector& new_dims) const
126  { return MT (m_matrix.reshape (new_dims)); }
127 
128  octave_value permute (const Array<int>& vec, bool inv = false) const
129  { return MT (m_matrix.permute (vec, inv)); }
130 
131  octave_value resize (const dim_vector& dv, bool fill = false) const;
132 
133  octave_value all (int dim = 0) const { return m_matrix.all (dim); }
134  octave_value any (int dim = 0) const { return m_matrix.any (dim); }
135 
136  MatrixType matrix_type (void) const { return m_typ ? *m_typ : MatrixType (); }
137  MatrixType matrix_type (const MatrixType& _typ) const;
138 
140  { return octave_value (m_matrix.diag (k)); }
141 
143  { return octave_value (m_matrix.diag (m, n)); }
144 
146  { return octave_value (m_matrix.sort (dim, mode)); }
148  sortmode mode = ASCENDING) const
149  { return octave_value (m_matrix.sort (sidx, dim, mode)); }
150 
152  { return m_matrix.issorted (mode); }
153 
155  { return m_matrix.sort_rows_idx (mode); }
156 
158  { return m_matrix.is_sorted_rows (mode); }
159 
160  bool is_matrix_type (void) const { return true; }
161 
162  bool isnumeric (void) const { return true; }
163 
164  bool is_defined (void) const { return true; }
165 
166  bool is_constant (void) const { return true; }
167 
168  OCTINTERP_API bool is_true (void) const;
169 
170  OCTINTERP_API bool print_as_scalar (void) const;
171 
172  OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
173 
174  OCTINTERP_API void
175  print_info (std::ostream& os, const std::string& prefix) const;
176 
177  OCTINTERP_API void short_disp (std::ostream& os) const;
178 
179  OCTINTERP_API float_display_format get_edit_display_format (void) const;
180 
181  OCTINTERP_API std::string
182  edit_display (const float_display_format& fmt,
183  octave_idx_type i, octave_idx_type j) const;
184 
185  MT& matrix_ref (void)
186  {
187  clear_cached_info ();
188  return m_matrix;
189  }
190 
191  const MT& matrix_ref (void) const
192  {
193  return m_matrix;
194  }
195 
196  OCTINTERP_API octave_value
197  fast_elem_extract (octave_idx_type n) const;
198 
199  OCTINTERP_API bool
200  fast_elem_insert (octave_idx_type n, const octave_value& x);
201 
202  // This function exists to support the MEX interface.
203  // You should not use it anywhere else.
204  const void * mex_get_data (void) const { return m_matrix.data (); }
205 
206 protected:
207 
209 
211  {
212  delete m_idx_cache;
213  m_idx_cache = (idx ? new octave::idx_vector (idx) : nullptr);
214  return idx;
215  }
216 
217  void clear_cached_info (void) const
218  {
219  delete m_typ; m_typ = nullptr;
220  delete m_idx_cache; m_idx_cache = nullptr;
221  }
222 
223  mutable MatrixType *m_typ;
225 
226 private:
227 
228  // No assignment.
229 
230  OCTINTERP_API octave_base_matrix& operator = (const octave_base_matrix&);
231 };
232 
233 #endif
OCTARRAY_API Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
Definition: Array-base.cc:2081
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
MatrixType matrix_type(void) const
Definition: ov-base-mat.h:136
octave_base_matrix(const MT &m, const MatrixType &t=MatrixType())
Definition: ov-base-mat.h:59
void maybe_economize(void)
Definition: ov-base-mat.h:82
int ndims(void) const
Definition: ov-base-mat.h:121
octave_value sort(Array< octave_idx_type > &sidx, octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:147
bool isnumeric(void) const
Definition: ov-base-mat.h:162
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition: ov-base-mat.h:92
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-base-mat.h:128
octave_base_matrix(void)
Definition: ov-base-mat.h:56
const void * mex_get_data(void) const
Definition: ov-base-mat.h:204
octave_idx_type numel(void) const
Definition: ov-base-mat.h:119
std::size_t byte_size(void) const
Definition: ov-base-mat.h:76
octave::idx_vector set_idx_cache(const octave::idx_vector &idx) const
Definition: ov-base-mat.h:210
octave_base_matrix(const octave_base_matrix &m)
Definition: ov-base-mat.h:67
octave::idx_vector * m_idx_cache
Definition: ov-base-mat.h:224
octave_value diag(octave_idx_type k=0) const
Definition: ov-base-mat.h:139
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-base-mat.h:142
bool is_constant(void) const
Definition: ov-base-mat.h:166
bool is_matrix_type(void) const
Definition: ov-base-mat.h:160
const MT & matrix_ref(void) const
Definition: ov-base-mat.h:191
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-base-mat.h:151
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-base-mat.h:157
octave_value full_value(void) const
Definition: ov-base-mat.h:80
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:145
MatrixType * m_typ
Definition: ov-base-mat.h:223
octave_value any(int dim=0) const
Definition: ov-base-mat.h:134
octave_value squeeze(void) const
Definition: ov-base-mat.h:78
octave_idx_type nnz(void) const
Definition: ov-base-mat.h:123
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:154
octave_value all(int dim=0) const
Definition: ov-base-mat.h:133
~octave_base_matrix(void)
Definition: ov-base-mat.h:74
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-base-mat.h:125
bool is_defined(void) const
Definition: ov-base-mat.h:164
MT & matrix_ref(void)
Definition: ov-base-mat.h:185
void clear_cached_info(void) const
Definition: ov-base-mat.h:217
dim_vector dims(void) const
Definition: ov-base-mat.h:117
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:200
virtual void assign(const std::string &, const octave_value &)
Definition: ov-base.h:340
virtual octave_value resize(const dim_vector &, bool fill=false) const
Definition: ov-base.cc:346
octave_value any(int dim=0) const
Definition: ov.h:732
octave_value all(int dim=0) const
Definition: ov.h:729
octave::idx_vector idx_vector
Definition: idx-vector.h:1039
F77_RET_T const F77_DBLE * x
bool is_true(const std::string &s)
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
sortmode
Definition: oct-sort.h:97
@ UNSORTED
Definition: oct-sort.h:97
@ ASCENDING
Definition: oct-sort.h:97
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
static float_display_format get_edit_display_format(const octave_value &val)