GNU Octave  6.2.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-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_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
51 {
52 public:
53 
55  : octave_base_value (), matrix (), typ (MatrixType ())
56  { }
57 
58  octave_base_sparse (const T& a)
59  : octave_base_value (), matrix (a), typ (MatrixType ())
60  {
61  if (matrix.ndims () == 0)
62  matrix.resize (dim_vector (0, 0));
63  }
64 
65  octave_base_sparse (const T& a, const MatrixType& t)
66  : octave_base_value (), matrix (a), typ (t)
67  {
68  if (matrix.ndims () == 0)
69  matrix.resize (dim_vector (0, 0));
70  }
71 
73  : octave_base_value (), matrix (a.matrix), typ (a.typ) { }
74 
75  ~octave_base_sparse (void) = default;
76 
77  octave_idx_type numel (void) const { return dims ().safe_numel (); }
78 
79  octave_idx_type nnz (void) const { return matrix.nnz (); }
80 
81  octave_idx_type nzmax (void) const { return matrix.nzmax (); }
82 
83  size_t byte_size (void) const { return matrix.byte_size (); }
84 
85  octave_value squeeze (void) const { return matrix.squeeze (); }
86 
87  octave_value full_value (void) const { return matrix.matrix_value (); }
88 
89  // We don't need to override all three forms of subsref. The using
90  // declaration will avoid warnings about partially-overloaded virtual
91  // functions.
93 
94  octave_value subsref (const std::string& type,
95  const std::list<octave_value_list>& idx);
96 
97  octave_value_list subsref (const std::string& type,
98  const std::list<octave_value_list>& idx, int)
99  { return subsref (type, idx); }
100 
101  octave_value subsasgn (const std::string& type,
102  const std::list<octave_value_list>& idx,
103  const octave_value& rhs);
104 
105  // FIXME: should we import the functions from the base class and
106  // overload them here, or should we use a different name so we don't
107  // have to do this? Without the using declaration or a name change,
108  // the base class functions will be hidden. That may be OK, but it
109  // can also cause some confusion.
111 
112  void assign (const octave_value_list& idx, const T& rhs);
113 
114  void delete_elements (const octave_value_list& idx);
115 
116  dim_vector dims (void) const { return matrix.dims (); }
117 
119  bool resize_ok = false);
120 
121  octave_value reshape (const dim_vector& new_dims) const
122  { return T (matrix.reshape (new_dims)); }
123 
124  octave_value permute (const Array<int>& vec, bool inv = false) const
125  { return T (matrix.permute (vec, inv)); }
126 
127  octave_value resize (const dim_vector& dv, bool = false) const;
128 
129  octave_value all (int dim = 0) const { return matrix.all (dim); }
130  octave_value any (int dim = 0) const { return matrix.any (dim); }
131 
132  // We don't need to override both forms of the diag method. The using
133  // declaration will avoid warnings about partially-overloaded virtual
134  // functions.
136 
138  { return octave_value (matrix.diag (k)); }
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 full_value ().issorted (mode); }
148 
149  MatrixType matrix_type (void) const { return typ; }
150  MatrixType matrix_type (const MatrixType& _typ) const
151  { MatrixType ret = typ; typ = _typ; return ret; }
152 
153  bool is_matrix_type (void) const { return true; }
154 
155  bool isnumeric (void) const { return true; }
156 
157  bool issparse (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 print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
172 
173  bool save_ascii (std::ostream& os);
174 
175  bool load_ascii (std::istream& is);
176 
178 
179  std::string edit_display (const float_display_format& fmt,
180  octave_idx_type i, octave_idx_type j) const;
181 
182  // Unsafe. These functions exists to support the MEX interface.
183  // You should not use them anywhere else.
184  void * mex_get_data (void) const { return matrix.mex_get_data (); }
185 
186  octave_idx_type * mex_get_ir (void) const { return matrix.mex_get_ir (); }
187 
188  octave_idx_type * mex_get_jc (void) const { return matrix.mex_get_jc (); }
189 
191 
192 protected:
193 
195 
197 
198  mutable MatrixType typ;
199 };
200 
201 #endif
SparseBoolMatrix reshape(const dim_vector &new_dims) const
Definition: boolSparse.cc:308
SparseBoolMatrix permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition: boolSparse.cc:314
SparseBoolMatrix diag(octave_idx_type k=0) const
Definition: boolSparse.cc:244
SparseBoolMatrix squeeze(void) const
Definition: boolSparse.cc:289
SparseBoolMatrix all(int dim=-1) const
Definition: boolSparse.cc:140
boolMatrix matrix_value(void) const
Definition: boolSparse.cc:250
SparseBoolMatrix any(int dim=-1) const
Definition: boolSparse.cc:146
octave_idx_type ndims(void) const
Definition: Sparse.h:505
Sparse< T > sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: Sparse.cc:2244
octave_idx_type * mex_get_ir(void) const
Definition: Sparse.h:528
octave_idx_type nnz(void) const
Actual number of nonzero terms.
Definition: Sparse.h:238
octave_idx_type nzmax(void) const
Amount of storage for nonzero elements.
Definition: Sparse.h:235
void resize(octave_idx_type r, octave_idx_type c)
Definition: Sparse.cc:963
size_t byte_size(void) const
Definition: Sparse.h:263
dim_vector dims(void) const
Definition: Sparse.h:270
void * mex_get_data(void) const
Definition: Sparse.h:526
octave_idx_type * mex_get_jc(void) const
Definition: Sparse.h:533
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
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:115
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:201
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_matrix_type(void) const
bool is_defined(void) const
~octave_base_sparse(void)=default
bool issparse(void) const
octave_idx_type * mex_get_ir(void) const
octave_value squeeze(void) const
octave_idx_type nnz(void) const
void print(std::ostream &os, bool pr_as_read_syntax=false)
bool isnumeric(void) const
octave_idx_type nzmax(void) const
void * mex_get_data(void) 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
MatrixType matrix_type(void) const
octave_value reshape(const dim_vector &new_dims) const
virtual void assign(const std::string &, const octave_value &)
Definition: ov-base.h:302
octave_value all(int dim=0) const
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)
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
size_t byte_size(void) const
octave_idx_type * mex_get_jc(void) const
void delete_elements(const octave_value_list &idx)
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
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)
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:974
virtual void assign(const std::string &, const octave_value &)
Definition: ov-base.h:302
octave_value map(unary_mapper_t umap) const
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov.h:1345
bool is_true(const std::string &s)
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()) ? '\'' :'"'))