fmatvec  2.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 
65  typedef AT value_type;
66 
71  explicit Matrix() : memEle(), ele(0) { }
72 
73 // template<class Ini=All<AT>>
74 // 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_) {
75 // init(ini);
76 // }
77 // template<class Ini=All<AT>>
78 // 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_) {
79 // init(ini);
80 // }
81 
82  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_) { }
83  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); }
84 
85  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_) { }
86  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); }
87 
88  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_) { }
89  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); }
90 
91 
97  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) {
98  copy(A);
99  }
100 
103  ~Matrix() = default;
104 
105  Matrix<Sparse,Ref,Ref,AT>& resize(int n_, int k_, Noinit) {
106  m = n_; n = n_; k = k_;
107  memEle.resize(k);
108  memI.resize(m+1);
109  memJ.resize(k);
110  ele = (AT*)memEle.get();
111  I = (int*)memI.get();
112  J = (int*)memJ.get();
113  return *this;
114  }
115 
116  Matrix<Sparse,Ref,Ref,AT>& resize(int n, int k, Init ini=INIT, const AT &a=AT()) { return resize(m,k,Noinit()).init(a); }
117 
125  FMATVEC_ASSERT(m == A.m, AT);
126  FMATVEC_ASSERT(k == A.k, AT);
127  return copy(A);
128  }
129 
136  template<class Type, class Row, class Col>
138  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
139  FMATVEC_ASSERT(m == A.cols(), AT);
140  FMATVEC_ASSERT(k == A.countElements(), AT);
141  return copy(A);
142  }
143 
151  m=A.m;
152  n=A.n;
153  k=A.k;
154  memEle = A.memEle;
155  memI = A.memI;
156  memJ = A.memJ;
157  ele = A.ele;
158  I = A.I;
159  J = A.J;
160  k = A.k;
161  return *this;
162  }
163 
170  template<class Type, class Row, class Col>
171  inline Matrix<Sparse,Ref,Ref,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
172  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
173  int k_ = A.countElements();
174  if(m!=A.rows() || k!=k_) resize(A.rows(),k_,NONINIT);
175  return copy(A);
176  }
177 
182  const AT* operator()() const {
183  return ele;
184  }
185 
191  AT* operator()() {
192  return ele;
193  }
194 
195  AT& operator()(int i, int j) {
196  throw std::runtime_error("Matrix<Sparse, Ref, Ref, AT>::operator(int i, int j) is not implemented.");
197  }
198 
199  const AT& operator()(int i, int j) const {
200  throw std::runtime_error("Matrix<Sparse, Ref, Ref, AT>::operator(int i, int j) const is not implemented.");
201  }
202 
203  int ldim() {
204  throw std::runtime_error("Matrix<Sparse, Ref, Ref, AT>::ldim() cannot be called.");
205  }
206 
211  const int* Ip() const {
212  return I;
213  }
214 
219  int* Ip() {
220  return I;
221  }
222 
227  const int* Jp() const {
228  return J;
229  }
230 
235  int* Jp() {
236  return J;
237  }
238 
243  int rows() const {return m;}
244 
249  int cols() const {return n;}
250 
253  const CBLAS_ORDER blasOrder() const {
254  throw std::runtime_error("Matrix<Sparse, Ref, Ref, AT>::blasOrder() cannot be called.");
255  }
256 
264  Matrix<Sparse,Ref,Ref,AT>& init(const AT &val);
265  inline Matrix<Sparse,Ref,Ref,AT>& init(Init, const AT &a=AT()) { return init(a); }
266  inline Matrix<Sparse,Ref,Ref,AT>& init(Noinit, const AT &a=AT()) { return *this; }
267 
268  int countElements() const { return k; }
269  };
270  // ------------------------- Constructors -------------------------------------
271  // ----------------------------------------------------------------------------
272 
273  template <class AT> template <class Type, class Row, class Col>
274  inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(const Matrix<Type,Row,Col,AT> &A) {
275  int k=0;
276  int i;
277  for(i=0; i<A.rows(); i++) {
278  ele[k]=A(i,i);
279  J[k]=i;
280  I[i]=k++;
281  for(int j=0; j<i; j++) {
282  // TODO eps
283  if(fabs(A(i,j))>1e-16) {
284  ele[k]=A(i,j);
285  J[k++]=j;
286  }
287  }
288  for(int j=i+1; j<A.cols(); j++) {
289  // TODO eps
290  if(fabs(A(i,j))>1e-16) {
291  ele[k]=A(i,j);
292  J[k++]=j;
293  }
294  }
295  }
296  if(n) I[i]=k;
297  return *this;
298  }
299 
300  template <class AT> template <class Row>
301  inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(const Matrix<Symmetric,Row,Row,AT> &A) {
302  int k=0;
303  int i;
304  for(i=0; i<A.size(); i++) {
305  ele[k]=A(i,i);
306  J[k]=i;
307  I[i]=k++;
308  for(int j=0; j<i; j++) {
309  // TODO eps
310  if(fabs(A(i,j))>1e-16) {
311  ele[k]=A(i,j);
312  J[k++]=j;
313  }
314  }
315  for(int j=i+1; j<A.size(); j++) {
316  // TODO eps
317  if(fabs(A(i,j))>1e-16) {
318  ele[k]=A(i,j);
319  J[k++]=j;
320  }
321  }
322  }
323  if(n) I[i]=k;
324  return *this;
325  }
326 
327  template <class AT>
328  inline Matrix<Sparse,Ref,Ref,AT>& Matrix<Sparse,Ref,Ref,AT>::copy(const Matrix<Sparse,Ref,Ref,AT> &A) {
329  for(int i=0; i<=m; i++) {
330  I[i] = A.I[i];
331  }
332  for(int i=0; i<k; i++) {
333  ele[i] = A.ele[i];
334  J[i] = A.J[i];
335  }
336  return *this;
337  }
338 
339  template <class AT>
341  for(int i=0; i<k; i++) {
342  ele[i] = val;
343  }
344  return *this;
345  }
346 
347 }
348 
349 #endif
Matrix()
Standard constructor.
Definition: sparse_matrix.h:71
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
Definition: types.h:85
const int * Jp() const
Pointer operator.
Definition: sparse_matrix.h:227
AT * operator()()
Pointer operator.
Definition: sparse_matrix.h:191
int rows() const
Number of rows.
Definition: sparse_matrix.h:243
Matrix< Sparse, Ref, Ref, AT > & init(const AT &val)
Initialization.
Definition: sparse_matrix.h:340
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:253
Definition: types.h:94
int * Ip()
Pointer operator.
Definition: sparse_matrix.h:219
Matrix< Sparse, Ref, Ref, AT > & operator&=(Matrix< Sparse, Ref, Ref, AT > &A)
Reference operator.
Definition: sparse_matrix.h:150
int * Jp()
Pointer operator.
Definition: sparse_matrix.h:235
int cols() const
Number of columns.
Definition: sparse_matrix.h:249
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:124
const int * Ip() const
Pointer operator.
Definition: sparse_matrix.h:211
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:97
Definition: types.h:84
const AT * operator()() const
Pointer operator.
Definition: sparse_matrix.h:182
Matrix< Sparse, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: sparse_matrix.h:137
int rows() const
Number of rows.