import java.util.*;
public class determinantSolve {
public static void main(String[] args) {
determinant function=new determinant();
System.out.print("enter the order :: ");
function.order=function.take.nextInt();
function.createMatrix();
function.printMatrix();
function.newDetCreate();
IO.waitPlease();
}
}
class determinant
{
public Scanner take=new Scanner(System.in);
public float[][] matrix,SecondOrderDet;
public int order;
public float val=1;
void initMatrix(int order1)
{
matrix=new float[order1][order1];
}
void initSecondOrderDet()
{
SecondOrderDet=new float[2][2];
}
void createMatrix()
{
initMatrix(order);
int i,j;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
matrix[i][j]=take.nextFloat();
}
}
}
void printMatrix()
{
int i,j;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
if(matrix[i][j]-(int)matrix[i][j]==0)
{
System.out.print((int)matrix[i][j]+"\t ");
}
else
{
System.out.print(matrix[i][j]+"\t ");
}
}
System.out.println("");
}
}
boolean cfOrderIsOne()
{
if(order==1)
{
return true;
}
else
{
return false;
}
}
void newDetCreate()
{
determinant recur=new determinant();
recur.order=order-1;
recur.initMatrix(recur.order);
int i,j;
if(matrix[0][0]==0)
{
int row=searchRowForNextNonZero();
int col=searchColForNextNonZero();
float swap;
if(row!=0)
{
val=val*(-1);
for(i=0;i<order;i++)
{
swap=matrix[0][i];
matrix[0][i]=matrix[row][i];
matrix[row][i]=swap;
}
}
if(col!=0)
{
val=val*(-1);
for(i=0;i<order;i++)
{
swap=matrix[i][0];
matrix[i][0]=matrix[i][col];
matrix[i][col]=swap;
}
}
}
for(i=0;i<recur.order;i++)
{
for(j=0;j<recur.order;j++)
{
recur.matrix[i][j]=SecondOrderSolve(matrix[0][0],matrix[0][j+1],matrix[i+1][0],matrix[i+1][j+1]);
}
}
recur.val=(float) Math.pow(matrix[0][0],order-2);
recur.val=(1/recur.val);
recur.val=recur.val*val;
if(recur.cfOrderIsOne()==false)
{
recur.newDetCreate();
}
else
{
System.out.println(recur.matrix[0][0]*recur.val);
}
}
float SecondOrderSolve(float i0,float i1,float i2,float i3)
{
float res=0;
initSecondOrderDet();
SecondOrderDet[0][0]=i0;
SecondOrderDet[0][1]=i1;
SecondOrderDet[1][0]=i2;
SecondOrderDet[1][1]=i3;
res=(SecondOrderDet[0][0]*SecondOrderDet[1][1])-(SecondOrderDet[0][1]*SecondOrderDet[1][0]);
return res;
}
int searchRowForNextNonZero()
{
int i,j;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
if(matrix[i][j]!=0)
{
break;
}
}
if(matrix[i][j]!=0)
{
break;
}
}
return i;
}
int searchColForNextNonZero()
{
int i,j=0;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
if(matrix[i][j]!=0)
{
break;
}
}
if(matrix[i][j]!=0)
{
break;
}
}
return j;
}
}
public class determinantSolve {
public static void main(String[] args) {
determinant function=new determinant();
System.out.print("enter the order :: ");
function.order=function.take.nextInt();
function.createMatrix();
function.printMatrix();
function.newDetCreate();
IO.waitPlease();
}
}
class determinant
{
public Scanner take=new Scanner(System.in);
public float[][] matrix,SecondOrderDet;
public int order;
public float val=1;
void initMatrix(int order1)
{
matrix=new float[order1][order1];
}
void initSecondOrderDet()
{
SecondOrderDet=new float[2][2];
}
void createMatrix()
{
initMatrix(order);
int i,j;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
matrix[i][j]=take.nextFloat();
}
}
}
void printMatrix()
{
int i,j;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
if(matrix[i][j]-(int)matrix[i][j]==0)
{
System.out.print((int)matrix[i][j]+"\t ");
}
else
{
System.out.print(matrix[i][j]+"\t ");
}
}
System.out.println("");
}
}
boolean cfOrderIsOne()
{
if(order==1)
{
return true;
}
else
{
return false;
}
}
void newDetCreate()
{
determinant recur=new determinant();
recur.order=order-1;
recur.initMatrix(recur.order);
int i,j;
if(matrix[0][0]==0)
{
int row=searchRowForNextNonZero();
int col=searchColForNextNonZero();
float swap;
if(row!=0)
{
val=val*(-1);
for(i=0;i<order;i++)
{
swap=matrix[0][i];
matrix[0][i]=matrix[row][i];
matrix[row][i]=swap;
}
}
if(col!=0)
{
val=val*(-1);
for(i=0;i<order;i++)
{
swap=matrix[i][0];
matrix[i][0]=matrix[i][col];
matrix[i][col]=swap;
}
}
}
for(i=0;i<recur.order;i++)
{
for(j=0;j<recur.order;j++)
{
recur.matrix[i][j]=SecondOrderSolve(matrix[0][0],matrix[0][j+1],matrix[i+1][0],matrix[i+1][j+1]);
}
}
recur.val=(float) Math.pow(matrix[0][0],order-2);
recur.val=(1/recur.val);
recur.val=recur.val*val;
if(recur.cfOrderIsOne()==false)
{
recur.newDetCreate();
}
else
{
System.out.println(recur.matrix[0][0]*recur.val);
}
}
float SecondOrderSolve(float i0,float i1,float i2,float i3)
{
float res=0;
initSecondOrderDet();
SecondOrderDet[0][0]=i0;
SecondOrderDet[0][1]=i1;
SecondOrderDet[1][0]=i2;
SecondOrderDet[1][1]=i3;
res=(SecondOrderDet[0][0]*SecondOrderDet[1][1])-(SecondOrderDet[0][1]*SecondOrderDet[1][0]);
return res;
}
int searchRowForNextNonZero()
{
int i,j;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
if(matrix[i][j]!=0)
{
break;
}
}
if(matrix[i][j]!=0)
{
break;
}
}
return i;
}
int searchColForNextNonZero()
{
int i,j=0;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
if(matrix[i][j]!=0)
{
break;
}
}
if(matrix[i][j]!=0)
{
break;
}
}
return j;
}
}
0 Comments