GSLMatrix.h
1#ifndef GSLMATRIX_H
2#define GSLMATRIX_H
3
4#include <gsl/gsl_matrix_double.h> //Science Library header file
5#include <gsl/gsl_linalg.h>
6#include "Matrix.h"
7
8template<typename T>
9class GSLMatrix : public Matrix<T>
10{
11private:
12 gsl_matrix *_matrix; //Pointer to the gsl_matrix struct
13 void setMatrix(gsl_matrix* newMatrix);
14
15public:
16 GSLMatrix(int nRow, int nCol);
17 ~GSLMatrix();
18
19 void resize(int nRow, int nCol); //If the matrix has elements in it should it keep those elements or is it ok if they are lost?
20
21 T getElement(int i, int j) const;
22 void setElement(int i, int j, T value);
23 gsl_matrix* getMatrix() const;
24
25 int getSize() const;
26 void setZero();
27 void invert();
28
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);
33
34};
35
36
37template<typename T>
38GSLMatrix<T>::GSLMatrix(int nRow, int nCol)
39{
40 _matrix = gsl_matrix_alloc(nRow, nCol);
41}
42
43
44template<typename T>
45GSLMatrix<T>::~GSLMatrix()
46{
47 if(_matrix != NULL)
48 gsl_matrix_free(_matrix);
49}
50
51
52template<typename T>
53void GSLMatrix<T>::setMatrix(gsl_matrix* newMatrix)
54{
55 //Free the old matrix, if there is one
56 if(_matrix != NULL)
57 gsl_matrix_free(_matrix);
58
59 _matrix = newMatrix;
60}
61
62
63template<typename T>
64void GSLMatrix<T>::resize(int nRow, int nCol)
65{
66 if(_matrix != NULL)
67 gsl_matrix_free(_matrix);
68
69 _matrix = NULL;
70
71 _matrix = gsl_matrix_alloc(nRow, nCol);
72}
73
74
75template<typename T>
76T GSLMatrix<T>::getElement(int i, int j) const
77{
78 return gsl_matrix_get(_matrix, i, j);
79}
80
81
82template<typename T>
83void GSLMatrix<T>::setElement(int i, int j, T value)
84{
85 gsl_matrix_set(_matrix, i, j, value);
86}
87
88
89template<typename T>
90void GSLMatrix<T>::invert()
91{
92 int sigNum;
93
94 //gsl_matrix for the output
95 gsl_matrix* outputMatrix = gsl_matrix_alloc(_matrix->size1, _matrix->size1 );
96
97 //Allocate permutation matrix
98 gsl_permutation* permutationM = gsl_permutation_alloc(_matrix->size1);
99
100 //get the LU decomposition of the matrix
101 gsl_linalg_LU_decomp((gsl_matrix*) _matrix, permutationM, &sigNum);
102
103 //invert the matrix
104 gsl_linalg_LU_invert(_matrix, permutationM, outputMatrix);
105
106 this->setMatrix(outputMatrix);
107
108 gsl_permutation_free(permutationM);
109}
110
111
112
113template<typename T>
114gsl_matrix* GSLMatrix<T>::getMatrix() const
115{
116 return _matrix;
117}
118
119
120template<typename T>
121int GSLMatrix<T>::getSize() const
122{
123 return _matrix->size1;
124}
125
126template<typename T>
127void GSLMatrix<T>::setZero()
128{
129 gsl_matrix_set_zero (_matrix);
130}
131
132
133template<typename T>
134Matrix<T>* GSLMatrix<T>::operator*(const Matrix<T>* RHS)
135{
136 //Check if its the same size
137 if(_matrix->size1 != RHS->getSize() || _matrix->size1 != _matrix->size1)
138 return NULL;
139
140 //Check if its the same type (GSL Matrix)
141 if(dynamic_cast<const GSLMatrix<T>*> (RHS) == NULL)
142 throw Error (InvalidParam, "GSLMatrix<T>::operator*",
143 "RHS is not a GSLMatrix");
144
145 //Create the new result matrix
146 GSLMatrix<T> *result = new GSLMatrix<T>(_matrix->size1, _matrix->size1);
147 result->setZero();
148
149 //For each row
150 for(int row = 0; row < _matrix->size1; row++)
151 {
152 //for each element in that row
153 for(int element = 0; element < _matrix->size2; element++)
154 {
155 //calculate the value for the current element
156 for(int i = 0; i < _matrix->size1; i++)
157 {
158 result->setElement(row, element, (result->getElement(row, element) ) + this->getElement(row, i) * RHS->getElement(i,element));
159 }
160 }
161 }
162
163 return result;
164}
165
166
167template<typename T>
168Matrix<T>* GSLMatrix<T>::operator+(const Matrix<T>* RHS)
169{
170 //Check if its the same size
171 if(_matrix->size1 != RHS->getSize() || _matrix->size1 != _matrix->size1)
172 return NULL;
173
174 //Check if its the same type (GSL Matrix)
175 if(dynamic_cast<const GSLMatrix<T>*> (RHS) == NULL)
176 throw Error (InvalidParam, "GSLMatrix<T>::operator*",
177 "RHS is not a GSLMatrix");
178
179 GSLMatrix<T> *result = new GSLMatrix<T>(_matrix->size1, _matrix->size1); //Create the new result matrix
180 result->setZero();
181
182 //For each row
183 for(int row = 0; row < _matrix->size1; row++)
184 {
185 //for each element in that row
186 for(int element = 0; element < _matrix->size1; element++)
187 {
188 result->setElement(row, element, this->getElement(row, element) + RHS->getElement(row, element) );
189 }
190 }
191
192 return result;
193}
194
195
196template<typename T>
197Matrix<T>* GSLMatrix<T>::operator-(const Matrix<T>* RHS)
198{
199 if(_matrix->size1 != RHS->getSize() || _matrix->size1 != _matrix->size1) //Check if its the same size
200 return NULL;
201
202 //Check if its the same type (GSL Matrix)
203 if(dynamic_cast<const GSLMatrix<T>*> (RHS) == NULL)
204 throw Error (InvalidParam, "GSLMatrix<T>::operator*",
205 "RHS is not a GSLMatrix");
206
207 GSLMatrix<T> *result = new GSLMatrix<T>(RHS->getSize(), RHS->getSize()); //Create the new result matrix
208 result->setZero();
209
210 //For each row
211 for(int row = 0; row < _matrix->size1; row++)
212 {
213 //for each element in that row
214 for(int element = 0; element < _matrix->size1; element++)
215 {
216 result->setElement(row, element, this->getElement(row, element) - RHS->getElement(row, element) );
217 }
218 }
219
220 return result;
221}
222
223
224template<typename T>
225void GSLMatrix<T>::operator=(const GSLMatrix<T>* RHS)
226{
227 if(_matrix->size1 != RHS->getSize() )
228 {
229 gsl_matrix_free(_matrix);
230 gsl_matrix_alloc(RHS->getSize(), RHS->getSize());
231 }
232
233 gsl_matrix_memcpy(_matrix, RHS->getMatrix());
234}
235
236
237#endif

Generated using doxygen 1.14.0