Skip to content

Commit 3c38b6f

Browse files
authored
Merge pull request #555 from Colocasian/gaussianEliminationInCpp
"Gaussian Elimination" Implementation in C++
2 parents 4e3ac16 + e04518e commit 3c38b6f

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
#include <iomanip>
4+
#include <iostream>
5+
#include <vector>
6+
7+
8+
void gaussianElimination(std::vector<std::vector<double> > &eqns) {
9+
// 'eqns' is the matrix, 'rows' is no. of vars
10+
int rows = eqns.size(), cols = eqns[0].size();
11+
12+
for (int i = 0; i < rows - 1; i++) {
13+
int pivot = i;
14+
15+
for (int j = i + 1; j < rows; j++) {
16+
if (fabs(eqns[j][i]) > fabs(eqns[pivot][i])) pivot = j;
17+
}
18+
19+
if (eqns[pivot][i] == 0.0)
20+
continue; // But continuing to simplify the matrix as much as possible
21+
22+
if (i != pivot) // Swapping the rows if new row with higher maxVals is found
23+
std::swap(eqns[pivot], eqns[i]); // C++ swap function
24+
25+
for (int j = i + 1; j < rows; j++) {
26+
double scale = eqns[j][i] / eqns[i][i];
27+
28+
for (int k = i + 1; k < cols; k++) // k doesn't start at 0, since
29+
eqns[j][k] -= scale * eqns[i][k]; // values before from 0 to i
30+
// are already 0
31+
eqns[j][i] = 0.0;
32+
}
33+
}
34+
}
35+
36+
void gaussJordan(std::vector<std::vector<double> > &eqns) {
37+
// 'eqns' is the (Row-echelon) matrix, 'rows' is no. of vars
38+
int rows = eqns.size();
39+
40+
for (int i = rows - 1; i >= 0; i--) {
41+
42+
if (eqns[i][i] != 0) {
43+
44+
eqns[i][rows] /= eqns[i][i];
45+
eqns[i][i] = 1; // We know that the only entry in this row is 1
46+
47+
// subtracting rows from below
48+
for (int j = i - 1; j >= 0; j--) {
49+
eqns[j][rows] -= eqns[j][i] * eqns[i][rows];
50+
eqns[j][i] = 0; // We also set all the other values in row to 0 directly
51+
}
52+
}
53+
}
54+
}
55+
56+
std::vector<double> backSubs(const std::vector<std::vector<double> > &eqns) {
57+
// 'eqns' is matrix, 'rows' is no. of variables
58+
int rows = eqns.size();
59+
60+
std::vector<double> ans(rows);
61+
for (int i = rows - 1; i >= 0; i--) {
62+
double sum = 0.0;
63+
64+
for (int j = i + 1; j < rows; j++) sum += eqns[i][j] * ans[j];
65+
66+
if (eqns[i][i] != 0)
67+
ans[i] = (eqns[i][rows] - sum) / eqns[i][i];
68+
else
69+
return std::vector<double>(0);
70+
}
71+
return ans;
72+
}
73+
74+
75+
void printMatrix(const std::vector<std::vector<double> > &matrix) {
76+
for (int row = 0; row < matrix.size(); row++) {
77+
std::cout << "[";
78+
79+
for (int col = 0; col < matrix[row].size() - 1; col++)
80+
std::cout << std::setw(8) << std::fixed << std::setprecision(3)
81+
<< matrix[row][col];
82+
83+
std::cout << " |" << std::setw(8) << std::fixed << std::setprecision(3)
84+
<< matrix[row].back() << " ]" << std::endl;
85+
}
86+
}
87+
88+
89+
int main() {
90+
std::vector<std::vector<double> > equations{
91+
{2, 3, 4, 6},
92+
{1, 2, 3, 4},
93+
{3, -4, 0, 10}};
94+
95+
std::cout << "Initial matrix:" << std::endl;
96+
printMatrix(equations);
97+
std::cout << std::endl;
98+
99+
gaussianElimination(equations);
100+
std::cout << "Matrix after gaussian elimination:" << std::endl;
101+
printMatrix(equations);
102+
std::cout << std::endl;
103+
104+
std::vector<double> ans = backSubs(equations);
105+
std::cout << "Solution from backsubstitution" << std::endl;
106+
std::cout << "x = " << ans[0] << ", y = " << ans[1] << ", z = " << ans[2]
107+
<< std::endl
108+
<< std::endl;
109+
110+
gaussJordan(equations);
111+
std::cout << "Matrix after Gauss Jordan:" << std::endl;
112+
printMatrix(equations);
113+
std::cout << std::endl;
114+
}

