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