All Classes Namespaces Functions Variables Typedefs Enumerations Pages
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  void deepCopy(const Matrix<Sparse,Ref,Ref,AT> &A);
57 
58  void deepCopy(const SquareMatrix<Ref,AT> &A);
59  void deepCopy(const Matrix<Symmetric, Var, Var,AT> &A);
60 
62 
63 
64  public:
65 
66  typedef AT value_type;
67 
72  Matrix() : memEle(), ele(0) { }
73 
74 // template<class Ini=All<AT> >
75 // 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_) {
76 // init(ini);
77 // }
78 // template<class Ini=All<AT> >
79 // 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_) {
80 // init(ini);
81 // }
82 
83  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_) { }
84  Matrix(int m_, int n_, int k_, Init ini=INIT, const AT &a=0) : 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); }
85 
86  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_) { }
87  Matrix(int n_, Init ini=INIT, const AT &a=0) : 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); }
88 
89  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_) { }
90  Matrix(int m_, int n_, Init ini=INIT, const AT &a=0) : 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); }
91 
92 
100  Matrix(const Matrix<Sparse,Ref,Ref,AT> &A) : memEle(A.memEle), memI(A.memI), memJ(A.memJ), ele(A.ele), I(A.I), J(A.J), m(A.m), n(A.n), k(A.k) {
101  }
102 
105  ~Matrix() = default;
106 
107  Matrix<Sparse,Ref,Ref,AT>& resize() {
108  m = n = k = 0;
109  memEle.resize(0);
110  memI.resize(0);
111  memJ.resize(0);
112  ele = 0;
113  I = J = 0;
114  return *this;
115  }
116 
117  Matrix<Sparse,Ref,Ref,AT>& resize(int n_, int k_, Noinit) {
118  m = n_; n = n_; k = k_;
119  memEle.resize(k);
120  memI.resize(m+1);
121  memJ.resize(k);
122  ele = (AT*)memEle.get();
123  I = (int*)memI.get();
124  J = (int*)memJ.get();
125  return *this;
126  }
127 
128  Matrix<Sparse,Ref,Ref,AT>& resize(int n, int k, Init ini=INIT, const AT &a=0) { return resize(m,k,Noinit()).init(a); }
129 
136  inline Matrix<Sparse,Ref,Ref,AT>& operator<<(const Matrix<Sparse,Ref,Ref,AT> &A);
137 
144  inline Matrix<Sparse,Ref,Ref,AT>& operator>>(const Matrix<Sparse,Ref,Ref,AT> &A);
145 
150  inline Matrix<Sparse,Ref,Ref,AT>& operator<<(const SquareMatrix<Ref,AT> &A);
151 
156  inline Matrix<Sparse,Ref,Ref,AT>& operator<<(const Matrix<Symmetric, Var, Var ,AT> &A);
157 
166  inline Matrix<Sparse,Ref,Ref,AT>& operator=(const Matrix<Sparse,Ref,Ref,AT> &A);
167 
172  const AT* operator()() const {
173  return ele;
174  };
175 
181  AT* operator()() {
182  return ele;
183  };
184 
185  AT& operator()(int i, int j) {
186  throw std::runtime_error("Matrix<Sparse, Ref, Ref, AT>::operator(int i, int j) is not implemented.");
187  }
188 
189  bool transposed() {
190  throw std::runtime_error("Matrix<Sparse, Ref, Ref, AT>::transposed() cannot be called.");
191  }
192 
193  int ldim() {
194  throw std::runtime_error("Matrix<Sparse, Ref, Ref, AT>::ldim() cannot be called.");
195  }
196 
201  const int* Ip() const {
202  return I;
203  };
204 
209  int* Ip() {
210  return I;
211  };
212 
217  const int* Jp() const {
218  return J;
219  };
220 
225  int* Jp() {
226  return J;
227  };
228 
233  int rows() const {return m;};
234 
239  int cols() const {return n;};
240 
246  Matrix<Sparse,Ref,Ref,AT> copy() const;
247 
255  Matrix<Sparse,Ref,Ref,AT>& init(const AT &val);
256  inline Matrix<Sparse,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); }
257  inline Matrix<Sparse,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
258 
259  };
260  // ------------------------- Constructors -------------------------------------
261  // ----------------------------------------------------------------------------
262 
263  template <class AT>
265 
266  m=A.m;
267  n=A.n;
268  k=A.k;
269  memEle = A.memEle;
270  memI = A.memI;
271  memJ = A.memJ;
272  ele = A.ele;
273  I = A.I;
274  J = A.J;
275  k = A.k;
276 
277  return *this;
278  }
279 
280  template <class AT>
282 
283  if(!ele) {
284  m = A.m;
285  n = A.n;
286  k = A.k;
287  memEle.resize(k);
288  memI.resize(m+1);
289  memJ.resize(k);
290  ele = (AT*)memEle.get();
291  I = (int*)memI.get();
292  J = (int*)memJ.get();
293  }
294  else {
295 #ifndef FMATVEC_NO_SIZE_CHECK
296  assert(m == A.m);
297  assert(n == A.n);
298  assert(k == A.k);
299 #endif
300  }
301 
302  deepCopy(A);
303 
304  return *this;
305  }
306 
307  template <class AT>
309 
310  if(m!=A.m || n!=A.n || k!=A.k) {
311  m = A.m;
312  n = A.n;
313  k = A.k;
314  memEle.resize(k);
315  memI.resize(m+1);
316  memJ.resize(k);
317  ele = (AT*)memEle.get();
318  I = (int*)memI.get();
319  J = (int*)memJ.get();
320  }
321 
322  deepCopy(A);
323 
324  return *this;
325  }
326 
327  template <class AT>
329 
330  if(m!=A.rows() || n!=A.cols()) {
331  m = A.rows();
332  n = A.cols();
333  k = countElements(A);
334  memEle.resize(k);
335  memI.resize(m+1);
336  memJ.resize(k);
337  ele = (AT*)memEle.get();
338  I = (int*)memI.get();
339  J = (int*)memJ.get();
340  } else {
341  int k_ = countElements(A);
342  if(k != k_) {
343  k = k_;
344  memEle.resize(k);
345  memJ.resize(k);
346  ele = (AT*)memEle.get();
347  J = (int*)memJ.get();
348  }
349  }
350 
351  deepCopy(A);
352 
353  return *this;
354  }
355 
356  template <class AT>
358 
359  if (m != A.rows() || n != A.cols()) {
360  m = A.rows();
361  n = A.cols();
362  k = countElementsLT(A) * 2 - A.rows();
363  memEle.resize(k);
364  memI.resize(m + 1);
365  memJ.resize(k);
366  ele = (AT*) memEle.get();
367  I = (int*) memI.get();
368  J = (int*) memJ.get();
369  }
370  else {
371  int k_ = countElementsLT(A) * 2 - A.rows();
372  if (k != k_) {
373  k = k_;
374  memEle.resize(k);
375  memJ.resize(k);
376  ele = (AT*) memEle.get();
377  J = (int*) memJ.get();
378  }
379  }
380 
381  deepCopy(A);
382 
383  return *this;
384  }
385 
386  template <class AT>
388 
389  Matrix<Sparse,Ref,Ref,AT> A(m,NONINIT);
390 
391  A.deepCopy(*this);
392 
393  return A;
394  }
395 
396  template <class AT>
398  for(int i=0; i<=m; i++) {
399  I[i] = A.I[i];
400  }
401  for(int i=0; i<k; i++) {
402  ele[i] = A.ele[i];
403  J[i] = A.J[i];
404  }
405  }
406 
407  template <class AT> void Matrix<Sparse,Ref,Ref,AT>::deepCopy(const SquareMatrix<Ref,AT> &A) {
408  int k=0;
409  int i;
410  for(i=0; i<A.size(); i++) {
411  ele[k]=A(i,i);
412  J[k]=i;
413  I[i]=k++;
414  for(int j=0; j<i; j++) {
415  // TODO eps
416  if(fabs(A(i,j))>1e-16) {
417  ele[k]=A(i,j);
418  J[k++]=j;
419  }
420  }
421  for(int j=i+1; j<A.size(); j++) {
422  // TODO eps
423  if(fabs(A(i,j))>1e-16) {
424  ele[k]=A(i,j);
425  J[k++]=j;
426  }
427  }
428  }
429  if(n) I[i]=k;
430  }
431 
432  template <class AT> void Matrix<Sparse,Ref,Ref,AT>::deepCopy(const Matrix<Symmetric, Var, Var,AT> &A) {
433  int k=0;
434  int i;
435  for(i=0; i<A.size(); i++) {
436  ele[k]=A(i,i);
437  J[k]=i;
438  I[i]=k++;
439  for(int j=0; j<i; j++) {
440  // TODO eps
441  if(fabs(A(i,j))>1e-16) {
442  ele[k]=A(i,j);
443  J[k++]=j;
444  }
445  }
446  for(int j=i+1; j<A.size(); j++) {
447  // TODO eps
448  if(fabs(A(i,j))>1e-16) {
449  ele[k]=A(i,j);
450  J[k++]=j;
451  }
452  }
453  }
454  if(n) I[i]=k;
455  }
456 
457  template <class AT>
459  for(int i=0; i<k; i++) {
460  ele[i] = val;
461  }
462  return *this;
463  }
464 
465 /* template <class AT> template <class Type> void Matrix<Sparse,Ref,Ref,AT>::deepCopy(const Matrix<Type, AT> &A) {
466  k=0;
467  AT *eleBuf = new AT[m*n];
468  int *JBuf = new int[m*n];
469  int i;
470  for(i=0; i<A.rows(); i++) {
471  eleBuf[k]=A(i,i);
472  JBuf[k]=i;
473  I[i]=k++;
474  for(int j=0; j<i; j++) {
475  // TODO eps
476  if(fabs(A(i,j))>1e-16) {
477  eleBuf[k]=A(i,j);
478  JBuf[k++]=j;
479  }
480  }
481  for(int j=i+1; j<A.cols(); j++) {
482  // TODO eps
483  if(fabs(A(i,j))>1e-16) {
484  eleBuf[k]=A(i,j);
485  JBuf[k++]=j;
486  }
487  }
488  }
489  I[i]=k;
490  memEle.resize(k);
491  memJ.resize(k);
492  ele = (AT*)memEle.get();
493  J = (int*)memJ.get();
494  for(int i=0; i<k; i++) {
495  ele[i] = eleBuf[i];
496  J[i] = JBuf[i];
497  }
498 
499  delete [] eleBuf;
500  delete [] JBuf;
501  }
502 
503  */
504 
505  //template <> extern void Matrix<Sparse,Ref,Ref,double>::deepCopy(const Matrix<Sparse,Ref,Ref,double> &A);
506 
507 }
508 
509 #endif
Matrix()
Standard constructor.
Definition: sparse_matrix.h:72
This is a matrix class for symmetric matrices.
Definition: var_symmetric_matrix.h:37
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
int rows() const
Number of rows.
Definition: var_symmetric_matrix.h:245
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:90
int rows() const
Number of rows.
Definition: general_matrix.h:273
Definition: matrix.h:43
AT * operator()()
Pointer operator.
Definition: sparse_matrix.h:181
Matrix< Sparse, Ref, Ref, AT > & init(const AT &val)
Initialization.
Definition: sparse_matrix.h:458
int countElements(const SquareMatrix< Ref, AT > &A)
Count nonzero elements.
Definition: linear_algebra.h:1839
This is a matrix class for sparse quadratic matrices.
Definition: sparse_matrix.h:44
Definition: types.h:62
int * Ip()
Pointer operator.
Definition: sparse_matrix.h:209
int rows() const
Number of rows.
Definition: sparse_matrix.h:233
const int * Jp() const
Pointer operator.
Definition: sparse_matrix.h:217
int * Jp()
Pointer operator.
Definition: sparse_matrix.h:225
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:166
const int * Ip() const
Pointer operator.
Definition: sparse_matrix.h:201
Shape class for sparse matrices.
Definition: types.h:140
Matrix(const Matrix< Sparse, Ref, Ref, AT > &A)
Copy Constructor.
Definition: sparse_matrix.h:100
Definition: matrix.h:42
This is a matrix class of general quadratic matrices.
Definition: square_matrix.h:39
int cols() const
Number of columns.
Definition: var_symmetric_matrix.h:251
int countElementsLT(const Matrix< Symmetric, Ref, Ref, AT > &A)
Count nonzero elements of the low triangular part of a symmetric matrix.
Definition: linear_algebra.h:1857
const AT * operator()() const
Pointer operator.
Definition: sparse_matrix.h:172
int cols() const
Number of columns.
Definition: sparse_matrix.h:239
int cols() const
Number of columns.
Definition: general_matrix.h:279

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home