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