GNU Octave  9.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-2024 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 () { clear_cached_info (); }
75 
76  std::size_t byte_size () const { return m_matrix.byte_size (); }
77 
78  octave_value squeeze () const { return MT (m_matrix.squeeze ()); }
79 
80  octave_value full_value () const { return m_matrix; }
81 
82  void maybe_economize () { 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 
93  simple_subsref (char type, octave_value_list& idx, int nargout);
94 
95  octave_value_list subsref (const std::string& type,
96  const std::list<octave_value_list>& idx, int)
97  { return subsref (type, idx); }
98 
99  OCTINTERP_API octave_value
100  subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
101  const octave_value& rhs);
102 
103  OCTINTERP_API octave_value
104  do_index_op (const octave_value_list& idx, bool resize_ok = false);
105 
106  // FIXME: should we import the functions from the base class and
107  // overload them here, or should we use a different name so we don't
108  // have to do this? Without the using declaration or a name change,
109  // the base class functions will be hidden. That may be OK, but it
110  // can also cause some confusion.
112 
113  OCTINTERP_API void assign (const octave_value_list& idx, const MT& rhs);
114 
115  OCTINTERP_API void
116  assign (const octave_value_list& idx, typename MT::element_type rhs);
117 
118  OCTINTERP_API void delete_elements (const octave_value_list& idx);
119 
120  dim_vector dims () const { return m_matrix.dims (); }
121 
122  octave_idx_type numel () const { return m_matrix.numel (); }
123 
124  int ndims () const { return m_matrix.ndims (); }
125 
126  octave_idx_type nnz () const { return m_matrix.nnz (); }
127 
128  octave_value reshape (const dim_vector& new_dims) const
129  { return MT (m_matrix.reshape (new_dims)); }
130 
131  octave_value permute (const Array<int>& vec, bool inv = false) const
132  { return MT (m_matrix.permute (vec, inv)); }
133 
134  octave_value resize (const dim_vector& dv, bool fill = false) const;
135 
136  octave_value all (int dim = 0) const { return m_matrix.all (dim); }
137  octave_value any (int dim = 0) const { return m_matrix.any (dim); }
138 
139  MatrixType matrix_type () const { return m_typ ? *m_typ : MatrixType (); }
140  MatrixType matrix_type (const MatrixType& _typ) const;
141 
143  { return octave_value (m_matrix.diag (k)); }
144 
146  { return octave_value (m_matrix.diag (m, n)); }
147 
149  { return octave_value (m_matrix.sort (dim, mode)); }
151  sortmode mode = ASCENDING) const
152  { return octave_value (m_matrix.sort (sidx, dim, mode)); }
153 
155  { return m_matrix.issorted (mode); }
156 
158  { return m_matrix.sort_rows_idx (mode); }
159 
161  { return m_matrix.is_sorted_rows (mode); }
162 
163  bool is_matrix_type () const { return true; }
164 
165  bool is_full_num_matrix () const { return true; }
166 
167  bool isnumeric () const { return true; }
168 
169  bool is_defined () const { return true; }
170 
171  bool is_constant () const { return true; }
172 
173  OCTINTERP_API bool is_true () const;
174 
175  OCTINTERP_API bool print_as_scalar () const;
176 
177  OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
178 
179  OCTINTERP_API void
180  print_info (std::ostream& os, const std::string& prefix) const;
181 
182  OCTINTERP_API void short_disp (std::ostream& os) const;
183 
184  OCTINTERP_API float_display_format get_edit_display_format () const;
185 
186  OCTINTERP_API std::string
187  edit_display (const float_display_format& fmt,
188  octave_idx_type i, octave_idx_type j) const;
189 
190  MT& matrix_ref ()
191  {
192  clear_cached_info ();
193  return m_matrix;
194  }
195 
196  const MT& matrix_ref () const
197  {
198  return m_matrix;
199  }
200 
201  OCTINTERP_API octave_value
202  fast_elem_extract (octave_idx_type n) const;
203 
204  OCTINTERP_API bool
205  fast_elem_insert (octave_idx_type n, const octave_value& x);
206 
207  // This function exists to support the MEX interface.
208  // You should not use it anywhere else.
209  const void * mex_get_data () const { return m_matrix.data (); }
210 
211 protected:
212 
214 
216  {
217  delete m_idx_cache;
218  m_idx_cache = new octave::idx_vector (idx);
219  return idx;
220  }
221 
222  void clear_cached_info () const
223  {
224  delete m_typ; m_typ = nullptr;
225  delete m_idx_cache; m_idx_cache = nullptr;
226  }
227 
228  mutable MatrixType *m_typ;
230 
231 private:
232 
233  // No assignment.
234 
235  OCTINTERP_API octave_base_matrix& operator = (const octave_base_matrix&);
236 };
237 
238 #endif
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
Definition: Array-base.cc:2079
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_base_matrix(const MT &m, const MatrixType &t=MatrixType())
Definition: ov-base-mat.h:59
octave_value sort(Array< octave_idx_type > &sidx, octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:150
bool is_matrix_type() const
Definition: ov-base-mat.h:163
bool is_defined() const
Definition: ov-base-mat.h:169
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition: ov-base-mat.h:95
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-base-mat.h:131
void maybe_economize()
Definition: ov-base-mat.h:82
std::size_t byte_size() const
Definition: ov-base-mat.h:76
octave::idx_vector set_idx_cache(const octave::idx_vector &idx) const
Definition: ov-base-mat.h:215
octave_base_matrix(const octave_base_matrix &m)
Definition: ov-base-mat.h:67
bool is_full_num_matrix() const
Definition: ov-base-mat.h:165
octave::idx_vector * m_idx_cache
Definition: ov-base-mat.h:229
octave_value diag(octave_idx_type k=0) const
Definition: ov-base-mat.h:142
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-base-mat.h:145
octave_value full_value() const
Definition: ov-base-mat.h:80
octave_idx_type numel() const
Definition: ov-base-mat.h:122
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-base-mat.h:154
const void * mex_get_data() const
Definition: ov-base-mat.h:209
octave_idx_type nnz() const
Definition: ov-base-mat.h:126
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-base-mat.h:160
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:148
bool is_constant() const
Definition: ov-base-mat.h:171
MatrixType * m_typ
Definition: ov-base-mat.h:228
int ndims() const
Definition: ov-base-mat.h:124
octave_value any(int dim=0) const
Definition: ov-base-mat.h:137
dim_vector dims() const
Definition: ov-base-mat.h:120
octave_value squeeze() const
Definition: ov-base-mat.h:78
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:157
octave_value all(int dim=0) const
Definition: ov-base-mat.h:136
MatrixType matrix_type() const
Definition: ov-base-mat.h:139
const MT & matrix_ref() const
Definition: ov-base-mat.h:196
bool isnumeric() const
Definition: ov-base-mat.h:167
void clear_cached_info() const
Definition: ov-base-mat.h:222
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-base-mat.h:128
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:248
virtual void assign(const std::string &, const octave_value &)
Definition: ov-base.h:354
virtual octave_value resize(const dim_vector &, bool fill=false) const
Definition: ov-base.cc:404
octave_value any(int dim=0) const
Definition: ov.h:687
octave_value all(int dim=0) const
Definition: ov.h:684
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
F77_RET_T const F77_DBLE * x
bool is_true(const std::string &s)
Definition: mkoctfile.cc:604
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761
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()) ? '\'' :'"'))