-
-
Notifications
You must be signed in to change notification settings - Fork 357
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
"Gaussian Elimination" Implementation in C++ #555
"Gaussian Elimination" Implementation in C++ #555
Conversation
In my second commit, I just changed it so as to make sure that the row with highest absolute pivot moves up. |
Unfortunately, we have a PR for gaussian elimination in C++ (#258). I'll see what the status is on that PR and get back to you. Sorry it's taking so long. |
The other pull request is closed, so this is the main one for Gaussian elimination. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't review the algorithm, I just listed of some improvements you could make. All though I'll review after.
#include <stdexcept> | ||
#include <vector> | ||
#include <cmath> | ||
using namespace std; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the use of using namespace std
is a good idea for the philosophy of AAA is. The idea is for the AAA to be used by new comers to the language to learn, also for people to use the algorithm in real work cases and show case the algorithm. I don't believe that using namespace std
achieves this.
#include <cmath> | ||
using namespace std; | ||
|
||
class EquationSolver |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There really is no need in making a class for solving Gaussian elimination.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, should I just define each function individually, because I thought encapsulating it would make it more user-friendly and show how it can be used to solve equations.
|
||
int main() { | ||
int varc = 3; | ||
vector< vector<long double> > equations { { 2, 3, 4, 6 }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why use long double
when double
works fine?
{ | ||
ans = solver.solveByGaussJordan(equations, varc); | ||
} | ||
catch(runtime_error &e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big fan of try catch.
private: | ||
|
||
// subtract row from another row after multiplcation | ||
void subRow(vector<long double> &toBeSubtracted, const vector<long double> &toSubtract, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need of making this function if it's used in one place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the row subtraction function was being showcased in the documentation, I thought a special mention would be necessary.
Looks much better now. But why is it so different from the C version? Gaussian elimination is not a complex algorithm. I expected it to be the C code with C++ features like |
I have as of now made some changes, and I do see your point. The main point of difference comes from the fact that I am trying to use the |
We are not adding checks into the code. We are assuming the code is correct. |
Then I'lll be removing all the parts of code that checks whether the matrix is singular or not. |
#include <vector> | ||
|
||
|
||
void gaussianElimination(std::vector< std::vector<double> > &eqns, int varc) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
verc
does not need to exist, you can just use int cols = eqns[0].size();
.
|
||
void gaussianElimination(std::vector< std::vector<double> > &eqns, int varc) { | ||
// 'eqns' is the matrix, 'varc' is no. of vars | ||
int err = 0; // marker for if matrix is singular |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're not using err
.
for (int i = 0; i < varc - 1; i++) { | ||
int pivot = i; | ||
|
||
for (int j = i + 1; j < varc; j++) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put {} on this for loop.
pivot = j; | ||
|
||
if (eqns[pivot][i] == 0.0) { | ||
err = 1; // Marking that matrix is singular |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Print "The matrix is singular.\n" here.
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 < varc; j++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
j should go to eqns.size()
not varc
.
} | ||
|
||
|
||
std::vector<double> solveByGaussJordan(std::vector<std::vector<double>> eqns, int varc) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this function just have this in main
.
} | ||
|
||
|
||
std::vector<double> backSubs(std::vector<std::vector<double>> &eqns, int varc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again no need for varc
.
} | ||
|
||
|
||
std::vector<double> solveByBacksubs(std::vector<std::vector<double>> eqns, int varc) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this function just have this in main
.
std::vector<double> ans; | ||
|
||
ans = solveByGaussJordan(equations, varc); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Print the matrix after the eliminations.
I don't think the |
[import:5-13, lang:"c"](code/c/gaussian_elimination.c) | ||
[import:19-34, lang:"c"](code/c/gaussian_elimination.c) | ||
{% sample lang="cpp" %} | ||
[import:25-25, lang:"cpp"](code/c++/gaussian_elimination.cpp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need this line, everyone know what std::swap
does.
[import:19-34, lang:"c"](code/c/gaussian_elimination.c) | ||
{% sample lang="cpp" %} | ||
[import:25-25, lang:"cpp"](code/c++/gaussian_elimination.cpp) | ||
[import:13-25, lang:"cpp"](code/c++/gaussian_elimination.cpp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be import:13-23
/
[import:36-41, lang:"c_cpp"](code/c/gaussian_elimination.c) | ||
[import:36-41, lang:"c"](code/c/gaussian_elimination.c) | ||
{% sample lang="cpp" %} | ||
[import:28-32, lang:"c_cpp"](code/c++/gaussian_elimination.cpp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be import:25-32
. Also "cpp"
.
[import:15-48, lang:"c_cpp"](code/c/gaussian_elimination.c) | ||
[import:15-48, lang:"c"](code/c/gaussian_elimination.c) | ||
{% sample lang="cpp" %} | ||
[import:8-36, lang:"cpp"](code/c++/gaussian_elimination.cpp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import:8-34
.
[import:64-82, lang:"c_cpp"](code/c/gaussian_elimination.c) | ||
[import:64-82, lang:"c"](code/c/gaussian_elimination.c) | ||
{% sample lang="cpp" %} | ||
[import:39-53, lang:"cpp"](code/c++/gaussian_elimination.cpp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import:36-54
.
I just remember that. |
Added an implementation for the Gaussian Elimination algorithm in C++, and encapsulating it in a C++ class so as to make it user-friendly, and used some of recent C++ features such as multi-dimensional vector to create matrix. Any advice for changes or such is appreciated.