-
-
Notifications
You must be signed in to change notification settings - Fork 360
"Gaussian Elimination" Implementation in C++ #555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Gathros
merged 9 commits into
algorithm-archivists:master
from
rishvic:gaussianEliminationInCpp
Jan 7, 2019
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e9527bb
Added Gaussian Elimimnation in C++
rishvic 53dbd1f
Modified to upmove the row with highest pivot & added more comments
rishvic daa272e
Removed the class; Removed 'using namespace std;'; Removed useless fu…
rishvic 08bf7f4
Resolved conflict with C code in md
rishvic 64426a1
Removed singular matrix checking & made the code simpler
rishvic fbe7bbc
Made changes as requested
rishvic 70ca766
Changed one broken line
rishvic 8ae70e6
Synced gaussian_elimination.md with cpp code
rishvic e04518e
Merge branch 'master' into gaussianEliminationInCpp
Gathros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
contents/gaussian_elimination/code/c++/gaussian_elimination.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
|
||
void gaussianElimination(std::vector<std::vector<double> > &eqns) { | ||
// 'eqns' is the matrix, 'rows' is no. of vars | ||
int rows = eqns.size(), cols = eqns[0].size(); | ||
|
||
for (int i = 0; i < rows - 1; i++) { | ||
int pivot = i; | ||
|
||
for (int j = i + 1; j < rows; j++) { | ||
if (fabs(eqns[j][i]) > fabs(eqns[pivot][i])) pivot = j; | ||
} | ||
|
||
if (eqns[pivot][i] == 0.0) | ||
continue; // But continuing to simplify the matrix as much as possible | ||
|
||
if (i != pivot) // Swapping the rows if new row with higher maxVals is found | ||
std::swap(eqns[pivot], eqns[i]); // C++ swap function | ||
|
||
for (int j = i + 1; j < rows; j++) { | ||
double scale = eqns[j][i] / eqns[i][i]; | ||
|
||
for (int k = i + 1; k < cols; k++) // k doesn't start at 0, since | ||
eqns[j][k] -= scale * eqns[i][k]; // values before from 0 to i | ||
// are already 0 | ||
eqns[j][i] = 0.0; | ||
} | ||
} | ||
} | ||
|
||
void gaussJordan(std::vector<std::vector<double> > &eqns) { | ||
// 'eqns' is the (Row-echelon) matrix, 'rows' is no. of vars | ||
int rows = eqns.size(); | ||
|
||
for (int i = rows - 1; i >= 0; i--) { | ||
|
||
if (eqns[i][i] != 0) { | ||
|
||
eqns[i][rows] /= eqns[i][i]; | ||
eqns[i][i] = 1; // We know that the only entry in this row is 1 | ||
|
||
// subtracting rows from below | ||
for (int j = i - 1; j >= 0; j--) { | ||
eqns[j][rows] -= eqns[j][i] * eqns[i][rows]; | ||
eqns[j][i] = 0; // We also set all the other values in row to 0 directly | ||
} | ||
} | ||
} | ||
} | ||
|
||
std::vector<double> backSubs(const std::vector<std::vector<double> > &eqns) { | ||
// 'eqns' is matrix, 'rows' is no. of variables | ||
int rows = eqns.size(); | ||
|
||
std::vector<double> ans(rows); | ||
for (int i = rows - 1; i >= 0; i--) { | ||
double sum = 0.0; | ||
|
||
for (int j = i + 1; j < rows; j++) sum += eqns[i][j] * ans[j]; | ||
|
||
if (eqns[i][i] != 0) | ||
ans[i] = (eqns[i][rows] - sum) / eqns[i][i]; | ||
else | ||
return std::vector<double>(0); | ||
} | ||
return ans; | ||
} | ||
|
||
|
||
void printMatrix(const std::vector<std::vector<double> > &matrix) { | ||
for (int row = 0; row < matrix.size(); row++) { | ||
std::cout << "["; | ||
|
||
for (int col = 0; col < matrix[row].size() - 1; col++) | ||
std::cout << std::setw(8) << std::fixed << std::setprecision(3) | ||
<< matrix[row][col]; | ||
|
||
std::cout << " |" << std::setw(8) << std::fixed << std::setprecision(3) | ||
<< matrix[row].back() << " ]" << std::endl; | ||
} | ||
} | ||
|
||
|
||
int main() { | ||
std::vector<std::vector<double> > equations{ | ||
{2, 3, 4, 6}, | ||
{1, 2, 3, 4}, | ||
{3, -4, 0, 10}}; | ||
|
||
std::cout << "Initial matrix:" << std::endl; | ||
printMatrix(equations); | ||
std::cout << std::endl; | ||
|
||
gaussianElimination(equations); | ||
std::cout << "Matrix after gaussian elimination:" << std::endl; | ||
printMatrix(equations); | ||
std::cout << std::endl; | ||
|
||
std::vector<double> ans = backSubs(equations); | ||
std::cout << "Solution from backsubstitution" << std::endl; | ||
std::cout << "x = " << ans[0] << ", y = " << ans[1] << ", z = " << ans[2] | ||
<< std::endl | ||
<< std::endl; | ||
|
||
gaussJordan(equations); | ||
std::cout << "Matrix after Gauss Jordan:" << std::endl; | ||
printMatrix(equations); | ||
std::cout << std::endl; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.