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