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 #include <cmath>
30 
31 namespace fmatvec {
32 
44  template <class AT>
45  class Matrix<Sparse, AT> {
46 
47  protected:
48 
50 
51  Memory<AT> memEle;
52  Memory<int> memI, memJ;
53  AT *ele;
54  int *I, *J;
55  int m, n, k;
56 
57  void deepCopy(const Matrix<Sparse, AT> &x);
58 
59  void deepCopy(const SquareMatrix<AT> &A);
60 
62 
63 
64  public:
65 
70  Matrix() : memEle(), memI(), memJ(), ele(0), I(0), J(0), m(0), n(0), k(0) {
71  }
72 
81  Matrix(int n_) : 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_) {
82 #ifndef FMATVEC_NO_INITIALIZATION
83  init(0);
84 #endif
85  }
86 
96 Matrix(int n_, int k_) : 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_) {
97 #ifndef FMATVEC_NO_INITIALIZATION
98  init(0);
99 #endif
100  }
101 
109  Matrix(const Matrix<Sparse, 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) {
110  }
111 
115  }
116 
123  Matrix<Sparse, AT>& operator<<(const Matrix<Sparse, AT> &A);
124 
132 
137  Matrix<Sparse, AT>& operator<<(const SquareMatrix<AT> &A);
138 
148 #ifndef FMATVEC_NO_DEEP_ASSIGNMENT
149  return operator<<(A);
150 #else
151  return operator>>(A);
152 #endif
153  }
154 
159  const AT* operator()() const {
160  return ele;
161  };
162 
168  AT* operator()() {
169  return ele;
170  };
171 
176  const int* Ip() const {
177  return I;
178  };
179 
184  int* Ip() {
185  return I;
186  };
187 
192  const int* Jp() const {
193  return J;
194  };
195 
200  int* Jp() {
201  return J;
202  };
203 
208  int rows() const {return m;};
209 
214  int cols() const {return n;};
215 
221  Matrix<Sparse, AT> copy() const;
222 
228  Matrix<Sparse, AT>& resize() {m=0;n=0;k=0;return *this;};
229 
239  Matrix<Sparse, AT>& resize(int n_, int k_) {
240  m=n_;n=n_;k=k_;
241  memEle.resize(k);
242  memI.resize(m+1);
243  memJ.resize(k);
244  ele = (AT*)memEle.get();
245  I = (int*)memI.get();
246  J = (int*)memJ.get();
247 
248  return *this;
249  }
250 
258  Matrix<Sparse, AT>& init(const AT &a);
259 
260  };
261  // ------------------------- Constructors -------------------------------------
262  // ----------------------------------------------------------------------------
263 
264  template <class AT>
266  if(m==0) {
267  m=A.m;
268  n=A.n;
269  k=A.k;
270  } else {
271 #ifdef FMATVEC_SIZE_CHECK
272  assert(m == A.m);
273  assert(n == A.n);
274  assert(k == A.k);
275 #endif
276  }
277 
278  memEle = A.memEle;
279  memI = A.memI;
280  memJ = A.memJ;
281  ele = A.ele;
282  I = A.I;
283  J = A.J;
284  k = A.k;
285 
286  return *this;
287  }
288 
289  template <class AT>
291 
292  if(A.rows() == 0 || A.cols() == 0)
293  return *this;
294 
295  if(m==0) {
296  m = A.m;
297  n = A.n;
298  k = A.k;
299  memEle.resize(k);
300  memI.resize(m+1);
301  memJ.resize(k);
302  ele = (AT*)memEle.get();
303  I = (int*)memI.get();
304  J = (int*)memJ.get();
305  } else {
306  assert(m == A.m);
307  assert(n == A.n);
308  assert(k >= A.k);
309  }
310 
311  deepCopy(A);
312 
313  return *this;
314  }
315 
316  template <class AT>
318 
319  if(A.rows() == 0 || A.cols() == 0)
320  return *this;
321 
322  if(m==0) {
323  m = A.rows();
324  n = A.cols();
325  k = countElements(A);
326  memEle.resize(k);
327  memI.resize(m+1);
328  memJ.resize(k);
329  ele = (AT*)memEle.get();
330  I = (int*)memI.get();
331  J = (int*)memJ.get();
332  } else {
333  assert(m == A.rows());
334  assert(n == A.cols());
335  }
336 
337  deepCopy(A);
338 
339  return *this;
340  }
341 
342  template <class AT>
344 
345  Matrix<Sparse, AT> A(m,NONINIT);
346 
347  A.deepCopy(*this);
348 
349  return A;
350  }
351 
352  template <class AT>
354  for(int i=0; i<=m; i++) {
355  I[i] = A.I[i];
356  }
357  for(int i=0; i<k; i++) {
358  ele[i] = A.ele[i];
359  J[i] = A.J[i];
360  }
361  }
362 
363  template <class AT> void Matrix<Sparse, AT>::deepCopy(const SquareMatrix<AT> &A) {
364  int k=0;
365  int i;
366  for(i=0; i<A.size(); i++) {
367  ele[k]=A(i,i);
368  J[k]=i;
369  I[i]=k++;
370  for(int j=0; j<i; j++) {
371  // TODO eps
372  if(fabs(A(i,j))>1e-16) {
373  ele[k]=A(i,j);
374  J[k++]=j;
375  }
376  }
377  for(int j=i+1; j<A.size(); j++) {
378  // TODO eps
379  if(fabs(A(i,j))>1e-16) {
380  ele[k]=A(i,j);
381  J[k++]=j;
382  }
383  }
384  }
385  I[i]=k;
386  }
387 
388  template <class AT>
390 
391  for(int i=0; i<k; i++) {
392  ele[i] = val;
393  }
394 
395  return *this;
396  }
397 
398 /* template <class AT> template <class Type> void Matrix<Sparse, AT>::deepCopy(const Matrix<Type, AT> &A) {
399  k=0;
400  AT *eleBuf = new AT[m*n];
401  int *JBuf = new int[m*n];
402  int i;
403  for(i=0; i<A.rows(); i++) {
404  eleBuf[k]=A(i,i);
405  JBuf[k]=i;
406  I[i]=k++;
407  for(int j=0; j<i; j++) {
408  // TODO eps
409  if(fabs(A(i,j))>1e-16) {
410  eleBuf[k]=A(i,j);
411  JBuf[k++]=j;
412  }
413  }
414  for(int j=i+1; j<A.cols(); j++) {
415  // TODO eps
416  if(fabs(A(i,j))>1e-16) {
417  eleBuf[k]=A(i,j);
418  JBuf[k++]=j;
419  }
420  }
421  }
422  I[i]=k;
423  memEle.resize(k);
424  memJ.resize(k);
425  ele = (AT*)memEle.get();
426  J = (int*)memJ.get();
427  for(int i=0; i<k; i++) {
428  ele[i] = eleBuf[i];
429  J[i] = JBuf[i];
430  }
431 
432  delete [] eleBuf;
433  delete [] JBuf;
434  }
435 
436  */
437 
438  //template <> extern void Matrix<Sparse, double>::deepCopy(const Matrix<Sparse, double> &A);
439 
440 }
441 
442 #endif