-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstexpr_func.cpp
More file actions
67 lines (54 loc) · 1.61 KB
/
constexpr_func.cpp
File metadata and controls
67 lines (54 loc) · 1.61 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
#include <array>
#include <iostream>
template <int N>
constexpr std::array<std::array<int, N - 1>, N - 1> FillSubMatrix(const std::array<std::array<int, N>, N>& matrix, int column) {
std::array<std::array<int, N - 1>, N - 1> subMatrix{};
unsigned long a = 0, b = 0;
for (unsigned long i = 1; i < N; ++i) {
for (unsigned long j = 0; j < N; ++j) {
if (j != column) {
(&std::get<0>((&std::get<0>(subMatrix))[a]))[b] = (&std::get<0>((&std::get<0>(matrix))[i]))[j];
b++;
}
}
a++;
b = 0;
}
return subMatrix;
}
template <int N>
constexpr int RecursionDet(const std::array<std::array<int, N>, N>& matrix) {
int determinant = 0;
unsigned long i = 0;
while (i < N) {
std::array<std::array<int, N - 1>, N - 1> subMatrix = FillSubMatrix<N>(matrix, i);
determinant += (&std::get<0>(std::get<0>(matrix)))[i] * (i & 1 ? -1 : 1) * RecursionDet<N - 1>(subMatrix);
++i;
}
return determinant;
}
template <int N>
constexpr int Det(const std::array<std::array<int, N>, N>& matrix) {
return RecursionDet<N>(matrix);
}
template <>
constexpr int RecursionDet<1>(const std::array<std::array<int, 1>, 1>& matrix) {
return std::get<0>(std::get<0>(matrix));
}
int main() {
constexpr std::array<std::array<int, 3>, 3> matrix = {{
{0, 1, 2},
{1, 2, 3},
{2, 3, 7}
}};
constexpr std::array<std::array<int, 2>, 2> matrix2 = {{
{1, 2},
{3, 4}
}};
constexpr std::array<std::array<int, 1>, 1> matrix1 = {{
{1}
}};
constexpr int result = Det<3>(matrix);
std::cout << result << std::endl;
return 0;
}