-
- Notifications
You must be signed in to change notification settings - Fork 359
Added gaussian elimination in js #464
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
Added gaussian elimination in js #464
Conversation
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.
Thanks for contributing, as always!
There are quite a few things I need you to change, but nothing major. Mostly style related.
- We use 2 spaces as indentation for JavaScript, not 4.
- We use camelCase for function and method names in JavaScript.
As a side note: Did you take the C implementation as a reference? Your code looks suspiciously similar to it. It's not wrong or bad. I just noticed. That would also explain the \n
in all your console.log
s.
@@ -0,0 +1,112 @@ | |||
function gaussian_elimination(a){ |
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.
Please add a space between )
and {
here and in all other function declarations where you forgot it.
@@ -0,0 +1,112 @@ | |||
function gaussian_elimination(a){ | |||
var rows = a.length |
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.
var
is a pretty outdated keyword in JavaScript. These days, you should really use either let
or const
to declare a variable. let
is an objectively better version of var
. const
works the same as let
except that you can't overwrite a variable declared with const
.
The rule of thumb is: Always use const
unless you have to overwrite a variable. That would mean that both rows
and cols
should be declared as const
.
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.
ah ok thanks!
} | ||
} | ||
| ||
if (a[pivot][col] == 0) { |
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.
Always use ===
or !==
.
continue; | ||
} | ||
| ||
if (col != pivot) { |
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.
Always use ===
or !==
.
} | ||
| ||
if (col != pivot) { | ||
let t=a[col]; |
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 const
as explained further up.
Also, please add spaces between operators and operands like t = a[col]
. That goes for the next 2 lines as well.
var row = 0; | ||
| ||
for (let col = 0; col < cols - 1; ++col) { | ||
if (a[row][col] != 0) { |
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.
Always use ===
or !==
.
} | ||
} | ||
| ||
var a = [[3, 2 , -4, 3 ], |
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.
a
should be const
.
| ||
gaussian_elimination(a); | ||
console.log("Gaussian elimination:\n"); | ||
for(let i=0;i<a.length;++i){ |
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.
As I mentioned earlier, please make sure to space things out to make them more readable. for (let i = 0; i < a.length; +) {
That goes for pretty much every line from here on out.
gaussian_elimination(a); | ||
console.log("Gaussian elimination:\n"); | ||
for(let i=0;i<a.length;++i){ | ||
let txt="" |
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 snippet of code appears 3 times. I suggest creating a function to handle the matrix output. Something like this:
function printMatrixRow(row) { const text = row .map(v => (v < 0 ? " " : " ") + v.toPrecision(8)) .join(""); console.log(text); } function printMatrix(a) { for (const row of a) { printMatrixRow(row); } }
Then you could just printMatrix(a)
and printMatrixRow(sol)
and you're done.
} | ||
| ||
if (a[pivot][col] == 0) { | ||
console.log("The matrix is singular.\n"); |
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.
The trailing \n
is not needed. console.log
does that for you.
Yeah used the c code as reference+keeping variables mostly the same so when switching between languages it's easier |
Don't forget to update the line numbers in the .md file according to the changes you made. |
Hmm the line numbers were the same unless I accidentally newlined somewhere |
Then everything seems to be fine. It was just a reminder, because people often forget that. I'll take a look at the updated code later and if not, tomorrow afternoon my time (CEST). So far it looks good, though. |
Ah ok thanks! |
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.
Just a couple more very minor things. If you fix these, the PR will be ready to merge. Sorry for being so nitpicky, but we want to make sure that the code examples are as readable as possible.
let row = 0; | ||
| ||
for (let col = 0; col < cols - 1; ++col) { | ||
|
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 empty line here looks weird. It seems quite random because it's the only for loop that has it. Could you remove it for consistency?
| ||
const a = [[3, 2, -4, 3], | ||
[ 2, 3, 3, 15], | ||
[ 5, -3, 1, 14]]; |
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 suggest you format the matrix like this:
const a = [ [3, 2, -4, 3], [2, 3, 3, 15], [5, -3, 1, 14] ];
function backSubstitution(a) { | ||
const rows = a.length; | ||
const cols = a[0].length; | ||
const sol=[]; |
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 spaces around the =
here.
} | ||
| ||
function backSubstitution(a) { | ||
const rows = a.length; |
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.
The rows
variable is unused. You can remove this declaration.
Okie I've made the changes |
Great, thanks! |
| ||
const a = [ | ||
[3, 2, -4, 3], | ||
[2, 3, 3, 15], |
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 line has a tab instead of 2 spaces.
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.
oh oops tab looked like 2 spaces to me thanks
I just went through the code one last time before I could merge and then I realized that I messed up during my review yesterday. You remember how I told you that the We'll get this PR merged today. I promise. :D |
whoops died on my bed too early yesterday haha, fixed the code |
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.
Alright. Finally. :D
No description provided.