GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-re-mat.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-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_re_mat_h)
27 #define octave_ov_re_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 
39 #include "error.h"
40 #include "oct-stream.h"
41 #include "ov-base.h"
42 #include "ov-base-mat.h"
43 #include "ov-typeinfo.h"
44 
45 #include "MatrixType.h"
46 
47 class octave_value_list;
48 
49 // Real matrix values.
50 
51 class
52 OCTINTERP_API
54 {
55 public:
56 
58  : octave_base_matrix<NDArray> () { }
59 
62 
63  octave_matrix (const Matrix& m, const MatrixType& t)
64  : octave_base_matrix<NDArray> (m, t) { }
65 
66  octave_matrix (const NDArray& nda)
67  : octave_base_matrix<NDArray> (nda) { }
68 
71 
74 
76  : octave_base_matrix<NDArray> (Matrix (v)) { }
77 
79  : octave_base_matrix<NDArray> (Matrix (v)) { }
80 
83 
85  bool zero_based = false, bool cache_index = false)
86  : octave_base_matrix<NDArray> (NDArray (idx, zero_based))
87  {
88  // Auto-create cache to speed up subsequent indexing.
89  if (zero_based && cache_index)
90  set_idx_cache (octave::idx_vector (idx));
91  }
92 
93  octave_matrix (const NDArray& nda, const octave::idx_vector& cache)
95  {
96  set_idx_cache (cache);
97  }
98 
99  ~octave_matrix () = default;
100 
101  octave_base_value * clone () const { return new octave_matrix (*this); }
102  octave_base_value * empty_clone () const { return new octave_matrix (); }
103 
104  type_conv_info numeric_demotion_function () const;
105 
106  octave_base_value * try_narrowing_conversion ();
107 
108  octave::idx_vector index_vector (bool /* require_integers */ = false) const
109  {
110  return m_idx_cache ? *m_idx_cache
111  : set_idx_cache (octave::idx_vector (m_matrix));
112  }
113 
115 
116  bool is_real_matrix () const { return true; }
117 
118  bool isreal () const { return true; }
119 
120  bool is_double_type () const { return true; }
121 
122  bool isfloat () const { return true; }
123 
125  int8_array_value () const { return int8NDArray (m_matrix); }
126 
128  int16_array_value () const { return int16NDArray (m_matrix); }
129 
131  int32_array_value () const { return int32NDArray (m_matrix); }
132 
134  int64_array_value () const { return int64NDArray (m_matrix); }
135 
137  uint8_array_value () const { return uint8NDArray (m_matrix); }
138 
140  uint16_array_value () const { return uint16NDArray (m_matrix); }
141 
143  uint32_array_value () const { return uint32NDArray (m_matrix); }
144 
146  uint64_array_value () const { return uint64NDArray (m_matrix); }
147 
148  double double_value (bool = false) const;
149 
150  float float_value (bool = false) const;
151 
152  double scalar_value (bool frc_str_conv = false) const
153  { return double_value (frc_str_conv); }
154 
155  Matrix matrix_value (bool = false) const;
156 
157  FloatMatrix float_matrix_value (bool = false) const;
158 
159  Complex complex_value (bool = false) const;
160 
161  FloatComplex float_complex_value (bool = false) const;
162 
163  ComplexMatrix complex_matrix_value (bool = false) const;
164 
165  FloatComplexMatrix float_complex_matrix_value (bool = false) const;
166 
167  ComplexNDArray complex_array_value (bool = false) const;
168 
169  FloatComplexNDArray float_complex_array_value (bool = false) const;
170 
171  boolNDArray bool_array_value (bool warn = false) const;
172 
173  charNDArray char_array_value (bool = false) const;
174 
175  NDArray array_value (bool = false) const { return m_matrix; }
176 
177  FloatNDArray float_array_value (bool = false) const { return m_matrix; }
178 
179  SparseMatrix sparse_matrix_value (bool = false) const;
180 
181  SparseComplexMatrix sparse_complex_matrix_value (bool = false) const;
182 
183  octave_value as_double () const;
184  octave_value as_single () const;
185 
186  octave_value as_int8 () const;
187  octave_value as_int16 () const;
188  octave_value as_int32 () const;
189  octave_value as_int64 () const;
190 
191  octave_value as_uint8 () const;
192  octave_value as_uint16 () const;
193  octave_value as_uint32 () const;
194  octave_value as_uint64 () const;
195 
196  octave_value diag (octave_idx_type k = 0) const;
197 
199 
200  octave_value reshape (const dim_vector& new_dims) const;
201 
202  octave_value squeeze () const;
203 
204  octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
206  sortmode mode = ASCENDING) const;
207 
208  sortmode issorted (sortmode mode = UNSORTED) const;
209 
210  Array<octave_idx_type> sort_rows_idx (sortmode mode = ASCENDING) const;
211 
212  sortmode is_sorted_rows (sortmode mode = UNSORTED) const;
213 
214  // Use matrix_ref here to clear index cache.
215  void increment () { matrix_ref () += 1.0; }
216 
217  void decrement () { matrix_ref () -= 1.0; }
218 
219  void changesign () { matrix_ref ().changesign (); }
220 
221  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
222 
223  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
224 
225  bool save_ascii (std::ostream& os);
226 
227  bool load_ascii (std::istream& is);
228 
229  bool save_binary (std::ostream& os, bool save_as_floats);
230 
231  bool load_binary (std::istream& is, bool swap,
233 
234  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
235 
236  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
237 
238  int write (octave::stream& os, int block_size,
239  oct_data_conv::data_type output_type, int skip,
240  octave::mach_info::float_format flt_fmt) const
241  { return os.write (m_matrix, block_size, output_type, skip, flt_fmt); }
242 
243  mxArray * as_mxArray (bool interleaved) const;
244 
245  octave_value map (unary_mapper_t umap) const;
246 
247 private:
248 
250 };
251 
252 #endif
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_matrix(const NDArray &nda, const octave::idx_vector &cache)
Definition: ov-re-mat.h:93
octave_matrix(const NDArray &nda)
Definition: ov-re-mat.h:66
void changesign()
Definition: ov-re-mat.h:219
octave_matrix(const Matrix &m, const MatrixType &t)
Definition: ov-re-mat.h:63
uint8NDArray uint8_array_value() const
Definition: ov-re-mat.h:137
octave_base_value * empty_clone() const
Definition: ov-re-mat.h:102
octave_matrix(const RowVector &v)
Definition: ov-re-mat.h:75
builtin_type_t builtin_type() const
Definition: ov-re-mat.h:114
octave_matrix(const Array< octave_idx_type > &idx, bool zero_based=false, bool cache_index=false)
Definition: ov-re-mat.h:84
int8NDArray int8_array_value() const
Definition: ov-re-mat.h:125
uint32NDArray uint32_array_value() const
Definition: ov-re-mat.h:143
int64NDArray int64_array_value() const
Definition: ov-re-mat.h:134
double scalar_value(bool frc_str_conv=false) const
Definition: ov-re-mat.h:152
void increment()
Definition: ov-re-mat.h:215
octave::idx_vector index_vector(bool=false) const
Definition: ov-re-mat.h:108
octave_matrix(const ColumnVector &v)
Definition: ov-re-mat.h:78
bool is_double_type() const
Definition: ov-re-mat.h:120
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-re-mat.h:238
void decrement()
Definition: ov-re-mat.h:217
~octave_matrix()=default
uint16NDArray uint16_array_value() const
Definition: ov-re-mat.h:140
octave_matrix(const Array< double > &m)
Definition: ov-re-mat.h:69
uint64NDArray uint64_array_value() const
Definition: ov-re-mat.h:146
int16NDArray int16_array_value() const
Definition: ov-re-mat.h:128
octave_matrix(const Matrix &m)
Definition: ov-re-mat.h:60
bool isfloat() const
Definition: ov-re-mat.h:122
octave_matrix(const octave_matrix &m)
Definition: ov-re-mat.h:81
bool is_real_matrix() const
Definition: ov-re-mat.h:116
octave_base_value * clone() const
Definition: ov-re-mat.h:101
int32NDArray int32_array_value() const
Definition: ov-re-mat.h:131
octave_matrix(const DiagMatrix &d)
Definition: ov-re-mat.h:72
FloatNDArray float_array_value(bool=false) const
Definition: ov-re-mat.h:177
bool isreal() const
Definition: ov-re-mat.h:118
NDArray array_value(bool=false) const
Definition: ov-re-mat.h:175
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
intNDArray< octave_int16 > int16NDArray
Definition: int16NDArray.h:36
intNDArray< octave_int32 > int32NDArray
Definition: int32NDArray.h:36
intNDArray< octave_int64 > int64NDArray
Definition: int64NDArray.h:36
intNDArray< octave_int8 > int8NDArray
Definition: int8NDArray.h:36
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
float_format
Definition: mach-info.h:38
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
int64_t octave_hdf5_id
sortmode
Definition: oct-sort.h:97
@ UNSORTED
Definition: oct-sort.h:97
@ ASCENDING
Definition: oct-sort.h:97
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:181
builtin_type_t
Definition: ov-base.h:83
@ btyp_double
Definition: ov-base.h:84
intNDArray< octave_uint16 > uint16NDArray
Definition: uint16NDArray.h:36
intNDArray< octave_uint32 > uint32NDArray
Definition: uint32NDArray.h:36
intNDArray< octave_uint64 > uint64NDArray
Definition: uint64NDArray.h:36
intNDArray< octave_uint8 > uint8NDArray
Definition: uint8NDArray.h:36