This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
forked from MikeMirzayanov/testlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cpp
59 lines (50 loc) · 1.58 KB
/
matrix.cpp
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
#include "matrix.hpp"
// Function to get the cofactor matrix (minor matrix)
template <ConvertibleToInt64_t T>
Matrix<T> Matrix<T>::getCofactor(uint64_t delRow, uint64_t delCol) const {
uint64_t i = 0, j = 0;
std::vector<std::vector<T>> temp(getSize().first-1, std::vector<T>(getSize().second-1));
for (uint64_t row = 0; row < getSize().first; ++row) {
for (uint64_t col = 0; col < getSize().second; ++col) {
if (row != delRow && col != delCol) {
temp[i][j++] = matrix[row][col];
if (j == getSize().second - 1) {
j = 0;
++i;
}
}
}
}
return std::move(temp);
}
template <ConvertibleToInt64_t T>
int64_t Matrix<T>::getTrace() const {
int64_t result = 0;
for (uint64_t i = 0; i < getSize().first; ++i) {
result += matrix[i][i];
}
return result;
}
template <ConvertibleToInt64_t T>
int64_t Matrix<T>::getDeterminant() const {
assert(isSquareMatrix());
int64_t det = 0;
if (getSize().first == 1) {
return matrix[0][0];
}
int64_t sign = 1;
for (uint64_t f = 0; f < getSize().first; ++f) {
Matrix<T> temp = getCofactor(matrix, 0, f);
det += sign * matrix[0][f] * getDeterminant(temp);
sign = -sign;
}
return det;
}
template <ConvertibleToInt64_t T>
static Matrix<T> constructIdentityMatrix(uint64_t size) {
std::vector<std::vector<T>> matrix(size, std::vector<T>(size, 0));
for (uint64_t i = 0; i < size; ++i) {
matrix[i][i] = 1;
}
return Matrix(matrix);
}