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 47 octave_base_diag : public octave_base_value 48 { 49 50 public: 51 octave_base_diag(void)52 octave_base_diag (void) 53 : octave_base_value (), matrix (), dense_cache () { } 54 octave_base_diag(const DMT & m)55 octave_base_diag (const DMT& m) 56 : octave_base_value (), matrix (m), dense_cache () 57 { } 58 octave_base_diag(const octave_base_diag & m)59 octave_base_diag (const octave_base_diag& m) 60 : octave_base_value (), matrix (m.matrix), dense_cache () { } 61 62 ~octave_base_diag (void) = default; 63 byte_size(void)64 std::size_t byte_size (void) const { return matrix.byte_size (); } 65 squeeze(void)66 octave_value squeeze (void) const { return matrix; } 67 full_value(void)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. 73 using octave_base_value::subsref; 74 75 octave_value subsref (const std::string& type, 76 const std::list<octave_value_list>& idx); 77 subsref(const std::string & type,const std::list<octave_value_list> & idx,int)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 dims(void)89 dim_vector dims (void) const { return matrix.dims (); } 90 nnz(void)91 octave_idx_type nnz (void) const { return diag ().nnz (); } 92 reshape(const dim_vector & new_dims)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 matrix_type(void)111 MatrixType matrix_type (void) const { return MatrixType::Diagonal; } matrix_type(const MatrixType &)112 MatrixType matrix_type (const MatrixType&) const 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. 118 using octave_base_value::diag; 119 120 octave_value diag (octave_idx_type k = 0) const; 121 122 octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const 123 { return to_dense ().sort (dim, mode); } 124 octave_value sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0, 125 sortmode mode = ASCENDING) const 126 { return to_dense ().sort (sidx, dim, mode); } 127 128 sortmode issorted (sortmode mode = UNSORTED) const 129 { return to_dense ().issorted (mode); } 130 131 Array<octave_idx_type> sort_rows_idx (sortmode mode = ASCENDING) const 132 { return to_dense ().sort_rows_idx (mode); } 133 134 sortmode is_sorted_rows (sortmode mode = UNSORTED) const 135 { return to_dense ().is_sorted_rows (mode); } 136 is_matrix_type(void)137 bool is_matrix_type (void) const { return true; } 138 isnumeric(void)139 bool isnumeric (void) const { return true; } 140 is_defined(void)141 bool is_defined (void) const { return true; } 142 is_constant(void)143 bool is_constant (void) const { return true; } 144 145 bool is_true (void) const; 146 is_diag_matrix(void)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 186 int8NDArray int8_array_value(void)187 int8_array_value (void) const { return to_dense ().int8_array_value (); } 188 189 int16NDArray int16_array_value(void)190 int16_array_value (void) const { return to_dense ().int16_array_value (); } 191 192 int32NDArray int32_array_value(void)193 int32_array_value (void) const { return to_dense ().int32_array_value (); } 194 195 int64NDArray int64_array_value(void)196 int64_array_value (void) const { return to_dense ().int64_array_value (); } 197 198 uint8NDArray uint8_array_value(void)199 uint8_array_value (void) const { return to_dense ().uint8_array_value (); } 200 201 uint16NDArray uint16_array_value(void)202 uint16_array_value (void) const { return to_dense ().uint16_array_value (); } 203 204 uint32NDArray uint32_array_value(void)205 uint32_array_value (void) const { return to_dense ().uint32_array_value (); } 206 207 uint64NDArray uint64_array_value(void)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 214 float_display_format get_edit_display_format (void) const; 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 250 mutable octave_value dense_cache; 251 252 }; 253 254 #endif 255