-
Notifications
You must be signed in to change notification settings - Fork 21.2k
Inverse Of a Square Matrix [Hacktoberfest] #2582
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
14 commits
Select commit
Hold shift + click to select a range
3538a66
Hill Cipher Implementation in JAVA
ojasva 4e6878e
Determinant of a Matrix
ojasva d337e1a
Merge branch 'TheAlgorithms:master' into master
ojasva c662f13
Delete HillCipher.java
ojasva 4570367
Add files via upload
ojasva 2821b9f
Delete DeterminantOfMatrix.java
ojasva bf37509
Merge branch 'master' into master
siriak 51c0d78
Merge branch 'TheAlgorithms:master' into master
ojasva d952e38
DeterminantOfMatrix
ojasva a3fcba1
Merge branch 'master' into master
siriak 9eb7ee9
Merge branch 'TheAlgorithms:master' into master
ojasva 2e219d0
Inverse Of a Square Matrix
ojasva a0576a6
Merge branch 'master' into master
ojasva e7c888a
Update InverseOfMatrix.java
ojasva 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package Misc; | ||
| import java.util.Scanner; | ||
|
|
||
| /* | ||
| * Wikipedia link : https://en.wikipedia.org/wiki/Invertible_matrix | ||
| * | ||
| * Here we use gauss elimination method to find the inverse of a given matrix. | ||
| * To understand gauss elimination method to find inverse of a matrix: https://www.sangakoo.com/en/unit/inverse-matrix-method-of-gaussian-elimination | ||
| * | ||
| * We can also find the inverse of a matrix | ||
| */ | ||
| public class InverseOfMatrix | ||
| { | ||
| public static void main(String argv[]) | ||
| { | ||
| Scanner input = new Scanner(System.in); | ||
| System.out.println("Enter the matrix size (Square matrix only): "); | ||
| int n = input.nextInt(); | ||
| double a[][]= new double[n][n]; | ||
| System.out.println("Enter the elements of matrix: "); | ||
| for(int i=0; i<n; i++) | ||
| for(int j=0; j<n; j++) | ||
| a[i][j] = input.nextDouble(); | ||
|
|
||
| double d[][] = invert(a); | ||
| System.out.println(); | ||
| System.out.println("The inverse is: "); | ||
| for (int i=0; i<n; ++i) | ||
| { | ||
| for (int j=0; j<n; ++j) | ||
| { | ||
| System.out.print(d[i][j]+" "); | ||
| } | ||
| System.out.println(); | ||
| } | ||
| input.close(); | ||
| } | ||
|
|
||
| public static double[][] invert(double a[][]) | ||
| { | ||
| int n = a.length; | ||
| double x[][] = new double[n][n]; | ||
| double b[][] = new double[n][n]; | ||
| int index[] = new int[n]; | ||
| for (int i=0; i<n; ++i) | ||
| b[i][i] = 1; | ||
|
|
||
| // Transform the matrix into an upper triangle | ||
| gaussian(a, index); | ||
|
|
||
| // Update the matrix b[i][j] with the ratios stored | ||
| for (int i=0; i<n-1; ++i) | ||
| for (int j=i+1; j<n; ++j) | ||
| for (int k=0; k<n; ++k) | ||
| b[index[j]][k] | ||
| -= a[index[j]][i]*b[index[i]][k]; | ||
|
|
||
| // Perform backward substitutions | ||
| for (int i=0; i<n; ++i) | ||
| { | ||
| x[n-1][i] = b[index[n-1]][i]/a[index[n-1]][n-1]; | ||
| for (int j=n-2; j>=0; --j) | ||
| { | ||
| x[j][i] = b[index[j]][i]; | ||
| for (int k=j+1; k<n; ++k) | ||
| { | ||
| x[j][i] -= a[index[j]][k]*x[k][i]; | ||
| } | ||
| x[j][i] /= a[index[j]][j]; | ||
| } | ||
| } | ||
| return x; | ||
| } | ||
|
|
||
| // Method to carry out the partial-pivoting Gaussian | ||
| // elimination. Here index[] stores pivoting order. | ||
|
|
||
| public static void gaussian(double a[][], int index[]) | ||
| { | ||
| int n = index.length; | ||
| double c[] = new double[n]; | ||
|
|
||
| // Initialize the index | ||
| for (int i=0; i<n; ++i) | ||
| index[i] = i; | ||
|
|
||
| // Find the rescaling factors, one from each row | ||
| for (int i=0; i<n; ++i) | ||
| { | ||
| double c1 = 0; | ||
| for (int j=0; j<n; ++j) | ||
| { | ||
| double c0 = Math.abs(a[i][j]); | ||
| if (c0 > c1) c1 = c0; | ||
| } | ||
| c[i] = c1; | ||
| } | ||
|
|
||
| // Search the pivoting element from each column | ||
| int k = 0; | ||
| for (int j=0; j<n-1; ++j) | ||
| { | ||
| double pi1 = 0; | ||
| for (int i=j; i<n; ++i) | ||
| { | ||
| double pi0 = Math.abs(a[index[i]][j]); | ||
| pi0 /= c[index[i]]; | ||
| if (pi0 > pi1) | ||
| { | ||
| pi1 = pi0; | ||
| k = i; | ||
| } | ||
| } | ||
| // Interchange rows according to the pivoting order | ||
| int itmp = index[j]; | ||
| index[j] = index[k]; | ||
| index[k] = itmp; | ||
| for (int i=j+1; i<n; ++i) | ||
| { | ||
| double pj = a[index[i]][j]/a[index[j]][j]; | ||
|
|
||
| // Record pivoting ratios below the diagonal | ||
| a[index[i]][j] = pj; | ||
|
|
||
| // Modify other elements accordingly | ||
| for (int l=j+1; l<n; ++l) | ||
| a[index[i]][l] -= pj*a[index[j]][l]; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.