GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-base-diag.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2008-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_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 
45 template <typename DMT, typename MT>
46 class
47 OCTINTERP_API
49 {
50 
51 public:
52 
54  : octave_base_value (), m_matrix (), m_dense_cache () { }
55 
56  octave_base_diag (const DMT& m)
57  : octave_base_value (), m_matrix (m), m_dense_cache ()
58  { }
59 
61  : octave_base_value (), m_matrix (m.m_matrix), m_dense_cache () { }
62 
63  ~octave_base_diag () = default;
64 
65  std::size_t byte_size () const { return m_matrix.byte_size (); }
66 
67  octave_value squeeze () const { return m_matrix; }
68 
69  octave_value full_value () const { return to_dense (); }
70 
71  // We don't need to override all three forms of subsref. The using
72  // declaration will avoid warnings about partially-overloaded virtual
73  // functions.
75 
76  OCTINTERP_API octave_value
77  subsref (const std::string& type, const std::list<octave_value_list>& idx);
78 
79  octave_value_list subsref (const std::string& type,
80  const std::list<octave_value_list>& idx, int)
81  { return subsref (type, idx); }
82 
83  OCTINTERP_API octave_value
84  do_index_op (const octave_value_list& idx, bool resize_ok = false);
85 
86  OCTINTERP_API octave_value
87  subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
88  const octave_value& rhs);
89 
90  dim_vector dims () const { return m_matrix.dims (); }
91 
92  octave_idx_type nnz () const { return diag ().nnz (); }
93 
94  octave_value reshape (const dim_vector& new_dims) const
95  { return to_dense ().reshape (new_dims); }
96 
97  octave_value permute (const Array<int>& vec, bool inv = false) const
98  {
99  if (vec.numel () == 2
100  && ((vec.xelem (0) == 1 && vec.xelem (1) == 0)
101  || (vec.xelem (0) == 0 && vec.xelem (1) == 1)))
102  return DMT (m_matrix);
103  else
104  return to_dense ().permute (vec, inv);
105  }
106 
107  OCTINTERP_API octave_value
108  resize (const dim_vector& dv, bool fill = false) const;
109 
110  octave_value all (int dim = 0) const { return MT (m_matrix).all (dim); }
111  octave_value any (int dim = 0) const { return MT (m_matrix).any (dim); }
112 
115  { return matrix_type (); }
116 
117  // We don't need to override both forms of the diag method. The using
118  // declaration will avoid warnings about partially-overloaded virtual
119  // functions.
121 
122  OCTINTERP_API octave_value diag (octave_idx_type k = 0) const;
123 
125  { return to_dense ().sort (dim, mode); }
127  sortmode mode = ASCENDING) const
128  { return to_dense ().sort (sidx, dim, mode); }
129 
131  { return to_dense ().issorted (mode); }
132 
134  { return to_dense ().sort_rows_idx (mode); }
135 
137  { return to_dense ().is_sorted_rows (mode); }
138 
139  bool is_matrix_type () const { return true; }
140 
141  bool isnumeric () const { return true; }
142 
143  bool is_defined () const { return true; }
144 
145  bool is_constant () const { return true; }
146 
147  OCTINTERP_API bool is_true () const;
148 
149  bool is_diag_matrix () const { return true; }
150 
151  OCTINTERP_API double double_value (bool = false) const;
152 
153  OCTINTERP_API float float_value (bool = false) const;
154 
155  double scalar_value (bool frc_str_conv = false) const
156  { return double_value (frc_str_conv); }
157 
158  OCTINTERP_API octave::idx_vector
159  index_vector (bool /* require_integers */ = false) const;
160 
161  OCTINTERP_API Matrix matrix_value (bool = false) const;
162 
163  OCTINTERP_API FloatMatrix float_matrix_value (bool = false) const;
164 
165  OCTINTERP_API Complex complex_value (bool = false) const;
166 
167  OCTINTERP_API FloatComplex float_complex_value (bool = false) const;
168 
169  OCTINTERP_API ComplexMatrix complex_matrix_value (bool = false) const;
170 
171  OCTINTERP_API FloatComplexMatrix
172  float_complex_matrix_value (bool = false) const;
173 
174  OCTINTERP_API ComplexNDArray complex_array_value (bool = false) const;
175 
176  OCTINTERP_API FloatComplexNDArray
177  float_complex_array_value (bool = false) const;
178 
179  OCTINTERP_API boolNDArray bool_array_value (bool warn = false) const;
180 
181  OCTINTERP_API charNDArray char_array_value (bool = false) const;
182 
183  OCTINTERP_API NDArray array_value (bool = false) const;
184 
185  OCTINTERP_API FloatNDArray float_array_value (bool = false) const;
186 
187  OCTINTERP_API SparseMatrix sparse_matrix_value (bool = false) const;
188 
189  OCTINTERP_API SparseComplexMatrix
190  sparse_complex_matrix_value (bool = false) const;
191 
193  int8_array_value () const { return to_dense ().int8_array_value (); }
194 
196  int16_array_value () const { return to_dense ().int16_array_value (); }
197 
199  int32_array_value () const { return to_dense ().int32_array_value (); }
200 
202  int64_array_value () const { return to_dense ().int64_array_value (); }
203 
205  uint8_array_value () const { return to_dense ().uint8_array_value (); }
206 
208  uint16_array_value () const { return to_dense ().uint16_array_value (); }
209 
211  uint32_array_value () const { return to_dense ().uint32_array_value (); }
212 
214  uint64_array_value () const { return to_dense ().uint64_array_value (); }
215 
216  OCTINTERP_API octave_value
217  convert_to_str_internal (bool pad, bool force, char type) const;
218 
219  OCTINTERP_API void
220  print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
221 
222  OCTINTERP_API float_display_format get_edit_display_format () const;
223 
224  OCTINTERP_API std::string
225  edit_display (const float_display_format& fmt,
226  octave_idx_type i, octave_idx_type j) const;
227 
228  OCTINTERP_API bool save_ascii (std::ostream& os);
229 
230  OCTINTERP_API bool load_ascii (std::istream& is);
231 
232  OCTINTERP_API int
233  write (octave::stream& os, int block_size,
234  oct_data_conv::data_type output_type, int skip,
235  octave::mach_info::float_format flt_fmt) const;
236 
237  OCTINTERP_API mxArray * as_mxArray (bool interleaved) const;
238 
239  OCTINTERP_API bool print_as_scalar () const;
240 
241  OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
242 
243  OCTINTERP_API void
244  print_info (std::ostream& os, const std::string& prefix) const;
245 
246  OCTINTERP_API void short_disp (std::ostream& os) const;
247 
248  OCTINTERP_API octave_value fast_elem_extract (octave_idx_type n) const;
249 
250 protected:
251 
252  DMT m_matrix;
253 
254  OCTINTERP_API octave_value to_dense () const;
255 
256  virtual bool chk_valid_scalar (const octave_value&,
257  typename DMT::element_type&) const = 0;
258 
259 private:
260 
261  mutable octave_value m_dense_cache;
262 
263 };
264 
265 #endif
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
Definition: Array-base.cc:2079
T & xelem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:524
octave_idx_type numel() const
Number of elements in the array.
Definition: Array.h:414
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
bool is_defined() const
Definition: ov-base-diag.h:143
MatrixType matrix_type(const MatrixType &) const
Definition: ov-base-diag.h:114
bool is_constant() const
Definition: ov-base-diag.h:145
bool is_matrix_type() const
Definition: ov-base-diag.h:139
bool is_diag_matrix() const
Definition: ov-base-diag.h:149
uint64NDArray uint64_array_value() const
Definition: ov-base-diag.h:214
octave_value all(int dim=0) const
Definition: ov-base-diag.h:110
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-base-diag.h:136
int16NDArray int16_array_value() const
Definition: ov-base-diag.h:196
octave_base_diag(const octave_base_diag &m)
Definition: ov-base-diag.h:60
~octave_base_diag()=default
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-base-diag.h:94
octave_value squeeze() const
Definition: ov-base-diag.h:67
int32NDArray int32_array_value() const
Definition: ov-base-diag.h:199
uint8NDArray uint8_array_value() const
Definition: ov-base-diag.h:205
uint16NDArray uint16_array_value() const
Definition: ov-base-diag.h:208
octave_value sort(Array< octave_idx_type > &sidx, octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-base-diag.h:126
bool isnumeric() const
Definition: ov-base-diag.h:141
octave_value any(int dim=0) const
Definition: ov-base-diag.h:111
octave_idx_type nnz() const
Definition: ov-base-diag.h:92
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition: ov-base-diag.h:79
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-base-diag.h:97
double scalar_value(bool frc_str_conv=false) const
Definition: ov-base-diag.h:155
int8NDArray int8_array_value() const
Definition: ov-base-diag.h:193
std::size_t byte_size() const
Definition: ov-base-diag.h:65
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-base-diag.h:130
MatrixType matrix_type() const
Definition: ov-base-diag.h:113
octave_base_diag(const DMT &m)
Definition: ov-base-diag.h:56
uint32NDArray uint32_array_value() const
Definition: ov-base-diag.h:211
dim_vector dims() const
Definition: ov-base-diag.h:90
virtual bool chk_valid_scalar(const octave_value &, typename DMT::element_type &) const =0
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Definition: ov-base-diag.h:133
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-base-diag.h:124
int64NDArray int64_array_value() const
Definition: ov-base-diag.h:202
octave_value full_value() const
Definition: ov-base-diag.h:69
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:248
virtual octave_value diag(octave_idx_type k=0) const
Definition: ov-base.cc:1041
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov.h:1416
octave_value any(int dim=0) const
Definition: ov.h:687
octave_value reshape(const dim_vector &dv) const
Definition: ov.h:571
octave_value all(int dim=0) const
Definition: ov.h:684
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
float_format
Definition: mach-info.h:38
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
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