GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-base-mat.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1998-2025 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
47template <typename MT>
48class OCTINTERP_TEMPLATE_API octave_base_matrix : public octave_base_value
49{
50public:
51
52 typedef MT object_type;
53
54 OCTINTERP_OVERRIDABLE_FUNC_API
56 : octave_base_value (), m_matrix (), m_typ (), m_idx_cache () { }
57
58 OCTINTERP_OVERRIDABLE_FUNC_API
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
67 OCTINTERP_OVERRIDABLE_FUNC_API
69 : octave_base_value (), m_matrix (m.m_matrix),
70 m_typ (m.m_typ ? new MatrixType (*m.m_typ) : nullptr),
71 m_idx_cache (m.m_idx_cache ? new octave::idx_vector (*m.m_idx_cache)
72 : nullptr)
73 { }
74
75 OCTINTERP_OVERRIDABLE_FUNC_API
76 ~octave_base_matrix () { clear_cached_info (); }
77
78 OCTINTERP_OVERRIDABLE_FUNC_API
79 std::size_t byte_size () const { return m_matrix.byte_size (); }
80
81 OCTINTERP_OVERRIDABLE_FUNC_API
82 octave_value squeeze () const { return MT (m_matrix.squeeze ()); }
83
84 OCTINTERP_OVERRIDABLE_FUNC_API
85 octave_value full_value () const { return m_matrix; }
86
87 OCTINTERP_OVERRIDABLE_FUNC_API
88 void maybe_economize () { m_matrix.maybe_economize (); }
89
90 // We don't need to override all three forms of subsref. The using
91 // declaration will avoid warnings about partially-overloaded virtual
92 // functions.
94
95 OCTINTERP_API octave_value
96 subsref (const std::string& type, const std::list<octave_value_list>& idx);
97
98 OCTINTERP_API octave_value_list
99 simple_subsref (char type, octave_value_list& idx, int nargout);
100
101 OCTINTERP_OVERRIDABLE_FUNC_API octave_value_list
102 subsref (const std::string& type, const std::list<octave_value_list>& idx, int)
103 { return subsref (type, idx); }
104
105 OCTINTERP_API octave_value
106 subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
107 const octave_value& rhs);
108
109 OCTINTERP_API octave_value
110 do_index_op (const octave_value_list& idx, bool resize_ok = false);
111
112 // FIXME: should we import the functions from the base class and
113 // overload them here, or should we use a different name so we don't
114 // have to do this? Without the using declaration or a name change,
115 // the base class functions will be hidden. That may be OK, but it
116 // can also cause some confusion.
118
119 OCTINTERP_API void assign (const octave_value_list& idx, const MT& rhs);
120
121 OCTINTERP_API void
122 assign (const octave_value_list& idx, typename MT::element_type rhs);
123
124 OCTINTERP_API void delete_elements (const octave_value_list& idx);
125
126 OCTINTERP_OVERRIDABLE_FUNC_API dim_vector
127 dims () const { return m_matrix.dims (); }
128
129 OCTINTERP_OVERRIDABLE_FUNC_API octave_idx_type
130 numel () const { return m_matrix.numel (); }
131
132 OCTINTERP_OVERRIDABLE_FUNC_API int
133 ndims () const { return m_matrix.ndims (); }
134
135 OCTINTERP_OVERRIDABLE_FUNC_API octave_idx_type
136 nnz () const { return m_matrix.nnz (); }
137
138 OCTINTERP_OVERRIDABLE_FUNC_API octave_value
139 reshape (const dim_vector& new_dims) const
140 { return MT (m_matrix.reshape (new_dims)); }
141
142 OCTINTERP_OVERRIDABLE_FUNC_API octave_value
143 permute (const Array<int>& vec, bool inv = false) const
144 { return MT (m_matrix.permute (vec, inv)); }
145
146 OCTINTERP_API octave_value
147 resize (const dim_vector& dv, bool fill = false) const;
148
149 OCTINTERP_OVERRIDABLE_FUNC_API octave_value all (int dim = 0) const
150 { return m_matrix.all (dim); }
151 OCTINTERP_OVERRIDABLE_FUNC_API octave_value any (int dim = 0) const
152 { return m_matrix.any (dim); }
153
154 OCTINTERP_OVERRIDABLE_FUNC_API MatrixType matrix_type () const
155 { return m_typ ? *m_typ : MatrixType (); }
156 OCTINTERP_API MatrixType matrix_type (const MatrixType& _typ) const;
157
158 OCTINTERP_OVERRIDABLE_FUNC_API octave_value
159 diag (octave_idx_type k = 0) const
160 { return octave_value (m_matrix.diag (k)); }
161
162 OCTINTERP_OVERRIDABLE_FUNC_API octave_value
164 { return octave_value (m_matrix.diag (m, n)); }
165
166 OCTINTERP_OVERRIDABLE_FUNC_API octave_value
167 sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const
168 { return octave_value (m_matrix.sort (dim, mode)); }
169 OCTINTERP_OVERRIDABLE_FUNC_API octave_value
171 sortmode mode = ASCENDING) const
172 { return octave_value (m_matrix.sort (sidx, dim, mode)); }
173
174 OCTINTERP_OVERRIDABLE_FUNC_API sortmode issorted (sortmode mode = UNSORTED) const
175 { return m_matrix.issorted (mode); }
176
177 OCTINTERP_OVERRIDABLE_FUNC_API Array<octave_idx_type>
179 { return m_matrix.sort_rows_idx (mode); }
180
181 OCTINTERP_OVERRIDABLE_FUNC_API sortmode
183 { return m_matrix.is_sorted_rows (mode); }
184
185 OCTINTERP_OVERRIDABLE_FUNC_API bool
186 is_matrix_type () const { return true; }
187
188 OCTINTERP_OVERRIDABLE_FUNC_API bool
189 is_full_num_matrix () const { return true; }
190
191 OCTINTERP_OVERRIDABLE_FUNC_API bool isnumeric () const { return true; }
192
193 OCTINTERP_OVERRIDABLE_FUNC_API bool is_defined () const { return true; }
194
195 OCTINTERP_OVERRIDABLE_FUNC_API bool is_constant () const { return true; }
196
197 OCTINTERP_API bool is_true () const;
198
199 OCTINTERP_API bool print_as_scalar () const;
200
201 OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
202
203 OCTINTERP_API void
204 print_info (std::ostream& os, const std::string& prefix) const;
205
206 OCTINTERP_API void short_disp (std::ostream& os) const;
207
208 OCTINTERP_API float_display_format get_edit_display_format () const;
209
210 OCTINTERP_API std::string
213
214 OCTINTERP_OVERRIDABLE_FUNC_API MT& matrix_ref ()
215 {
216 clear_cached_info ();
217 return m_matrix;
218 }
219
220 OCTINTERP_OVERRIDABLE_FUNC_API const MT& matrix_ref () const
221 {
222 return m_matrix;
223 }
224
225 OCTINTERP_API octave_value
227
228 OCTINTERP_API bool
230
231 // This function exists to support the MEX interface.
232 // You should not use it anywhere else.
233 OCTINTERP_OVERRIDABLE_FUNC_API const void *
234 mex_get_data () const { return m_matrix.data (); }
235
236protected:
237
239
240 OCTINTERP_OVERRIDABLE_FUNC_API octave::idx_vector
241 set_idx_cache (const octave::idx_vector& idx) const
242 {
243 delete m_idx_cache;
244 m_idx_cache = new octave::idx_vector (idx);
245 return idx;
246 }
247
248 OCTINTERP_OVERRIDABLE_FUNC_API void clear_cached_info () const
249 {
250 delete m_typ; m_typ = nullptr;
251 delete m_idx_cache; m_idx_cache = nullptr;
252 }
253
255 mutable octave::idx_vector *m_idx_cache;
256
257private:
258
259 // No assignment.
260
261 OCTINTERP_API octave_base_matrix& operator = (const octave_base_matrix&);
262};
263
264#endif
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
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
bool is_matrix_type() const
bool is_defined() const
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
octave_value permute(const Array< int > &vec, bool inv=false) const
std::size_t byte_size() const
Definition ov-base-mat.h:79
octave::idx_vector set_idx_cache(const octave::idx_vector &idx) const
octave_base_matrix(const octave_base_matrix &m)
Definition ov-base-mat.h:68
bool is_full_num_matrix() const
octave::idx_vector * m_idx_cache
octave_value diag(octave_idx_type k=0) const
octave_value diag(octave_idx_type m, octave_idx_type n) const
const void * mex_get_data() const
octave_value full_value() const
Definition ov-base-mat.h:85
octave_idx_type numel() const
sortmode issorted(sortmode mode=UNSORTED) const
octave_idx_type nnz() const
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
bool is_constant() const
MatrixType * m_typ
int ndims() const
octave_value any(int dim=0) const
dim_vector dims() const
octave_value squeeze() const
Definition ov-base-mat.h:82
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
octave_value all(int dim=0) const
MatrixType matrix_type() const
bool isnumeric() const
void clear_cached_info() const
octave_value reshape(const dim_vector &new_dims) const
const MT & matrix_ref() const
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition ov-base.cc:245
virtual void short_disp(std::ostream &os) const
Definition ov-base.h:761
virtual octave_value fast_elem_extract(octave_idx_type n) const
Definition ov-base.cc:1394
virtual MatrixType matrix_type() const
Definition ov-base.cc:407
virtual octave_value_list simple_subsref(char type, octave_value_list &idx, int nargout)
Definition ov-base.cc:237
virtual bool is_true() const
Definition ov-base.h:529
virtual octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition ov-base.cc:270
virtual float_display_format get_edit_display_format() const
Definition ov-base.cc:503
virtual void assign(const std::string &, const octave_value &)
Definition ov-base.h:363
virtual std::string edit_display(const float_display_format &, octave_idx_type, octave_idx_type) const
Definition ov-base.h:769
virtual bool print_as_scalar() const
Definition ov-base.h:746
virtual bool fast_elem_insert(octave_idx_type n, const octave_value &x)
Definition ov-base.cc:1400
virtual void print_info(std::ostream &os, const std::string &prefix) const
Definition ov-base.cc:509
virtual void print(std::ostream &os, bool pr_as_read_syntax=false)
Definition ov-base.cc:457
virtual octave_value resize(const dim_vector &, bool fill=false) const
Definition ov-base.cc:401
virtual octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
Definition ov-base.cc:294
friend class octave_value
Definition ov-base.h:278
octave_value any(int dim=0) const
Definition ov.h:687
octave_value all(int dim=0) const
Definition ov.h:684
F77_RET_T const F77_DBLE * x
sortmode
Definition oct-sort.h:97
@ UNSORTED
Definition oct-sort.h:97
@ ASCENDING
Definition oct-sort.h:97