This repository was archived by the owner on May 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.inl
More file actions
526 lines (448 loc) · 12.9 KB
/
Matrix.inl
File metadata and controls
526 lines (448 loc) · 12.9 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include "Matrix.h"
template<typename T>
Matrix<T>::Matrix(const size_t rows, const size_t cols, const T &value)
: m_rows{ rows }, m_cols{ cols }
{
//If dimension is wrong throw exception
if (m_rows > maxSize() || m_cols > maxSize())
throw std::out_of_range("Wrong dimensions");
//Resize matrix
m_data.resize(m_rows);
for (auto &row : m_data)
row.resize(m_cols);
//Filling by value
for (auto &row : m_data)
for (auto &el : row)
el = value;
}
template<typename T>
Matrix<T>::Matrix(const Matrix<T> &mat)
: m_rows{ mat.m_rows }, m_cols{ mat.m_cols }
{
//Resize matrix
m_data.resize(m_rows);
for (auto &row : m_data)
row.resize(m_cols);
//Copying data of matrix
for (size_t i{ 0 }; i < m_rows; i++)
for (size_t j{ 0 }; j < m_cols; j++)
m_data[i][j] = mat.m_data[i][j];
}
template<typename T>
Matrix<T>::Matrix(const std::initializer_list<std::initializer_list<T>> &list)
: m_rows{ list.size() }, m_cols{ list.begin()->size() }
{
//If dimension is wrong throw exception
if (m_rows > maxSize() || m_cols > maxSize())
throw std::out_of_range("Wrong dimensions");
//Resize matrix
m_data.resize(m_rows);
for (auto &row : m_data)
row.resize(m_cols);
//Copying data of initializer list to data
size_t curRow{ 0 };
for (const auto &listRow : list)
{
if (listRow.size() != m_cols)
throw std::out_of_range("Wrong dimensions");
size_t curCol{ 0 };
for (const auto &listEl : listRow)
{
m_data[curRow][curCol] = listEl;
++curCol;
}
++curRow;
}
}
template<typename T>
Matrix<T>& Matrix<T>::operator=(const Matrix<T> &mat)
{
if (this == &mat)
return *this;
m_rows = mat.m_rows;
m_cols = mat.m_cols;
m_data.clear();
//Resize matrix
m_data.resize(m_rows);
for (auto &row : m_data)
row.resize(m_cols);
//Copying data of matrix
for (size_t i{ 0 }; i < m_rows; i++)
for (size_t j{ 0 }; j < m_cols; j++)
m_data[i][j] = mat.m_data[i][j];
return *this;
}
template<typename T>
T& Matrix<T>::at(const size_t row, const size_t col)
{
//If dimension is wrong throw exception
if (row >= m_rows || col >= m_cols)
throw std::out_of_range("Wrong dimensions");
return m_data[row][col];
}
template<typename T>
const T& Matrix<T>::at(const size_t row, const size_t col) const
{
//If dimension is wrong throw exception
if (row >= m_rows || col >= m_cols)
throw std::out_of_range("Wrong dimensions");
return m_data[row][col];
}
template<typename T>
std::vector<T>& Matrix<T>::operator[](const size_t row)
{
//If dimension is wrong throw exception
if (row >= m_rows)
throw std::out_of_range("Wrong dimensions");
//return row of matrix
return m_data[row];
}
template<typename T>
const std::vector<T>& Matrix<T>::operator[](const size_t row) const
{
//If dimension is wrong throw exception
if (row >= m_rows)
throw std::out_of_range("Wrong dimensions");
//return row of matrix
return m_data[row];
}
template<typename T>
std::vector<T> Matrix<T>::getRow(size_t rowId) const
{
//If dimension is wrong throw exception
if (rowId >= m_rows)
throw std::out_of_range("Wrong dimensions");
//Copy row and return
std::vector<T> row{ m_data[rowId] };
return row;
}
template<typename T>
std::vector<T> Matrix<T>::getCol(size_t colId) const
{
//If dimension is wrong throw exception
if (colId >= m_cols)
throw std::out_of_range("Wrong dimensions");
//Copy column and return
std::vector<T> col;
for (size_t i{ 0 }; i < m_rows; ++i)
col.push_back(m_data[i][colId]);
return col;
}
template<typename T>
size_t Matrix<T>::rang() const
{
size_t res{ 0 };
//It shows which rows and columns was chosen for creating minor.
std::vector<int8_t> chosenRows(m_rows, 0);
std::vector<int8_t> chosenCols(m_cols, 0);
//Count of all chosen rows and columns.
size_t cntChosenRows{ 0 };
size_t cntChosenCols{ 0 };
//Id of previous chosen row and column. Need to delite if minor equal zero.
size_t prevChosenRow{ 0 };
size_t prevChosenCol{ 0 };
while (res < m_rows && res < m_cols)
{
SqrMatrix<T> minor{ res + 1 };
size_t minorRow{ 0 };
for (size_t i{ 0 }; i < m_rows; ++i)
{
//Check if need to choose one more row
if (chosenRows[i] == -1)
continue;
if (chosenRows[i] == 0)
if (cntChosenRows == minor.m_rows)
continue;
else
{
++cntChosenRows;
chosenRows[i] = 1;
prevChosenRow = i;
}
size_t minorCol{ 0 };
for (size_t j{ 0 }; j < m_cols; ++j)
{
//Check if need to choose one more row
if (chosenCols[j] == -1)
continue;
if (chosenCols[j] == 0)
if (cntChosenCols == minor.m_cols)
continue;
else
{
++cntChosenCols;
chosenCols[j] = 1;
prevChosenCol = j;
}
//filling minor
minor[minorRow][minorCol] = m_data[i][j];
++minorCol;
}
++minorRow;
}
T minorDet = minor.determinant();
if (minorDet != T(0))
{
++res;
for (size_t i{ 0 }; i < chosenRows.size(); ++i)
if (chosenRows[i] == -1)
chosenRows[i] = 0;
for (size_t i{ 0 }; i < chosenCols.size(); ++i)
if (chosenCols[i] == -1)
chosenCols[i] = 0;
}
//If In matrix no more not zero minors we return rang.
else if (std::find(chosenRows.begin(), chosenRows.end(), 0) == chosenRows.end() && std::find(chosenCols.begin(), chosenCols.end(), 0) == chosenCols.end())
return res;
else
{
--cntChosenCols;
chosenCols[prevChosenCol] = -1;
//if In matrix no more not chosen colums we restart all chosings in columns and continue with new row.
if (std::find(chosenCols.begin(), chosenCols.end(), 0) == chosenCols.end())
{
for (size_t i{ 0 }; i < chosenCols.size(); ++i)
if (chosenCols[i] == -1)
chosenCols[i] = 0;
--cntChosenRows;
chosenRows[prevChosenRow] = -1;
}
}
}
return res;
}
template<typename T>
void Matrix<T>::resize(const size_t rows, const size_t cols, const T &value)
{
//If dimension is wrong throw exception
if (rows > maxSize() || cols > maxSize())
throw std::out_of_range("Wrong dimensions");
//If one of new dimensions equal zero matrix become empty
if (rows == 0 || cols == 0)
{
m_data.clear();
m_rows = 0;
m_cols = 0;
return;
}
//Change count of rows
if (rows != m_rows)
{
m_data.resize(rows, std::vector<T>(m_cols, value));
m_rows = rows;
}
//Change count of cols
if (cols != m_cols)
{
for (size_t i{ 0 }; i < m_rows; ++i)
m_data[i].resize(cols, value);
m_cols = cols;
}
}
template<typename T>
void Matrix<T>::clear()
{
m_rows = 0;
m_cols = 0;
m_data.clear();
}
template<typename T>
void Matrix<T>::insertRow(const size_t rowId, const std::vector<T> &row)
{
//If dimension is wrong throw exception
if (row.size() != m_cols || rowId >= m_rows)
throw std::out_of_range("Wrong dimensions");
//Change row in matrix
m_data[rowId] = row;
}
template<typename T>
void Matrix<T>::insertCol(const size_t colId, const std::vector<T>& col)
{
//If dimension is wrong throw exception
if (col.size() != m_rows || colId >= m_cols)
throw std::out_of_range("Wrong dimensions");
//Change column in matrix
for (size_t i{ 0 }; i < m_rows; ++i)
m_data[i][colId] = col[i];
}
template<typename T>
void Matrix<T>::changeRows(const size_t row1Id, const size_t row2Id)
{
//If dimension is wrong throw exception
if (row1Id >= m_rows || row2Id >= m_rows)
throw std::out_of_range("Wrong dimensions");
//If they are same rows don't change them
if (row1Id == row2Id)
return;
//Change rows
std::vector<T> temp{ m_data[row1Id] };
m_data[row1Id] = m_data[row2Id];
m_data[row2Id] = temp;
}
template<typename T>
void Matrix<T>::changeCols(const size_t col1Id, const size_t col2Id)
{
//If dimension is wrong throw exception
if (col1Id >= m_cols || col2Id >= m_cols)
throw std::out_of_range("Wrong dimensions");
//If they are same columns don't change them
if (col1Id == col2Id)
return;
//Change columns
for (size_t i{ 0 }; i < m_rows; ++i)
{
T temp{ m_data[i][col1Id] };
m_data[i][col1Id] = m_data[i][col2Id];
m_data[i][col2Id] = temp;
}
}
template<typename T>
Matrix<T> Matrix<T>::getTransposed() const
{
//Create transposed matrix and fill it
Matrix<T> res{ m_cols, m_rows };
for (size_t i{ 0 }; i < m_rows; ++i)
for (size_t j{ 0 }; j < m_cols; ++j)
res.m_data[i][j] = m_data[j][i];
return res;
}
template<typename T>
Matrix<T> Matrix<T>::operator-() const
{
//Copy matrix with change sign of all elements
Matrix<T> res = *this;
for (auto &row : res.m_data)
for (auto &el : row)
el = -el;
return res;
}
template<typename T>
Matrix<T> operator+(const Matrix<T>& mat1, const Matrix<T>& mat2)
{
//If dimentions not equal throw exctprion
if (mat1.m_rows != mat2.m_rows || mat1.m_cols != mat2.m_cols)
throw std::logic_error("Dimentions of matrices must be equal");
//Copy first matrix and add to similar first matrix elements elements of second matrix
Matrix<T> res{ mat1 };
for (size_t i{ 0 }; i < res.m_rows; ++i)
for (size_t j{ 0 }; j < res.m_cols; ++j)
res.m_data[i][j] += mat2[i][j];
return res;
}
template<typename T>
Matrix<T> operator*(const Matrix<T>& mat, const T &k)
{
Matrix<T> res{ mat };
//Multiplying all elements of matrix to constant value
for (auto &row : res.m_data)
for (auto &el : row)
el *= k;
return res;
}
template<typename T>
inline Matrix<T> operator*(const Matrix<T>& mat1, const Matrix<T>& mat2)
{
//If dimentions not equal throw exctprion
if (mat1.m_cols != mat2.m_rows)
throw std::logic_error("Number of columns in left matrix must be equal to number of rows in right matrix");
Matrix<T> res{ mat1.m_rows, mat2.m_cols };
//Multiplying every row from left matrix to every column from right matrix
for (size_t i{ 0 }; i < res.m_rows; ++i)
for (size_t j{ 0 }; j < res.m_cols; ++j)
for (size_t k{ 0 }; k < mat1.m_cols; ++k)
res.m_data[i][j] += mat1.m_data[i][k] * mat2.m_data[k][j];
return res;
}
template<typename T>
SqrMatrix<T>::SqrMatrix(const Matrix<T> &mat)
: Matrix<T>(mat)
{
//If dimension is wrong throw exception
if (this->m_rows != this->m_cols)
throw std::logic_error("Wrong dimensions");
}
template<typename T>
SqrMatrix<T>::SqrMatrix(const std::initializer_list<std::initializer_list<T>> &list)
: Matrix<T>(list)
{
//If dimension is wrong throw exception
if (this->m_rows != this->m_cols)
throw std::logic_error("Wrong dimensions");
}
template<typename T>
Matrix<T>& SqrMatrix<T>::operator=(const Matrix<T> &mat)
{
this->Matrix<T>::operator=(mat);
//If dimension is wrong throw exception
if (this->m_rows != this->m_cols)
throw std::logic_error("Wrong dimensions");
return *this;
}
template<typename T>
T SqrMatrix<T>::determinant() const
{
if (this->m_cols == 1)
return this->m_data[0][0];
T res{ T(0) };
//Decompose on the first column
for (size_t k{ 0 }; k < this->m_rows; ++k)
{
if (this->m_data[k][0] == T(0))
continue;
SqrMatrix<T> minor{ this->m_rows - 1 };
for (size_t i{ 0 }; i < this->m_rows; ++i)
for (size_t j{ 1 }; j < this->m_cols; ++j)
{
if (i == k)
continue;
else if (i < k)
minor.m_data[i][j - 1] = this->m_data[i][j];
else if (i > k)
minor.m_data[i - 1][j - 1] = this->m_data[i][j];
}
if (k % 2 == 0)
res += this->m_data[k][0] * minor.determinant();
else
res -= this->m_data[k][0] * minor.determinant();
}
return res;
}
template<typename T>
SqrMatrix<T> SqrMatrix<T>::getInverse() const
{
//Check if determinant equal zero then inverse matrix don't exist.
T det{ this->determinant() };
if (det == T(0))
throw std::logic_error("Inverse matrix don't exist");
//Create matrix of minors
SqrMatrix<T> minorMatrix{ this->m_cols };
//Get minor for all elements
for (size_t minorRow{ 0 }; minorRow < this->m_rows; ++minorRow)
for (size_t minorCol{ 0 }; minorCol < this->m_cols; ++minorCol)
{
//fill minor
SqrMatrix<T> minor{ this->m_rows - 1 };
for (size_t i{ 0 }; i < this->m_rows; ++i)
for (size_t j{ 0 }; j < this->m_cols; ++j)
{
if (i == minorRow || j == minorCol)
continue;
else if (i < minorRow && j < minorCol)
minor[i][j] = this->m_data[i][j];
else if (i < minorRow && j > minorCol)
minor[i][j - 1] = this->m_data[i][j];
else if (i > minorRow && j < minorCol)
minor[i - 1][j] = this->m_data[i][j];
else if (i > minorRow && j > minorCol)
minor[i - 1][j - 1] = this->m_data[i][j];
}
//Put minor to matrix of minors
//Change sign of elements which have odd sum of indexes
if ((minorRow + minorCol) % 2 == 0)
minorMatrix.m_data[minorRow][minorCol] = minor.determinant();
else
minorMatrix.m_data[minorRow][minorCol] = -minor.determinant();
}
//return Inverse matrix
return 1 / det * minorMatrix.getTransposed();
}