| 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 |  | 
|---|
| 13 | #include <gsl/gsl_blas.h> | 
|---|
| 14 | #include <cmath> | 
|---|
| 15 |  | 
|---|
| 16 | Matrix::Matrix() | 
|---|
| 17 | { | 
|---|
| 18 | content = gsl_matrix_calloc(NDIM, NDIM); | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | Matrix::Matrix(const double* src){ | 
|---|
| 22 | content = gsl_matrix_alloc(NDIM, NDIM); | 
|---|
| 23 | this->at(0,0) = src[0]; | 
|---|
| 24 | this->at(1,0) = src[1]; | 
|---|
| 25 | this->at(2,0) = src[2]; | 
|---|
| 26 |  | 
|---|
| 27 | this->at(0,1) = src[3]; | 
|---|
| 28 | this->at(1,1) = src[4]; | 
|---|
| 29 | this->at(2,1) = src[5]; | 
|---|
| 30 |  | 
|---|
| 31 | this->at(0,2) = src[6]; | 
|---|
| 32 | this->at(1,2) = src[7]; | 
|---|
| 33 | this->at(2,2) = src[8]; | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | Matrix::Matrix(const Matrix &src){ | 
|---|
| 37 | content = gsl_matrix_alloc(NDIM, NDIM); | 
|---|
| 38 | gsl_matrix_memcpy(content,src.content); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | Matrix::Matrix(gsl_matrix* _content) : | 
|---|
| 42 | content(_content) | 
|---|
| 43 | {} | 
|---|
| 44 |  | 
|---|
| 45 | Matrix::~Matrix() | 
|---|
| 46 | { | 
|---|
| 47 | gsl_matrix_free(content); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | Matrix &Matrix::operator=(const Matrix &src){ | 
|---|
| 51 | if(&src!=this){ | 
|---|
| 52 | gsl_matrix_memcpy(content,src.content); | 
|---|
| 53 | } | 
|---|
| 54 | return *this; | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | Matrix &Matrix::operator+=(const Matrix &rhs){ | 
|---|
| 58 | gsl_matrix_add(content, rhs.content); | 
|---|
| 59 | return *this; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | Matrix &Matrix::operator-=(const Matrix &rhs){ | 
|---|
| 63 | gsl_matrix_sub(content, rhs.content); | 
|---|
| 64 | return *this; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | Matrix &Matrix::operator*=(const Matrix &rhs){ | 
|---|
| 68 | (*this) = (*this)*rhs; | 
|---|
| 69 | return *this; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | Matrix Matrix::operator+(const Matrix &rhs) const{ | 
|---|
| 73 | Matrix tmp = *this; | 
|---|
| 74 | tmp+=rhs; | 
|---|
| 75 | return tmp; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | Matrix Matrix::operator-(const Matrix &rhs) const{ | 
|---|
| 79 | Matrix tmp = *this; | 
|---|
| 80 | tmp-=rhs; | 
|---|
| 81 | return tmp; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | Matrix Matrix::operator*(const Matrix &rhs) const{ | 
|---|
| 85 | gsl_matrix *res = gsl_matrix_alloc(NDIM, NDIM); | 
|---|
| 86 | gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, this->content, rhs.content, 0.0, res); | 
|---|
| 87 | return Matrix(res); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | double &Matrix::at(size_t i, size_t j){ | 
|---|
| 91 | return *gsl_matrix_ptr (content, i, j); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | double Matrix::determinant(){ | 
|---|
| 95 | return at(0,0)*at(1,1)*at(2,2) | 
|---|
| 96 | + at(0,1)*at(1,2)*at(2,0) | 
|---|
| 97 | + at(0,2)*at(1,0)*at(2,1) | 
|---|
| 98 | - at(2,0)*at(1,1)*at(0,2) | 
|---|
| 99 | - at(2,1)*at(1,2)*at(0,0) | 
|---|
| 100 | - at(2,2)*at(1,0)*at(0,1); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | Matrix Matrix::invert(){ | 
|---|
| 104 | double det = determinant(); | 
|---|
| 105 | if(fabs(det)<MYEPSILON){ | 
|---|
| 106 | throw NotInvertibleException(__FILE__,__LINE__); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | double detReci = 1./det; | 
|---|
| 110 | Matrix res; | 
|---|
| 111 | res.at(0,0) =  detReci*RDET2(at(1,1),at(2,1),at(1,2),at(2,2));    // A_11 | 
|---|
| 112 | res.at(1,0) = -detReci*RDET2(at(1,0),at(2,0),at(1,2),at(2,2));    // A_21 | 
|---|
| 113 | res.at(2,0) =  detReci*RDET2(at(1,0),at(2,0),at(1,1),at(2,1));    // A_31 | 
|---|
| 114 | res.at(0,1) = -detReci*RDET2(at(0,1),at(2,1),at(0,2),at(2,2));    // A_12 | 
|---|
| 115 | res.at(1,1) =  detReci*RDET2(at(0,0),at(2,0),at(0,2),at(2,2));    // A_22 | 
|---|
| 116 | res.at(2,1) = -detReci*RDET2(at(0,0),at(2,0),at(0,1),at(2,1));    // A_32 | 
|---|
| 117 | res.at(0,2) =  detReci*RDET2(at(0,1),at(1,1),at(0,2),at(1,2));    // A_13 | 
|---|
| 118 | res.at(1,2) = -detReci*RDET2(at(0,0),at(1,0),at(0,2),at(1,2));    // A_23 | 
|---|
| 119 | res.at(2,2) =  detReci*RDET2(at(0,0),at(1,0),at(0,1),at(1,1));    // A_33 | 
|---|
| 120 | return res; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | Matrix &Matrix::operator*=(const double factor){ | 
|---|
| 124 | gsl_matrix_scale(content, factor); | 
|---|
| 125 | return *this; | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | Matrix operator*(const double factor,const Matrix& mat){ | 
|---|
| 129 | Matrix tmp = mat; | 
|---|
| 130 | tmp*=factor; | 
|---|
| 131 | return tmp; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | Matrix operator*(const Matrix &mat,const double factor){ | 
|---|
| 135 | return factor*mat; | 
|---|
| 136 | } | 
|---|