GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-base-mat.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1998-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_mat_h)
27 #define octave_ov_base_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 #include "MatrixType.h"
39 
40 #include "error.h"
41 #include "ovl.h"
42 #include "ov-base.h"
43 #include "ov-typeinfo.h"
44 
45 // Real matrix values.
46 
47 template <typename MT>
48 class
50 {
51 public:
52 
54  : octave_base_value (), matrix (), typ (), idx_cache () { }
55 
56  octave_base_matrix (const MT& m, const MatrixType& t = MatrixType ())
57  : octave_base_value (), matrix (m),
58  typ (t.is_known () ? new MatrixType (t) : nullptr), idx_cache ()
59  {
60  if (matrix.ndims () == 0)
61  matrix.resize (dim_vector (0, 0));
62  }
63 
65  : octave_base_value (), matrix (m.matrix),
66  typ (m.typ ? new MatrixType (*m.typ) : nullptr),
67  idx_cache (m.idx_cache ? new idx_vector (*m.idx_cache) : nullptr)
68  { }
69 
70  ~octave_base_matrix (void) { clear_cached_info (); }
71 
72  size_t byte_size (void) const { return matrix.byte_size (); }
73 
74  octave_value squeeze (void) const { return MT (matrix.squeeze ()); }
75 
76  octave_value full_value (void) const { return matrix; }
77 
78  void maybe_economize (void) { matrix.maybe_economize (); }
79 
80  // We don't need to override all three forms of subsref. The using
81  // declaration will avoid warnings about partially-overloaded virtual
82  // functions.
84 
85  octave_value subsref (const std::string& type,
86  const std::list<octave_value_list>& idx);
87 
88  octave_value_list subsref (const std::string& type,
89  const std::list<octave_value_list>& idx, int)
90  { return subsref (type, idx); }
91 
92  octave_value subsasgn (const std::string& type,
93  const std::list<octave_value_list>& idx,
94  const octave_value& rhs);
95 
96  octave_value do_index_op (const octave_value_list& idx,
97  bool resize_ok = false);
98 
99  // FIXME: should we import the functions from the base class and
100  // overload them here, or should we use a different name so we don't
101  // have to do this? Without the using declaration or a name change,
102  // the base class functions will be hidden. That may be OK, but it
103  // can also cause some confusion.
105 
106  void assign (const octave_value_list& idx, const MT& rhs);
107 
108  void assign (const octave_value_list& idx, typename MT::element_type rhs);
109 
110  void delete_elements (const octave_value_list& idx);
111 
112  dim_vector dims (void) const { return matrix.dims (); }
113 
114  octave_idx_type numel (void) const { return matrix.numel (); }
115 
116  int ndims (void) const { return matrix.ndims (); }
117 
118  octave_idx_type nnz (void) const { return matrix.nnz (); }
119 
120  octave_value reshape (const dim_vector& new_dims) const
121  { return MT (matrix.reshape (new_dims)); }
122 
123  octave_value permute (const Array<int>& vec, bool inv = false) const
124  { return MT (matrix.permute (vec, inv)); }
125 
126  octave_value resize (const dim_vector& dv, bool fill = false) const;
127 
128  octave_value all (int dim = 0) const { return matrix.all (dim); }
129  octave_value any (int dim = 0) const { return matrix.any (dim); }
130 
131  MatrixType matrix_type (void) const { return typ ? *typ : MatrixType (); }
132  MatrixType matrix_type (const MatrixType& _typ) const;
133 
135  { return octave_value (matrix.diag (k)); }
136 
138  { return octave_value (matrix.diag (m, n)); }
139 
141  { return octave_value (matrix.sort (dim, mode)); }
143  sortmode mode = ASCENDING) const
144  { return octave_value (matrix.sort (sidx, dim, mode)); }
145 
147  { return matrix.issorted (mode); }
148 
150  { return matrix.sort_rows_idx (mode); }
151 
153  { return matrix.is_sorted_rows (mode); }
154 
155  bool is_matrix_type (void) const { return true; }
156 
157  bool isnumeric (void) const { return true; }
158 
159  bool is_defined (void) const { return true; }
160 
161  bool is_constant (void) const { return true; }
162 
163  bool is_true (void) const;
164 
165  bool print_as_scalar (void) const;
166 
167  void print (std::ostream& os, bool pr_as_read_syntax = false);
168 
169  void print_info (std::ostream& os, const std::string& prefix) const;
170 
171  void short_disp (std::ostream& os) const;
172 
174 
175  std::string edit_display (const float_display_format& fmt,
176  octave_idx_type i, octave_idx_type j) const;
177 
178  MT& matrix_ref (void)
179  {
180  clear_cached_info ();
181  return matrix;
182  }
183 
184  const MT& matrix_ref (void) const
185  {
186  return matrix;
187  }
188 
190  fast_elem_extract (octave_idx_type n) const;
191 
192  bool
193  fast_elem_insert (octave_idx_type n, const octave_value& x);
194 
195 protected:
196 
197  MT matrix;
198 
200  {
201  delete idx_cache;
202  idx_cache = (idx ? new idx_vector (idx) : nullptr);
203  return idx;
204  }
205 
206  void clear_cached_info (void) const
207  {
208  delete typ; typ = nullptr;
209  delete idx_cache; idx_cache = nullptr;
210  }
211 
212  mutable MatrixType *typ;
214 
215 private:
216 
217  // No assignment.
218 
220 };
221 
222 #endif
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
Definition: Array.cc:2066
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
MatrixType matrix_type(void) const
Definition: ov-base-mat.h:131
octave_base_matrix(const MT &m, const MatrixType &t=MatrixType())
Definition: ov-base-mat.h:56
void maybe_economize(void)
Definition: ov-base-mat.h:78
int ndims(void) const
Definition: ov-base-mat.h:116
octave_value sort(Array< octave_idx_type > &sidx, octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:142
bool isnumeric(void) const
Definition: ov-base-mat.h:157
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition: ov-base-mat.h:88
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-base-mat.h:123
octave_base_matrix(void)
Definition: ov-base-mat.h:53
octave_idx_type numel(void) const
Definition: ov-base-mat.h:114
octave_base_matrix(const octave_base_matrix &m)
Definition: ov-base-mat.h:64
octave_value diag(octave_idx_type k=0) const
Definition: ov-base-mat.h:134
idx_vector * idx_cache
Definition: ov-base-mat.h:213
octave_value diag(octave_idx_type m, octave_idx_type n) const
Definition: ov-base-mat.h:137
MatrixType * typ
Definition: ov-base-mat.h:212
bool is_constant(void) const
Definition: ov-base-mat.h:161
bool is_matrix_type(void) const
Definition: ov-base-mat.h:155
const MT & matrix_ref(void) const
Definition: ov-base-mat.h:184
size_t byte_size(void) const
Definition: ov-base-mat.h:72
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-base-mat.h:146
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-base-mat.h:152
octave_value full_value(void) const
Definition: ov-base-mat.h:76
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:140
octave_value any(int dim=0) const
Definition: ov-base-mat.h:129
octave_value squeeze(void) const
Definition: ov-base-mat.h:74
octave_idx_type nnz(void) const
Definition: ov-base-mat.h:118
idx_vector set_idx_cache(const idx_vector &idx) const
Definition: ov-base-mat.h:199
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Definition: ov-base-mat.h:149
octave_value all(int dim=0) const
Definition: ov-base-mat.h:128
~octave_base_matrix(void)
Definition: ov-base-mat.h:70
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-base-mat.h:120
bool is_defined(void) const
Definition: ov-base-mat.h:159
MT & matrix_ref(void)
Definition: ov-base-mat.h:178
void clear_cached_info(void) const
Definition: ov-base-mat.h:206
dim_vector dims(void) const
Definition: ov-base-mat.h:112
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:201
virtual void assign(const std::string &, const octave_value &)
Definition: ov-base.h:302
virtual octave_value resize(const dim_vector &, bool fill=false) const
Definition: ov-base.cc:347
octave_value any(int dim=0) const
Definition: ov.h:640
octave_value all(int dim=0) const
Definition: ov.h:637
F77_RET_T const F77_DBLE * x
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)
sortmode
Definition: oct-sort.h:95
@ UNSORTED
Definition: oct-sort.h:95
@ ASCENDING
Definition: oct-sort.h:95
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))