1 #ifndef MATH_SPARSE_MATRIX_TEMPLATE_H 2 #define MATH_SPARSE_MATRIX_TEMPLATE_H 4 #include "MatrixTemplate.h" 5 #include "VectorTemplate.h" 6 #include "SparseVectorTemplate.h" 55 void initialize(
int m,
int n);
56 void resize(
int m,
int n);
59 T& operator () (
int i,
int j);
60 void insertEntry(
int i,
int j,
const T&);
62 const T*
getEntry(
int i,
int j)
const;
63 void eraseEntry(
int i,
int j);
65 void copy(
const MyT& m);
68 void copySubMatrix(
int i,
int j,
const MyT&);
69 void copySubMatrix(
int i,
int j,
const MatrixT&,T zeroTol=0);
71 void set(
const MatrixT& m,T zeroTol=0);
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); }
81 void mul(
const MyT&, T s);
82 void mul(
const VectorT& y,VectorT& x)
const;
83 void mulTranspose(
const VectorT& y,VectorT& x)
const;
84 void madd(
const VectorT& y,VectorT& x)
const;
85 void maddTranspose(
const VectorT& y,VectorT& x)
const;
86 void mul(
const MatrixT& y,MatrixT& x)
const;
87 void mulTranspose(
const MatrixT& y,MatrixT& x)
const;
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();
97 void inplaceMulRow(
int i,T c);
98 void inplaceMulCol(
int i,T c);
99 void eraseZeros(T zeroTol=0);
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;
110 typedef typename SparseArray<T>::iterator RowIterator;
111 typedef typename SparseArray<T>::const_iterator ConstRowIterator;
113 std::vector<RowT> rows;
134 void initialize(
int m,
int n,
int num_entries);
135 void resize(
int m,
int n,
int num_entries);
139 const T*
getEntry(
int i,
int j)
const;
141 void copy(
const MyT&);
144 void set(
const MatrixT&,T zeroTol=Zero);
145 void getCopy(MyT& m)
const { m.copy(*
this); }
146 void get(MatrixT&)
const;
148 void mul(
const MyT&, T s);
149 void mul(
const VectorT& y, VectorT& x)
const;
150 void mulTranspose(
const VectorT& y, VectorT& x)
const;
151 void madd(
const VectorT& y,VectorT& x)
const;
152 void maddTranspose(
const VectorT& y,VectorT& x)
const;
153 void mul(
const MatrixT& w, MatrixT& v)
const;
154 void mulTranspose(
const MatrixT& w, MatrixT& v)
const;
155 T dotRow(
int i,
const VectorT&)
const;
156 T dotCol(
int j,
const VectorT&)
const;
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);
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); }
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]; }
192 static void self_test();
193 static void self_test(
int m,
int n,
int nnz);
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;
218 std::ostream& operator << (std::ostream&, const SparseMatrixTemplate_RM<T>&);
222 std::ostream& operator << (std::ostream&, const SparseMatrixTemplate_CR<T>&);
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