contents/gaussian_elimination/gaussian_elimination.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ In code, this process might look like this:
314314
{% sample lang="c" %}
315315
[import:5-13, lang:"c"](code/c/gaussian_elimination.c)
316316
[import:19-34, lang:"c"](code/c/gaussian_elimination.c)
317+
{% sample lang="cpp" %}
318+
[import:13-23, lang:"cpp"](code/c++/gaussian_elimination.cpp)
317319
{% sample lang="hs" %}
318320
[import:10-17, lang:"haskell"](code/haskell/gaussianElimination.hs)
319321
[import:44-46, lang:"haskell"](code/haskell/gaussianElimination.hs)
@@ -384,6 +386,8 @@ Here is what it might look like in code:
384386
[import:32-40, lang:"java"](code/java/GaussianElimination.java)
385387
{% sample lang="c" %}
386388
[import:36-41, lang:"c"](code/c/gaussian_elimination.c)
389+
{% sample lang="cpp" %}
390+
[import:25-32, lang:"cpp"](code/c++/gaussian_elimination.cpp)
387391
{% sample lang="hs" %}
388392
[import:19-33, lang:"haskell"](code/haskell/gaussianElimination.hs)
389393
[import:42-42, lang:"haskell"](code/haskell/gaussianElimination.hs)
@@ -403,6 +407,8 @@ When we put everything together, it looks like this:
403407
[import:1-45, lang:"julia"](code/julia/gaussian_elimination.jl)
404408
{% sample lang="c" %}
405409
[import:15-48, lang:"c"](code/c/gaussian_elimination.c)
410+
{% sample lang="cpp" %}
411+
[import:8-34, lang:"cpp"](code/c++/gaussian_elimination.cpp)
406412
{% sample lang="rs" %}
407413
[import:41-78, lang:"rust"](code/rust/gaussian_elimination.rs)
408414
{% sample lang="hs" %}
@@ -440,6 +446,8 @@ Here it is in code:
440446
[import:67-93, lang:"julia"](code/julia/gaussian_elimination.jl)
441447
{% sample lang="c" %}
442448
[import:64-82, lang:"c"](code/c/gaussian_elimination.c)
449+
{% sample lang="cpp" %}
450+
[import:36-54, lang:"cpp"](code/c++/gaussian_elimination.cpp)
443451
{% sample lang="rs" %}
444452
This code does not exist yet in rust, so here's Julia code (sorry for the inconvenience)
445453
[import:67-93, lang:"julia"](code/julia/gaussian_elimination.jl)
@@ -481,6 +489,8 @@ In code, it looks like this:
481489
[import:47-64, lang:"julia"](code/julia/gaussian_elimination.jl)
482490
{% sample lang="c" %}
483491
[import:50-62, lang:"c"](code/c/gaussian_elimination.c)
492+
{% sample lang="cpp" %}
493+
[import:56-72, lang:"cpp"](code/c++/gaussian_elimination.cpp)
484494
{% sample lang="rs" %}
485495
[import:79-94, lang:"rust"](code/rust/gaussian_elimination.rs)
486496
{% sample lang="hs" %}
@@ -547,6 +557,8 @@ Here's a video describing Gaussian elimination:
547557
[import, lang:"julia"](code/julia/gaussian_elimination.jl)
548558
{% sample lang="c" %}
549559
[import, lang:"c"](code/c/gaussian_elimination.c)
560+
{% sample lang="cpp" %}
561+
[import, lang:"cpp"](code/c++/gaussian_elimination.cpp)
550562
{% sample lang="rs" %}
551563
[import, lang:"rust"](code/rust/gaussian_elimination.rs)
552564
{% sample lang="hs" %}

0 commit comments

Comments
 (0)