1 //////////////////////////////////////////////////////////////////////// 2 // 3 // Copyright (C) 1996-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_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 53 octave_matrix : public octave_base_matrix<NDArray> 54 { 55 public: 56 octave_matrix(void)57 octave_matrix (void) 58 : octave_base_matrix<NDArray> () { } 59 octave_matrix(const Matrix & m)60 octave_matrix (const Matrix& m) 61 : octave_base_matrix<NDArray> (m) { } 62 octave_matrix(const Matrix & m,const MatrixType & t)63 octave_matrix (const Matrix& m, const MatrixType& t) 64 : octave_base_matrix<NDArray> (m, t) { } 65 octave_matrix(const NDArray & nda)66 octave_matrix (const NDArray& nda) 67 : octave_base_matrix<NDArray> (nda) { } 68 octave_matrix(const Array<double> & m)69 octave_matrix (const Array<double>& m) 70 : octave_base_matrix<NDArray> (NDArray (m)) { } 71 octave_matrix(const DiagMatrix & d)72 octave_matrix (const DiagMatrix& d) 73 : octave_base_matrix<NDArray> (Matrix (d)) { } 74 octave_matrix(const RowVector & v)75 octave_matrix (const RowVector& v) 76 : octave_base_matrix<NDArray> (Matrix (v)) { } 77 octave_matrix(const ColumnVector & v)78 octave_matrix (const ColumnVector& v) 79 : octave_base_matrix<NDArray> (Matrix (v)) { } 80 octave_matrix(const octave_matrix & m)81 octave_matrix (const octave_matrix& m) 82 : octave_base_matrix<NDArray> (m) { } 83 84 octave_matrix (const Array<octave_idx_type>& idx, 85 bool zero_based = false, bool cache_index = false) NDArray(idx,zero_based)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 (idx_vector (idx)); 91 } 92 octave_matrix(const NDArray & nda,const idx_vector & cache)93 octave_matrix (const NDArray& nda, const idx_vector& cache) 94 : octave_base_matrix<NDArray> (nda) 95 { 96 set_idx_cache (cache); 97 } 98 99 ~octave_matrix (void) = default; 100 clone(void)101 octave_base_value * clone (void) const { return new octave_matrix (*this); } empty_clone(void)102 octave_base_value * empty_clone (void) const { return new octave_matrix (); } 103 104 type_conv_info numeric_demotion_function (void) const; 105 106 octave_base_value * try_narrowing_conversion (void); 107 108 idx_vector index_vector (bool /* require_integers */ = false) const 109 { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (matrix)); } 110 builtin_type(void)111 builtin_type_t builtin_type (void) const { return btyp_double; } 112 is_real_matrix(void)113 bool is_real_matrix (void) const { return true; } 114 isreal(void)115 bool isreal (void) const { return true; } 116 is_double_type(void)117 bool is_double_type (void) const { return true; } 118 isfloat(void)119 bool isfloat (void) const { return true; } 120 121 int8NDArray int8_array_value(void)122 int8_array_value (void) const { return int8NDArray (matrix); } 123 124 int16NDArray int16_array_value(void)125 int16_array_value (void) const { return int16NDArray (matrix); } 126 127 int32NDArray int32_array_value(void)128 int32_array_value (void) const { return int32NDArray (matrix); } 129 130 int64NDArray int64_array_value(void)131 int64_array_value (void) const { return int64NDArray (matrix); } 132 133 uint8NDArray uint8_array_value(void)134 uint8_array_value (void) const { return uint8NDArray (matrix); } 135 136 uint16NDArray uint16_array_value(void)137 uint16_array_value (void) const { return uint16NDArray (matrix); } 138 139 uint32NDArray uint32_array_value(void)140 uint32_array_value (void) const { return uint32NDArray (matrix); } 141 142 uint64NDArray uint64_array_value(void)143 uint64_array_value (void) const { return uint64NDArray (matrix); } 144 145 double double_value (bool = false) const; 146 147 float float_value (bool = false) const; 148 149 double scalar_value (bool frc_str_conv = false) const 150 { return double_value (frc_str_conv); } 151 152 Matrix matrix_value (bool = false) const; 153 154 FloatMatrix float_matrix_value (bool = false) const; 155 156 Complex complex_value (bool = false) const; 157 158 FloatComplex float_complex_value (bool = false) const; 159 160 ComplexMatrix complex_matrix_value (bool = false) const; 161 162 FloatComplexMatrix float_complex_matrix_value (bool = false) const; 163 164 ComplexNDArray complex_array_value (bool = false) const; 165 166 FloatComplexNDArray float_complex_array_value (bool = false) const; 167 168 boolNDArray bool_array_value (bool warn = false) const; 169 170 charNDArray char_array_value (bool = false) const; 171 172 NDArray array_value (bool = false) const { return matrix; } 173 174 FloatNDArray float_array_value (bool = false) const { return matrix; } 175 176 SparseMatrix sparse_matrix_value (bool = false) const; 177 178 SparseComplexMatrix sparse_complex_matrix_value (bool = false) const; 179 180 octave_value as_double (void) const; 181 octave_value as_single (void) const; 182 183 octave_value as_int8 (void) const; 184 octave_value as_int16 (void) const; 185 octave_value as_int32 (void) const; 186 octave_value as_int64 (void) const; 187 188 octave_value as_uint8 (void) const; 189 octave_value as_uint16 (void) const; 190 octave_value as_uint32 (void) const; 191 octave_value as_uint64 (void) const; 192 193 octave_value diag (octave_idx_type k = 0) const; 194 195 octave_value diag (octave_idx_type m, octave_idx_type n) const; 196 197 octave_value reshape (const dim_vector& new_dims) const; 198 199 octave_value squeeze (void) const; 200 201 octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const; 202 octave_value sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0, 203 sortmode mode = ASCENDING) const; 204 205 sortmode issorted (sortmode mode = UNSORTED) const; 206 207 Array<octave_idx_type> sort_rows_idx (sortmode mode = ASCENDING) const; 208 209 sortmode is_sorted_rows (sortmode mode = UNSORTED) const; 210 211 // Use matrix_ref here to clear index cache. increment(void)212 void increment (void) { matrix_ref () += 1.0; } 213 decrement(void)214 void decrement (void) { matrix_ref () -= 1.0; } 215 changesign(void)216 void changesign (void) { matrix_ref ().changesign (); } 217 218 octave_value convert_to_str_internal (bool pad, bool force, char type) const; 219 220 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; 221 222 bool save_ascii (std::ostream& os); 223 224 bool load_ascii (std::istream& is); 225 226 bool save_binary (std::ostream& os, bool save_as_floats); 227 228 bool load_binary (std::istream& is, bool swap, 229 octave::mach_info::float_format fmt); 230 231 bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats); 232 233 bool load_hdf5 (octave_hdf5_id loc_id, const char *name); 234 write(octave::stream & os,int block_size,oct_data_conv::data_type output_type,int skip,octave::mach_info::float_format flt_fmt)235 int write (octave::stream& os, int block_size, 236 oct_data_conv::data_type output_type, int skip, 237 octave::mach_info::float_format flt_fmt) const 238 { return os.write (matrix, block_size, output_type, skip, flt_fmt); } 239 240 // Unsafe. This function exists to support the MEX interface. 241 // You should not use it anywhere else. mex_get_data(void)242 void * mex_get_data (void) const { return matrix.mex_get_data (); } 243 244 mxArray * as_mxArray (void) const; 245 246 octave_value map (unary_mapper_t umap) const; 247 248 private: 249 250 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA 251 }; 252 253 #endif 254