-
Notifications
You must be signed in to change notification settings - Fork 20k
Feat : Implemented Gaussian Elimination #2810
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
38988bf
Add files via upload
Sachwin-Kohli 311004b
Update directory
aa83426
Fix : Updated code as per requested changes
Sachwin-Kohli 9e1b93c
Update Maths/Gaussian.java
siriak 61cde7b
Update Maths/Gaussian.java
siriak 9806e7c
Update Maths/Gaussian.java
siriak 6c4cc70
Merge branch 'master' into master
siriak 58eb094
Update directory
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
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
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,68 @@ | ||
/** | ||
* \file | ||
* \brief [Gaussian elimination | ||
* method](https://en.wikipedia.org/wiki/Gaussian_elimination) | ||
*/ | ||
package Maths; | ||
import java.util.*; | ||
|
||
/** Main function */ | ||
public class Gaussian { | ||
|
||
public static void main(String[] args) { | ||
int mat_size, i, j, step; | ||
Scanner sc = new Scanner(System.in); | ||
|
||
System.out.println("Matrix Size : "); | ||
mat_size = sc.nextInt(); | ||
|
||
double [][] mat = new double[mat_size+1][mat_size+1]; | ||
double [][] x = new double[mat_size][mat_size+1]; | ||
|
||
System.out.println("Enter value of the matrix"); | ||
System.out.println(' '); | ||
for (i = 0; i < mat_size; i++) { | ||
for (j = 0; j <= mat_size; j++) { | ||
mat[i][j] = sc.nextInt(); | ||
} | ||
} | ||
|
||
// perform Gaussian elimination | ||
for (step = 0; step < mat_size - 1; step++) { | ||
for (i = step; i < mat_size - 1; i++) { | ||
double a = (mat[i + 1][step] / mat[step][step]); | ||
|
||
for (j = step; j <= mat_size; j++) | ||
mat[i + 1][j] = mat[i + 1][j] - (a * mat[step][j]); | ||
} | ||
} | ||
|
||
System.out.println("Matrix using Gaussian Elimination method: "); | ||
System.out.println(" "); | ||
for (i = 0; i < mat_size; i++) { | ||
for (j = 0; j <= mat_size; j++) { | ||
x[i][j] = mat[i][j]; | ||
System.out.print(mat[i][j] + " "); | ||
} | ||
System.out.println(); | ||
} | ||
System.out.println( "Value of the Gaussian Elimination method: "); | ||
System.out.println(" "); | ||
|
||
for (i = mat_size - 1; i >= 0; i--) { | ||
double sum = 0; | ||
for (j = mat_size - 1; j > i; j--) { | ||
x[i][j] = x[j][j] * x[i][j]; | ||
sum = x[i][j] + sum; | ||
} | ||
if (x[i][i] == 0){ | ||
x[i][i] = 0; | ||
} | ||
else{ | ||
x[i][i] = (x[i][mat_size] - sum) / (x[i][i]); | ||
} | ||
System.out.print("x" + i + "=" + x[i][i]); | ||
System.out.println(" "); | ||
} | ||
} | ||
} |
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.