GNU Octave  4.4.1
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) 2004-2018 David Bateman
4 Copyright (C) 1998-2004 Andy Adler
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if ! defined (octave_MSparse_h)
25 #define octave_MSparse_h 1
26 
27 #include "octave-config.h"
28 
29 #include "Array-util.h"
30 #include "MArray.h"
31 #include "Sparse.h"
32 #include "lo-error.h"
33 #include "quit.h"
34 
35 
36 // Two dimensional sparse array with math ops.
37 template <typename T>
38 class
39 MSparse : public Sparse<T>
40 {
41 public:
42 
43  MSparse (void) : Sparse<T> () { }
44 
46 
48  : Sparse<T> (dv, nz) { }
49 
50  MSparse (const MSparse<T>& a) : Sparse<T> (a) { }
51 
52  MSparse (const MSparse<T>& a, const dim_vector& dv) : Sparse<T> (a, dv) { }
53 
54  MSparse (const Sparse<T>& a) : Sparse<T> (a) { }
55 
56  template <typename U>
57  MSparse (const Sparse<U>& a) : Sparse<T> (a) { }
58 
59  MSparse (const Array<T>& a, const idx_vector& r, const idx_vector& c,
60  octave_idx_type nr = -1, octave_idx_type nc = -1,
61  bool sum_terms = true, octave_idx_type nzm = -1)
62  : Sparse<T> (a, r, c, nr, nc, sum_terms, nzm) { }
63 
65  : Sparse<T> (r, c, val) { }
66 
67  explicit MSparse (const PermMatrix& a) : Sparse<T>(a) { }
68 
70  : Sparse<T> (r, c, num_nz) { }
71 
72  ~MSparse (void) = default;
73 
75  {
77  return *this;
78  }
79 
81  {
82  Sparse<T>::insert (a, r, c);
83  return *this;
84  }
85 
87  {
88  Sparse<T>::insert (a, indx);
89  return *this;
90  }
91 
92  MSparse<T> transpose (void) const { return Sparse<T>::transpose (); }
93 
94  MSparse<T> squeeze (void) const { return Sparse<T>::squeeze (); }
95 
96  MSparse<T> reshape (const dim_vector& new_dims) const
97  { return Sparse<T>::reshape (new_dims); }
98 
99  MSparse<T> permute (const Array<octave_idx_type>& vec, bool inv = false) const
100  { return Sparse<T>::permute (vec, inv); }
101 
103  { return Sparse<T>::ipermute (vec); }
104 
106  {
107  return Sparse<T>::diag (k);
108  }
109 
110  // FIXME: should go away.
111  template <typename U>
112  MSparse<U>
113  map (U (&fcn) (T)) const
114  { return Sparse<T>::template map<U> (fcn); }
115 
116  template <typename U>
117  MSparse<U>
118  map (U (&fcn) (const T&)) const
119  { return Sparse<T>::template map<U> (fcn); }
120 };
121 
122 // Include operator templates for MSparse
123 #include "MSparse.cc"
124 
125 // A macro that can be used to declare and instantiate OP= operators.
126 #define SPARSE_OP_ASSIGN_DECL(T, OP, API) \
127  template API MSparse<T>& \
128  operator OP (MSparse<T>&, const MSparse<T>&)
129 
130 // A macro that can be used to declare and instantiate unary operators.
131 #define SPARSE_UNOP_DECL(T, OP, API) \
132  template API MSparse<T> \
133  operator OP (const MSparse<T>&)
134 
135 // A macro that can be used to declare and instantiate binary operators.
136 #define SPARSE_BINOP_DECL(A_T, T, F, API, X_T, Y_T) \
137  template API A_T<T> \
138  F (const X_T&, const Y_T&)
139 
140 // A function that can be used to forward OP= operations from derived
141 // classes back to us.
142 #define SPARSE_OP_ASSIGN_FWD_FCN(R, F, T, C_X, X_T, C_Y, Y_T) \
143  inline R \
144  F (X_T& x, const Y_T& y) \
145  { \
146  return R (F (C_X (x), C_Y (y))); \
147  }
148 
149 // A function that can be used to forward unary operations from derived
150 // classes back to us.
151 #define SPARSE_UNOP_FWD_FCN(R, F, T, C_X, X_T) \
152  inline R \
153  F (const X_T& x) \
154  { \
155  return R (F (C_X (x))); \
156  }
157 
158 // A function that can be used to forward binary operations from derived
159 // classes back to us.
160 #define SPARSE_BINOP_FWD_FCN(R, F, T, C_X, X_T, C_Y, Y_T) \
161  inline R \
162  F (const X_T& x, const Y_T& y) \
163  { \
164  return R (F (C_X (x), C_Y (y))); \
165  }
166 
167 // Instantiate all the MSparse friends for MSparse element type T.
168 #define INSTANTIATE_SPARSE_FRIENDS(T, API) \
169  SPARSE_OP_ASSIGN_DECL (T, +=, API); \
170  SPARSE_OP_ASSIGN_DECL (T, -=, API); \
171  SPARSE_UNOP_DECL (T, +, API); \
172  SPARSE_UNOP_DECL (T, -, API); \
173  SPARSE_BINOP_DECL (MArray, T, operator +, API, MSparse<T>, T); \
174  SPARSE_BINOP_DECL (MArray, T, operator -, API, MSparse<T>, T); \
175  SPARSE_BINOP_DECL (MSparse, T, operator *, API, MSparse<T>, T); \
176  SPARSE_BINOP_DECL (MSparse, T, operator /, API, MSparse<T>, T); \
177  SPARSE_BINOP_DECL (MArray, T, operator +, API, T, MSparse<T>); \
178  SPARSE_BINOP_DECL (MArray, T, operator -, API, T, MSparse<T>); \
179  SPARSE_BINOP_DECL (MSparse, T, operator *, API, T, MSparse<T>); \
180  SPARSE_BINOP_DECL (MSparse, T, operator /, API, T, MSparse<T>); \
181  SPARSE_BINOP_DECL (MSparse, T, operator +, API, MSparse<T>, MSparse<T>); \
182  SPARSE_BINOP_DECL (MSparse, T, operator -, API, MSparse<T>, MSparse<T>); \
183  SPARSE_BINOP_DECL (MSparse, T, quotient, API, MSparse<T>, MSparse<T>); \
184  SPARSE_BINOP_DECL (MSparse, T, product, API, MSparse<T>, MSparse<T>);
185 
186 // Define all the MSparse forwarding functions for return type R and
187 // MSparse element type T
188 #define SPARSE_FORWARD_DEFS(B, R, F, T) \
189  SPARSE_OP_ASSIGN_FWD_FCN (R, operator +=, T, dynamic_cast<B<T>&>, \
190  R, dynamic_cast<const B<T>&>, R) \
191  SPARSE_OP_ASSIGN_FWD_FCN (R, operator -=, T, dynamic_cast<B<T>&>, \
192  R, dynamic_cast<const B<T>&>, R) \
193  SPARSE_UNOP_FWD_FCN (R, operator +, T, dynamic_cast<const B<T>&>, R) \
194  SPARSE_UNOP_FWD_FCN (R, operator -, T, dynamic_cast<const B<T>&>, R) \
195  SPARSE_BINOP_FWD_FCN (F, operator +, T, dynamic_cast<const B<T>&>, R, , T) \
196  SPARSE_BINOP_FWD_FCN (F, operator -, T, dynamic_cast<const B<T>&>, R, , T) \
197  SPARSE_BINOP_FWD_FCN (R, operator *, T, dynamic_cast<const B<T>&>, R, , T) \
198  SPARSE_BINOP_FWD_FCN (R, operator /, T, dynamic_cast<const B<T>&>, R, , T) \
199  SPARSE_BINOP_FWD_FCN (F, operator +, T, , T, dynamic_cast<const B<T>&>, R) \
200  SPARSE_BINOP_FWD_FCN (F, operator -, T, , T, dynamic_cast<const B<T>&>, R) \
201  SPARSE_BINOP_FWD_FCN (R, operator *, T, , T, dynamic_cast<const B<T>&>, R) \
202  SPARSE_BINOP_FWD_FCN (R, operator /, T, , T, dynamic_cast<const B<T>&>, R) \
203  SPARSE_BINOP_FWD_FCN (R, operator +, T, dynamic_cast<const B<T>&>, \
204  R, dynamic_cast<const B<T>&>, R) \
205  SPARSE_BINOP_FWD_FCN (R, operator -, T, dynamic_cast<const B<T>&>, \
206  R, dynamic_cast<const B<T>&>, R) \
207  SPARSE_BINOP_FWD_FCN (R, product, T, dynamic_cast<const B<T>&>, \
208  R, dynamic_cast<const B<T>&>, R) \
209  SPARSE_BINOP_FWD_FCN (R, quotient, T, dynamic_cast<const B<T>&>, \
210  R, dynamic_cast<const B<T>&>, R)
211 
212 #endif
MSparse(const Array< T > &a, const idx_vector &r, const 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:59
MSparse< T > & insert(const Sparse< T > &a, const Array< octave_idx_type > &indx)
Definition: MSparse.h:86
Sparse< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition: Sparse.cc:891
MSparse< T > & insert(const Sparse< T > &a, octave_idx_type r, octave_idx_type c)
Definition: MSparse.h:80
MSparse(const Sparse< U > &a)
Definition: MSparse.h:57
MSparse(const PermMatrix &a)
Definition: MSparse.h:67
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
for large enough k
Definition: lu.cc:617
MSparse(octave_idx_type r, octave_idx_type c, octave_idx_type num_nz)
Definition: MSparse.h:69
Sparse< T > diag(octave_idx_type k=0) const
Definition: Sparse.cc:2404
Sparse< T > transpose(void) const
Definition: Sparse.cc:1092
MSparse< U > map(U(&fcn)(const T &)) const
Definition: MSparse.h:118
MSparse< T > diag(octave_idx_type k=0) const
Definition: MSparse.h:105
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
MSparse< T > reshape(const dim_vector &new_dims) const
Definition: MSparse.h:96
octave_function * fcn
Definition: ov-class.cc:1754
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:400
MSparse< U > map(U(&fcn)(T)) const
Definition: MSparse.h:113
MSparse(const MSparse< T > &a, const dim_vector &dv)
Definition: MSparse.h:52
MSparse< T > squeeze(void) const
Definition: MSparse.h:94
MSparse(const Sparse< T > &a)
Definition: MSparse.h:54
MSparse< T > transpose(void) const
Definition: MSparse.h:92
MSparse(const MSparse< T > &a)
Definition: MSparse.h:50
MSparse(const dim_vector &dv, octave_idx_type nz=0)
Definition: MSparse.h:47
Sparse< T > & operator=(const Sparse< T > &a)
Definition: Sparse.cc:695
MSparse(octave_idx_type r, octave_idx_type c, T val)
Definition: MSparse.h:64
MDiagArray2< T > & operator=(const MDiagArray2< T > &a)
Definition: MDiagArray2.h:77
MSparse< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition: MSparse.h:99
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:125
MSparse(octave_idx_type n, octave_idx_type m)
Definition: MSparse.h:45
Sparse< T > squeeze(void) const
Definition: Sparse.h:280
Sparse< T > & insert(const Sparse< T > &a, octave_idx_type r, octave_idx_type c)
Definition: Sparse.cc:1000
Sparse< T > reshape(const dim_vector &new_dims) const
Definition: Sparse.cc:812
Sparse< T > ipermute(const Array< octave_idx_type > &vec) const
Definition: Sparse.h:451
MSparse< T > ipermute(const Array< octave_idx_type > &vec) const
Definition: MSparse.h:102
MSparse(void)
Definition: MSparse.h:43
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
dim_vector dv
Definition: sub2ind.cc:263