In: Computer Science
im trying to write a java code that take a matrix of vector and fine it's inverse (the the inverse in linear algebra) then multiple this matrix with a vector to fine other vector
(matrix)-1 × ( vector ) = (vector)
Hope this helps
import java.util.Scanner;
class newclass
{
public static void main(String argv[])
{
Scanner sc = new Scanner(System.in);
System.out.println("dimension of square matrix: ");
int n = sc.nextInt();
double a[][]= new double[n][n];
System.out.println("elements of matrix: ");
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
a[i][j] = sc.nextDouble();
double d[][] = invert(a);
System.out.println("Inverse of matrix is: ");
for (int i=0; i<n; ++i)
{
for (int j=0; j<n; ++j)
{
System.out.print(d[i][j]+" ");
}
System.out.println();
}
double res[][] = new double[n][1] ;
double m[][] = new double[n][1] ;
System.out.println("matrix to multiply of deimension "+
n+"*1");
for(int i=0;i<n;i++)
{
for(int j=0;j<1;j++)
{
m[i][j]=sc.nextDouble();
}
}
double sum=0;
int ma,na,qa,pa;
ma=n;
na=n;
pa=n;
qa=1;
for (int c = 0; c < ma; c++)
{
for (int da = 0; da < qa; da++)
{
for (int k = 0; k < pa; k++)
{
sum = sum + d[c][k]*m[k][da];
}
res[c][da] = sum;
sum = 0;
}
}
System.out.println("mutliplication of matrix with given vector
:");
for (int c = 0; c < n; c++)
{
for (int q = 0; q < 1; q++)
System.out.print(res[c][q]+"\t");
System.out.print("\n");
}
}
public static double[][] invert(double a[][])
{
int n = a.length;
double x[][] = new double[n][n];
double b[][] = new double[n][n];
int index[] = new int[n];
for (int i=0; i<n; ++i)
b[i][i] = 1;
gaussian(a, index);
for (int i=0; i<n-1; ++i)
for (int j=i+1; j<n; ++j)
for (int k=0; k<n; ++k)
b[index[j]][k]
-= a[index[j]][i]*b[index[i]][k];
for (int i=0; i<n; ++i)
{
x[n-1][i] = b[index[n-1]][i]/a[index[n-1]][n-1];
for (int j=n-2; j>=0; --j)
{
x[j][i] = b[index[j]][i];
for (int k=j+1; k<n; ++k)
{
x[j][i] -= a[index[j]][k]*x[k][i];
}
x[j][i] /= a[index[j]][j];
}
}
return x;
}
public static void gaussian(double a[][], int index[])
{
int n = index.length;
double c[] = new double[n];
for (int i=0; i<n; ++i)
index[i] = i;
for (int i=0; i<n; ++i)
{
double c1 = 0;
for (int j=0; j<n; ++j)
{
double c0 = Math.abs(a[i][j]);
if (c0 > c1) c1 = c0;
}
c[i] = c1;
}
int k = 0;
for (int j=0; j<n-1; ++j)
{
double pi1 = 0;
for (int i=j; i<n; ++i)
{
double pi0 = Math.abs(a[index[i]][j]);
pi0 /= c[index[i]];
if (pi0 > pi1)
{
pi1 = pi0;
k = i;
}
}
int itmp = index[j];
index[j] = index[k];
index[k] = itmp;
for (int i=j+1; i<n; ++i)
{
double pj = a[index[i]][j]/a[index[j]][j];
a[index[i]][j] = pj;
for (int l=j+1; l<n; ++l)
a[index[i]][l] -= pj*a[index[j]][l];
}
}
}
}