Jump to content

Need help on matrix.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#1
sania21

sania21

    Newbie

  • Members
  • PipPip
  • 29 posts
ok,well here is my problem i am trying to work on pivot method of matrix,but i have got stuck in it :confused:
In my program,with the first pivot element (ie 00 element) the complete row gets divided.than the operation takes place on second till the end of rows now after this operation completes again i want the second pivot element (ei. 11 element ) and again divide now this time second row with 11 element and the process goes on so as to get the inverse on the other side.

Below shown is my code can anyone give me some advice so that i could proceed on my work it wil be most welcome.

here MAtr A is user given matrix and Matr I is identity matrix and refresh(A) method gives current state of matrix.


I am unable to get the second pivot element ie(11 element) using for loop :mad:,i can get it manualy but each time i have to write a for loop for that bcoz after getting each pivot element i have to divide the complete row with that as well as perform some operations on other rows also i want to make my program generalised so that it could work on large matrix.

public void operation1(Matr I) throws NumberFormatException, IOException
{
Matr A=this;
double d1=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++)
{

System.out.println("val of d1 "+d1);

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


}

#2
oubless

oubless

    Newbie

  • Members
  • PipPip
  • 22 posts
Please post you whole code in code tags :).
I'm not sure I understood what is the logic that should apply, is it:
A matrix PxQ size
For each row < min( P,Q)
for all members of this row:
new value = old value / old value of p,q element
Is this the logic that should apply?
How often should the state of the matrix be saved?
Is there a requirement about the version of JSE ?

#3
sania21

sania21

    Newbie

  • Members
  • PipPip
  • 29 posts
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.


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

}

#4
oubless

oubless

    Newbie

  • Members
  • PipPip
  • 22 posts
Does this algorithm work if matrix is not a square?
I, currently working on implementation for square matrix

#5
sania21

sania21

    Newbie

  • Members
  • PipPip
  • 29 posts
No it does not work if matrix is not a square matrix.

It works only on square matrix.:)

I want to implement it only on square matrix that can be of any dimension may be 50 by 50 but my coding should be limited.

#6
oubless

oubless

    Newbie

  • Members
  • PipPip
  • 22 posts

sania21 said:

No it does not work if matrix is not a square matrix.

It works only on square matrix.:)

I want to implement it only on square matrix that can be of any dimension may be 50 by 50 but my coding should be limited.

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 = startRow; 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 = startRow; 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);
		}
	}
}

try this one ... I hope it is what you want :)
Write if something is not ok

#7
sania21

sania21

    Newbie

  • Members
  • PipPip
  • 29 posts
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);




}



}

#8
oubless

oubless

    Newbie

  • Members
  • PipPip
  • 22 posts
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:
		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 !?:
for(i=0;i<1;i++)
		{
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:
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 ...:
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);
		}
	}
}



#9
sania21

sania21

    Newbie

  • Members
  • PipPip
  • 29 posts
well!!
Thank you very much for your advice i will try to implement these points in my any of the applications.:)

#10
sania21

sania21

    Newbie

  • Members
  • PipPip
  • 29 posts
Hi!!

i have been working on the code given by you but,the loop startrow is moving downwards by adding 1,now i am trying to work on upper rows but i am unable to decrement the rowcount bcoz initialy the value of startrow is set to 0,so it is not getting decremented any solution for this?
Please let me know,or just give me some hint so that i could proceed.:)

#11
oubless

oubless

    Newbie

  • Members
  • PipPip
  • 22 posts

sania21 said:

Hi!!

i have been working on the code given by you but,the loop startrow is moving downwards by adding 1,now i am trying to work on upper rows but i am unable to decrement the rowcount bcoz initialy the value of startrow is set to 0,so it is not getting decremented any solution for this?
Please let me know,or just give me some hint so that i could proceed.:)
Try starting from the last row and decrement its value...
this:
for(int startRow = 0; startRow < matrix.length; startRow++)
to this:
//note it starts from the last (matrix.length-1 not matrix.length)
for(int startRow = matrix.length-1 ; startRow >= 0 ; startRow--)
BTW are sure you want to change startRow and not currentRow values or both?

#12
sania21

sania21

    Newbie

  • Members
  • PipPip
  • 29 posts
Hi!!
i was sure i had to start from startrow only not the current row,bcoz first i did
page work then i was trying it,but finally i got my program of inverse using pivot method :D that is generalised,i have tried it on many matrix of different dimension its giving the inverse,but one condition is the input should be a valid matrix,what i mean by this is whose inverse exist.
Anyways thanks for your kinda help :)