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