我有一个3x3矩阵程序,我想找到矩阵的逆矩阵。说明书说C是B的倒数;即,C * B = B * C = 1,其中1是单位矩阵。我知道这可能很简单,但我只需要一些帮助或指导
以下是代码:
import java.util.Scanner;
import java.lang.*;
import java.io.*;
import java.text.*;
import java.lang.Math.*;
import java.text.DecimalFormat;
import java.lang.String;
public class Program9
{
static Scanner scan= new Scanner(System.in);
public static void main(String[] args)
{
Scanner scan= new Scanner(System.in);
int[][]A=new int[3][3];
int[][]B=new int[3][3];
int[][]C=new int[3][3];
int i, j, num;
do
{
System.out.println("--------------------------------------");
System.out.println("- MAIN MENU -");
System.out.println("- 1. INPUT FOR MATRIX A & B -");
System.out.println("- 2. REPLACE MATRIX A=C -");
System.out.println("- 3. ADD MATRIX A+B=C -");
System.out.println("- 4. SUBTRACT MATRIX A+B=C -");
System.out.println("- 5. MULTIPLY 3 TO MATRIX A -");
System.out.println("- 6. MULTIPLY MATRIX A*B -");
System.out.println("- 7. REPLACE MATRIX C=0 -");
System.out.println("- 8. REPLACE MATRIX C=1 -");
System.out.println("- 9. IDENTITY MATRIX -");
System.out.println("- 10. TRANSPOSE MATRIX A -");
System.out.println("- 11. QUIT -");
System.out.println("--------------------------------------");
System.out.println();
System.out.print("Please enter a number: ");
num= scan.nextInt();
switch(num)
{
case 1: input(A, B);
break;
case 2: replaceA (A);
break;
case 3: add(A, B);
break;
case 4: subtract(A, B);
break;
case 5: multiply3(A);
break;
case 6: multiplyAB(A,B);
break;
case 7: replace0();
break;
case 8: replace1();
break;
case 9: identity();
break;
case 10: trans(A);
break;
case 11: inverse(B);
break;
case 12: System.out.println("Hope you enjoyed the program");
break;
default: System.out.println("Please enter a number");
break;
}
}
while(num !=11);
}
public static void input(int[][] a, int[][] b)
{
int i;
int j;
System.out.print("Enter numbers into the matrix for matrix A: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=scan.nextInt();
System.out.println();
System.out.print("Enter numbers into the matrix for matrix B: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=scan.nextInt();
System.out.printf("%15s%n", "Matrix A");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", a[i][j]);
}
System.out.println();
}
System.out.printf("%15s%n", "Matrix B");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", b[i][j]);
}
System.out.println();
}
}
public static void replaceA(int[][] a)
{
int[][] C= new int[3][3];
int i, j;
System.out.printf("%15s%n", "Matrix C");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
C[i][j]=a[i][j];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", C[i][j]);
}
System.out.println();
}
}
public static void add(int[][] a, int[][] b)
{
int[][] C= new int[3][3];
int i, j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
C[i][j]=a[i][j] + b[i][j];
System.out.printf("%15s%n", "Matrix A");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", a[i][j]);
}
System.out.println();
}
System.out.printf("%15s%n", "Matrix B");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", b[i][j]);
}
System.out.println();
}
System.out.printf("%15s%n", "Matrix C");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", C[i][j]);
}
System.out.println();
}
}
public static void subtract(int[][] a, int[][] b)
{
int[][] C= new int[3][3];
int i, j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
C[i][j]=a[i][j] - b[i][j];
System.out.printf("%15s%n", "Matrix A");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", a[i][j]);
}
System.out.println();
}
System.out.printf("%15s%n", "Matrix B");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", b[i][j]);
}
System.out.println();
}
System.out.printf("%15s%n", "Matrix C");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", C[i][j]);
}
System.out.println();
}
}
public static void multiply3(int[][] a)
{
int[][] C= new int[3][3];
int i, j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
C[i][j]=a[i][j] * 3;
System.out.printf("%15s%n", "Matrix A");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", a[i][j]);
}
System.out.println();
}
System.out.printf("%15s%n", "Matrix A times 3");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", C[i][j]);
}
System.out.println();
}
}
public static void multiplyAB(int[][] a, int[][] b)
{
int[][] C= new int[3][3];
int i, j;
for(i=0;i<3;i++)
C[i][0] = (a[0][0]*b[0][i]) + (a[i][1]*b[1][0]) + (a[i][2]*b[2][0]);
for(i=0;i<3;i++)
C[i][1] = (a[1][0]*b[0][i]) + (a[1][1]*b[1][i]) + (a[1][2]*b[2][i]);
for(i=0;i<3;i++)
C[i][0] = (a[2][0]*b[0][i]) + (a[2][1]*b[1][i]) + (a[2][2]*b[2][i]);
System.out.printf("%15s%n", "Matrix A");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", a[i][j]);
}
System.out.println();
}
System.out.printf("%15s%n", "Matrix B");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", b[i][j]);
}
System.out.println();
}
System.out.printf("%15s%n", "Matrix A*B");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", C[i][j]);
}
System.out.println();
}
}
public static void replace0()
{
int[][] C= new int[3][3];
int i, j;
System.out.printf("%15s%n", "Matrix C");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
C[i][j]=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", C[i][j]);
}
System.out.println();
}
}
public static void replace1()
{
int[][] C= new int[3][3];
int i, j;
System.out.printf("%15s%n", "Matrix C");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
C[i][j]=1;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", C[i][j]);
}
System.out.println();
}
}
public static void identity()
{
int[][] C = new int[3][3];
int i, j;
System.out.printf("%15s%n", "Matrix C");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
if(i == 0 && j == 0)
C[0][0]=1;
else if(i == 1 && j == 1)
C[1][1]=1;
else if(i == 2 && j == 2)
C[2][2]=1;
else
C[i][j]=0;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.printf("%5d", C[i][j]);
}
System.out.println();
}
}
public static void trans(int[][] a)
{
int[][] C= new int[3][3];
int i,j;
int temp=0;
for(i=0;i<3;i++)
{
temp= a[i][0];
a[i][0] = a[0][i];
a[0][i]=temp;
}
for(i=1;i<=2;i++)
{
temp=a[i][1];
a[i][1]= a[1][i];
a[1][i]= temp;
}
System.out.printf("%15s%n", "Matrix C");
for(i=0; i<3; i++)
for(j=0; j<3; j++)
C[i][j]= a[i][j];
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
System.out.printf("%5d", C[i][j]);
}
System.out.println();
}
}
public static void inverse(int[][] b)
{
int[][] C= new int[3][3];
int i, j;
}
}
答案 0 :(得分:0)
只需一行即可。
System.out.println(new LUDecompositionImpl(
MatrixUtils.createRealMatrix(new double[][] { { 1, 2 }, { 3, 4 } })
).getSolver().getInverse());