import java.io.*;
/*
/**矩阵运算
*/
class MatrixMult
{
public double abs_double(double x) //求绝对值
{
if (x < 0)
x = 0 - x;
return x;
}
public void matrixMult(double [] A, double [] B, double [] C, int m, int r, int n)
{
int i, j, k;
double [] temp = new double[m * n]; //A * B 返回 C
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++)
{
temp[ (i - 1) * n + j - 1] = 0;
for (k = 1; k <= r; k++)
temp[ (i - 1) * n + j - 1] += A[ (i - 1) * r + k -
1] * B[ (k - 1) * n + j - 1];
}
for (i = 1; i <= m * n; i++)
C[i - 1] = temp[i - 1];
}
public void interChange(double[] x, int n, double[] y) // 求矩阵X转置返回Y
{
int i, j;
double[] temp = new double[n * n];
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
temp[ (i - 1) * n + j - 1] = x[ (j - 1) * n + i - 1];
for (i = 1; i <= n * n; i++)
y[i - 1] = temp[i - 1];
}
public double matrixDat(double[] A, int n)//求A 行列式,返回double 值
{
double det = 0;
if (n == 1) {
det = A[n - 1];
}
else if (n == 2) {
det = A[0] * A[3] - A[1] * A[2];
}
else if (n > 2)
{
int i, j, k, m;
double[] temp = new double[ (n - 1) * (n - 1)];
for (k = 1; k <= n; k++)
{
m = 1;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
if (i != 1 && j != k)
{
temp[m - 1] = A[ (i - 1) * n + j - 1];
m++;
}
}
if (k % 2 == 1)
det += A[k - 1] * matrixDat(temp, n - 1);
else
det -= A[k - 1] * matrixDat(temp, n - 1);
}
}
return det;
}
/*
/**
*/
public double matrixAlgCofactor(double[] A, int n, int k) //求代数行列式
{
int i, j, m, ai, aj;
double ac;
double[] temp = new double[ (n - 1) * (n - 1)];
ai = (k - 1) / n + 1;
aj = (k - 1) % n + 1;
m = 1;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
if (i != ai && j != aj)
{
temp[m - 1] = A[ (i - 1) * n + j - 1];
m++;
}
}
ac = matrixDat(temp, n - 1);
if ( (ai + aj) % 2 == 0) {
return ac;
}
else {
return 0 - ac;
}
}
/*
/**
*/
public double matrixInverse(double [] A, double [] B, int n)
{
double det = matrixDat(A, n);
if (det != 0)
{
int i;
double [] temp = new double[n * n];
for (i = 1; i <= n * n; i++)
temp[i - 1] = matrixAlgCofactor(A, n, i) / det;
for (i = 1; i <= n * n; i++)
B[i - 1] = temp[i - 1];
interChange(B, n, B);
return 1;
}
else {
return -1;
}
}
/*
/**
*
* @param A
* @param B
* @param m
* @param n
* @param k
*/
public void matrixNumMult(double[] A, double[] B, int m, int n, int k)//B = k* A,返回B
{
int i;
for (i = 1; i <= m * n; i++)
B[i - 1] = k * A[i - 1];
}
/*
/**
*/
public void matrixAdd(double [] A, double [] B, double [] C, int m, int n,
int k, int l) //返回C = k * A + l * B
{
int i, j;
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++)
C[ (i - 1) * n + j - 1] = k * A[ (i - 1) * n + j - 1] +
l * B[ (i - 1) * n + j - 1];
}
/*
/**
*/
public void matrixInit(double [] A,int m,int n) // 矩阵A 初始化
{
for(int i = 0; i < m; i++)
{
for(int j = 0;j < n; j++)
{
System.out.println("Please input the element in row "+(i+1)+" and line "+(j+1)+":");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
A[i*n+j] = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
}
}
System.out.println("The matrix is:");
matrixOutput(A, m, n);
}
public void matrixOutput(double [] A,int m,int n) //输出A
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print(A[i*n+j]+" ");
}
System.out.print("/n");
}
}
}
public class Frame1
{
public static void main(String [] args)
{
int sw = 0;
int row = 0;
int line = 0;
double [] array1 = new double[100];
double [] array2 = new double[100];
double [] array3 = new double[100]; //
MatrixMult A = new MatrixMult();
System.out.println("您可以做以下操作:");
System.out.println("求矩阵的转置,按 1");
System.out.println("如果您想求矩阵的乘积,按 2");
System.out.println("如果您想求矩阵的行列式,请按3");
System.out.println("如果您想求矩阵的线形运算,请按4");
System.out.println("如果您想求矩阵的逆,请按5");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
sw = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
switch(sw)
{
case 1:
{
System.out.println("请输入A 的行数:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
row = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
System.out.println("请输入A 的列数:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
line = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
A.matrixInit(array1, row, line);
System.out.println("请输入B 的列数:");
A.interChange(array1,row,array2);
A.matrixOutput(array2,row,line);
break;
}
case 2:
{
int row1 = 0;
int line1 = 0;
int row2 = 0;
int line2 = 0;
System.out.println("请输入A 的行数:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
row1 = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
System.out.println("请输入A 的列数:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
line1 = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
System.out.println("请输入B 的列数:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
line2 = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
/*System.out.println("The inition of the matrix is completed!");*/
A.matrixInit(array1,row1,line1);
A.matrixInit(array2,line1,line2);
A.matrixMult(array1, array2, array3, row1, line1, line2);
System.out.println("A 与 B 的乘积:");
A.matrixOutput(array3,row1,line2);
break;
}
case 3:
{
System.out.println("请输入A 的行数:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
row = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
A.matrixInit(array1, row, row);
System.out.println("A 的行列式为:"+ A.matrixDat(array1, row));
break;
}
case 4:
{
int k = 0;
int l = 0;
System.out.println("请输入矩阵的行数:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
row = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
System.out.println("请输入矩阵的列数:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
line = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
System.out.println("请输入A 前系数:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
k = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
System.out.println("请输入B 前系数:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
l = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
A.matrixInit(array1,row,line);
A.matrixInit(array2,row,line);
A.matrixAdd(array1, array2, array3, row, line, k, l);
System.out.println("k * A + l * B 的值为:");
A.matrixOutput(array3,row,line);
break;
}
case 5:
{
System.out.println("请输入矩阵的行数(列)数:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.
in));
row = Integer.parseInt(br.readLine());
}
catch (Exception e) {}
A.matrixInit(array1, row, row);
A.matrixInverse(array1,array2,row);
System.out.println("矩阵的逆为:");
A.matrixOutput(array2,row,row);
}
default :System.out.println("over!");
}
}
}