NRMatrix.h
1#ifndef VECTOR_MATRIX_H
2#define VECTOR_MATRIX_H
3
4#include "Matrix.h"
5#include "GaussJordan.h"
6#include <vector>
7
8template<class T>
9class VectorMatrix : public Matrix<T>
10{
11
12private:
13 std::vector<std::vector<T> > _matrix;
14
15 void setMatrix(const std::vector<std::vector<T> > *matrix);
16
17public:
18
19 VectorMatrix(std::vector<std::vector<T> > matrix);
20 VectorMatrix(int nRows = 0, int nCols = 0);
21 ~VectorMatrix();
22
23
24 void resize( int nRow, int nCol);
25 T getElement(int i, int j) const;
26 void setElement( int i, int j, T value);
27 void invert();
28
29 std::vector<std::vector<T> > getMatrix();
30 int getSize() const;
31 std::vector<std::vector<T> > getMatrix() const;
32
33 void setZero();
34
35 Matrix<T>* operator*(const Matrix<T>* RHS);
36 Matrix<T>* operator+(const Matrix<T>* RHS);
37 Matrix<T>* operator-(const Matrix<T>* RHS);
38 void operator=(const VectorMatrix<T>* RHS);
39
40};
41
42
43template<typename T>
44VectorMatrix<T>::VectorMatrix(std::vector<std::vector<T> > matrix) : Matrix<T>()
45{
46 _matrix = matrix;
47}
48
49
50template<typename T>
51VectorMatrix<T>::VectorMatrix(int nRows, int nCols) : Matrix<T>()
52{
53 for(int i = 0; i < nRows; i++)
54 {
55 std::vector<T> temp;
56 _matrix.push_back(temp);
57
58 for(int j = 0; j < nCols; j++)
59 {
60 _matrix[i].push_back(0);
61 }
62 }
63}
64
65template<typename T>
66VectorMatrix<T>::~VectorMatrix()
67{
68}
69
70
71
72template<typename T>
73void VectorMatrix<T>::resize(int nRow, int nCol)
74{
75 //Determine if it needs to get smaller or bigger
76 bool bigger = false;;
77
78 //tests to see if rows need to get bigger
79 if(nRow > _matrix.size() )
80 {
81 bigger = true;
82 }
83
84 //tests to see if cols need to get bigger
85 for(int i = 0; i < _matrix.size(); i++)
86 {
87 if(_matrix[i].size() < nCol)
88 {
89 bigger = true;
90 break;
91 }
92 }
93
94 //Make the matrix bigger
95 if(bigger)
96 {
97 //Make it bigger
98 //cout << "Bigger" << endl;
99
100 //Add extra cols to existing rows if needed
101 for(int i = 0; i < _matrix.size(); i++)
102 {
103 if(_matrix[i].size() < nCol)
104 {
105 while(_matrix[i].size() != nCol)
106 _matrix[i].push_back(0);
107 }
108 }
109
110 //Add extra rows if needed
111 if(nRow > _matrix.size())
112 {
113 int amount = nRow - _matrix.size();
114 for(int i = 0; i < amount; i++)
115 {
116 int currentNum = 0;
117 std::vector<T> vec;
118
119 //Pad the new row with 0's
120 while(currentNum != nCol)
121 {
122 vec.push_back(0);
123 currentNum++;
124 }
125 _matrix.push_back(vec);
126 }
127 }
128 }
129 //else make it smaller
130 else
131 {
132 //cout << "Smaller" << endl;
133
134 typename std::vector<std::vector<T> >::iterator start;
135 typename std::vector<T>::iterator colStart;
136
137 //delete unwanted rows
138 if(nRow <_matrix.size())
139 {
140 start = _matrix.begin() + nRow;
141 _matrix.erase(start, _matrix.end() );
142 }
143
144 //delete unwanted column elements in the remaining rows
145 for(int i = 0; i < _matrix.size(); i++)
146 {
147 if(_matrix[i].size() > nCol)
148 {
149 colStart = _matrix[i].begin() + nCol;
150 _matrix[i].erase(colStart, _matrix[i].end() );
151 }
152 }
153 }
154}
155
156
157template<typename T>
158void VectorMatrix<T>::invert()
159{
160 std::vector<std::vector<T> > dummy;
161 MEAL::GaussJordan(_matrix, dummy);
162}
163
164template<typename T>
165T VectorMatrix<T>::getElement(int i, int j) const
166{
167 return _matrix[i][j];
168}
169
170
171template<typename T>
172void VectorMatrix<T>::setElement(int i, int j, T value)
173{
174 _matrix[i][j] = value;
175}
176
177
178template<typename T>
179std::vector<std::vector<T> > VectorMatrix<T>::getMatrix() const
180{
181 return _matrix;
182}
183
184template<typename T>
185std::vector<std::vector<T> > VectorMatrix<T>::getMatrix()
186{
187 return _matrix;
188}
189
190template<typename T>
191void VectorMatrix<T>::setMatrix(const std::vector<std::vector<T> >* matrix)
192{
193 _matrix = matrix;
194}
195
196
197template<typename T>
198void VectorMatrix<T>::setZero()
199{
200 for(int i = 0; i < _matrix.size(); i++)
201 {
202 for(int j = 0; j < _matrix.size(); j++)
203 {
204 _matrix[i][j] = 0;
205 }
206 }
207}
208
209template<typename T>
210int VectorMatrix<T>::getSize() const
211{
212 return _matrix.size();
213}
214
215
216template<typename T>
217Matrix<T>* VectorMatrix<T>::operator*(const Matrix<T>* RHS)
218{
219 if(_matrix.size() != RHS->getSize() || _matrix.size() != RHS->getSize())
220 return NULL;
221
222 VectorMatrix<T> *result = new VectorMatrix<T>(); //Create the new result matrix
223 result->resize(_matrix.size(), _matrix.size()); //Resize the matrix
224
225 //For each row
226 for(int row = 0; row < _matrix.size(); row++)
227 {
228 //for each element in that row
229 for(int element = 0; element < _matrix[row].size(); element++)
230 {
231 //calculate the value for the current element
232 for(int i = 0; i < _matrix.size(); i++)
233 {
234 result->setElement(row, element, (result->getElement(row, element) ) + _matrix[row][i] * RHS->getElement(i,element));
235 }
236 }
237 }
238
239 return result;
240}
241
242
243
244template<typename T>
245Matrix<T>* VectorMatrix<T>::operator+(const Matrix<T>* RHS)
246{
247 if(_matrix.size() != RHS->getSize() || _matrix.size() != RHS->getSize())
248 return NULL;
249
250 VectorMatrix<T> *result = new VectorMatrix<T>(); //Create the new result matrix
251 result->resize(_matrix.size(), _matrix.size()); //Resize the result matrix
252
253 //For each row
254 for(int row = 0; row < _matrix.size(); row++)
255 {
256 //for each element in that row
257 for(int element = 0; element < _matrix[row].size(); element++)
258 {
259 result->setElement(row, element, _matrix[row][element] + RHS->getElement(row, element) );
260 }
261 }
262
263 return result;
264}
265
266
267template<typename T>
268Matrix<T>* VectorMatrix<T>::operator-(const Matrix<T>* RHS)
269{
270 if(_matrix.size() != RHS->getSize() || _matrix.size() != RHS->getSize())
271 return NULL;
272
273 VectorMatrix<T> *result = new VectorMatrix<T>(); //Create the new result matrix
274 result->resize(_matrix.size(), _matrix.size()); //Resize the result matrix
275
276 //For each row
277 for(int row = 0; row < _matrix.size(); row++)
278 {
279 //for each element in that row
280 for(int element = 0; element < _matrix[row].size(); element++)
281 {
282 result->setElement(row, element, _matrix[row][element] - RHS->getElement(row, element) );
283 }
284 }
285
286 return result;
287}
288
289
290template<typename T>
291void VectorMatrix<T>::operator=(const VectorMatrix<T>* RHS)
292{
293 if(_matrix.size() != RHS->getSize())
294 this->resize(RHS->getSize(), RHS->getSize() );
295
296 for(int i = 0; i < _matrix.size(); i++)
297 {
298 for(int j = 0; j < _matrix.size(); j++)
299 {
300 setElement(i, j, RHS->getElement(i, j) );
301 }
302 }
303}
304
305
306
307#endif
T GaussJordan(std::vector< std::vector< T > > &a, std::vector< std::vector< U > > &b, int nrow=-1, double singular_threshold=0.0, std::vector< const char * > *names=0)
Definition GaussJordan.h:44

Generated using doxygen 1.14.0