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