GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
MSparse.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_MSparse_h)
27#define octave_MSparse_h 1
28
29#include "octave-config.h"
30
31#include "Array-util.h"
32#include "MArray.h"
33#include "Sparse.h"
34#include "lo-error.h"
35#include "quit.h"
36
37
38// forward declare template with visibility attributes
39template <typename T> class OCTAVE_API MSparse;
40
41// Two dimensional sparse array with math ops.
42template <typename T>
43class OCTAVE_API MSparse : public Sparse<T>
44{
45public:
46
47 MSparse () : Sparse<T> () { }
48
50
52 : Sparse<T> (dv, nz) { }
53
54 MSparse (const MSparse<T>& a) : Sparse<T> (a) { }
55
56 MSparse (const MSparse<T>& a, const dim_vector& dv) : Sparse<T> (a, dv) { }
57
58 MSparse (const Sparse<T>& a) : Sparse<T> (a) { }
59
60 template <typename U>
61 MSparse (const Sparse<U>& a) : Sparse<T> (a) { }
62
63 MSparse (const Array<T>& a, const octave::idx_vector& r, const octave::idx_vector& c,
64 octave_idx_type nr = -1, octave_idx_type nc = -1,
65 bool sum_terms = true, octave_idx_type nzm = -1)
66 : Sparse<T> (a, r, c, nr, nc, sum_terms, nzm) { }
67
69 : Sparse<T> (r, c, val) { }
70
71 explicit MSparse (const PermMatrix& a) : Sparse<T> (a) { }
72
74 : Sparse<T> (r, c, num_nz) { }
75
76 ~MSparse () = default;
77
79 {
81 return *this;
82 }
83
85 {
86 Sparse<T>::insert (a, r, c);
87 return *this;
88 }
89
91 {
92 Sparse<T>::insert (a, indx);
93 return *this;
94 }
95
97
98 MSparse<T> squeeze () const { return Sparse<T>::squeeze (); }
99
100 MSparse<T> reshape (const dim_vector& new_dims) const
101 { return Sparse<T>::reshape (new_dims); }
102
103 MSparse<T> permute (const Array<octave_idx_type>& vec, bool inv = false) const
104 { return Sparse<T>::permute (vec, inv); }
105
107 { return Sparse<T>::ipermute (vec); }
108
110 {
111 return Sparse<T>::diag (k);
112 }
113
114 // FIXME: should go away.
115 template <typename U>
117 map (U (&fcn) (T)) const
118 { return Sparse<T>::template map<U> (fcn); }
119
120 template <typename U>
122 map (U (&fcn) (const T&)) const
123 { return Sparse<T>::template map<U> (fcn); }
124};
125
126// Include operator templates for MSparse
127#include "MSparse.cc"
128
129// A macro that can be used to declare and instantiate OP= operators.
130#define SPARSE_OP_ASSIGN_DECL(T, OP, API) \
131 template API MSparse<T>& \
132 operator OP (MSparse<T>&, const MSparse<T>&)
133
134// A macro that can be used to declare and instantiate unary operators.
135#define SPARSE_UNOP_DECL(T, OP, API) \
136 template API MSparse<T> \
137 operator OP (const MSparse<T>&)
138
139// A macro that can be used to declare and instantiate binary operators.
140#define SPARSE_BINOP_DECL(A_T, T, F, API, X_T, Y_T) \
141 template API A_T<T> \
142 F (const X_T&, const Y_T&)
143
144// A function that can be used to forward OP= operations from derived
145// classes back to us.
146#define SPARSE_OP_ASSIGN_FWD_FCN(R, F, T, C_X, X_T, C_Y, Y_T) \
147 inline R \
148 F (X_T& x, const Y_T& y) \
149 { \
150 return R (F (C_X (x), C_Y (y))); \
151 }
152
153// A function that can be used to forward unary operations from derived
154// classes back to us.
155#define SPARSE_UNOP_FWD_FCN(R, F, T, C_X, X_T) \
156 inline R \
157 F (const X_T& x) \
158 { \
159 return R (F (C_X (x))); \
160 }
161
162// A function that can be used to forward binary operations from derived
163// classes back to us.
164#define SPARSE_BINOP_FWD_FCN(R, F, T, C_X, X_T, C_Y, Y_T) \
165 inline R \
166 F (const X_T& x, const Y_T& y) \
167 { \
168 return R (F (C_X (x), C_Y (y))); \
169 }
170
171// Instantiate all the MSparse friends for MSparse element type T.
172#define INSTANTIATE_SPARSE_FRIENDS(T, API) \
173 SPARSE_OP_ASSIGN_DECL (T, +=, API); \
174 SPARSE_OP_ASSIGN_DECL (T, -=, API); \
175 SPARSE_UNOP_DECL (T, +, API); \
176 SPARSE_UNOP_DECL (T, -, API); \
177 SPARSE_BINOP_DECL (MArray, T, operator +, API, MSparse<T>, T); \
178 SPARSE_BINOP_DECL (MArray, T, operator -, API, MSparse<T>, T); \
179 SPARSE_BINOP_DECL (MSparse, T, operator *, API, MSparse<T>, T); \
180 SPARSE_BINOP_DECL (MSparse, T, operator /, API, MSparse<T>, T); \
181 SPARSE_BINOP_DECL (MArray, T, operator +, API, T, MSparse<T>); \
182 SPARSE_BINOP_DECL (MArray, T, operator -, API, T, MSparse<T>); \
183 SPARSE_BINOP_DECL (MSparse, T, operator *, API, T, MSparse<T>); \
184 SPARSE_BINOP_DECL (MSparse, T, operator /, API, T, MSparse<T>); \
185 SPARSE_BINOP_DECL (MSparse, T, operator +, API, MSparse<T>, MSparse<T>); \
186 SPARSE_BINOP_DECL (MSparse, T, operator -, API, MSparse<T>, MSparse<T>); \
187 SPARSE_BINOP_DECL (MSparse, T, quotient, API, MSparse<T>, MSparse<T>); \
188 SPARSE_BINOP_DECL (MSparse, T, product, API, MSparse<T>, MSparse<T>);
189
190// Define all the MSparse forwarding functions for return type R and
191// MSparse element type T
192#define SPARSE_FORWARD_DEFS(B, R, F, T) \
193 SPARSE_OP_ASSIGN_FWD_FCN (R, operator +=, T, dynamic_cast<B<T>&>, \
194 R, dynamic_cast<const B<T>&>, R) \
195 SPARSE_OP_ASSIGN_FWD_FCN (R, operator -=, T, dynamic_cast<B<T>&>, \
196 R, dynamic_cast<const B<T>&>, R) \
197 SPARSE_UNOP_FWD_FCN (R, operator +, T, dynamic_cast<const B<T>&>, R) \
198 SPARSE_UNOP_FWD_FCN (R, operator -, T, dynamic_cast<const B<T>&>, R) \
199 SPARSE_BINOP_FWD_FCN (F, operator +, T, dynamic_cast<const B<T>&>, R, , T) \
200 SPARSE_BINOP_FWD_FCN (F, operator -, T, dynamic_cast<const B<T>&>, R, , T) \
201 SPARSE_BINOP_FWD_FCN (R, operator *, T, dynamic_cast<const B<T>&>, R, , T) \
202 SPARSE_BINOP_FWD_FCN (R, operator /, T, dynamic_cast<const B<T>&>, R, , T) \
203 SPARSE_BINOP_FWD_FCN (F, operator +, T, , T, dynamic_cast<const B<T>&>, R) \
204 SPARSE_BINOP_FWD_FCN (F, operator -, T, , T, dynamic_cast<const B<T>&>, R) \
205 SPARSE_BINOP_FWD_FCN (R, operator *, T, , T, dynamic_cast<const B<T>&>, R) \
206 SPARSE_BINOP_FWD_FCN (R, operator /, T, , T, dynamic_cast<const B<T>&>, R) \
207 SPARSE_BINOP_FWD_FCN (R, operator +, T, dynamic_cast<const B<T>&>, \
208 R, dynamic_cast<const B<T>&>, R) \
209 SPARSE_BINOP_FWD_FCN (R, operator -, T, dynamic_cast<const B<T>&>, \
210 R, dynamic_cast<const B<T>&>, R) \
211 SPARSE_BINOP_FWD_FCN (R, product, T, dynamic_cast<const B<T>&>, \
212 R, dynamic_cast<const B<T>&>, R) \
213 SPARSE_BINOP_FWD_FCN (R, quotient, T, dynamic_cast<const B<T>&>, \
214 R, dynamic_cast<const B<T>&>, R)
215
216#endif
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
MSparse(const MSparse< T > &a)
Definition MSparse.h:54
MSparse(octave_idx_type n, octave_idx_type m)
Definition MSparse.h:49
Sparse< U > map(U(&fcn)(const T &)) const
Definition MSparse.h:122
MSparse(octave_idx_type r, octave_idx_type c, T val)
Definition MSparse.h:68
MSparse< T > & insert(const Sparse< T > &a, const Array< octave_idx_type > &indx)
Definition MSparse.h:90
MSparse< T > transpose() const
Definition MSparse.h:96
Sparse< U > map(U(&fcn)(T)) const
Definition MSparse.h:117
MSparse(const Sparse< U > &a)
Definition MSparse.h:61
MSparse(const Sparse< T > &a)
Definition MSparse.h:58
MSparse(octave_idx_type r, octave_idx_type c, octave_idx_type num_nz)
Definition MSparse.h:73
MSparse< T > diag(octave_idx_type k=0) const
Definition MSparse.h:109
MSparse< T > & insert(const Sparse< T > &a, octave_idx_type r, octave_idx_type c)
Definition MSparse.h:84
MSparse< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition MSparse.h:103
MSparse(const Array< T > &a, const octave::idx_vector &r, const octave::idx_vector &c, octave_idx_type nr=-1, octave_idx_type nc=-1, bool sum_terms=true, octave_idx_type nzm=-1)
Definition MSparse.h:63
MSparse< T > reshape(const dim_vector &new_dims) const
Definition MSparse.h:100
MSparse< T > squeeze() const
Definition MSparse.h:98
~MSparse()=default
MSparse(const MSparse< T > &a, const dim_vector &dv)
Definition MSparse.h:56
MSparse(const dim_vector &dv, octave_idx_type nz=0)
Definition MSparse.h:51
MSparse(const PermMatrix &a)
Definition MSparse.h:71
MSparse< T > ipermute(const Array< octave_idx_type > &vec) const
Definition MSparse.h:106
MSparse()
Definition MSparse.h:47
Sparse< T, Alloc > diag(octave_idx_type k=0) const
Definition Sparse.cc:2492
Sparse< T, Alloc > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition Sparse.cc:930
Sparse< T, Alloc > squeeze() const
Definition Sparse.h:370
Sparse< T, Alloc > & insert(const Sparse< T, Alloc > &a, octave_idx_type r, octave_idx_type c)
Definition Sparse.cc:1043
Sparse< T, Alloc > ipermute(const Array< octave_idx_type > &vec) const
Definition Sparse.h:542
Sparse< T, Alloc > & operator=(const Sparse< T, Alloc > &a)
Definition Sparse.cc:714
Sparse< T, Alloc > transpose() const
Definition Sparse.cc:1139
Sparse< T, Alloc > reshape(const dim_vector &new_dims) const
Definition Sparse.cc:847
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
#define OCTAVE_API
Definition main.in.cc:55