GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-base-sparse.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1998-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_base_sparse_h)
27 #define octave_ov_base_sparse_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstdlib>
32 
33 #include <iosfwd>
34 #include <string>
35 
36 #include "str-vec.h"
37 
38 #include "error.h"
39 #include "ovl.h"
40 #include "ov-base.h"
41 #include "ov-typeinfo.h"
42 
43 #include "boolSparse.h"
44 #include "MatrixType.h"
45 
47 
48 template <typename T>
49 class
50 OCTINTERP_API
52 {
53 public:
54 
56  : octave_base_value (), matrix (), typ (MatrixType ())
57  { }
58 
59  octave_base_sparse (const T& a)
60  : octave_base_value (), matrix (a), typ (MatrixType ())
61  {
62  if (matrix.ndims () == 0)
63  matrix.resize (dim_vector (0, 0));
64  }
65 
66  octave_base_sparse (const T& a, const MatrixType& t)
67  : octave_base_value (), matrix (a), typ (t)
68  {
69  if (matrix.ndims () == 0)
70  matrix.resize (dim_vector (0, 0));
71  }
72 
74  : octave_base_value (), matrix (a.matrix), typ (a.typ) { }
75 
76  ~octave_base_sparse () = default;
77 
78  octave_idx_type numel () const { return dims ().safe_numel (); }
79 
80  octave_idx_type nnz () const { return matrix.nnz (); }
81 
82  octave_idx_type nzmax () const { return matrix.nzmax (); }
83 
84  std::size_t byte_size () const { return matrix.byte_size (); }
85 
86  octave_value squeeze () const { return matrix.squeeze (); }
87 
88  octave_value full_value () const { return matrix.matrix_value (); }
89 
90  // We don't need to override all three forms of subsref. The using
91  // declaration will avoid warnings about partially-overloaded virtual
92  // functions.
94 
95  OCTINTERP_API octave_value
96  subsref (const std::string& type, const std::list<octave_value_list>& idx);
97 
98  octave_value_list subsref (const std::string& type,
99  const std::list<octave_value_list>& idx, int)
100  { return subsref (type, idx); }
101 
102  OCTINTERP_API octave_value
103  subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
104  const octave_value& rhs);
105 
106  // FIXME: should we import the functions from the base class and
107  // overload them here, or should we use a different name so we don't
108  // have to do this? Without the using declaration or a name change,
109  // the base class functions will be hidden. That may be OK, but it
110  // can also cause some confusion.
112 
113  template <typename RHS_T>
114  void assign (const octave_value_list& idx, const RHS_T& rhs)
115  {
116  octave_idx_type len = idx.length ();
117 
118  // If we catch an indexing error in index_vector, we flag an error in
119  // index k. Ensure it is the right value before each idx_vector call.
120  // Same variable as used in the for loop in the default case.
121 
122  octave_idx_type k = 0;
123 
124  try
125  {
126  switch (len)
127  {
128  case 1:
129  {
130  octave::idx_vector i = idx (0).index_vector ();
131 
132  matrix.assign (i, rhs);
133 
134  break;
135  }
136 
137  case 2:
138  {
139  octave::idx_vector i = idx (0).index_vector ();
140 
141  k = 1;
142  octave::idx_vector j = idx (1).index_vector ();
143 
144  matrix.assign (i, j, rhs);
145 
146  break;
147  }
148 
149  default:
150  error ("sparse indexing needs 1 or 2 indices");
151  }
152  }
153  catch (octave::index_exception& ie)
154  {
155  // Rethrow to allow more info to be reported later.
156  ie.set_pos_if_unset (len, k+1);
157  throw;
158  }
159 
160  // Invalidate matrix type.
161  typ.invalidate_type ();
162  }
163 
164  OCTINTERP_API void delete_elements (const octave_value_list& idx);
165 
166  dim_vector dims () const { return matrix.dims (); }
167 
168  OCTINTERP_API octave_value
169  do_index_op (const octave_value_list& idx, bool resize_ok = false);
170 
171  octave_value reshape (const dim_vector& new_dims) const
172  { return T (matrix.reshape (new_dims)); }
173 
174  octave_value permute (const Array<int>& vec, bool inv = false) const
175  { return T (matrix.permute (vec, inv)); }
176 
177  OCTINTERP_API octave_value resize (const dim_vector& dv, bool = false) const;
178 
179  octave_value all (int dim = 0) const { return matrix.all (dim); }
180  octave_value any (int dim = 0) const { return matrix.any (dim); }
181 
182  // We don't need to override both forms of the diag method. The using
183  // declaration will avoid warnings about partially-overloaded virtual
184  // functions.
186 
188  { return octave_value (matrix.diag (k)); }
189 
191  { return octave_value (matrix.sort (dim, mode)); }
193  sortmode mode = ASCENDING) const
194  { return octave_value (matrix.sort (sidx, dim, mode)); }
195 
197  { return full_value ().issorted (mode); }
198 
199  MatrixType matrix_type () const { return typ; }
200  MatrixType matrix_type (const MatrixType& _typ) const
201  { MatrixType ret = typ; typ = _typ; return ret; }
202 
203  bool is_matrix_type () const { return true; }
204 
205  bool isnumeric () const { return true; }
206 
207  bool issparse () const { return true; }
208 
209  bool is_defined () const { return true; }
210 
211  bool is_constant () const { return true; }
212 
213  OCTINTERP_API bool is_true () const;
214 
215  OCTINTERP_API bool print_as_scalar () const;
216 
217  OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
218 
219  OCTINTERP_API void
220  print_info (std::ostream& os, const std::string& prefix) const;
221 
222  OCTINTERP_API void
223  print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
224 
225  OCTINTERP_API bool save_ascii (std::ostream& os);
226 
227  OCTINTERP_API bool load_ascii (std::istream& is);
228 
229  OCTINTERP_API float_display_format get_edit_display_format () const;
230 
231  OCTINTERP_API std::string
233  octave_idx_type i, octave_idx_type j) const;
234 
235  // These functions exists to support the MEX interface.
236  // You should not use them anywhere else.
237  const void * mex_get_data () const { return matrix.data (); }
238 
239  const octave_idx_type * mex_get_ir () const { return matrix.ridx (); }
240 
241  const octave_idx_type * mex_get_jc () const { return matrix.cidx (); }
242 
243  OCTINTERP_API octave_value fast_elem_extract (octave_idx_type n) const;
244 
245 protected:
246 
247  OCTINTERP_API octave_value
249 
251 
252  mutable MatrixType typ;
253 };
254 
255 #endif
void invalidate_type()
Definition: MatrixType.h:138
SparseBoolMatrix reshape(const dim_vector &new_dims) const
Definition: boolSparse.cc:306
SparseBoolMatrix permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition: boolSparse.cc:312
SparseBoolMatrix diag(octave_idx_type k=0) const
Definition: boolSparse.cc:242
SparseBoolMatrix all(int dim=-1) const
Definition: boolSparse.cc:140
boolMatrix matrix_value() const
Definition: boolSparse.cc:248
SparseBoolMatrix squeeze() const
Definition: boolSparse.cc:287
SparseBoolMatrix any(int dim=-1) const
Definition: boolSparse.cc:146
octave_idx_type nzmax() const
Amount of storage for nonzero elements.
Definition: Sparse.h:336
octave_idx_type * cidx()
Definition: Sparse.h:596
void assign(const octave::idx_vector &i, const Sparse< T, Alloc > &rhs)
Definition: Sparse.cc:1881
void resize(octave_idx_type r, octave_idx_type c)
Definition: Sparse.cc:993
T * data()
Definition: Sparse.h:574
Sparse< T, Alloc > sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: Sparse.cc:2318
octave_idx_type * ridx()
Definition: Sparse.h:583
octave_idx_type ndims() const
Definition: Sparse.h:609
std::size_t byte_size() const
Definition: Sparse.h:364
octave_idx_type nnz() const
Actual number of nonzero terms.
Definition: Sparse.h:339
dim_vector dims() const
Definition: Sparse.h:371
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type safe_numel() const
The following function will throw a std::bad_alloc () exception if the requested size is larger than ...
Definition: dim-vector.cc:98
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:248
bool is_matrix_type() const
MatrixType matrix_type() const
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
octave_value diag(octave_idx_type k=0) const
octave_value permute(const Array< int > &vec, bool inv=false) const
bool is_constant() const
void print(std::ostream &os, bool pr_as_read_syntax=false)
octave_value squeeze() const
std::size_t byte_size() const
octave_idx_type nzmax() const
bool is_defined() const
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
octave_value sort(Array< octave_idx_type > &sidx, octave_idx_type dim=0, sortmode mode=ASCENDING) const
octave_value reshape(const dim_vector &new_dims) const
void assign(const octave_value_list &idx, const RHS_T &rhs)
const void * mex_get_data() const
const octave_idx_type * mex_get_ir() const
octave_value all(int dim=0) const
octave_value fast_elem_extract(octave_idx_type n) const
octave_base_sparse(const T &a, const MatrixType &t)
octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
octave_base_sparse(const T &a)
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
octave_base_sparse(const octave_base_sparse &a)
octave_value full_value() const
void delete_elements(const octave_value_list &idx)
~octave_base_sparse()=default
octave_value any(int dim=0) const
bool issparse() const
sortmode issorted(sortmode mode=UNSORTED) const
float_display_format get_edit_display_format() const
MatrixType matrix_type(const MatrixType &_typ) const
octave_idx_type numel() const
octave_value resize(const dim_vector &dv, bool=false) const
void print_info(std::ostream &os, const std::string &prefix) const
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
bool isnumeric() const
const octave_idx_type * mex_get_jc() const
octave_idx_type nnz() const
std::string edit_display(const float_display_format &fmt, octave_idx_type i, octave_idx_type j) const
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:248
virtual octave_value diag(octave_idx_type k=0) const
Definition: ov-base.cc:1041
virtual void assign(const std::string &, const octave_value &)
Definition: ov-base.h:354
octave_value map(unary_mapper_t umap) const
octave_idx_type length() const
Definition: ovl.h:113
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov.h:1422
void() error(const char *fmt,...)
Definition: error.cc:988
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
bool is_true(const std::string &s)
Definition: mkoctfile.cc:604
octave_idx_type n
Definition: mx-inlines.cc:761
sortmode
Definition: oct-sort.h:97
@ UNSORTED
Definition: oct-sort.h:97
@ ASCENDING
Definition: oct-sort.h:97
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
F77_RET_T len
Definition: xerbla.cc:61