View Single Post
  #3 (permalink)  
Old 06-14-2007, 04:13 AM
sania21 sania21 is offline
Newbie
 
Join Date: May 2007
Posts: 29
Rep Power: 6
sania21 is on a distinguished road
Default logic behind the matrix working

well,below i am explaning what i am actualy trying to implement.
First of all there wil be two matrix one matrix given by user as input, second the identity matrix.
Suppose Matrix A= 4 2 2
4 6 8
-2 2 4

Identity MAtrix I= 1 0 0
0 1 0
0 0 1

The operation which i wil perform on matrix A
will also be performed on MAtrix I so at the
end i will get Inverse of matrix.
The elements wil be stored in Matrix A as
A[0][0]=4 A[1][0]=4 A[2][0]=-2
A[0][1]=2 A[1][1]=6 A[2][1]=2
A[0][2]=2 A[1][2]=8 A[2][2]=4

this i have shown for first row later for the other
rows it will be same like 10,11,12,20,21,22 element.

when i==j in a for loop(here i and j is index of matrix A) ie at the first element where 0==0 the value is 4 in matrix A
at that time the whole row wil get divided by 4(here 4 is pivot element).
so we get the output as


Matrix A= 1 0.5 0.5
4 6 8
-2 2 4

same operation on Matrix I= 0.25 0 0
0 1 0
0 0 1

This state of matrix is to be stored,this becomes the new matrix.

after this the second row and third row operation are as

A[1][0]=A[1][0]-(A[1][0]*A[0][0]);
A[1][1]=A[1][1]-(A[1][0]*A[0][1]);(for this i have written loops)
A[1][2]=A[1][2]-(A[1][0]*A[0][2]);

4=4-(4*1)=0
6=6-(4*0.5)=4(the multiple element is 4 which is A[1][0] element)
8=8-(4*0.5)=6

Above shown was second row operation
now second row becomes
the output is as shown


A= 1 0.5 0.5 I=0.25 0 0
0 4 6 -1 1 0
-2 2 4 0 0 1


after this third row operation is



A[2][0]=A[2][0]-(A[2][0]*A[0][0]);
A[2][1]=A[2][1]-(A[2][0]*A[0][1]);(here the multiple element is -2 ie A[2][0])
A[2][2]=A[2][2]-(A[2][0]*A[0][2]);


-2=-2-(-2*1)=0
2=2-(-2*0.5)=3
4=4-(-2*0.5)=5


The matrix becomes as

A= 1 0.5 0.5
0 4 6
0 3 5


I= 0.25 0 0
-1 1 0
0.5 0 1

Now this is new state of matrix A which is to be stored

from Matrix A now the next pivot element is

A[1][0]=4
now again this time second row will
be divided by 4 so weget matrix A as

MAtrix A= 1 0.5 0.5 I= 0.25 0 0
0 1 1.5 -0.25 0.25 0
0 3 5 0.5 0 1
you will be noticing that matrix A is reducing to identity matrix and matrix I to its inverse.



but my problem comes here,when you wil check my code you wil find that
i have used three double element d1 ,d2 , d5.these three element are used
bcoz whenever i want a pivot element(pivot elements are the diagonal elements ie 00,11,22) ,everytime when i want a pivot element i have to give condition

i==j

take the element from matrix save it in any double like d1

than divide the whole row by it,so that the element becomes 1 and other element can than be reduced to 0
so as to get identity matrix.Again when one complete columns elements have been reduced to 0 "than only i have to fetch the next pivot element"
i cannot take all the pivot elements and divide all rows at once so that diagonal elements become 1,till one complete columns does not reduce i cannot fetch for other pivot element.

My program works well for 3 by 3 matrix,bcoz in my program three times i hav given condition i==j and taken three elements
in d1 d2 and d5.But i am interested in working on large matrix so how many times wil i write code for a forloop when i==j.
if any advice or suggestion atleast any clue given on this problem it is most welcome.

whan you will run below shown program you can get inverse of any 3 by 3 matrix.

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();

}
Reply With Quote