You are on page 1of 4

File - E:\Projects\Inverse of a Matrix - LA Assignment\src\Main.

java
1 import java.util.Scanner;
2 /**
3 * Created by Rumman Sadiq on 3/20/2017.
4 */
5 public class Main {
6
7 public static void main(String[] args) {
8
9 Scanner in = new Scanner(System.in);
10 System.out.print("Enter the size of the nxn Matrix: ");
11 int size = in.nextInt();
12
13 double matrix[][] = new double[size][size];
14 double identity[][] = new double[size][size];
15
16 //Populating the Matrix with user data
17 for (int i = 0; i < size; i++) {
18 System.out.print("Enter the Values for Row " + (i+1) +
": " );
19 for (int j = 0; j < size; j++) {
20 matrix[i][j] = in.nextDouble();
21 }
22 }
23
24 //Populating the Identity Matrix
25 for (int i = 0; i < size; i++) {
26 for (int j = 0; j < size; j++) {
27 if(i == j) {
28 identity[i][j] = 1;
29 }
30 else {
31 identity[i][j] = 0;
32 }
33 }
34 }
35
36 int temp = 0;
37 for (int i = 0; i < size - 1; i++) {
38 //Check If Pivot is Zero Interchange it with some other
row
39 checkPivot(matrix, identity, size, i);
40
41 if((matrix[i][i] != 1) & (matrix[i][i] != 0)) {
42 //Making the pivot 1 of every row.
43 double matrixValue = matrix[i][i];
44 for (int j = 0; j < size; j++) {
45 matrix[i][j] = matrix[i][j]/matrixValue;
46 identity[i][j] = identity[i][j]/matrixValue;
47 }
48 }
49 makeEchelon(matrix, identity, size, i);
50 }
51 //Making the Pivot of Last Row 1.
Page 1 of 4
File - E:\Projects\Inverse of a Matrix - LA Assignment\src\Main.java
52 if((matrix[size-1][size-1] != 1) & (matrix[size-1][size-1]
!= 0)) {
53 //Making the pivot 1 of every row.
54 double matrixValue = matrix[size -1][size -1];
55 for (int j = 0; j < size; j++) {
56 matrix[size -1][j] = matrix[size -1][j]/matrixValue
;
57 identity[size -1][j] = identity[size -1][j]/
matrixValue;
58 }
59 }
60 removeNegZero(size, matrix);
61 removeNegZero(size, identity);
62
63 int singularCount = 0;
64
65 for (int i = 0; i < size; i++) {
66 singularCount = 0;
67 for (int j = 0; j < size; j++) {
68 if(matrix[i][j] == 0) {
69 singularCount++;
70 }
71 }
72 }
73 if(singularCount == size) {
74 System.out.println("\nThe matrix is Singular. So, It's
Inverse doesn't exist.");
75 }
76 else {
77 for (int i = size-1; i > 0; i--) {
78 makeReducedEchelon(matrix, identity, size, i);
79 }
80 System.out.println("\nInverse of the given Matrix");
81 printMatrix(size, identity);
82 }
83
84 }
85
86 private static void printMatrix(int size, double[][] m) {
87 System.out.println(
"---------------------------------------");
88 for (int i = 0; i < size; i++) {
89 for (int j = 0; j < size; j++) {
90 System.out.printf("%.2f ", m[i][j]);
91 }
92 System.out.println();
93 }
94 }
95
96 private static void removeNegZero(int size, double[][] m) {
97 for (int i = 0; i < size; i++) {
98 for (int j = 0; j < size; j++) {
99 if(m[i][j] == -0.0) {
Page 2 of 4
File - E:\Projects\Inverse of a Matrix - LA Assignment\src\Main.java
100 m[i][j] = 0;
101 }
102 }
103 }
104 }
105
106 private static void makeReducedEchelon(double[][] matrix,
double[][] identity, int size, int i) {
107 int count = 1;
108 double matrixMultiple1 = matrix[i][i];
109
110 for (int j = i; j > 0; j--) {
111 if( matrix[i - count][i] != 0) {
112 double temp = 0;
113 double matrixMultiple2 = matrix[i - count][i];
114
115 for (int k = 0; k < size ; k++) {
116 temp = (matrix[i - count][k] * matrixMultiple1)
- (matrix[i][k] * matrixMultiple2);
117 matrix[i - count][k] = temp;
118
119 temp = (identity[i - count][k] *
matrixMultiple1) - (identity[i][k] * matrixMultiple2);
120 identity[i - count][k] = temp;
121 }
122 }
123 count++;
124 }
125 }
126
127 private static void makeEchelon(double[][] matrix, double[][]
identity, int size, int i) {
128 int count = 1;
129 double matrixMultiple1 = matrix[i][i];
130
131 for (int j = i; j < size - 1; j++) {
132 if( matrix[i + count][i] != 0) {
133 double temp = 0;
134 double matrixMultiple2 = matrix[i + count][i];
135
136 for (int k = 0; k < size ; k++) {
137 temp = (matrix[i + count][k] * matrixMultiple1)
- (matrix[i][k] * matrixMultiple2);
138 matrix[i + count][k] = temp;
139
140 temp = (identity[i + count][k] *
matrixMultiple1) - (identity[i][k] * matrixMultiple2);
141 identity[i + count][k] = temp;
142 }
143 }
144 count++;
145 }
146 }
Page 3 of 4
File - E:\Projects\Inverse of a Matrix - LA Assignment\src\Main.java
147
148 private static void checkPivot(double[][] matrix, double[][]
identity, int size, int oldRow) {
149 if(matrix[oldRow][oldRow] == 0) {
150
151 //Scanning other rows with non-zero entries in that row
and column. Starting from bottom
152 int newRow = 0;
153 for (int j = size-1; j > oldRow ; j--) {
154 if(matrix[j][oldRow] != 0) {
155 newRow = j;
156 break;
157 }
158 }
159
160 //Now Replace the Row "i" with Row "newRow"
161 double temp;
162 for (int j = 0; j < size; j++) {
163 temp = matrix[oldRow][j];
164 matrix[oldRow][j] = matrix[newRow][j];
165 matrix[newRow][j] = temp;
166
167 temp = identity[oldRow][j];
168 identity[oldRow][j] = identity[newRow][j];
169 identity[newRow][j] = temp;
170 }
171 }
172 }
173 }
174

Page 4 of 4

You might also like