4#include <gsl/gsl_matrix_double.h>
5#include <gsl/gsl_linalg.h>
9class GSLMatrix :
public Matrix<T>
13 void setMatrix(gsl_matrix* newMatrix);
16 GSLMatrix(
int nRow,
int nCol);
19 void resize(
int nRow,
int nCol);
21 T getElement(
int i,
int j)
const;
22 void setElement(
int i,
int j, T value);
23 gsl_matrix* getMatrix()
const;
29 Matrix<T>* operator*(
const Matrix<T>* RHS);
30 Matrix<T>* operator+(
const Matrix<T>* RHS);
31 Matrix<T>* operator-(
const Matrix<T>* RHS);
32 void operator=(
const GSLMatrix<T>* RHS);
38GSLMatrix<T>::GSLMatrix(
int nRow,
int nCol)
40 _matrix = gsl_matrix_alloc(nRow, nCol);
45GSLMatrix<T>::~GSLMatrix()
48 gsl_matrix_free(_matrix);
53void GSLMatrix<T>::setMatrix(gsl_matrix* newMatrix)
57 gsl_matrix_free(_matrix);
64void GSLMatrix<T>::resize(
int nRow,
int nCol)
67 gsl_matrix_free(_matrix);
71 _matrix = gsl_matrix_alloc(nRow, nCol);
76T GSLMatrix<T>::getElement(
int i,
int j)
const
78 return gsl_matrix_get(_matrix, i, j);
83void GSLMatrix<T>::setElement(
int i,
int j, T value)
85 gsl_matrix_set(_matrix, i, j, value);
90void GSLMatrix<T>::invert()
95 gsl_matrix* outputMatrix = gsl_matrix_alloc(_matrix->size1, _matrix->size1 );
98 gsl_permutation* permutationM = gsl_permutation_alloc(_matrix->size1);
101 gsl_linalg_LU_decomp((gsl_matrix*) _matrix, permutationM, &sigNum);
104 gsl_linalg_LU_invert(_matrix, permutationM, outputMatrix);
106 this->setMatrix(outputMatrix);
108 gsl_permutation_free(permutationM);
114gsl_matrix* GSLMatrix<T>::getMatrix()
const
121int GSLMatrix<T>::getSize()
const
123 return _matrix->size1;
127void GSLMatrix<T>::setZero()
129 gsl_matrix_set_zero (_matrix);
134Matrix<T>* GSLMatrix<T>::operator*(
const Matrix<T>* RHS)
137 if(_matrix->size1 != RHS->getSize() || _matrix->size1 != _matrix->size1)
141 if(
dynamic_cast<const GSLMatrix<T>*
> (RHS) == NULL)
142 throw Error (InvalidParam,
"GSLMatrix<T>::operator*",
143 "RHS is not a GSLMatrix");
146 GSLMatrix<T> *result =
new GSLMatrix<T>(_matrix->size1, _matrix->size1);
150 for(
int row = 0; row < _matrix->size1; row++)
153 for(
int element = 0; element < _matrix->size2; element++)
156 for(
int i = 0; i < _matrix->size1; i++)
158 result->setElement(row, element, (result->getElement(row, element) ) + this->getElement(row, i) * RHS->getElement(i,element));
168Matrix<T>* GSLMatrix<T>::operator+(
const Matrix<T>* RHS)
171 if(_matrix->size1 != RHS->getSize() || _matrix->size1 != _matrix->size1)
175 if(
dynamic_cast<const GSLMatrix<T>*
> (RHS) == NULL)
176 throw Error (InvalidParam,
"GSLMatrix<T>::operator*",
177 "RHS is not a GSLMatrix");
179 GSLMatrix<T> *result =
new GSLMatrix<T>(_matrix->size1, _matrix->size1);
183 for(
int row = 0; row < _matrix->size1; row++)
186 for(
int element = 0; element < _matrix->size1; element++)
188 result->setElement(row, element, this->getElement(row, element) + RHS->getElement(row, element) );
197Matrix<T>* GSLMatrix<T>::operator-(
const Matrix<T>* RHS)
199 if(_matrix->size1 != RHS->getSize() || _matrix->size1 != _matrix->size1)
203 if(
dynamic_cast<const GSLMatrix<T>*
> (RHS) == NULL)
204 throw Error (InvalidParam,
"GSLMatrix<T>::operator*",
205 "RHS is not a GSLMatrix");
207 GSLMatrix<T> *result =
new GSLMatrix<T>(RHS->getSize(), RHS->getSize());
211 for(
int row = 0; row < _matrix->size1; row++)
214 for(
int element = 0; element < _matrix->size1; element++)
216 result->setElement(row, element, this->getElement(row, element) - RHS->getElement(row, element) );
225void GSLMatrix<T>::operator=(
const GSLMatrix<T>* RHS)
227 if(_matrix->size1 != RHS->getSize() )
229 gsl_matrix_free(_matrix);
230 gsl_matrix_alloc(RHS->getSize(), RHS->getSize());
233 gsl_matrix_memcpy(_matrix, RHS->getMatrix());