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