GNU Octave 7.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-2022 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
49OCTINTERP_API
51{
52public:
53
54 typedef MT object_type;
55
57 : octave_base_value (), matrix (), typ (), idx_cache () { }
58
59 octave_base_matrix (const MT& m, const MatrixType& t = MatrixType ())
60 : octave_base_value (), matrix (m),
61 typ (t.is_known () ? new MatrixType (t) : nullptr), idx_cache ()
62 {
63 if (matrix.ndims () == 0)
64 matrix.resize (dim_vector (0, 0));
65 }
66
68 : octave_base_value (), matrix (m.matrix),
69 typ (m.typ ? new MatrixType (*m.typ) : nullptr),
70 idx_cache (m.idx_cache ? new octave::idx_vector (*m.idx_cache) : nullptr)
71 { }
72
73 ~octave_base_matrix (void) { clear_cached_info (); }
74
75 std::size_t byte_size (void) const { return matrix.byte_size (); }
76
77 octave_value squeeze (void) const { return MT (matrix.squeeze ()); }
78
79 octave_value full_value (void) const { return matrix; }
80
81 void maybe_economize (void) { matrix.maybe_economize (); }
82
83 // We don't need to override all three forms of subsref. The using
84 // declaration will avoid warnings about partially-overloaded virtual
85 // functions.
87
88 OCTINTERP_API octave_value
89 subsref (const std::string& type, const std::list<octave_value_list>& idx);
90
91 octave_value_list subsref (const std::string& type,
92 const std::list<octave_value_list>& idx, int)
93 { return subsref (type, idx); }
94
95 OCTINTERP_API octave_value
96 subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
97 const octave_value& rhs);
98
99 OCTINTERP_API octave_value
100 do_index_op (const octave_value_list& idx, bool resize_ok = false);
101
102 // FIXME: should we import the functions from the base class and
103 // overload them here, or should we use a different name so we don't
104 // have to do this? Without the using declaration or a name change,
105 // the base class functions will be hidden. That may be OK, but it
106 // can also cause some confusion.
108
109 OCTINTERP_API void assign (const octave_value_list& idx, const MT& rhs);
110
111 OCTINTERP_API void
112 assign (const octave_value_list& idx, typename MT::element_type rhs);
113
114 OCTINTERP_API void delete_elements (const octave_value_list& idx);
115
116 dim_vector dims (void) const { return matrix.dims (); }
117
118 octave_idx_type numel (void) const { return matrix.numel (); }
119
120 int ndims (void) const { return matrix.ndims (); }
121
122 octave_idx_type nnz (void) const { return matrix.nnz (); }
123
124 octave_value reshape (const dim_vector& new_dims) const
125 { return MT (matrix.reshape (new_dims)); }
126
127 octave_value permute (const Array<int>& vec, bool inv = false) const
128 { return MT (matrix.permute (vec, inv)); }
129
130 octave_value resize (const dim_vector& dv, bool fill = false) const;
131
132 octave_value all (int dim = 0) const { return matrix.all (dim); }
133 octave_value any (int dim = 0) const { return matrix.any (dim); }
134
135 MatrixType matrix_type (void) const { return typ ? *typ : MatrixType (); }
136 MatrixType matrix_type (const MatrixType& _typ) const;
137
139 { return octave_value (matrix.diag (k)); }
140
142 { return octave_value (matrix.diag (m, n)); }
143
145 { return octave_value (matrix.sort (dim, mode)); }
147 sortmode mode = ASCENDING) const
148 { return octave_value (matrix.sort (sidx, dim, mode)); }
149
151 { return matrix.issorted (mode); }
152
154 { return matrix.sort_rows_idx (mode); }
155
157 { return matrix.is_sorted_rows (mode); }
158
159 bool is_matrix_type (void) const { return true; }
160
161 bool isnumeric (void) const { return true; }
162
163 bool is_defined (void) const { return true; }
164
165 bool is_constant (void) const { return true; }
166
167 OCTINTERP_API bool is_true (void) const;
168
169 OCTINTERP_API bool print_as_scalar (void) const;
170
171 OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
172
173 OCTINTERP_API void
174 print_info (std::ostream& os, const std::string& prefix) const;
175
176 OCTINTERP_API void short_disp (std::ostream& os) const;
177
178 OCTINTERP_API float_display_format get_edit_display_format (void) const;
179
180 OCTINTERP_API std::string
181 edit_display (const float_display_format& fmt,
183
184 MT& matrix_ref (void)
185 {
186 clear_cached_info ();
187 return matrix;
188 }
189
190 const MT& matrix_ref (void) const
191 {
192 return matrix;
193 }
194
195 OCTINTERP_API octave_value
196 fast_elem_extract (octave_idx_type n) const;
197
198 OCTINTERP_API bool
199 fast_elem_insert (octave_idx_type n, const octave_value& x);
200
201 // This function exists to support the MEX interface.
202 // You should not use it anywhere else.
203 const void * mex_get_data (void) const { return matrix.data (); }
204
205protected:
206
208
210 {
211 delete idx_cache;
212 idx_cache = (idx ? new octave::idx_vector (idx) : nullptr);
213 return idx;
214 }
215
216 void clear_cached_info (void) const
217 {
218 delete typ; typ = nullptr;
219 delete idx_cache; idx_cache = nullptr;
220 }
221
222 mutable MatrixType *typ;
224
225private:
226
227 // No assignment.
228
229 OCTINTERP_API octave_base_matrix& operator = (const octave_base_matrix&);
230};
231
232#endif
OCTARRAY_API Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
Definition: Array.cc:2059
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
const MT & matrix_ref(void) const
Definition: ov-base-mat.h:190
MatrixType matrix_type(void) const
Definition: ov-base-mat.h:135
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:81
int ndims(void) const
Definition: ov-base-mat.h:120
octave_value sort(Array< octave_idx_type > &sidx, octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:146
bool isnumeric(void) const
Definition: ov-base-mat.h:161
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition: ov-base-mat.h:91
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-base-mat.h:127
octave_base_matrix(void)
Definition: ov-base-mat.h:56
octave_idx_type numel(void) const
Definition: ov-base-mat.h:118
std::size_t byte_size(void) const
Definition: ov-base-mat.h:75
octave::idx_vector * idx_cache
Definition: ov-base-mat.h:223
octave::idx_vector set_idx_cache(const octave::idx_vector &idx) const
Definition: ov-base-mat.h:209
octave_base_matrix(const octave_base_matrix &m)
Definition: ov-base-mat.h:67
octave_value diag(octave_idx_type k=0) const
Definition: ov-base-mat.h:138
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-base-mat.h:141
MatrixType * typ
Definition: ov-base-mat.h:222
bool is_constant(void) const
Definition: ov-base-mat.h:165
const void * mex_get_data(void) const
Definition: ov-base-mat.h:203
bool is_matrix_type(void) const
Definition: ov-base-mat.h:159
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-base-mat.h:150
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-base-mat.h:156
octave_value full_value(void) const
Definition: ov-base-mat.h:79
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:144
octave_value any(int dim=0) const
Definition: ov-base-mat.h:133
MT & matrix_ref(void)
Definition: ov-base-mat.h:184
octave_value squeeze(void) const
Definition: ov-base-mat.h:77
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:153
octave_idx_type nnz(void) const
Definition: ov-base-mat.h:122
octave_value all(int dim=0) const
Definition: ov-base-mat.h:132
~octave_base_matrix(void)
Definition: ov-base-mat.h:73
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-base-mat.h:124
bool is_defined(void) const
Definition: ov-base-mat.h:163
void clear_cached_info(void) const
Definition: ov-base-mat.h:216
dim_vector dims(void) const
Definition: ov-base-mat.h:116
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:201
virtual void assign(const std::string &, const octave_value &)
Definition: ov-base.h:332
virtual octave_value resize(const dim_vector &, bool fill=false) const
Definition: ov-base.cc:347
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:1037
F77_RET_T const F77_DBLE * x
bool is_true(const std::string &s)
static float_display_format get_edit_display_format(const octave_value &val)
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()) ? '\'' :'"'))