1 | /*
|
---|
2 | * Matrix.cpp
|
---|
3 | *
|
---|
4 | * Created on: Jun 25, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "Helpers/MemDebug.hpp"
|
---|
9 |
|
---|
10 | #include "LinearAlgebra/Matrix.hpp"
|
---|
11 | #include "Helpers/Assert.hpp"
|
---|
12 | #include "Exceptions/NotInvertibleException.hpp"
|
---|
13 | #include "Helpers/fast_functions.hpp"
|
---|
14 | #include "Helpers/Assert.hpp"
|
---|
15 | #include "LinearAlgebra/Vector.hpp"
|
---|
16 | #include "VectorContent.hpp"
|
---|
17 | #include "MatrixContent.hpp"
|
---|
18 |
|
---|
19 | #include <gsl/gsl_blas.h>
|
---|
20 | #include <gsl/gsl_eigen.h>
|
---|
21 | #include <gsl/gsl_matrix.h>
|
---|
22 | #include <gsl/gsl_multimin.h>
|
---|
23 | #include <gsl/gsl_vector.h>
|
---|
24 | #include <cmath>
|
---|
25 | #include <iostream>
|
---|
26 |
|
---|
27 | using namespace std;
|
---|
28 |
|
---|
29 | Matrix::Matrix()
|
---|
30 | {
|
---|
31 | content = new MatrixContent();
|
---|
32 | content->content = gsl_matrix_calloc(NDIM, NDIM);
|
---|
33 | createViews();
|
---|
34 | }
|
---|
35 |
|
---|
36 | Matrix::Matrix(const double* src){
|
---|
37 | content = new MatrixContent();
|
---|
38 | content->content = gsl_matrix_alloc(NDIM, NDIM);
|
---|
39 | set(0,0, src[0]);
|
---|
40 | set(1,0, src[1]);
|
---|
41 | set(2,0, src[2]);
|
---|
42 |
|
---|
43 | set(0,1, src[3]);
|
---|
44 | set(1,1, src[4]);
|
---|
45 | set(2,1, src[5]);
|
---|
46 |
|
---|
47 | set(0,2, src[6]);
|
---|
48 | set(1,2, src[7]);
|
---|
49 | set(2,2, src[8]);
|
---|
50 | createViews();
|
---|
51 | }
|
---|
52 |
|
---|
53 | Matrix::Matrix(const Matrix &src){
|
---|
54 | content = new MatrixContent();
|
---|
55 | content->content = gsl_matrix_alloc(NDIM, NDIM);
|
---|
56 | gsl_matrix_memcpy(content->content,src.content->content);
|
---|
57 | createViews();
|
---|
58 | }
|
---|
59 |
|
---|
60 | Matrix::Matrix(MatrixContent* _content) :
|
---|
61 | content(_content)
|
---|
62 | {
|
---|
63 | createViews();
|
---|
64 | }
|
---|
65 |
|
---|
66 | Matrix::~Matrix()
|
---|
67 | {
|
---|
68 | // delete all views
|
---|
69 | for(int i=NDIM;i--;){
|
---|
70 | delete rows_ptr[i];
|
---|
71 | }
|
---|
72 | for(int i=NDIM;i--;){
|
---|
73 | delete columns_ptr[i];
|
---|
74 | }
|
---|
75 | delete diagonal_ptr;
|
---|
76 | gsl_matrix_free(content->content);
|
---|
77 | delete content;
|
---|
78 | }
|
---|
79 |
|
---|
80 | void Matrix::createViews(){
|
---|
81 | // create row views
|
---|
82 | for(int i=NDIM;i--;){
|
---|
83 | VectorContent *rowContent = new VectorViewContent(gsl_matrix_row(content->content,i));
|
---|
84 | rows_ptr[i] = new Vector(rowContent);
|
---|
85 | }
|
---|
86 | // create column views
|
---|
87 | for(int i=NDIM;i--;){
|
---|
88 | VectorContent *columnContent = new VectorViewContent(gsl_matrix_column(content->content,i));
|
---|
89 | columns_ptr[i] = new Vector(columnContent);
|
---|
90 | }
|
---|
91 | // create diagonal view
|
---|
92 | VectorContent *diagonalContent = new VectorViewContent(gsl_matrix_diagonal(content->content));
|
---|
93 | diagonal_ptr = new Vector(diagonalContent);
|
---|
94 | }
|
---|
95 |
|
---|
96 | void Matrix::one(){
|
---|
97 | for(int i=NDIM;i--;){
|
---|
98 | for(int j=NDIM;j--;){
|
---|
99 | set(i,j,i==j);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | void Matrix::zero(){
|
---|
105 | for(int i=NDIM;i--;){
|
---|
106 | for(int j=NDIM;j--;){
|
---|
107 | set(i,j,0.);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | Matrix &Matrix::operator=(const Matrix &src){
|
---|
113 | if(&src!=this){
|
---|
114 | gsl_matrix_memcpy(content->content,src.content->content);
|
---|
115 | }
|
---|
116 | return *this;
|
---|
117 | }
|
---|
118 |
|
---|
119 | const Matrix &Matrix::operator+=(const Matrix &rhs){
|
---|
120 | gsl_matrix_add(content->content, rhs.content->content);
|
---|
121 | return *this;
|
---|
122 | }
|
---|
123 |
|
---|
124 | const Matrix &Matrix::operator-=(const Matrix &rhs){
|
---|
125 | gsl_matrix_sub(content->content, rhs.content->content);
|
---|
126 | return *this;
|
---|
127 | }
|
---|
128 |
|
---|
129 | const Matrix &Matrix::operator*=(const Matrix &rhs){
|
---|
130 | (*this) = (*this)*rhs;
|
---|
131 | return *this;
|
---|
132 | }
|
---|
133 |
|
---|
134 | const Matrix Matrix::operator+(const Matrix &rhs) const{
|
---|
135 | Matrix tmp = *this;
|
---|
136 | tmp+=rhs;
|
---|
137 | return tmp;
|
---|
138 | }
|
---|
139 |
|
---|
140 | const Matrix Matrix::operator-(const Matrix &rhs) const{
|
---|
141 | Matrix tmp = *this;
|
---|
142 | tmp-=rhs;
|
---|
143 | return tmp;
|
---|
144 | }
|
---|
145 |
|
---|
146 | const Matrix Matrix::operator*(const Matrix &rhs) const{
|
---|
147 | gsl_matrix *res = gsl_matrix_alloc(NDIM, NDIM);
|
---|
148 | gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, content->content, rhs.content->content, 0.0, res);
|
---|
149 | MatrixContent *content= new MatrixContent();
|
---|
150 | content->content = res;
|
---|
151 | return Matrix(content);
|
---|
152 | }
|
---|
153 |
|
---|
154 | double &Matrix::at(size_t i, size_t j){
|
---|
155 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
156 | ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
|
---|
157 | return *gsl_matrix_ptr (content->content, i, j);
|
---|
158 | }
|
---|
159 |
|
---|
160 | const double Matrix::at(size_t i, size_t j) const{
|
---|
161 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
162 | ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
|
---|
163 | return gsl_matrix_get(content->content, i, j);
|
---|
164 | }
|
---|
165 |
|
---|
166 | Vector &Matrix::row(size_t i){
|
---|
167 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
168 | return *rows_ptr[i];
|
---|
169 | }
|
---|
170 |
|
---|
171 | const Vector &Matrix::row(size_t i) const{
|
---|
172 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
173 | return *rows_ptr[i];
|
---|
174 | }
|
---|
175 |
|
---|
176 | Vector &Matrix::column(size_t i){
|
---|
177 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
178 | return *columns_ptr[i];
|
---|
179 | }
|
---|
180 |
|
---|
181 | const Vector &Matrix::column(size_t i) const{
|
---|
182 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
183 | return *columns_ptr[i];
|
---|
184 | }
|
---|
185 |
|
---|
186 | Vector &Matrix::diagonal(){
|
---|
187 | return *diagonal_ptr;
|
---|
188 | }
|
---|
189 |
|
---|
190 | const Vector &Matrix::diagonal() const{
|
---|
191 | return *diagonal_ptr;
|
---|
192 | }
|
---|
193 |
|
---|
194 | void Matrix::set(size_t i, size_t j, const double value){
|
---|
195 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
196 | ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
|
---|
197 | gsl_matrix_set(content->content,i,j,value);
|
---|
198 | }
|
---|
199 |
|
---|
200 | double Matrix::determinant() const{
|
---|
201 | return at(0,0)*at(1,1)*at(2,2)
|
---|
202 | + at(0,1)*at(1,2)*at(2,0)
|
---|
203 | + at(0,2)*at(1,0)*at(2,1)
|
---|
204 | - at(2,0)*at(1,1)*at(0,2)
|
---|
205 | - at(2,1)*at(1,2)*at(0,0)
|
---|
206 | - at(2,2)*at(1,0)*at(0,1);
|
---|
207 | }
|
---|
208 |
|
---|
209 | Matrix Matrix::transpose() const
|
---|
210 | {
|
---|
211 | Matrix copy(*this);
|
---|
212 | copy.transpose();
|
---|
213 | return copy;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | void Matrix::transpose()
|
---|
218 | {
|
---|
219 | double tmp;
|
---|
220 | for (int i=0;i<NDIM;i++)
|
---|
221 | for (int j=i+1;j<NDIM;j++) {
|
---|
222 | tmp = at(j,i);
|
---|
223 | at(i,j) = tmp;
|
---|
224 | at(j,i) = tmp;
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | Matrix Matrix::invert() const{
|
---|
230 | double det = determinant();
|
---|
231 | if(fabs(det)<MYEPSILON){
|
---|
232 | throw NotInvertibleException(__FILE__,__LINE__);
|
---|
233 | }
|
---|
234 |
|
---|
235 | double detReci = 1./det;
|
---|
236 | Matrix res;
|
---|
237 | res.set(0,0, detReci*RDET2(at(1,1),at(2,1),at(1,2),at(2,2))); // A_11
|
---|
238 | res.set(1,0, -detReci*RDET2(at(1,0),at(2,0),at(1,2),at(2,2))); // A_21
|
---|
239 | res.set(2,0, detReci*RDET2(at(1,0),at(2,0),at(1,1),at(2,1))); // A_31
|
---|
240 | res.set(0,1, -detReci*RDET2(at(0,1),at(2,1),at(0,2),at(2,2))); // A_12
|
---|
241 | res.set(1,1, detReci*RDET2(at(0,0),at(2,0),at(0,2),at(2,2))); // A_22
|
---|
242 | res.set(2,1, -detReci*RDET2(at(0,0),at(2,0),at(0,1),at(2,1))); // A_32
|
---|
243 | res.set(0,2, detReci*RDET2(at(0,1),at(1,1),at(0,2),at(1,2))); // A_13
|
---|
244 | res.set(1,2, -detReci*RDET2(at(0,0),at(1,0),at(0,2),at(1,2))); // A_23
|
---|
245 | res.set(2,2, detReci*RDET2(at(0,0),at(1,0),at(0,1),at(1,1))); // A_33
|
---|
246 | return res;
|
---|
247 | }
|
---|
248 |
|
---|
249 | Vector Matrix::transformToEigenbasis()
|
---|
250 | {
|
---|
251 | gsl_eigen_symmv_workspace *T = gsl_eigen_symmv_alloc(NDIM);
|
---|
252 | gsl_vector *eval = gsl_vector_alloc(NDIM);
|
---|
253 | gsl_matrix *evec = gsl_matrix_alloc(NDIM, NDIM);
|
---|
254 | gsl_eigen_symmv(content->content, eval, evec, T);
|
---|
255 | gsl_eigen_symmv_free(T);
|
---|
256 | gsl_matrix_memcpy(content->content, evec);
|
---|
257 | Vector evalues(gsl_vector_get(eval,0), gsl_vector_get(eval,1), gsl_vector_get(eval,2));
|
---|
258 | return evalues;
|
---|
259 | }
|
---|
260 |
|
---|
261 | const Matrix &Matrix::operator*=(const double factor){
|
---|
262 | gsl_matrix_scale(content->content, factor);
|
---|
263 | return *this;
|
---|
264 | }
|
---|
265 |
|
---|
266 | const Matrix operator*(const double factor,const Matrix& mat){
|
---|
267 | Matrix tmp = mat;
|
---|
268 | tmp*=factor;
|
---|
269 | return tmp;
|
---|
270 | }
|
---|
271 |
|
---|
272 | const Matrix operator*(const Matrix &mat,const double factor){
|
---|
273 | return factor*mat;
|
---|
274 | }
|
---|
275 |
|
---|
276 | bool Matrix::operator==(const Matrix &rhs) const{
|
---|
277 | for(int i=NDIM;i--;){
|
---|
278 | for(int j=NDIM;j--;){
|
---|
279 | if(fabs(at(i,j)-rhs.at(i,j))>MYEPSILON){
|
---|
280 | return false;
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|
284 | return true;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix.
|
---|
288 | * \param *symm 6-dim array of unique symmetric matrix components
|
---|
289 | * \return allocated NDIM*NDIM array with the symmetric matrix
|
---|
290 | */
|
---|
291 | Matrix ReturnFullMatrixforSymmetric(const double * const symm)
|
---|
292 | {
|
---|
293 | Matrix matrix;
|
---|
294 | matrix.set(0,0, symm[0]);
|
---|
295 | matrix.set(1,0, symm[1]);
|
---|
296 | matrix.set(2,0, symm[3]);
|
---|
297 | matrix.set(0,1, symm[1]);
|
---|
298 | matrix.set(1,1, symm[2]);
|
---|
299 | matrix.set(2,1, symm[4]);
|
---|
300 | matrix.set(0,2, symm[3]);
|
---|
301 | matrix.set(1,2, symm[4]);
|
---|
302 | matrix.set(2,2, symm[5]);
|
---|
303 | return matrix;
|
---|
304 | };
|
---|
305 |
|
---|
306 | ostream &operator<<(ostream &ost,const Matrix &mat){
|
---|
307 | for(int i = 0;i<NDIM;++i){
|
---|
308 | ost << "\n";
|
---|
309 | for(int j = 0; j<NDIM;++j){
|
---|
310 | ost << mat.at(i,j);
|
---|
311 | if(j!=NDIM-1)
|
---|
312 | ost << "; ";
|
---|
313 | }
|
---|
314 | }
|
---|
315 | return ost;
|
---|
316 | }
|
---|
317 |
|
---|
318 | Vector operator*(const Matrix &mat,const Vector &vec){
|
---|
319 | gsl_vector *res = gsl_vector_calloc(NDIM);
|
---|
320 | gsl_blas_dgemv( CblasNoTrans, 1.0, mat.content->content, vec.content->content, 0.0, res);
|
---|
321 | VectorContent *content = new VectorContent();
|
---|
322 | content->content = res;
|
---|
323 | return Vector(content);
|
---|
324 | }
|
---|
325 |
|
---|
326 | Vector &operator*=(Vector& lhs,const Matrix &mat){
|
---|
327 | lhs = mat*lhs;
|
---|
328 | return lhs;
|
---|
329 | }
|
---|
330 |
|
---|