GNU Octave 7.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-2022 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
50OCTINTERP_API
52{
53public:
54
57 { }
58
59 octave_base_sparse (const T& a)
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 (void) = default;
77
78 octave_idx_type numel (void) const { return dims ().safe_numel (); }
79
80 octave_idx_type nnz (void) const { return matrix.nnz (); }
81
82 octave_idx_type nzmax (void) const { return matrix.nzmax (); }
83
84 std::size_t byte_size (void) const { return matrix.byte_size (); }
85
86 octave_value squeeze (void) const { return matrix.squeeze (); }
87
88 octave_value full_value (void) 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.
162 }
163
164 OCTINTERP_API void delete_elements (const octave_value_list& idx);
165
166 dim_vector dims (void) 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 (void) const { return typ; }
201 { MatrixType ret = typ; typ = _typ; return ret; }
202
203 bool is_matrix_type (void) const { return true; }
204
205 bool isnumeric (void) const { return true; }
206
207 bool issparse (void) const { return true; }
208
209 bool is_defined (void) const { return true; }
210
211 bool is_constant (void) const { return true; }
212
213 OCTINTERP_API bool is_true (void) const;
214
215 OCTINTERP_API bool print_as_scalar (void) 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 (void) const;
230
231 OCTINTERP_API std::string
234
235 // These functions exists to support the MEX interface.
236 // You should not use them anywhere else.
237 const void * mex_get_data (void) const { return matrix.data (); }
238
239 const octave_idx_type * mex_get_ir (void) const { return matrix.ridx (); }
240
241 const octave_idx_type * mex_get_jc (void) const { return matrix.cidx (); }
242
243 OCTINTERP_API octave_value fast_elem_extract (octave_idx_type n) const;
244
245protected:
246
247 OCTINTERP_API octave_value
249
251
253};
254
255#endif
void invalidate_type(void)
Definition: MatrixType.h:138
OCTAVE_API SparseBoolMatrix reshape(const dim_vector &new_dims) const
Definition: boolSparse.cc:306
OCTAVE_API SparseBoolMatrix permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition: boolSparse.cc:312
OCTAVE_API SparseBoolMatrix diag(octave_idx_type k=0) const
Definition: boolSparse.cc:242
OCTAVE_API SparseBoolMatrix squeeze(void) const
Definition: boolSparse.cc:287
OCTAVE_API SparseBoolMatrix all(int dim=-1) const
Definition: boolSparse.cc:140
OCTAVE_API boolMatrix matrix_value(void) const
Definition: boolSparse.cc:248
OCTAVE_API SparseBoolMatrix any(int dim=-1) const
Definition: boolSparse.cc:146
T * data(void)
Definition: Sparse.h:574
octave_idx_type ndims(void) const
Definition: Sparse.h:609
std::size_t byte_size(void) const
Definition: Sparse.h:364
OCTAVE_API void resize(octave_idx_type r, octave_idx_type c)
Definition: Sparse.cc:991
OCTAVE_API void assign(const octave::idx_vector &i, const Sparse< T, Alloc > &rhs)
Definition: Sparse.cc:1879
octave_idx_type nnz(void) const
Actual number of nonzero terms.
Definition: Sparse.h:339
dim_vector dims(void) const
Definition: Sparse.h:371
octave_idx_type * ridx(void)
Definition: Sparse.h:583
OCTAVE_API Sparse< T, Alloc > sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: Sparse.cc:2317
octave_idx_type * cidx(void)
Definition: Sparse.h:596
octave_idx_type nzmax(void) const
Amount of storage for nonzero elements.
Definition: Sparse.h:336
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
OCTAVE_API octave_idx_type safe_numel(void) const
The following function will throw a std::bad_alloc () exception if the requested size is larger than ...
Definition: dim-vector.cc:98
void set_pos_if_unset(octave_idx_type nd_arg, octave_idx_type dim_arg)
OCTINTERP_API bool print_as_scalar(void) const
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
const octave_idx_type * mex_get_jc(void) const
octave_value diag(octave_idx_type k=0) const
octave_value permute(const Array< int > &vec, bool inv=false) const
bool is_matrix_type(void) const
bool is_defined(void) const
~octave_base_sparse(void)=default
bool issparse(void) const
OCTINTERP_API octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
octave_value squeeze(void) const
octave_idx_type nnz(void) const
OCTINTERP_API void print(std::ostream &os, bool pr_as_read_syntax=false)
std::size_t byte_size(void) const
bool isnumeric(void) const
octave_idx_type nzmax(void) const
OCTINTERP_API bool load_ascii(std::istream &is)
const octave_idx_type * mex_get_ir(void) const
OCTINTERP_API 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
MatrixType matrix_type(void) const
octave_value reshape(const dim_vector &new_dims) const
void assign(const octave_value_list &idx, const RHS_T &rhs)
OCTINTERP_API bool save_ascii(std::ostream &os)
octave_value all(int dim=0) const
OCTINTERP_API octave_value fast_elem_extract(octave_idx_type n) const
octave_idx_type numel(void) const
octave_base_sparse(const T &a, const MatrixType &t)
OCTINTERP_API 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(void) const
OCTINTERP_API void delete_elements(const octave_value_list &idx)
const void * mex_get_data(void) const
bool is_constant(void) const
octave_value any(int dim=0) const
sortmode issorted(sortmode mode=UNSORTED) const
MatrixType matrix_type(const MatrixType &_typ) const
OCTINTERP_API octave_value resize(const dim_vector &dv, bool=false) const
OCTINTERP_API void print_info(std::ostream &os, const std::string &prefix) const
OCTINTERP_API octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
OCTINTERP_API 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:201
virtual octave_value diag(octave_idx_type k=0) const
Definition: ov-base.cc:1036
virtual void assign(const std::string &, const octave_value &)
Definition: ov-base.h:332
octave_value map(unary_mapper_t umap) const
octave_idx_type length(void) const
Definition: ovl.h:113
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov.h:1543
void error(const char *fmt,...)
Definition: error.cc:980
bool is_true(const std::string &s)
static float_display_format get_edit_display_format(const octave_value &val)
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