KrisLibrary  1.0.0
SparseMatrixTemplate.h
Go to the documentation of this file.
1 #ifndef MATH_SPARSE_MATRIX_TEMPLATE_H
2 #define MATH_SPARSE_MATRIX_TEMPLATE_H
3 
4 #include "MatrixTemplate.h"
5 #include "VectorTemplate.h"
6 #include "SparseVectorTemplate.h"
7 #include <vector>
8 
33 namespace Math {
34 
43 template <class T>
45 {
46  public:
48  typedef VectorTemplate<T> VectorT;
49  typedef MatrixTemplate<T> MatrixT;
51 
53  SparseMatrixTemplate_RM(int m,int n);
54  SparseMatrixTemplate_RM(const MyT&);
55  void initialize(int m, int n);
56  void resize(int m, int n);
57  void clear();
58 
59  T& operator () (int i, int j);
60  void insertEntry(int i, int j, const T&);
61  T* getEntry(int i, int j);
62  const T* getEntry(int i,int j) const;
63  void eraseEntry(int i, int j);
64 
65  void copy(const MyT& m);
66  template <class T2>
67  void copy(const SparseMatrixTemplate_RM<T2>& m); //cast
68  void copySubMatrix(int i, int j, const MyT&);
69  void copySubMatrix(int i, int j, const MatrixT&,T zeroTol=0);
70  void swap(MyT&);
71  void set(const MatrixT& m,T zeroTol=0);
72  void setZero();
73  void setIdentity();
74  void setNegative(const MyT&);
75  void setTranspose(const MyT&);
76  void setAdjoint(const MyT&);
77  void getCopy(MyT& m) const { m.copy(*this); }
78  void get(MatrixT& m) const;
79  void getTranspose(MyT& m) const { m.setTranspose(*this); }
80 
81  void mul(const MyT&, T s);
82  void mul(const VectorT& y,VectorT& x) const; //x = this*y
83  void mulTranspose(const VectorT& y,VectorT& x) const; //x = this^t*y
84  void madd(const VectorT& y,VectorT& x) const; //x = x+this*y
85  void maddTranspose(const VectorT& y,VectorT& x) const; //x = x+this^t*y
86  void mul(const MatrixT& y,MatrixT& x) const; //x = this*y
87  void mulTranspose(const MatrixT& y,MatrixT& x) const; //x = this^t*y
88  void copyRow(int i,const VectorT& x,T zeroTol=0);
89  void copyRow(int i,const SparseVectorT& x);
90  void copyCol(int j,const VectorT& x,T zeroTol=0);
91  void copyCol(int j,const SparseVectorT& x);
92  T dotRow(int i, const VectorT&) const;
93  T dotCol(int j, const VectorT&) const;
94  void inplaceNegative();
95  void inplaceMul(T c);
96  void inplaceDiv(T c);
97  void inplaceMulRow(int i,T c);
98  void inplaceMulCol(int i,T c);
99  void eraseZeros(T zeroTol=0);
100 
101  bool isValid() const;
102  inline bool isEmpty() const { return m==0&&n==0; }
103  inline bool isSquare() const { return m==n; }
104  inline bool isValidRow(int i) const { return 0<=i&&i<m; }
105  inline bool isValidCol(int j) const { return 0<=j&&j<n; }
106  inline bool isValidIndex(int i,int j) const { return 0<=i&&i<m&&0<=j&&j<n; }
107  size_t numNonZeros() const;
108 
109  typedef SparseArray<T> RowT;
110  typedef typename SparseArray<T>::iterator RowIterator;
111  typedef typename SparseArray<T>::const_iterator ConstRowIterator;
112 
113  std::vector<RowT> rows;
114  int m,n;
115 };
116 
124 template <class T>
126 {
127  public:
129  typedef VectorTemplate<T> VectorT;
130  typedef MatrixTemplate<T> MatrixT;
131 
134  void initialize(int m, int n, int num_entries);
135  void resize(int m, int n, int num_entries);
136  void clear();
137 
138  T* getEntry(int i,int j);
139  const T* getEntry(int i,int j) const;
140 
141  void copy(const MyT&);
142  template <class T2>
143  void copy(const SparseMatrixTemplate_CR<T2>&);
144  void set(const MatrixT&,T zeroTol=Zero);
145  void getCopy(MyT& m) const { m.copy(*this); }
146  void get(MatrixT&) const;
147 
148  void mul(const MyT&, T s);
149  void mul(const VectorT& y, VectorT& x) const; //x = this*y;
150  void mulTranspose(const VectorT& y, VectorT& x) const; //x = this^t*y
151  void madd(const VectorT& y,VectorT& x) const; //x = x+this*y
152  void maddTranspose(const VectorT& y,VectorT& x) const; //x = x+this^t*y
153  void mul(const MatrixT& w, MatrixT& v) const; //dense matrix multiply v = this*w
154  void mulTranspose(const MatrixT& w, MatrixT& v) const; //w = this^t*w
155  T dotRow(int i, const VectorT&) const;
156  T dotCol(int j, const VectorT&) const;
157  //assumes lower triangle is filled, upper is transpose
158  T dotSymmL(int i, const VectorT&) const;
159  void inplaceMul(T c);
160  void inplaceDiv(T c);
161  void inplaceMulRow(int i,T c);
162  void inplaceMulCol(int i,T c);
163 
164  bool isValid() const;
165  inline bool isEmpty() const { return m == 0 && n == 0; }
166  inline bool hasDims(int M, int N) const { return m == M && n == N; }
167  inline bool isSquare() const { return m == n; }
168  inline bool isValidRow(int i) const { return i >= 0 && i < m; }
169  inline bool isValidCol(int j) const { return j >= 0 && j < n; }
170  inline bool isValidIndex(int i,int j) const { return isValidRow(i)&&isValidCol(j); }
171 
172  inline int* rowIndices(int i) const { return col_indices + row_offsets[i]; }
173  inline T* rowValues(int i) const { return val_array + row_offsets[i]; }
174  inline int numRowEntries(int i) const { return row_offsets[i+1]-row_offsets[i]; }
175 
176  /***************************************************************
177  * Compressed row format:
178  * row_offsets indexes into the column index array, by row, and is size m+1.
179  * The number of entries per row can be determined by subtraction from
180  * the next value (the last value in row_offsets is num_entries)
181  * The col_indices array marks the column of the corresponding
182  * element in the value array, and each column is sorted in
183  * increasing order.
184  ****************************************************************/
185  int* row_offsets;
186  int* col_indices;
187  T* val_array;
188 
189  int m,n;
190  int num_entries;
191 
192  static void self_test();
193  static void self_test(int m, int n, int nnz);
194 };
195 
197 template <class T>
199 {
200  for(size_t i=0;i<x.rows.size();i++) {
201  typename SparseMatrixTemplate_RM<T>::ConstRowIterator j;
202  for(j=x.rows[i].begin();j!=x.rows[i].end();j++)
203  if(!IsFinite(j->second)) return false;
204  }
205  return true;
206 }
207 
208 
209 class Complex;
216 
217 template <class T>
218 std::ostream& operator << (std::ostream&, const SparseMatrixTemplate_RM<T>&);
219 template <class T>
220 std::istream& operator >> (std::istream&, SparseMatrixTemplate_RM<T>&);
221 template <class T>
222 std::ostream& operator << (std::ostream&, const SparseMatrixTemplate_CR<T>&);
223 
224 } //namespace Math
225 
226 #endif
A sparse 1D array class.
Definition: SparseArray.h:13
Row-major sparse matrix.
Definition: SparseMatrixTemplate.h:44
Definition: SparseVectorTemplate.h:14
Complex number class (x + iy).
Definition: complex.h:17
T * getEntry(int i, int j)
returns NULL if the entry does not exist
Definition: SparseMatrixTemplate.cpp:68
A matrix over the field T.
Definition: function.h:10
Row-major, compressed-row sparse matrix.
Definition: SparseMatrixTemplate.h:125
Contains all definitions in the Math package.
Definition: WorkspaceBound.h:12
A vector over the field T.
Definition: function.h:9
int IsFinite(double x)
Returns nonzero unless x is infinite or a NaN.
Definition: infnan.cpp:75