fmatvec  0.0.0
Fast Matrix/Vector Library
sparse_matrix.h
1 /* Copyright (C) 2003-2005 Martin Förg
2 
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact:
18  * martin.o.foerg@googlemail.com
19  *
20  */
21 
22 #ifndef sparse_matrix_h
23 #define sparse_matrix_h
24 
25 #include "matrix.h"
26 #include "square_matrix.h"
27 #include "types.h"
28 #include "_memory.h"
29 
30 namespace fmatvec {
31 
43  template <class AT>
44  class Matrix<Sparse,Ref,Ref,AT> {
45 
46  protected:
47 
49 
50  Memory<AT> memEle;
51  Memory<int> memI, memJ;
52  AT *ele;
53  int *I{nullptr}, *J{nullptr};
54  int m{0}, n{0}, k{0};
55 
56  template <class Type, class Row, class Col> inline Matrix<Sparse,Ref,Ref,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
57  template <class Row> inline Matrix<Sparse,Ref,Ref,AT>& copy(const Matrix<Symmetric,Row,Row,AT> &A);
59 
61 
62 
63  public:
64  static constexpr bool isVector {false};
65 
66  typedef AT value_type;
67  typedef Sparse shape_type;
68 
73  explicit Matrix() : memEle(), ele(0) { }
74 
75 // template<class Ini=All<AT>>
76 // Matrix(int n_, Ini ini=All<AT>()) : memEle(n_*n_), memI(n_+1), memJ(n_*n_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(n_*n_) {
77 // init(ini);
78 // }
79 // template<class Ini=All<AT>>
80 // Matrix(int m_, int n_, Ini ini) : memEle(n_*n_), memI(n_+1), memJ(n_*n_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(n_*n_) {
81 // init(ini);
82 // }
83 
84  explicit Matrix(int m_, int n_, int k_, Noinit) : memEle(k_), memI(n_+1), memJ(k_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(k_) { }
85  explicit Matrix(int m_, int n_, int k_, Init ini=INIT, const AT &a=AT()) : memEle(k_), memI(n_+1), memJ(k_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(k_) { init(a); }
86 
87  explicit Matrix(int n_, Noinit) : memEle(n_*n_), memI(n_+1), memJ(n_*n_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(n_*n_) { }
88  explicit Matrix(int n_, Init ini=INIT, const AT &a=AT()) : memEle(n_*n_), memI(n_+1), memJ(n_*n_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(n_*n_) { init(a); }
89 
90  explicit Matrix(int m_, int n_, Noinit) : memEle(n_*n_), memI(n_+1), memJ(n_*n_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(n_*n_) { }
91  explicit Matrix(int m_, int n_, Init ini=INIT, const AT &a=AT()) : memEle(n_*n_), memI(n_+1), memJ(n_*n_), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(n_), n(n_), k(n_*n_) { init(a); }
92 
93 
99  Matrix(const Matrix<Sparse,Ref,Ref,AT> &A) : memEle(A.k), memI(A.n+1), memJ(A.k), ele((AT*)memEle.get()), I((int*)memI.get()), J((int*)memJ.get()), m(A.m), n(A.n), k(A.k) {
100  copy(A);
101  }
102 
105  ~Matrix() = default;
106 
107  Matrix<Sparse,Ref,Ref,AT>& resize(int n_, int k_, Noinit) {
108  m = n_; n = n_; k = k_;
109  memEle.resize(k);
110  memI.resize(m+1);
111  memJ.resize(k);
112  ele = (AT*)memEle.get();
113  I = (int*)memI.get();
114  J = (int*)memJ.get();
115  return *this;
116  }
117 
118  Matrix<Sparse,Ref,Ref,AT>& resize(int n, int k, Init ini=INIT, const AT &a=AT()) { return resize(m,k,Noinit()).init(a); }
119 
127  FMATVEC_ASSERT(m == A.m, AT);
128  FMATVEC_ASSERT(k == A.k, AT);
129  return copy(A);
130  }
131 
138  template<class Type, class Row, class Col>
140  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
141  FMATVEC_ASSERT(m == A.cols(), AT);
142  FMATVEC_ASSERT(k == A.countElements(), AT);
143  return copy(A);
144  }
145 
153  m=A.m;
154  n=A.n;
155  k=A.k;
156  memEle = A.memEle;
157  memI = A.memI;
158  memJ = A.memJ;
159  ele = A.ele;
160  I = A.I;
161  J = A.J;
162  k = A.k;
163  return *this;
164  }
165 
172  template<class Type, class Row, class Col>
173  inline Matrix<Sparse,Ref,Ref,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
174  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
175  int k_ = A.countElements();
176  if(m!=A.rows() || k!=k_) resize(A.rows(),k_,NONINIT);
177  return copy(A);
178  }
179 
184  const AT* operator()() const {
185  return ele;
186  }
187 
193  AT* operator()() {
194  return ele;
195  }
196 
197  AT& operator()(int i, int j) {
198  throw std::runtime_error("Matrix<Sparse, Ref, Ref, AT>::operator(int i, int j) is not implemented.");
199  }
200 
201  const AT& operator()(int i, int j) const {
202  throw std::runtime_error("Matrix<Sparse, Ref, Ref, AT>::operator(int i, int j) const is not implemented.");
203  }
204 
205  int ldim() {
206  throw std::runtime_error("Matrix<Sparse, Ref, Ref, AT>::ldim() cannot be called.");
207  }
208 
213  const int* Ip() const {
214  return I;
215  }
216 
221  int* Ip() {
222  return I;
223  }
224 
229  const int* Jp() const {
230  return J;
231  }
232 
237  int* Jp() {
238  return J;
239  }
240 
245  int rows() const {return m;}
246 
251  int cols() const {return n;}
252 
255  const CBLAS_ORDER blasOrder() const {
256  throw std::runtime_error("Matrix<Sparse, Ref, Ref, AT>::blasOrder() cannot be called.");
257  }
258 
266  Matrix<Sparse,Ref,Ref,AT>& init(const AT &val);
267  inline Matrix<Sparse,Ref,Ref,AT>& init(Init, const AT &a=AT()) { return init(a); }
268  inline Matrix<Sparse,Ref,Ref,AT>& init(Noinit, const AT &a=AT()) { return *this; }
269 
270  int countElements() const { return k; }
271  };
272  // ------------------------- Constructors -------------------------------------
273  // ----------------------------------------------------------------------------
274 
275  template <class AT> template <class Type, class Row, class Col>
276  inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(const Matrix<Type,Row,Col,AT> &A) {
277  int k=0;
278  int i;
279  for(i=0; i<A.rows(); i++) {
280  ele[k]=A(i,i);
281  J[k]=i;
282  I[i]=k++;
283  for(int j=0; j<i; j++) {
284  // TODO eps
285  if(fabs(A(i,j))>1e-16) {
286  ele[k]=A(i,j);
287  J[k++]=j;
288  }
289  }
290  for(int j=i+1; j<A.cols(); j++) {
291  // TODO eps
292  if(fabs(A(i,j))>1e-16) {
293  ele[k]=A(i,j);
294  J[k++]=j;
295  }
296  }
297  }
298  if(n) I[i]=k;
299  return *this;
300  }
301 
302  template <class AT> template <class Row>
303  inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(const Matrix<Symmetric,Row,Row,AT> &A) {
304  int k=0;
305  int i;
306  for(i=0; i<A.size(); i++) {
307  ele[k]=A(i,i);
308  J[k]=i;
309  I[i]=k++;
310  for(int j=0; j<i; j++) {
311  // TODO eps
312  if(fabs(A(i,j))>1e-16) {
313  ele[k]=A(i,j);
314  J[k++]=j;
315  }
316  }
317  for(int j=i+1; j<A.size(); j++) {
318  // TODO eps
319  if(fabs(A(i,j))>1e-16) {
320  ele[k]=A(i,j);
321  J[k++]=j;
322  }
323  }
324  }
325  if(n) I[i]=k;
326  return *this;
327  }
328 
329  template <class AT>
330  inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(const Matrix<Sparse,Ref,Ref,AT> &A) {
331  for(int i=0; i<=m; i++) {
332  I[i] = A.I[i];
333  }
334  for(int i=0; i<k; i++) {
335  ele[i] = A.ele[i];
336  J[i] = A.J[i];
337  }
338  return *this;
339  }
340 
341  template <class AT>
343  for(int i=0; i<k; i++) {
344  ele[i] = val;
345  }
346  return *this;
347  }
348 
349 }
350 
351 #endif
Matrix()
Standard constructor.
Definition: sparse_matrix.h:73
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Definition: types.h:85
const int * Jp() const
Pointer operator.
Definition: sparse_matrix.h:229
AT * operator()()
Pointer operator.
Definition: sparse_matrix.h:193
int rows() const
Number of rows.
Definition: sparse_matrix.h:245
This is a matrix class for sparse quadratic matrices.
Definition: sparse_matrix.h:44
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: sparse_matrix.h:255
Definition: types.h:94
int * Ip()
Pointer operator.
Definition: sparse_matrix.h:221
Matrix< Sparse, Ref, Ref, AT > & operator&=(Matrix< Sparse, Ref, Ref, AT > &A)
Reference operator.
Definition: sparse_matrix.h:152
int * Jp()
Pointer operator.
Definition: sparse_matrix.h:237
int cols() const
Number of columns.
Definition: sparse_matrix.h:251
int cols() const
Number of columns.
Matrix< Sparse, Ref, Ref, AT > & operator=(const Matrix< Sparse, Ref, Ref, AT > &A)
Assignment operator.
Definition: sparse_matrix.h:126
const int * Ip() const
Pointer operator.
Definition: sparse_matrix.h:213
Namespace fmatvec.
Definition: _memory.cc:28
Shape class for sparse matrices.
Definition: types.h:172
Matrix(const Matrix< Sparse, Ref, Ref, AT > &A)
Copy Constructor.
Definition: sparse_matrix.h:99
Definition: types.h:84
const AT * operator()() const
Pointer operator.
Definition: sparse_matrix.h:184
Matrix< Sparse, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: sparse_matrix.h:139
int rows() const
Number of rows.