GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-base-sparse.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1998-2025 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
48template <typename T>
49class OCTINTERP_TEMPLATE_API octave_base_sparse : public octave_base_value
50{
51public:
52
53 OCTINTERP_OVERRIDABLE_FUNC_API
55 : octave_base_value (), matrix (), typ (MatrixType ())
56 { }
57
58 OCTINTERP_OVERRIDABLE_FUNC_API
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 OCTINTERP_OVERRIDABLE_FUNC_API
67 octave_base_sparse (const T& a, const MatrixType& t)
68 : octave_base_value (), matrix (a), typ (t)
69 {
70 if (matrix.ndims () == 0)
71 matrix.resize (dim_vector (0, 0));
72 }
73
74 OCTINTERP_OVERRIDABLE_FUNC_API
76 : octave_base_value (), matrix (a.matrix), typ (a.typ) { }
77
78 OCTINTERP_OVERRIDABLE_FUNC_API ~octave_base_sparse () = default;
79
80 OCTINTERP_OVERRIDABLE_FUNC_API octave_idx_type numel () const
81 { return dims ().safe_numel (); }
82
83 OCTINTERP_OVERRIDABLE_FUNC_API octave_idx_type nnz () const
84 { return matrix.nnz (); }
85
86 OCTINTERP_OVERRIDABLE_FUNC_API octave_idx_type nzmax () const
87 { return matrix.nzmax (); }
88
89 OCTINTERP_OVERRIDABLE_FUNC_API std::size_t byte_size () const
90 { return matrix.byte_size (); }
91
92 OCTINTERP_OVERRIDABLE_FUNC_API octave_value squeeze () const
93 { return matrix.squeeze (); }
94
95 OCTINTERP_OVERRIDABLE_FUNC_API octave_value full_value () const
96 { return matrix.matrix_value (); }
97
98 // We don't need to override all three forms of subsref. The using
99 // declaration will avoid warnings about partially-overloaded virtual
100 // functions.
102
103 OCTINTERP_API octave_value
104 subsref (const std::string& type, const std::list<octave_value_list>& idx);
105
106 OCTINTERP_OVERRIDABLE_FUNC_API octave_value_list
107 subsref (const std::string& type, const std::list<octave_value_list>& idx,
108 int)
109 { return subsref (type, idx); }
110
111 OCTINTERP_API octave_value
112 subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
113 const octave_value& rhs);
114
115 // FIXME: should we import the functions from the base class and
116 // overload them here, or should we use a different name so we don't
117 // have to do this? Without the using declaration or a name change,
118 // the base class functions will be hidden. That may be OK, but it
119 // can also cause some confusion.
121
122 template <typename RHS_T>
123 void assign (const octave_value_list& idx, const RHS_T& rhs)
124 {
125 octave_idx_type len = idx.length ();
126
127 // If we catch an indexing error in index_vector, we flag an error in
128 // index k. Ensure it is the right value before each idx_vector call.
129 // Same variable as used in the for loop in the default case.
130
131 octave_idx_type k = 0;
132
133 try
134 {
135 switch (len)
136 {
137 case 1:
138 {
139 octave::idx_vector i = idx (0).index_vector ();
140
141 matrix.assign (i, rhs);
142
143 break;
144 }
145
146 case 2:
147 {
148 octave::idx_vector i = idx (0).index_vector ();
149
150 k = 1;
151 octave::idx_vector j = idx (1).index_vector ();
152
153 matrix.assign (i, j, rhs);
154
155 break;
156 }
157
158 default:
159 error ("sparse indexing needs 1 or 2 indices");
160 }
161 }
162 catch (octave::index_exception& ie)
163 {
164 // Rethrow to allow more info to be reported later.
165 ie.set_pos_if_unset (len, k+1);
166 throw;
167 }
168
169 // Invalidate matrix type.
170 typ.invalidate_type ();
171 }
172
173 OCTINTERP_API void delete_elements (const octave_value_list& idx);
174
175 OCTINTERP_OVERRIDABLE_FUNC_API dim_vector dims () const
176 { return matrix.dims (); }
177
178 OCTINTERP_API octave_value
179 do_index_op (const octave_value_list& idx, bool resize_ok = false);
180
181 OCTINTERP_OVERRIDABLE_FUNC_API octave_value reshape (const dim_vector& new_dims) const
182 { return T (matrix.reshape (new_dims)); }
183
184 OCTINTERP_OVERRIDABLE_FUNC_API octave_value
185 permute (const Array<int>& vec, bool inv = false) const
186 { return T (matrix.permute (vec, inv)); }
187
188 OCTINTERP_API octave_value resize (const dim_vector& dv, bool = false) const;
189
190 OCTINTERP_OVERRIDABLE_FUNC_API octave_value all (int dim = 0) const
191 { return matrix.all (dim); }
192
193 OCTINTERP_OVERRIDABLE_FUNC_API octave_value any (int dim = 0) const
194 { return matrix.any (dim); }
195
196 // We don't need to override both forms of the diag method. The using
197 // declaration will avoid warnings about partially-overloaded virtual
198 // functions.
200
201 OCTINTERP_OVERRIDABLE_FUNC_API octave_value diag (octave_idx_type k = 0) const
202 { return octave_value (matrix.diag (k)); }
203
204 OCTINTERP_OVERRIDABLE_FUNC_API octave_value
205 sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const
206 { return octave_value (matrix.sort (dim, mode)); }
207
208 OCTINTERP_OVERRIDABLE_FUNC_API octave_value
210 sortmode mode = ASCENDING) const
211 { return octave_value (matrix.sort (sidx, dim, mode)); }
212
213 OCTINTERP_OVERRIDABLE_FUNC_API sortmode
215 { return full_value ().issorted (mode); }
216
217 OCTINTERP_OVERRIDABLE_FUNC_API MatrixType matrix_type () const
218 { return typ; }
219 OCTINTERP_OVERRIDABLE_FUNC_API MatrixType
220 matrix_type (const MatrixType& _typ) const
221 { MatrixType ret = typ; typ = _typ; return ret; }
222
223 OCTINTERP_OVERRIDABLE_FUNC_API bool is_matrix_type () const { return true; }
224
225 OCTINTERP_OVERRIDABLE_FUNC_API bool isnumeric () const { return true; }
226
227 OCTINTERP_OVERRIDABLE_FUNC_API bool issparse () const { return true; }
228
229 OCTINTERP_OVERRIDABLE_FUNC_API bool is_defined () const { return true; }
230
231 OCTINTERP_OVERRIDABLE_FUNC_API bool is_constant () const { return true; }
232
233 OCTINTERP_API bool is_true () const;
234
235 OCTINTERP_API bool print_as_scalar () const;
236
237 OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
238
239 OCTINTERP_API void
240 print_info (std::ostream& os, const std::string& prefix) const;
241
242 OCTINTERP_API void
243 print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
244
245 OCTINTERP_API bool save_ascii (std::ostream& os);
246
247 OCTINTERP_API bool load_ascii (std::istream& is);
248
249 OCTINTERP_API float_display_format get_edit_display_format () const;
250
251 OCTINTERP_API std::string
254
255 // These functions exists to support the MEX interface.
256 // You should not use them anywhere else.
257 OCTINTERP_OVERRIDABLE_FUNC_API const void * mex_get_data () const
258 { return matrix.data (); }
259
260 OCTINTERP_OVERRIDABLE_FUNC_API const octave_idx_type * mex_get_ir () const
261 { return matrix.ridx (); }
262
263 OCTINTERP_OVERRIDABLE_FUNC_API const octave_idx_type * mex_get_jc () const
264 { return matrix.cidx (); }
265
266 OCTINTERP_API octave_value fast_elem_extract (octave_idx_type n) const;
267
268protected:
269
270 OCTINTERP_API octave_value
272
274
276};
277
278#endif
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
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
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
octave_value squeeze() const
std::size_t byte_size() const
octave_idx_type nzmax() const
bool is_defined() 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)
octave_value all(int dim=0) const
const octave_idx_type * mex_get_jc() const
octave_base_sparse(const T &a, const MatrixType &t)
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
~octave_base_sparse()=default
octave_value any(int dim=0) const
dim_vector dims() const
bool issparse() const
sortmode issorted(sortmode mode=UNSORTED) const
MatrixType matrix_type(const MatrixType &_typ) const
octave_idx_type numel() const
const octave_idx_type * mex_get_ir() const
bool isnumeric() const
octave_idx_type nnz() const
const void * mex_get_data() const
virtual octave_value full_value() const
Definition ov-base.cc:147
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition ov-base.cc:245
virtual bool save_ascii(std::ostream &os)
Definition ov-base.cc:986
virtual octave_value fast_elem_extract(octave_idx_type n) const
Definition ov-base.cc:1394
virtual octave_value diag(octave_idx_type k=0) const
Definition ov-base.cc:1036
virtual bool is_true() const
Definition ov-base.h:529
virtual octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition ov-base.cc:270
virtual float_display_format get_edit_display_format() const
Definition ov-base.cc:503
virtual octave_value map(unary_mapper_t) const
Definition ov-base.cc:1170
virtual void assign(const std::string &, const octave_value &)
Definition ov-base.h:363
virtual std::string edit_display(const float_display_format &, octave_idx_type, octave_idx_type) const
Definition ov-base.h:769
virtual void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition ov-base.cc:463
virtual bool load_ascii(std::istream &is)
Definition ov-base.cc:992
virtual dim_vector dims() const
Definition ov-base.h:382
virtual bool print_as_scalar() const
Definition ov-base.h:746
virtual void print_info(std::ostream &os, const std::string &prefix) const
Definition ov-base.cc:509
virtual void print(std::ostream &os, bool pr_as_read_syntax=false)
Definition ov-base.cc:457
virtual octave_value resize(const dim_vector &, bool fill=false) const
Definition ov-base.cc:401
virtual octave_value subsasgn(const std::string &type, const std::list< octave_value_list > &idx, const octave_value &rhs)
Definition ov-base.cc:294
friend class octave_value
Definition ov-base.h:278
octave_idx_type length() const
Definition ovl.h:111
octave_value any(int dim=0) const
Definition ov.h:687
sortmode issorted(sortmode mode=UNSORTED) const
Definition ov.h:1437
octave_value all(int dim=0) const
Definition ov.h:684
Matrix matrix_value(bool frc_str_conv=false) const
Definition ov.h:859
octave_value squeeze() const
Definition ov.h:431
void error(const char *fmt,...)
Definition error.cc:1003
sortmode
Definition oct-sort.h:97
@ UNSORTED
Definition oct-sort.h:97
@ ASCENDING
Definition oct-sort.h:97
F77_RET_T len
Definition xerbla.cc:61