1 | /*
|
---|
2 | * Matrix.cpp
|
---|
3 | *
|
---|
4 | * Created on: Jun 25, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "Matrix.hpp"
|
---|
9 | #include "Helpers/Assert.hpp"
|
---|
10 | #include "Exceptions/NotInvertibleException.hpp"
|
---|
11 | #include "Helpers/fast_functions.hpp"
|
---|
12 | #include "Helpers/Assert.hpp"
|
---|
13 |
|
---|
14 | #include <gsl/gsl_blas.h>
|
---|
15 | #include <cmath>
|
---|
16 |
|
---|
17 | Matrix::Matrix()
|
---|
18 | {
|
---|
19 | content = gsl_matrix_calloc(NDIM, NDIM);
|
---|
20 | }
|
---|
21 |
|
---|
22 | Matrix::Matrix(const double* src){
|
---|
23 | content = gsl_matrix_alloc(NDIM, NDIM);
|
---|
24 | set(0,0, src[0]);
|
---|
25 | set(1,0, src[1]);
|
---|
26 | set(2,0, src[2]);
|
---|
27 |
|
---|
28 | set(0,1, src[3]);
|
---|
29 | set(1,1, src[4]);
|
---|
30 | set(2,1, src[5]);
|
---|
31 |
|
---|
32 | set(0,2, src[6]);
|
---|
33 | set(1,2, src[7]);
|
---|
34 | set(2,2, src[8]);
|
---|
35 | }
|
---|
36 |
|
---|
37 | Matrix::Matrix(const Matrix &src){
|
---|
38 | content = gsl_matrix_alloc(NDIM, NDIM);
|
---|
39 | gsl_matrix_memcpy(content,src.content);
|
---|
40 | }
|
---|
41 |
|
---|
42 | Matrix::Matrix(gsl_matrix* _content) :
|
---|
43 | content(_content)
|
---|
44 | {}
|
---|
45 |
|
---|
46 | Matrix::~Matrix()
|
---|
47 | {
|
---|
48 | gsl_matrix_free(content);
|
---|
49 | }
|
---|
50 |
|
---|
51 | void Matrix::one(){
|
---|
52 | gsl_matrix_free(content);
|
---|
53 | content = gsl_matrix_calloc(NDIM, NDIM);
|
---|
54 | for(int i = NDIM;i--;){
|
---|
55 | set(i,i,1.);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | Matrix &Matrix::operator=(const Matrix &src){
|
---|
60 | if(&src!=this){
|
---|
61 | gsl_matrix_memcpy(content,src.content);
|
---|
62 | }
|
---|
63 | return *this;
|
---|
64 | }
|
---|
65 |
|
---|
66 | Matrix &Matrix::operator+=(const Matrix &rhs){
|
---|
67 | gsl_matrix_add(content, rhs.content);
|
---|
68 | return *this;
|
---|
69 | }
|
---|
70 |
|
---|
71 | Matrix &Matrix::operator-=(const Matrix &rhs){
|
---|
72 | gsl_matrix_sub(content, rhs.content);
|
---|
73 | return *this;
|
---|
74 | }
|
---|
75 |
|
---|
76 | Matrix &Matrix::operator*=(const Matrix &rhs){
|
---|
77 | (*this) = (*this)*rhs;
|
---|
78 | return *this;
|
---|
79 | }
|
---|
80 |
|
---|
81 | Matrix Matrix::operator+(const Matrix &rhs) const{
|
---|
82 | Matrix tmp = *this;
|
---|
83 | tmp+=rhs;
|
---|
84 | return tmp;
|
---|
85 | }
|
---|
86 |
|
---|
87 | Matrix Matrix::operator-(const Matrix &rhs) const{
|
---|
88 | Matrix tmp = *this;
|
---|
89 | tmp-=rhs;
|
---|
90 | return tmp;
|
---|
91 | }
|
---|
92 |
|
---|
93 | Matrix Matrix::operator*(const Matrix &rhs) const{
|
---|
94 | gsl_matrix *res = gsl_matrix_alloc(NDIM, NDIM);
|
---|
95 | gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, this->content, rhs.content, 0.0, res);
|
---|
96 | return Matrix(res);
|
---|
97 | }
|
---|
98 |
|
---|
99 | double &Matrix::at(size_t i, size_t j){
|
---|
100 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
101 | ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
|
---|
102 | return *gsl_matrix_ptr (content, i, j);
|
---|
103 | }
|
---|
104 |
|
---|
105 | const double Matrix::at(size_t i, size_t j) const{
|
---|
106 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
107 | ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
|
---|
108 | return gsl_matrix_get(content, i, j);
|
---|
109 | }
|
---|
110 |
|
---|
111 | void Matrix::set(size_t i, size_t j, const double value){
|
---|
112 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
113 | ASSERT(j>=0&&j<NDIM,"Index j for Matrix access out of range");
|
---|
114 | gsl_matrix_set(content,i,j,value);
|
---|
115 | }
|
---|
116 |
|
---|
117 | double Matrix::determinant() const{
|
---|
118 | return at(0,0)*at(1,1)*at(2,2)
|
---|
119 | + at(0,1)*at(1,2)*at(2,0)
|
---|
120 | + at(0,2)*at(1,0)*at(2,1)
|
---|
121 | - at(2,0)*at(1,1)*at(0,2)
|
---|
122 | - at(2,1)*at(1,2)*at(0,0)
|
---|
123 | - at(2,2)*at(1,0)*at(0,1);
|
---|
124 | }
|
---|
125 |
|
---|
126 | Matrix Matrix::invert() const{
|
---|
127 | double det = determinant();
|
---|
128 | if(fabs(det)<MYEPSILON){
|
---|
129 | throw NotInvertibleException(__FILE__,__LINE__);
|
---|
130 | }
|
---|
131 |
|
---|
132 | double detReci = 1./det;
|
---|
133 | Matrix res;
|
---|
134 | res.set(0,0, detReci*RDET2(at(1,1),at(2,1),at(1,2),at(2,2))); // A_11
|
---|
135 | res.set(1,0, -detReci*RDET2(at(1,0),at(2,0),at(1,2),at(2,2))); // A_21
|
---|
136 | res.set(2,0, detReci*RDET2(at(1,0),at(2,0),at(1,1),at(2,1))); // A_31
|
---|
137 | res.set(0,1, -detReci*RDET2(at(0,1),at(2,1),at(0,2),at(2,2))); // A_12
|
---|
138 | res.set(1,1, detReci*RDET2(at(0,0),at(2,0),at(0,2),at(2,2))); // A_22
|
---|
139 | res.set(2,1, -detReci*RDET2(at(0,0),at(2,0),at(0,1),at(2,1))); // A_32
|
---|
140 | res.set(0,2, detReci*RDET2(at(0,1),at(1,1),at(0,2),at(1,2))); // A_13
|
---|
141 | res.set(1,2, -detReci*RDET2(at(0,0),at(1,0),at(0,2),at(1,2))); // A_23
|
---|
142 | res.set(2,2, detReci*RDET2(at(0,0),at(1,0),at(0,1),at(1,1))); // A_33
|
---|
143 | return res;
|
---|
144 | }
|
---|
145 |
|
---|
146 | Matrix &Matrix::operator*=(const double factor){
|
---|
147 | gsl_matrix_scale(content, factor);
|
---|
148 | return *this;
|
---|
149 | }
|
---|
150 |
|
---|
151 | Matrix operator*(const double factor,const Matrix& mat){
|
---|
152 | Matrix tmp = mat;
|
---|
153 | tmp*=factor;
|
---|
154 | return tmp;
|
---|
155 | }
|
---|
156 |
|
---|
157 | Matrix operator*(const Matrix &mat,const double factor){
|
---|
158 | return factor*mat;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix.
|
---|
162 | * \param *symm 6-dim array of unique symmetric matrix components
|
---|
163 | * \return allocated NDIM*NDIM array with the symmetric matrix
|
---|
164 | */
|
---|
165 | Matrix ReturnFullMatrixforSymmetric(const double * const symm)
|
---|
166 | {
|
---|
167 | Matrix matrix;
|
---|
168 | matrix.set(0,0, symm[0]);
|
---|
169 | matrix.set(1,0, symm[1]);
|
---|
170 | matrix.set(2,0, symm[3]);
|
---|
171 | matrix.set(0,1, symm[1]);
|
---|
172 | matrix.set(1,1, symm[2]);
|
---|
173 | matrix.set(2,1, symm[4]);
|
---|
174 | matrix.set(0,2, symm[3]);
|
---|
175 | matrix.set(1,2, symm[4]);
|
---|
176 | matrix.set(2,2, symm[5]);
|
---|
177 | return matrix;
|
---|
178 | };
|
---|