View Single Post
  #7 (permalink)  
Old 06-15-2007, 02:30 AM
sania21 sania21 is offline
Newbie
 
Join Date: May 2007
Posts: 29
Rep Power: 6
sania21 is on a distinguished road
Default Something incorrect in the code....

I tried ur code of matrix
the logic u have implemented
it is partialy correct for user input matrix but
the operations you are performing
on identity matrix are not correct at the end we get the
reduced form of matrix A but the
inverse in identity matrix is wrong.


The concept is not mere reducing the elements in matrix A
to identity but getting the inverse is the main point,
Although the code you have made makes the pivot element 1.


Below i am showing each step
that i have performed on matrix A as
wel as matrix I



Enter the dimension for Matrix :
3
Enter the elements of Matrix
Matrix A =
4
2
2
4
6
8
-2
2
4
4.0 2.0 2.0
4.0 6.0 8.0
-2.0 2.0 4.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
1.0 0.5 0.5
0.0 4.0 6.0
0.0 3.0 5.0
Mat I after first operation
0.25 0.0 0.0
-1.0 1.0 0.0
0.5 0.0 1.0



Now u wil notice here that i have divided
the "complete row" in matrix A as well as complete row in Matrix I by pivotal element.
Here is the main differnce between my code and yours.

Mat A after second operation
1.0 0.5 0.5
0.0 1.0 1.5
0.0 3.0 5.0
Mat I after second operation
0.25 0.0 0.0
-0.25 0.25 0.0
0.5 0.0 1.0


i want to mention that the row operation has to be performed on "complete row".Since the row operation r performed on complete row the upper elements will also change as well so that later they can be reduced to 0 .

Mat A after third operation
1.0 0.0 -0.25
0.0 1.0 1.5
0.0 3.0 5.0
Mat I after third operation
0.375 -0.125 0.0
-0.25 0.25 0.0
0.5 0.0 1.0





Mat A after fourth operation
1.0 0.0 -0.25
0.0 1.0 1.5
0.0 0.0 0.5
Mat I after fourth operation
0.375 -0.125 0.0
-0.25 0.25 0.0
1.25 -0.75 1.0




Mat A after fifth operation
1.0 0.0 -0.25
0.0 1.0 1.5
0.0 0.0 1.0
Mat I after fifth operation
0.375 -0.125 0.0
-0.25 0.25 0.0
2.5 -1.5 2.0


Now this is the inverse shown below.

Mat A after sixth operation
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0

Mat I(the inverse) after sixth operation
1.0 -0.5 0.5
-4.0 2.5 -3.0
2.5 -1.5 2.0



This time i am giving my complete
source code if any changes you could make in it
so that i could get what i realy want
it wil be realy helpful.Thanks in advance.


Try this code and let me know using this code you will get the output in just one step.



package com.Data.Calculation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Matr {

private static BufferedReader buff;
private double[][] data=null;

private int i;
private int j;


double d6=0;
int M;
int N;


public void Matrix1(int M, int N) throws NumberFormatException, IOException
{
this.M = M;
this.N = N;
data = new double[M][N];


}

public void Matrix3() throws NumberFormatException, IOException

{

buff=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the dimension for Matrix :");
M=Integer.parseInt(buff.readLine());
N=M;
Matrix1(M,N);

}

public void Matri2(double[][] data1) throws NumberFormatException, IOException

{
buff=new BufferedReader(new InputStreamReader(System.in));
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
data[i][j]=Double.parseDouble(buff.readLine());
}
}
show();
}
public void Matri(double[][] data2) throws NumberFormatException, IOException
{

for(i=0;i<M ;i++)
{
for(j=0;j<N;j++)
{
if(i==j)
{
data[i][j]=1;
}
else
{
data[i][j]=0;
}
}

}

show();
}

public void show() throws NumberFormatException, IOException
{
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
System.out.print("\t"+data[i][j]);
System.out.println();

}


}
public Matr refresh(Matr A)
{
return A=this;
}



public void operation1(Matr I) throws NumberFormatException, IOException
{
Matr A=this;
double d1=0;
double d2=0;
double d5=0;

for(i=0;i<1;i++)
{
for(j=0;j<N;j++)
{


if(i==j)
{
d1=A.data[i][j];
}

for(i=0;i<1;i++)
{
for(j=0;j<N;j++)
{
I.data[i][j]=(I.data[i][j]/d1);
A.data[i][j]=(A.data[i][j]/d1);
}
}

}
}
refresh(A);

for(i=1;i<M;i++)
{
for(j=N-1;j>=0;j--)
{
I.data[i][j]=(I.data[i][j]-(A.data[i][j-j]*I.data[i-i][j]));
A.data[i][j]=(A.data[i][j]-(A.data[i][j-j]*A.data[i-i][j]));
}

}

refresh(A);



for(i=1;i<2;i++)
{
for(j=0;j<N;j++)
{
if(i==j)
{
d2=A.data[i][j];
}
}
}

for(i=1;i<2;i++)
{
for(j=0;j<N;j++)
{

I.data[i][j]=(I.data[i][j]/d2);
A.data[i][j]=(A.data[i][j]/d2);
}
}


refresh(A);

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]));
}

}
}



for(i=2;i<M;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-(i-1)][j]));
A.data[i][j]=(A.data[i][j]-(A.data[i][j-(j-1)]*A.data[i-(i-1)][j]));
}

}
}



for(i=2;i<M;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-(i-1)][j]));
A.data[i][j]=(A.data[i][j]-(A.data[i][j-(j-1)]*A.data[i-(i-1)][j]));
}


}
}
refresh(A);


for(i=2;i<M;i++)
{
for(j=0;j<N;j++)
{
if(i==j)
{
d5=A.data[i][j];
}
}

for(i=2;i<M;i++)
{
for(j=0;j<N;j++)
{
I.data[i][j]=(I.data[i][j]/d5);
A.data[i][j]=(A.data[i][j]/d5);
}
}

}

for(i=0;i<M-1;i++)
{
for(j=N-1;j>=0;j--)
{
if(j!=N-1)
{
I.data[i][j]=(I.data[i][j]-(A.data[i][j-(j-2)]*I.data[j-(j-2)][j]));
A.data[i][j]=(A.data[i][j]-(A.data[i][j-(j-2)]*A.data[j-(j-2)][j]));
}
}
}
for(i=0;i<M-1;i++)
{
for(j=N-1;j>=0;j--)
{
if(j==N-1)
{
I.data[i][j]=(I.data[i][j]-(A.data[i][j-(j-2)]*I.data[j-(j-2)][j]));
A.data[i][j]=(A.data[i][j]-(A.data[i][j-(j-2)]*A.data[j-(j-2)][j]));
}
}
}

System.out.println("Mat A after first operation");
A.show();
System.out.println("Mat I after first operation");
I.show();

}
}







package com.Data.Matdata;
import com.Data.Calculation.Matr;
import java.io.IOException;


public class Matrix {

private static double[][] data1;
private static double[][] data2;
public static void main(String[] args) throws NumberFormatException, IOException {
Matr A=new Matr();

A.Matrix3();
System.out.println("Enter the elements of Matrix");
System.out.println("Matrix A =");
A.Matri2(data1);


Matr I=new Matr();
I.Matrix3();
System.out.println("Matrix I =");
I.Matri(data2);

A.operation1(I);




}



}
Reply With Quote