-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.h
475 lines (403 loc) · 14 KB
/
matrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#ifndef MATRIX_H
#define MATRIX_H
#include <stddef.h>
#include <functional>
#include <string>
#include <grid.h>
#include <QDebug>
#include <QString>
template<class T>
class Grid;
template<class T>
class Matrix: public Grid<T> {
public:
//Constructors
Matrix():Grid<T>(){}
Matrix(size_t _rows, size_t _cols) : Grid<T>(_rows, _cols){}
Matrix(std::initializer_list< std::initializer_list<T> > il2d) : Grid<T>(il2d){}
//Destructor
~Matrix(){}
//Copy Constructor
/*
template<class T2>
Matrix(const Matrix<T2> &other) : Grid<T>(other){}
void Setvalues(std::initializer_list< std::initializer_list<T> > list){
auto inner_list = list.begin();
auto value = inner_list->begin();
for(auto it = this->begin(); it != this->end();it++){
if(it.Col == 0){
value = inner_list->begin();
inner_list++;
}
it = *value;
value++;
}
}
*/
Matrix(const Grid<T> &other) : Grid<T>(other){
}
void ARange(T start, T interval = 0){
if(interval != 0){
for(auto it = this->begin(); it != this->end();it++){
it = start;
start += interval;
}
}else{
std::iota(this->begin(), this->end(), start);
}
}
void Random(T Start, T End){
for(auto it = this->begin(); it != this->end();it++){
it = fmod(rand(), End-Start) + End;
}
}
//OPERATOR OVERLOADING
template<class T2>
Matrix<T> operator +(const Matrix<T2> &other){
if(this->Rows != other.Rows|| this->Cols != other.Cols){
qDebug() << "ERROR: Matrix::operator+=, Size not Equal!";
throw std::bad_array_new_length();
}
return AddSub(other, std::plus<T>());
}
template<class T2>
Matrix<T> &operator+=(const Matrix<T2> &other){
if(this->Rows != other.Rows|| this->Cols != other.Cols){
qDebug() << "ERROR: Matrix::operator+=, Size not Equal!";
throw std::bad_array_new_length();
}
*this = AddSub(other, std::plus<T>());
return *this;
}
template<class T2>
Matrix<T> operator -(const Matrix<T2> &other){
return AddSub(other, std::minus<T>());
}
Matrix operator* (const Matrix<std::string> &other){
qDebug() << "Cannot multiply with a string Matrix!";
}
template<class T2>
Matrix<T> operator* (const Matrix<T2> &other){
if(this->Cols != other.rows()){
qDebug("WARNING: Matrix have not the same size for multiplication!");
throw;
}
Matrix<T> m = Matrix<T>(this->Rows, other.cols());
//qDebug() << "T: " << typeid(T).name() << "T2: " << typeid(T2).name();
for(auto it = m.begin(); it != m.end();it++){
for(size_t c = 0; c < this->Cols;c++){
*it += this->values[it.Row*this->Cols+c] * other.GetValue(c, it.Col);
}
}
return m;
}
Matrix operator* (T number){
Matrix m = Matrix(this->Rows, this->Cols);
for(auto it = this->begin(); it != this->end();it++){
m[it.Row][it.Col] = *it * number;
}
return m;
}
Matrix &operator =(const Matrix &other){
Grid<T>::operator =(other);
return *this;
}
Matrix &operator =(Grid<T> &other){
Grid<T>::operator =(other);
return *this;
}
Matrix &operator =(Matrix<char*> &other){
Grid<T>::operator =(other);
return *this;
}
template<class T2>
Matrix &operator =(Matrix<T2> &other){
Grid<T>::operator =(other);
return *this;
}
template<class T2, class OPERATOR>
Matrix AddSub(const Matrix<T2> &other, OPERATOR op){
if(this->Rows != other.Rows || this->Cols != other.Cols){
qDebug("WARNING: Matrix have not the same size for addition!");
throw;
}
Matrix m = Matrix(this->Rows, this->Cols);
for(auto it = this->begin(); it != this->end();it++){
m[it.Row][it.Col] = op(*it, other.GetValue(it.Row, it.Col));
}
return m;
}
void OneValues(){
for(auto it = this->begin(); it != this->end();it++){
*it = 1;
}
}
bool isSquare(){ return this->Rows == this->Cols;}
Matrix<T> GetUnitMatrix(){
if(isSquare()){
Matrix<T> UM(this->Rows, this->Cols);
for(size_t i = 0; i < this->Rows; i++){
UM[i][i] = 1;
}
return UM;
}
qDebug() << "WARNING: Matrix GetUnitMatrix()"<<this->Rows<<"x"<<this->Cols<<": Matrix is not squared";
}
T Sum(){
T sum = 0;
for(auto it = this->begin(); it != this->end();it++){
sum += *it;
}
return sum;
}
void RowMultiply(size_t row_index, T factor){
for(size_t c = 0; c < this->Cols; c++){
this->values[row_index*this->Cols+c] *= factor;
}
}
void RowDivide(size_t row_index, T factor){
if(factor != 0){
for(size_t c = 0; c < this->Cols; c++){
this->values[row_index*this->Cols+c] /= factor;
}
}
}
void RowAdd(size_t row_index, T factor){
for(size_t c = 0; c < this->Cols; c++){
this->values[row_index*this->Cols+c] += factor;
}
}
void RowSubtract(size_t row_index, T factor){
for(size_t c = 0; c < this->Cols; c++){
this->values[row_index*this->Cols+c] -= factor;
}
}
void SwitchRows(size_t r1, size_t r2){
for(size_t c = 0; c < this->Cols;c++){
T temp;
temp = this->values[r1*this->Cols+c];
this->values[r1*this->Cols+c] = this->values[r2*this->Cols+c];
this->values[r2*this->Cols+c] = temp;
}
}
void SwitchCols(size_t c1, size_t c2){
for(size_t r = 0; r < this->Rows;r++){
T temp;
temp = this->values[r*this->Cols+c1];
this->values[r*this->Cols+c1] = this->values[r*this->Cols+c2];
this->values[r*this->Cols+c2] = temp;
}
}
T determinant3x3(){
T det = 0;
for(size_t line = 0; line < 6;line++){
T value = 1;
for(size_t rc = 0; rc < 3;rc++){
int col = (rc+line) % 3;
col = line > 2 ? 2 - col : col;
value *= this->values[rc*this->Cols+col];
}
det = line < 3 ? det + value : det - value;
}
return det;
}
//Turn maybe in a better algorithm
T determinant(){
if(isSquare()){
T Det = 0;
if(this->Rows == 3) return this->determinant3x3();
else{
for(int r = 0; r < this->Rows; r++){
Matrix<T> detMrc(this->Rows-1, this->Cols-1);
auto itdet = detMrc.begin();
for(auto it = this->begin(); it != this->end();it++){
if(it.Row != r && it.Col != 0){
*itdet = *it;
itdet++;
}
}
Det += detMrc.determinant() * (powf((-1), r) * this->values[r*this->Cols]);
}
}
return Det;
}
qDebug() << "Matrix determinante(): Is not Squared!";
throw;
}
Matrix<T> transpose(){
Matrix<T> transposedMatrix(this->cols(), this->rows());
for(auto it = this->begin(); it != this->end();it++){
transposedMatrix[it.Col][it.Row] = *it;
}
return transposedMatrix;
}
Matrix<T> inverse(){
if(isSquare()){
Matrix<T> TempM = *this;
Matrix<T> IM = this->GetUnitMatrix();
for(size_t rc = 0; rc < this->Rows; rc++){
//Calculate KeyElement
if(TempM[rc][rc] == -1){
TempM.RowMultiply(rc, -1);
IM.RowMultiply(rc, -1);
}else if(TempM[rc][rc] != 1){
//Check if 1 exist in Column
bool bisOneInCol = false;
size_t RowIndexOfOne;
for(size_t r_inner = 0; r_inner < IM.rows();r_inner++){
if(rc != r_inner){
if(TempM[r_inner][rc] == 1 || TempM[r_inner][rc] == -1){
bisOneInCol = true;
RowIndexOfOne = r_inner;
break;
}
}
}
if(bisOneInCol){
TempM.SwitchRows(rc, RowIndexOfOne);
IM.SwitchRows(rc, RowIndexOfOne);
if(TempM[rc][rc] < 0){
TempM.RowMultiply(rc, -1);
IM.RowMultiply(rc, -1);
}
}else{
T rowfactor = TempM[rc][rc];
TempM.RowDivide(rc, rowfactor);
IM.RowDivide(rc, rowfactor);
}
}
TempM.eliminateCol(rc, rc, false, &IM);
}
return IM;
}
qDebug() << "Matrix inverse(): Is not Squared!";
throw;
}
size_t Rank(){
size_t Rank = this->Rows;
Matrix<T> TempM = *this;
for(int rc = 0; rc < this->Rows; rc++){
//Calculate KeyElement
if(TempM[rc][rc] != 1 && TempM[rc][rc] != 0){
TempM.RowDivide(rc, TempM[rc][rc]);
}
else if(TempM[rc][rc] == 0){
int RowIndexOfAny;
bool bisAnyInCol = TempM.IsAnyNumberInCol(rc, RowIndexOfAny, rc+1);
if(bisAnyInCol) TempM.SwitchRows(rc, RowIndexOfAny);
else Rank--;
}
//Eliminate Col
if(TempM[rc][rc] == 1){
TempM.eliminateCol(rc, rc, true);
}
}
return Rank;
}
void eliminateCol(size_t main_row_i, size_t col_i, bool bOnlyUnder = false, Matrix *Use_Operations = nullptr){
if(this->values[main_row_i*this->Cols+col_i] != 1){
qDebug() << "ERROR: Main row Index must have the value of 1!";
throw;
}
for(int r = bOnlyUnder ? main_row_i:0 ; r < this->Rows; r++){
if(this->values[r*this->Cols+col_i] != 0 && r != main_row_i){
T rowFactor = this->values[r*this->Cols+col_i];
for(int c = 0; c < this->Cols; c++){
this->values[r*this->Cols+c] -= rowFactor * this->values[main_row_i*this->Cols+c];
if(Use_Operations){
Use_Operations->GetValue(r,c) -= rowFactor * Use_Operations->GetValue(main_row_i, c);
}
}
}
}
}
bool IsAnyNumberInCol(size_t col_i, int &OutRowIndex = -1, size_t start_row_i = 0, int end_row_i = -1 ){
end_row_i = end_row_i < 0 ? this->Rows-1 : end_row_i;
for(int r = start_row_i; r <= end_row_i;r++){
if(this->values[r*this->Cols+col_i] != 0){
OutRowIndex = r;
return true;
}
}
return false;
}
bool IsNumberInCol(T number, size_t col_i, int &OutRowIndex = -1, bool ignoreSign = false, size_t start_row_i = 0, int end_row_i = -1 ){
end_row_i = end_row_i < 0 ? this->Rows-1 : end_row_i;
for(int r = start_row_i; r <= end_row_i;r++){
if(this->values[r*this->Cols+col_i] == number || (ignoreSign && this->values[r*this->Cols+col_i] == -number)){
OutRowIndex = r;
return true;
}
}
return false;
}
//Not working
/*
size_t* IsNumberInCol(const SSearchFilter2D<T> &filter){
size_t indicies[this->Rows];
size_t row_end_i = filter.row_end_i < 0 ? this->Rows-1 : filter.row_end_i;
for(int r = filter.row_start_i; r <= row_end_i;r++){
for(auto elem : filter.search_elements){
if(this->values[r][filter.col_start_i] == elem || (filter.bIgnoreSign && this->values[r][filter.col_start_i] == -elem)){
indicies[r] = r;
}
}
}
return indicies;
}
*/
T GetMeanOfRow(size_t row_i){
T mean = 0;
for(int c = 0; c < this->Cols; c++){
mean += this->values[row_i*this->Cols+c];
}
return mean / this->Cols;
}
T GetMeanOfCol(size_t col_i){
T mean = 0;
for(int r = 0; r < this->Rows; r++){
mean += this->values[r*this->Cols+col_i];
}
return mean / this->Rows;
}
void CenteredRow(size_t row_i){
T mean = GetMeanOfRow(row_i);
for(int c = 0; c < this->Cols; c++){
this->values[row_i*this->Cols+c] -= mean;
}
}
Matrix<T> PullCol(size_t col_i){
Matrix<T> pulledM(this->Rows, 1);
Matrix<T> newM(this->Rows, this->Cols-1);
for(size_t r = 0; r < this->Rows; r++){
int shift = 0;
for(size_t c = 0; c < this->Cols; c++){
if(c == col_i){
pulledM[r][0] = this->values[r*this->Cols+c];
shift++;
}else{
newM[r][c-shift] = this->values[r*this->Cols+c];
}
}
}
*this = newM;
return pulledM;
}
void AddCol(size_t col_i, T fill_number = 0){
if(col_i > this->Rows) col_i = this->Cols;
Matrix<T> newM(this->Rows, this->Cols+1);
for(size_t r = 0; r < newM.Rows; r++){
int shift = 0;
for(size_t c = 0; c < newM.Cols; c++){
if(c == col_i){
newM[r][c] = fill_number;
shift++;
}else{
newM[r][c] = this->values[r*this->Cols+c-shift];
}
}
}
*this = newM;
}
};
#endif // MATRIX_H