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("
");
}
}
}
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!");
}
}
}