-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
121 lines (107 loc) · 2.67 KB
/
main.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
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
#include <bits/stdc++.h>
using namespace std;
void printMatrix(vector<vector<double>> a, int n) {
// print the matrix
// cout << "------------------" << endl;
// for (int i = 0; i < n; i++) {
// for (int j = 0; j <= n; j++) {
// cout << a[i][j] << " ";
// }
// cout << endl;
// }
// cout << "------------------" << endl;
// Make it also account the negative numbers, and make the numbers in a column
// aligned
cout << "------------------" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= n; j++) {
cout << setw(10) << a[i][j] << " ";
}
cout << endl;
}
cout << "------------------" << endl;
}
double determinant(vector<vector<double>> a, int n) {
double det = 1.0;
for (int i = 0; i < n; i++) {
int pivot = i;
for (int j = i + 1; j < n; j++) {
if (abs(a[j][i]) > abs(a[pivot][i])) {
pivot = j;
}
}
if (pivot != i) {
swap(a[i], a[pivot]);
det *= -1;
}
if (a[i][i] == 0) {
return 0;
}
det *= a[i][i];
for (int j = i + 1; j < n; j++) {
double factor = a[j][i] / a[i][i];
for (int k = i + 1; k < n; k++) {
a[j][k] -= factor * a[i][k];
}
}
}
return det;
}
int main() {
// 2 variable equation solver using jordan elimination
int n;
cin >> n;
vector<vector<double>> a(n, vector<double>(n + 1));
for (int i = 0; i < n; i++) {
for (int j = 0; j <= n; j++) {
cin >> a[i][j];
}
}
if (determinant(a, n) == 0) {
cout << "Inconsistent determinant" << endl;
return 0;
}
// jordan elimination
// Row Echelon Form
// Make the (1,1) element 1
for (int t = 0; t < n; t++) {
double oneOne = a[t][t];
for (int i = 0; i <= n; i++) {
if (oneOne != 0)
a[t][i] /= oneOne;
else {
// swap with the next row, after checking if it isn't the last row
if (t != n - 1) {
for (int j = t + 1; j < n; j++) {
if (a[j][t] != 0) {
swap(a[t], a[j]);
break;
}
}
t--;
break;
}
}
}
printMatrix(a, n);
for (int i = t + 1; i < n; i++) {
// Make the first column 0 (except the first row)
double firstCol = a[i][t];
for (int j = 0; j <= n; j++) {
a[i][j] -= firstCol * a[t][j];
}
}
printMatrix(a, n);
}
// Back Substitution (Reduced Row Echelon Form)
for (int t = n - 1; t >= 0; t--) {
for (int i = t - 1; i >= 0; i--) {
double firstCol = a[i][t];
for (int j = 0; j <= n; j++) {
a[i][j] -= firstCol * a[t][j];
}
}
}
printMatrix(a, n);
// printf("Determinant: %lf\n", determinant(a, n));
}