KASKADE 7 development version
simpleLAPmatrix.hh
Go to the documentation of this file.
1#ifndef SIMPLELAPMATRIX_HH
2#define SIMPLELAPMATRIX_HH
3
4#include <vector>
5#include <dune/grid/config.h>
7
8namespace Kaskade
9{
10
26{
27public:
28 UNCMINVector(const std::vector<double>& v) : data(v) {};
29 UNCMINVector(int n, double v=0.0) : data(n,v) {};
31 UNCMINVector(double* start, double* stop) { for(double* s=start; s<stop; ++s) data.push_back(*s); };
32 UNCMINVector& operator=(const UNCMINVector &A) { data=A.data; return *this; }
33 UNCMINVector& operator=(const double &s) { for(int i=0; i<data.size(); ++i) data[i]=s; return *this; }
34 UNCMINVector& operator*=(const double &s) { for(int i=0; i<data.size(); ++i) data[i]*=s; return *this; }
35 double& operator()(int i) { return data[i-1]; }
36 int size() { return data.size(); }
37 void newsize(int n) {data.resize(n); }
38
39 std::vector<double> data;
40};
41
60template<class Num, int offset=0>
62{
63public:
64template<class Mat>
65 SLAPMatrix(Mat const& mat)
66 {
67 col = mat.M();
68 row = mat.N();
69 lda = mat.N();
70 data.reserve(col*row);
71 // Insert elements in COLUMN-MAJOR ordering
72 for(int i=0; i<col; ++i)
73 for(int j=0; j<row; ++j)
74 data.push_back(mat[j][i]);
75 }
76
78 {
79 col=mat.cols();
80 row=mat.rows();
81 lda=mat.LDA();
82 data=mat.data;
83 }
84
94 SLAPMatrix(int row_, int col_, int lda_ = -1):
95 data(std::max(lda_,row_)*col_,0), col(col_), row(row_), lda(std::max(lda_,row_))
96 {
97 assert(row>=0 && col>=0);
98 }
99
100
102 {
103 col = mat.ncols();
104 row = mat.nrows();
105 lda = mat.nrows();
106 data.resize(col*row);
107 // This should be COLUMN-MAJOR ordering
108 for(int i=0; i<mat.data.size(); ++i)
109 (*this)(mat.ridx[i],mat.cidx[i]) = mat.data[i];
110 }
111
112 void print() const
113 {
114 std::cout << "[" << std::endl;
115 for(int i=0; i<rows(); ++i)
116 {
117 for(int j=0; j<cols();++j)
118 std::cout << (*this)(i,j) << ",";
119 std::cout << std::endl;
120 }
121 std::cout << "]" << std::endl;
122 }
123
124
126 int cols() const {return col;}
128 int rows() const {return row;}
129
130 int size() {return col*row;}
131
133 int LDA() const {return lda;}
134
135template<class Mat>
136 void toMatrix(Mat& M)
137 {
138 for(int i=0; i<rows(); ++i)
139 for(int k=0; k<cols(); ++k)
140 M[i][k] = (*this)(i,k);
141 }
142
143template<class RT>
145 {
146 for(int i=0; i<rows(); ++i)
147 for(int k=0; k<cols(); ++k)
148 {
149 M.cidx.push_back(i);
150 M.ridx.push_back(k);
151 M.data.push_back((*this)(k,i));
152 }
153 }
154
156 Num* ptrToData() {return &data[0];}
157
161 Num const& operator()(int r, int c) const { return data[lda*(c-offset)+(r-offset)]; }
162 Num& operator()(int r, int c) { return data[lda*(c-offset)+(r-offset)]; }
163
166 int num_columns() const {return col;}
168 int num_rows() const {return row;}
169
171 {
172 for(int i=0; i<rows(); ++i)
173 for(int j=0; j<cols(); ++j)
174 (*this)(i,j) *= alpha;
175 return *this;
176 }
177
178 SLAPMatrix<Num,offset> scaleCols(std::vector<Num> scaling)
179 {
180 for(int i=0; i<rows(); ++i)
181 for(int j=0; j<cols(); ++j)
182 (*this)(i,j) *= scaling[j];
183 return *this;
184 }
185
186 SLAPMatrix<Num,offset> scaleRows(std::vector<Num> scaling)
187 {
188 for(int i=0; i<rows(); ++i)
189 for(int j=0; j<cols(); ++j)
190 (*this)(i,j) *= scaling[i];
191 return *this;
192 }
193
194private:
195 std::vector<Num> data;
196 int col,row,lda;
197};
198
200
206void LeastSquares(SLAPMatrix<double> a, std::vector<double> const& b, std::vector<double>& x);
207
208void SymmetricEigenvalues(SLAPMatrix<double> a, std::vector<double>& eig);
209
216template <class Scalar>
218
219
227void MatrixMultiply(SLAPMatrix<double> A, std::vector<double>& in, std::vector<double>& out);
228
229void TransposedMatrixMultiply(SLAPMatrix<double> A, std::vector<double>& in, std::vector<double>& out);
230
232
234
235} // namespace Kaskade
236#endif
SparseIndexInt nrows() const
Returns number of rows (computes them, if not known)
Definition: triplet.hh:202
SparseIndexInt ncols() const
Returns number of cols (computes them, if not known)
Definition: triplet.hh:215
std::vector< SparseIndexInt > ridx
row indices
Definition: triplet.hh:669
std::vector< SparseIndexInt > cidx
column indices
Definition: triplet.hh:671
std::vector< Scalar > data
data
Definition: triplet.hh:673
Very simple dense matrix class for interfacing with LAPACK routines and the optimization tool uncmin.
void toTriplet(MatrixAsTriplet< RT > &M)
SLAPMatrix(MatrixAsTriplet< Num > const &mat)
SLAPMatrix(int row_, int col_, int lda_=-1)
Construction by specifying the size of the matrix directly.
SLAPMatrix< Num, offset > scaleCols(std::vector< Num > scaling)
int num_rows() const
Rows.
SLAPMatrix(Mat const &mat)
int rows() const
Rows.
SLAPMatrix< Num, offset > scaleRows(std::vector< Num > scaling)
int LDA() const
Increment for row iteration.
Num & operator()(int r, int c)
Num const & operator()(int r, int c) const
Access to elements.
SLAPMatrix< Num, offset > operator*=(Num alpha)
int cols() const
Columns.
SLAPMatrix(SLAPMatrix< Num, offset > const &mat)
Num * ptrToData()
Access to the raw data. This is needed by LAPACK routines.
Very simple vector wrapper for the use of the optimization tool uncmin, which is not capable of using...
double & operator()(int i)
UNCMINVector(int n, double v=0.0)
UNCMINVector(const std::vector< double > &v)
std::vector< double > data
UNCMINVector & operator=(const double &s)
UNCMINVector(double *start, double *stop)
UNCMINVector & operator=(const UNCMINVector &A)
UNCMINVector & operator*=(const double &s)
Dune::FieldVector< T, n > max(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise maximum.
Definition: fixdune.hh:110
void MatrixMultiply(SLAPMatrix< double > A, std::vector< double > &in, std::vector< double > &out)
Matrix multiplication.
Dune::FieldVector< Scalar, d > eig(Dune::FieldMatrix< Scalar, d, d > &A, bool computeEigenvectors=true)
Computes the eigenvalues and eigenvectors of a symmetric matrix .
void MatrixKT_A_K(SLAPMatrix< double > A, SLAPMatrix< double > &K, SLAPMatrix< double > &out)
void LeastSquares(SLAPMatrix< double > a, std::vector< double > const &b, std::vector< double > &x)
Solve linear least squares problem, given by a*x=b.
void MatrixMultiplication(SLAPMatrix< double > A, SLAPMatrix< double > &B, SLAPMatrix< double > &AB)
void pseudoinverse(SLAPMatrix< Scalar > A, SLAPMatrix< Scalar > &Ainv)
void SymmetricEigenvalues(SLAPMatrix< double > a, std::vector< double > &eig)
void TransposedMatrixMultiply(SLAPMatrix< double > A, std::vector< double > &in, std::vector< double > &out)