OK, if you don't mind here are several pieces of advice:
1. COMMENT your code, use MEANINGFUL NAMES - I got lost in it, I can't tell what is intended to do what.
Explain this fragment for example:
Code:
for(i=0;i<1;i++)
{
for(j=0;j<N;j++)
{
if(j!=i+1)
{
I.data[i][j]=(I.data[i][j]-(A.data[i][j-(j-1)]*I.data[i+(j-(j-1))][j]));
A.data[i][j]=(A.data[i][j]-(A.data[i][j-(j-1)]*A.data[i+(j-(j-1))][j]));
}
}
}
for(i=0;i<1;i++)
{
for(j=0;j<N;j++)
{
if(j==i+1)
{
I.data[i][j]=(I.data[i][j]-(A.data[i][j-(j-1)]*I.data[i+(j-(j-1))][j]));
A.data[i][j]=(A.data[i][j]-(A.data[i][j-(j-1)]*A.data[i+(j-(j-1))][j]));
}
}
}
It looks like the same thing written twice.
This will execute only once !?:
2. Please put your code in code tags. [ code ] your code [/ code ] ( without spaces in the tags )
3. Try to formulate the rules you must implement for a N-sized matrix. If you do it right you will have your algorithm in front of you.
4. Try to keep fields that are not relevant for the class state local to methods, don't make them fields of the class ( like BufferedReader in your class )
5. Avoid keeping copies the same information in several places ( like the size of the matrix - the 2 dimensional array keeps that information inside and makes it accessible trough [i].length[i].
btw:
Code:
Enter the dimension for Matrix :
3
Enter the elements of Matrix
Matrix A =
1
1
1
1
1
1
1
1
1
1.0 1.0 1.0
1.0 1.0 1.0
1.0 1.0 1.0
Enter the dimension for Matrix :
3
Matrix I =
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
Mat A after first operation
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
Mat I after first operation
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
Try this ...:
Code:
package matrix;
import java.io.PrintStream;
public class Matrix {
private double matrix[][];
public Matrix(int rows, int cols){
if( rows<1 || cols < 1)
throw new IllegalArgumentException();
matrix = new double[rows][cols];
}
public double get(int p,int q){
return matrix[p][q];
}
public void set(int p,int q, double value){
matrix[p][q] = value;
}
public Matrix invert(){
//if not square - return null
if( matrix.length != matrix[0].length )
return null;
Matrix result = new IMatrix(matrix.length);
/* Starting row - from 0 to last */
for(int startRow = 0; startRow < matrix.length; startRow++){
/* divide all elements in that row on the first one */
double divideOn = matrix[startRow][startRow];
for(int col = 0; col<matrix[startRow].length; col++){
matrix[startRow][col] = matrix[startRow][col] / divideOn;
result.matrix[startRow][col] = result.matrix[startRow][col] / divideOn;
}
//output
display(System.out);
result.display(System.out);
/* for the rest of the rows: */
for(int currentRow = startRow+1; currentRow < matrix.length; currentRow++){
double multiplier = matrix[currentRow][startRow]; //take the first not 0 value
for(int currentCol = 0; currentCol<matrix[currentRow].length; currentCol++){
matrix[currentRow][currentCol] = matrix[currentRow][currentCol] - multiplier*matrix[startRow][currentCol];
result.matrix[currentRow][currentCol] = result.matrix[currentRow][currentCol] - multiplier*result.matrix[startRow][currentCol];
}
//output - after each row change
display(System.out);
result.display(System.out);
}
}
return result;
}
public void display(PrintStream ps){
for(double row[] : matrix){
for( double d:row)
ps.print(d+"\t");
ps.println();
}
ps.println();
ps.println();
}
public static void main(String args[]){
Matrix m = new Matrix(3,3);
m.set(0, 0, 4);
m.set(0, 1, 2);
m.set(0, 2, 2);
m.set(1, 0, 4);
m.set(1, 1, 6);
m.set(1, 2, 8);
m.set(2, 0, -2);
m.set(2, 1, 2);
m.set(2, 2, 4);
m.invert().display(System.out);
}
}
class IMatrix extends Matrix{
public IMatrix(int side){
super(side,side);
for( int i=0; i<side;i++){
super.set(i, i, 1.0);
}
}
}