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_perm_h)
27 #define octave_ov_perm_h 1
28 
29 #include "octave-config.h"
30 
31 #include "mx-base.h"
32 #include "str-vec.h"
33 
34 #include "ov-base.h"
35 #include "ov-typeinfo.h"
36 #include "ovl.h"
37 
38 class
39 OCTINTERP_API
40 octave_perm_matrix : public octave_base_value
41 {
42 public:
octave_perm_matrix(void)43   octave_perm_matrix (void) : matrix (), dense_cache () { }
44 
octave_perm_matrix(const PermMatrix & p)45   octave_perm_matrix (const PermMatrix& p) : matrix (p), dense_cache () { }
46 
clone(void)47   octave_base_value * clone (void) const
48   { return new octave_perm_matrix (*this); }
empty_clone(void)49   octave_base_value * empty_clone (void) const
50   { return new octave_perm_matrix (); }
51 
52   type_conv_info numeric_conversion_function (void) const;
53 
54   octave_base_value * try_narrowing_conversion (void);
55 
byte_size(void)56   std::size_t byte_size (void) const { return matrix.byte_size (); }
57 
squeeze(void)58   octave_value squeeze (void) const { return matrix; }
59 
full_value(void)60   octave_value full_value (void) const { return to_dense (); }
61 
62   // We don't need to override all three forms of subsref.  The using
63   // declaration will avoid warnings about partially-overloaded virtual
64   // functions.
65   using octave_base_value::subsref;
66 
67   octave_value subsref (const std::string& type,
68                         const std::list<octave_value_list>& idx);
69 
subsref(const std::string & type,const std::list<octave_value_list> & idx,int)70   octave_value_list subsref (const std::string& type,
71                              const std::list<octave_value_list>& idx, int)
72   { return subsref (type, idx); }
73 
74   octave_value do_index_op (const octave_value_list& idx,
75                             bool resize_ok = false);
76 
dims(void)77   dim_vector dims (void) const { return matrix.dims (); }
78 
nnz(void)79   octave_idx_type nnz (void) const { return matrix.rows (); }
80 
reshape(const dim_vector & new_dims)81   octave_value reshape (const dim_vector& new_dims) const
82   { return to_dense ().reshape (new_dims); }
83 
84   octave_value permute (const Array<int>& vec, bool inv = false) const
85   { return to_dense ().permute (vec, inv); }
86 
87   octave_value resize (const dim_vector& dv, bool fill = false) const
88   { return to_dense ().resize (dv, fill); }
89 
90   octave_value all (int dim = 0) const { return to_dense ().all (dim); }
91   octave_value any (int dim = 0) const { return to_dense ().any (dim); }
92 
matrix_type(void)93   MatrixType matrix_type (void) const { return MatrixType::Permuted_Diagonal; }
matrix_type(const MatrixType &)94   MatrixType matrix_type (const MatrixType&) const
95   { return matrix_type (); }
96 
97   // We don't need to override both forms of the diag method.  The using
98   // declaration will avoid warnings about partially-overloaded virtual
99   // functions.
100   using octave_base_value::diag;
101 
102   octave_value diag (octave_idx_type k = 0) const
103   { return to_dense () .diag (k); }
104 
105   octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const
106   { return to_dense ().sort (dim, mode); }
107   octave_value sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0,
108                      sortmode mode = ASCENDING) const
109   { return to_dense ().sort (sidx, dim, mode); }
110 
111   sortmode issorted (sortmode mode = UNSORTED) const
112   { return to_dense ().issorted (mode); }
113 
114   Array<octave_idx_type> sort_rows_idx (sortmode mode = ASCENDING) const
115   { return to_dense ().sort_rows_idx (mode); }
116 
117   sortmode is_sorted_rows (sortmode mode = UNSORTED) const
118   { return to_dense ().is_sorted_rows (mode); }
119 
builtin_type(void)120   builtin_type_t builtin_type (void) const { return btyp_double; }
121 
is_perm_matrix(void)122   bool is_perm_matrix (void) const { return true; }
123 
is_matrix_type(void)124   bool is_matrix_type (void) const { return true; }
125 
isnumeric(void)126   bool isnumeric (void) const { return true; }
127 
is_defined(void)128   bool is_defined (void) const { return true; }
129 
is_constant(void)130   bool is_constant (void) const { return true; }
131 
is_real_matrix(void)132   bool is_real_matrix (void) const { return true; }
133 
isreal(void)134   bool isreal (void) const { return true; }
135 
is_double_type(void)136   bool is_double_type (void) const { return true; }
137 
isfloat(void)138   bool isfloat (void) const { return true; }
139 
140   bool is_true (void) const;
141 
142   double double_value (bool = false) const;
143 
144   float float_value (bool = false) const;
145 
146   double scalar_value (bool frc_str_conv = false) const
147   { return double_value (frc_str_conv); }
148 
149   idx_vector index_vector (bool require_integers = false) const;
150 
perm_matrix_value(void)151   PermMatrix perm_matrix_value (void) const
152   { return matrix; }
153 
154   Matrix matrix_value (bool = false) const;
155 
156   FloatMatrix float_matrix_value (bool = false) const;
157 
158   Complex complex_value (bool = false) const;
159 
160   FloatComplex float_complex_value (bool = false) const;
161 
162   ComplexMatrix complex_matrix_value (bool = false) const;
163 
164   FloatComplexMatrix float_complex_matrix_value (bool = false) const;
165 
166   ComplexNDArray complex_array_value (bool = false) const;
167 
168   FloatComplexNDArray float_complex_array_value (bool = false) const;
169 
170   boolNDArray bool_array_value (bool warn = false) const;
171 
172   charNDArray char_array_value (bool = false) const;
173 
174   NDArray array_value (bool = false) const;
175 
176   FloatNDArray float_array_value (bool = false) const;
177 
178   SparseMatrix sparse_matrix_value (bool = false) const;
179 
180   SparseBoolMatrix sparse_bool_matrix_value (bool = false) const;
181 
182   SparseComplexMatrix sparse_complex_matrix_value (bool = false) const;
183 
184   int8NDArray
int8_array_value(void)185   int8_array_value (void) const { return to_dense ().int8_array_value (); }
186 
187   int16NDArray
int16_array_value(void)188   int16_array_value (void) const { return to_dense ().int16_array_value (); }
189 
190   int32NDArray
int32_array_value(void)191   int32_array_value (void) const { return to_dense ().int32_array_value (); }
192 
193   int64NDArray
int64_array_value(void)194   int64_array_value (void) const { return to_dense ().int64_array_value (); }
195 
196   uint8NDArray
uint8_array_value(void)197   uint8_array_value (void) const { return to_dense ().uint8_array_value (); }
198 
199   uint16NDArray
uint16_array_value(void)200   uint16_array_value (void) const { return to_dense ().uint16_array_value (); }
201 
202   uint32NDArray
uint32_array_value(void)203   uint32_array_value (void) const { return to_dense ().uint32_array_value (); }
204 
205   uint64NDArray
uint64_array_value(void)206   uint64_array_value (void) const { return to_dense ().uint64_array_value (); }
207 
208   octave_value convert_to_str_internal (bool pad, bool force, char type) const;
209 
210   octave_value as_double (void) const;
211   octave_value as_single (void) const;
212 
213   octave_value as_int8 (void) const;
214   octave_value as_int16 (void) const;
215   octave_value as_int32 (void) const;
216   octave_value as_int64 (void) const;
217 
218   octave_value as_uint8 (void) const;
219   octave_value as_uint16 (void) const;
220   octave_value as_uint32 (void) const;
221   octave_value as_uint64 (void) const;
222 
223   void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
224 
225   float_display_format get_edit_display_format (void) const;
226 
227   std::string edit_display (const float_display_format& fmt,
228                             octave_idx_type i, octave_idx_type j) const;
229 
230   bool save_ascii (std::ostream& os);
231 
232   bool load_ascii (std::istream& is);
233 
234   bool save_binary (std::ostream& os, bool save_as_floats);
235 
236   bool load_binary (std::istream& is, bool swap,
237                     octave::mach_info::float_format fmt);
238 
239   int write (octave::stream& os, int block_size,
240              oct_data_conv::data_type output_type, int skip,
241              octave::mach_info::float_format flt_fmt) const;
242 
243   mxArray * as_mxArray (void) const;
244 
245   bool print_as_scalar (void) const;
246 
247   void print (std::ostream& os, bool pr_as_read_syntax = false);
248 
249   void print_info (std::ostream& os, const std::string& prefix) const;
250 
251   void short_disp (std::ostream& os) const;
252 
map(unary_mapper_t umap)253   octave_value map (unary_mapper_t umap) const
254   { return to_dense ().map (umap); }
255 
256   octave_value fast_elem_extract (octave_idx_type n) const;
257 
258 protected:
259 
260   PermMatrix matrix;
261 
262   virtual octave_value to_dense (void) const;
263 
264   mutable octave_value dense_cache;
265 
266 private:
267 
268   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
269 };
270 
271 #endif
272