Skip to content
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

Merged
merged 9 commits into from
Jan 7, 2019
Merged

"Gaussian Elimination" Implementation in C++ #555

merged 9 commits into from
Jan 7, 2019

Conversation

rishvic
Copy link
Contributor

@rishvic rishvic commented Dec 17, 2018

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.

@rishvic rishvic changed the title "Gaussian Elimimnation" Implementation in C++ "Gaussian Elimination" Implementation in C++ Dec 17, 2018
@Gathros Gathros added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Dec 17, 2018
@rishvic
Copy link
Contributor Author

rishvic commented Dec 18, 2018

In my second commit, I just changed it so as to make sure that the row with highest absolute pivot moves up.

@leios
Copy link
Member

leios commented Dec 23, 2018

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.

@Gathros
Copy link
Contributor

Gathros commented Jan 4, 2019

The other pull request is closed, so this is the main one for Gaussian elimination.

Copy link
Contributor

@Gathros Gathros left a 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;
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor Author

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 },
Copy link
Contributor

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)
Copy link
Contributor

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,
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@Gathros
Copy link
Contributor

Gathros commented Jan 5, 2019

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 std::vector.

@rishvic
Copy link
Contributor Author

rishvic commented Jan 6, 2019

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 gaussianElimination function to try to find out whether the system of equations is solvable or not. If it's not necessary, then I can remove it.

@Gathros
Copy link
Contributor

Gathros commented Jan 6, 2019

We are not adding checks into the code. We are assuming the code is correct.

@rishvic
Copy link
Contributor Author

rishvic commented Jan 6, 2019

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) {
Copy link
Contributor

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
Copy link
Contributor

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++)
Copy link
Contributor

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
Copy link
Contributor

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++) {
Copy link
Contributor

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) {
Copy link
Contributor

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)
Copy link
Contributor

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) {
Copy link
Contributor

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);

Copy link
Contributor

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.

@rishvic
Copy link
Contributor Author

rishvic commented Jan 7, 2019

I don't think the gaussian_elimination.md has been synced with my code. So, should I do so right now?

[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)
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import:36-54.

@Gathros
Copy link
Contributor

Gathros commented Jan 7, 2019

I just remember that.

@Gathros Gathros merged commit 3c38b6f into algorithm-archivists:master Jan 7, 2019
@rishvic rishvic deleted the gaussianEliminationInCpp branch January 7, 2019 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants