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-diag.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2008-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_diag_h)
27#define octave_ov_base_diag_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
39#include "ovl.h"
40#include "ov-base.h"
41#include "ov-typeinfo.h"
42
43// Real matrix values.
44
45template <typename DMT, typename MT>
46class OCTINTERP_TEMPLATE_API octave_base_diag : public octave_base_value
47{
48
49public:
50
51 OCTINTERP_OVERRIDABLE_FUNC_API
53 : octave_base_value (), m_matrix (), m_dense_cache () { }
54
55 OCTINTERP_OVERRIDABLE_FUNC_API
56 octave_base_diag (const DMT& m)
57 : octave_base_value (), m_matrix (m), m_dense_cache () { }
58
59 OCTINTERP_OVERRIDABLE_FUNC_API
61 : octave_base_value (), m_matrix (m.m_matrix), m_dense_cache () { }
62
63 OCTINTERP_OVERRIDABLE_FUNC_API ~octave_base_diag () = default;
64
65 OCTINTERP_OVERRIDABLE_FUNC_API std::size_t byte_size () const
66 { return m_matrix.byte_size (); }
67
68 OCTINTERP_OVERRIDABLE_FUNC_API octave_value squeeze () const
69 { return m_matrix; }
70
71 OCTINTERP_OVERRIDABLE_FUNC_API octave_value full_value () const
72 { return to_dense (); }
73
74 // We don't need to override all three forms of subsref. The using
75 // declaration will avoid warnings about partially-overloaded virtual
76 // functions.
78
79 OCTINTERP_API octave_value
80 subsref (const std::string& type, const std::list<octave_value_list>& idx);
81
82 OCTINTERP_OVERRIDABLE_FUNC_API octave_value_list
83 subsref (const std::string& type, const std::list<octave_value_list>& idx,
84 int)
85 { return subsref (type, idx); }
86
87 OCTINTERP_API octave_value
88 do_index_op (const octave_value_list& idx, bool resize_ok = false);
89
90 OCTINTERP_API octave_value
91 subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
92 const octave_value& rhs);
93
94 OCTINTERP_OVERRIDABLE_FUNC_API dim_vector dims () const
95 { return m_matrix.dims (); }
96
97 OCTINTERP_OVERRIDABLE_FUNC_API octave_idx_type nnz () const
98 { return diag ().nnz (); }
99
100 OCTINTERP_OVERRIDABLE_FUNC_API octave_value reshape (const dim_vector& new_dims) const
101 { return to_dense ().reshape (new_dims); }
102
103 OCTINTERP_OVERRIDABLE_FUNC_API octave_value
104 permute (const Array<int>& vec, bool inv = false) const
105 {
106 if (vec.numel () == 2
107 && ((vec.xelem (0) == 1 && vec.xelem (1) == 0)
108 || (vec.xelem (0) == 0 && vec.xelem (1) == 1)))
109 return DMT (m_matrix);
110 else
111 return to_dense ().permute (vec, inv);
112 }
113
114 OCTINTERP_API octave_value
115 resize (const dim_vector& dv, bool fill = false) const;
116
117 OCTINTERP_OVERRIDABLE_FUNC_API octave_value all (int dim = 0) const
118 { return MT (m_matrix).all (dim); }
119
120 OCTINTERP_OVERRIDABLE_FUNC_API octave_value any (int dim = 0) const
121 { return MT (m_matrix).any (dim); }
122
123 OCTINTERP_OVERRIDABLE_FUNC_API MatrixType matrix_type () const
124 { return MatrixType::Diagonal; }
125 OCTINTERP_OVERRIDABLE_FUNC_API MatrixType matrix_type (const MatrixType&) const
126 { return matrix_type (); }
127
128 // We don't need to override both forms of the diag method. The using
129 // declaration will avoid warnings about partially-overloaded virtual
130 // functions.
132
133 OCTINTERP_API octave_value diag (octave_idx_type k = 0) const;
134
135 OCTINTERP_OVERRIDABLE_FUNC_API octave_value
136 sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const
137 { return to_dense ().sort (dim, mode); }
138 OCTINTERP_OVERRIDABLE_FUNC_API octave_value
140 sortmode mode = ASCENDING) const
141 { return to_dense ().sort (sidx, dim, mode); }
142
143 OCTINTERP_OVERRIDABLE_FUNC_API sortmode
145 { return to_dense ().issorted (mode); }
146
147 OCTINTERP_OVERRIDABLE_FUNC_API Array<octave_idx_type>
149 { return to_dense ().sort_rows_idx (mode); }
150
151 OCTINTERP_OVERRIDABLE_FUNC_API sortmode
153 { return to_dense ().is_sorted_rows (mode); }
154
155 OCTINTERP_OVERRIDABLE_FUNC_API bool is_matrix_type () const
156 { return true; }
157
158 OCTINTERP_OVERRIDABLE_FUNC_API bool isnumeric () const { return true; }
159
160 OCTINTERP_OVERRIDABLE_FUNC_API bool is_defined () const { return true; }
161
162 OCTINTERP_OVERRIDABLE_FUNC_API bool is_constant () const { return true; }
163
164 OCTINTERP_API bool is_true () const;
165
166 OCTINTERP_OVERRIDABLE_FUNC_API bool is_diag_matrix () const { return true; }
167
168 OCTINTERP_API double double_value (bool = false) const;
169
170 OCTINTERP_API float float_value (bool = false) const;
171
172 OCTINTERP_OVERRIDABLE_FUNC_API double
173 scalar_value (bool frc_str_conv = false) const
174 { return double_value (frc_str_conv); }
175
176 OCTINTERP_API octave::idx_vector
177 index_vector (bool /* require_integers */ = false) const;
178
179 OCTINTERP_API Matrix matrix_value (bool = false) const;
180
181 OCTINTERP_API FloatMatrix float_matrix_value (bool = false) const;
182
183 OCTINTERP_API Complex complex_value (bool = false) const;
184
185 OCTINTERP_API FloatComplex float_complex_value (bool = false) const;
186
187 OCTINTERP_API ComplexMatrix complex_matrix_value (bool = false) const;
188
189 OCTINTERP_API FloatComplexMatrix
190 float_complex_matrix_value (bool = false) const;
191
192 OCTINTERP_API ComplexNDArray complex_array_value (bool = false) const;
193
194 OCTINTERP_API FloatComplexNDArray
195 float_complex_array_value (bool = false) const;
196
197 OCTINTERP_API boolNDArray bool_array_value (bool warn = false) const;
198
199 OCTINTERP_API charNDArray char_array_value (bool = false) const;
200
201 OCTINTERP_API NDArray array_value (bool = false) const;
202
203 OCTINTERP_API FloatNDArray float_array_value (bool = false) const;
204
205 OCTINTERP_API SparseMatrix sparse_matrix_value (bool = false) const;
206
207 OCTINTERP_API SparseComplexMatrix
208 sparse_complex_matrix_value (bool = false) const;
209
210 OCTINTERP_OVERRIDABLE_FUNC_API int8NDArray int8_array_value () const
211 { return to_dense ().int8_array_value (); }
212
213 OCTINTERP_OVERRIDABLE_FUNC_API int16NDArray int16_array_value () const
214 { return to_dense ().int16_array_value (); }
215
216 OCTINTERP_OVERRIDABLE_FUNC_API int32NDArray int32_array_value () const
217 { return to_dense ().int32_array_value (); }
218
219 OCTINTERP_OVERRIDABLE_FUNC_API int64NDArray int64_array_value () const
220 { return to_dense ().int64_array_value (); }
221
222 OCTINTERP_OVERRIDABLE_FUNC_API uint8NDArray uint8_array_value () const
223 { return to_dense ().uint8_array_value (); }
224
225 OCTINTERP_OVERRIDABLE_FUNC_API uint16NDArray uint16_array_value () const
226 { return to_dense ().uint16_array_value (); }
227
228 OCTINTERP_OVERRIDABLE_FUNC_API uint32NDArray uint32_array_value () const
229 { return to_dense ().uint32_array_value (); }
230
231 OCTINTERP_OVERRIDABLE_FUNC_API uint64NDArray uint64_array_value () const
232 { return to_dense ().uint64_array_value (); }
233
234 OCTINTERP_API octave_value
235 convert_to_str_internal (bool pad, bool force, char type) const;
236
237 OCTINTERP_API void
238 print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
239
240 OCTINTERP_API float_display_format get_edit_display_format () const;
241
242 OCTINTERP_API std::string
245
246 OCTINTERP_API bool save_ascii (std::ostream& os);
247
248 OCTINTERP_API bool load_ascii (std::istream& is);
249
250 OCTINTERP_API int
251 write (octave::stream& os, int block_size,
252 oct_data_conv::data_type output_type, int skip,
253 octave::mach_info::float_format flt_fmt) const;
254
255 OCTINTERP_API mxArray * as_mxArray (bool interleaved) const;
256
257 OCTINTERP_API bool print_as_scalar () const;
258
259 OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
260
261 OCTINTERP_API void
262 print_info (std::ostream& os, const std::string& prefix) const;
263
264 OCTINTERP_API void short_disp (std::ostream& os) const;
265
266 OCTINTERP_API octave_value fast_elem_extract (octave_idx_type n) const;
267
268protected:
269
271
272 OCTINTERP_API octave_value to_dense () const;
273
274 virtual bool chk_valid_scalar (const octave_value&,
275 typename DMT::element_type&) const = 0;
276
277private:
278
279 mutable octave_value m_dense_cache;
280
281};
282
283#endif
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
T & xelem(octave_idx_type n)
Size of the specified dimension.
Definition Array.h:525
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
octave_idx_type numel() const
Number of elements in the array.
Definition Array.h:418
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
bool is_defined() const
MatrixType matrix_type(const MatrixType &) const
bool is_constant() const
bool is_matrix_type() const
bool is_diag_matrix() const
uint64NDArray uint64_array_value() const
octave_value all(int dim=0) const
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
int16NDArray int16_array_value() const
octave_base_diag(const octave_base_diag &m)
~octave_base_diag()=default
octave_value reshape(const dim_vector &new_dims) const
octave_value squeeze() const
int32NDArray int32_array_value() const
uint8NDArray uint8_array_value() const
uint16NDArray uint16_array_value() const
octave_value sort(Array< octave_idx_type > &sidx, octave_idx_type dim=0, sortmode mode=ASCENDING) const
bool isnumeric() const
octave_value any(int dim=0) const
octave_idx_type nnz() 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
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
double scalar_value(bool frc_str_conv=false) const
int8NDArray int8_array_value() const
std::size_t byte_size() const
sortmode issorted(sortmode mode=UNSORTED) const
MatrixType matrix_type() const
octave_base_diag(const DMT &m)
uint32NDArray uint32_array_value() const
dim_vector dims() const
virtual bool chk_valid_scalar(const octave_value &, typename DMT::element_type &) const =0
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
int64NDArray int64_array_value() const
octave_value full_value() const
virtual SparseMatrix sparse_matrix_value(bool=false) const
Definition ov-base.cc:692
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition ov-base.cc:245
virtual float float_value(bool=false) const
Definition ov-base.cc:585
virtual bool save_ascii(std::ostream &os)
Definition ov-base.cc:986
virtual void short_disp(std::ostream &os) const
Definition ov-base.h:761
virtual ComplexNDArray complex_array_value(bool=false) const
Definition ov-base.cc:647
virtual octave_value fast_elem_extract(octave_idx_type n) const
Definition ov-base.cc:1394
virtual boolNDArray bool_array_value(bool=false) const
Definition ov-base.cc:672
virtual MatrixType matrix_type() const
Definition ov-base.cc:407
virtual FloatComplexNDArray float_complex_array_value(bool=false) const
Definition ov-base.cc:653
virtual charNDArray char_array_value(bool=false) const
Definition ov-base.cc:686
virtual octave_value diag(octave_idx_type k=0) const
Definition ov-base.cc:1036
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 FloatComplex float_complex_value(bool=false) const
Definition ov-base.cc:627
virtual octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition ov-base.cc:443
virtual FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition ov-base.cc:640
virtual ComplexMatrix complex_matrix_value(bool=false) const
Definition ov-base.cc:633
virtual std::string edit_display(const float_display_format &, octave_idx_type, octave_idx_type) const
Definition ov-base.h:769
virtual void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition ov-base.cc:463
virtual octave::idx_vector index_vector(bool require_integers=false) const
Definition ov-base.cc:277
virtual mxArray * as_mxArray(bool interleaved) const
Definition ov-base.cc:1030
virtual bool load_ascii(std::istream &is)
Definition ov-base.cc:992
virtual FloatNDArray float_array_value(bool=false) const
Definition ov-base.cc:615
virtual FloatMatrix float_matrix_value(bool=false) const
Definition ov-base.cc:603
virtual SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition ov-base.cc:698
virtual bool print_as_scalar() const
Definition ov-base.h:746
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 int write(octave::stream &os, int block_size, oct_data_conv::data_type output_type, int skip, octave::mach_info::float_format flt_fmt) const
Definition ov-base.cc:1023
virtual NDArray array_value(bool=false) const
Definition ov-base.cc:609
virtual octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
Definition ov-base.cc:294
virtual double double_value(bool=false) const
Definition ov-base.cc:579
virtual Matrix matrix_value(bool=false) const
Definition ov-base.cc:597
virtual Complex complex_value(bool=false) const
Definition ov-base.cc:621
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition ov.h:1431
octave_value any(int dim=0) const
Definition ov.h:687
octave_value reshape(const dim_vector &dv) const
Definition ov.h:571
octave_idx_type nnz() const
Definition ov.h:565
octave_value all(int dim=0) const
Definition ov.h:684
std::complex< double > Complex
Definition oct-cmplx.h:33
std::complex< float > FloatComplex
Definition oct-cmplx.h:34
sortmode
Definition oct-sort.h:97
@ UNSORTED
Definition oct-sort.h:97
@ ASCENDING
Definition oct-sort.h:97