矩阵类实现
java实现矩阵类,包括矩阵的创建,运算等多种算法
比较初级,适合小白,当时学淑芬的时候照着书本写的
代码实现
import java.util.Scanner;
public class Matrix {
int row , column;
double data[][];
//构造方法
Matrix ( int row , int column ) {
this.row = row;
this.column = column;
data = new double[row][column];
}
Matrix ( int row ) {
this.row = row;
this.column = row;
data = new double[row][row];
}
Matrix () {
this.row = 3;
this.column = 3;
data = new double[3][3];
}
//测试用的主函数
public static void main(String[] args) {
//Matrix s = new Matrix(3 , 1);
//double d = (double) 2 / 3;
Matrix a = new Matrix(3);
// Matrix b = new Matrix(4 , 1);
a.input(a);
// Matrix b1 = a.inverse(a);
// Matrix b = a.inverse2(a);
//Matrix b = new Matrix(4,2);
// b1.output(b1);
//b.input(b);
// Matrix b=a.transport(a);
Matrix c = a.inverse(a);
c = numMultiply(c, -1);
c.output(c);
Matrix b = new Matrix(3);
b.input(b);
c = Compute.mupltiply(c, b);
c.output(c);
//b.input(b);
//Matrix c = Compute.mupltiply(a, b);
//c.output(c);
//System.out.println(g);
//s.input(s);
//s.output(s);
//double d = s.vectorModule(s);
//System.out.println(d);
//s = s.vectorUnit(s);
//s.output(s);
//double d = s.vectorProduct(s);
//System.out.println(d);
//double t = s.countMatrix(s);
//System.out.print(t);
//s.numMultiply(s, -1);
//s.output(s);
//System.out.println("打印行列式计算结果!!!");
//System.out.println(s.countMatrix(s));
//s = s.inverse(s);
//s.output(s);
//s = s.exchangeLine(s, 1, 2);
//s.output(s);
//s = s.transport(s);
//s.output(s);
//s = s.cofactor(s, 2, 1);
//s.output(s);
}
//矩阵求逆运算方法
private Matrix inverse3(Matrix a) {
UnitMatrix I = new UnitMatrix(a.row);
Matrix b = a.extern(a , I);//拓展矩阵获得
int n = b.row;
double[] k = new double[n];
//选主元的高斯消元过程
for(int i = 0;i < n-1;i++) {
int temp = i;
for(int j=i+1;j<n;j++) {
if(Math.abs(b.data[j][i]) > Math.abs(b.data[temp][i]))
temp = j;
}
if(temp != i)
b = b.exchangeLine(b, i, temp);
b.output(b);
for(int j=i+1;j<n;j++) {
k[j] = b.data[j][i] / b.data[i][i];
}
for(int j=i+1;j<n;j++) {
for(int r = 0;r < b.column;r++) {
b.data[j][r] = b.data[j][r] - k[j] * b.data[i][r];
}
}
b.output(b);
if(b.data[i][i] != 1) {
double t = b.data[i][i];
for(int j = i;j < b.column;j++) {
b.data[i][j] = b.data[i][j] / t;
}
}
b.output(b);
}
//最后一行化为1
if(b.data[n-1][n-1] != 1) {
double t = b.data[n-1][n-1];
for(int j = n-1;j < b.column;j++) {
b.data[n-1][j] = b.data[n-1][j] / t;
}
}
//开始回代
for(int i = n-1;i >0;i--) {
for(int j = i-1;j >=0;j--) {
double t = b.data[j][i];
for(int r = 0;r < b.column;r++) {
b.data[j][r] = b.data[j][r] - t * b.data[i][r];
}
}
}
Matrix c = new Matrix(a.row);
for(int i = 0;i < a.row;i++) {
for(int j = 0;j < a.column;j++) {
c.data[i][j] = b.data[i][j + a.column];
}
}
b.output(b);
c.output(c);
return c;
}
//拓展 矩阵的计算方法
private Matrix extern(Matrix a, Matrix i) {
Matrix b = new Matrix(a.row , a.column + i.column);
int k = a.column + i.column;
for(int m = 0;m < a.row;m++) {
for(int n = 0; n < a.column;n++) {
b.data[m][n] = a.data[m][n];
}
for(int n = a.column; n < k;n++) {
b.data[m][n] = i.data[m][n-a.column];
}
}
return b;
}
//矩阵的求逆运算2解法
Matrix inverse2(Matrix a) {
Matrix b = a.copy(a);
Matrix I = new UnitMatrix(a.row);
Matrix c = I;
Matrix d = new Matrix(a.row);
int k = a.row;
for(int t = 0;t < k-1;t++) {
int tempt = t;
for(int i = t+1;i < k;i++)
if(Math.abs(b.data[tempt][t]) < Math.abs(b.data[i][t]))
tempt = i;
for(int j = t;j <k;j++) {
double temp = b.data[tempt][j];
b.data[tempt][j] = b.data[t][j];
b.data[t][j] = temp;
}
for(int i = 0;i < k;i++) {
double temp = c.data[t][i];
c.data[t][i] = c.data[tempt][i];
c.data[tempt][i] = temp;
}
for(int i = t+1;i < k;i++) {
double m = b.data[i][t] / b.data[t][t];
for(int j = t+1;j < k;j++)
b.data[i][j] = b.data[i][j] - m * b.data[t][j];
for(int j = 0;j < k;j++)
c.data[i][j] = c.data[i][j] - m * c.data[t][j];
}
}
for(int p = 0;p < k;p++) {
d.data[k-1][p] = c.data[k-1][p] / b.data[k-1][k-1];
for(int i = k-2;i>=0;i--) {
double temp = 0;
for(int j = i+1;j < k;j++ ) {
temp = temp + b.data[i][j] * d.data[j][p];
}
d.data[i][p] = (c.data[i][p] - temp) / b.data[i][i];
}
}
return d;
}
//矩阵求逆算法自己写的
//给矩阵赋值的输入函数
void input(Matrix a) {
Scanner s = new Scanner(System.in);
System.out.println("请对"+a.row+"行"+a.column+"列"+"的矩阵进行赋值");
for(int i = 0;i < a.row;i++) {
System.out.println("请输入第"+(i+1)+"行的元素...");
for(int j = 0;j < a.column;j++) {
a.data[i][j] = s.nextDouble();
}
}
//s.close();
System.out.println("输入完毕!");
}
//打印现有矩阵的输出函数
void output(Matrix a) {
System.out.println("打印"+a.row+"行"+a.column+"列"+"的矩阵如下");
for(int i = 0;i < a.row;i++) {
for(int j = 0;j < a.column;j++) {
System.out.printf("%4.1f ",a.data[i][j]);
}
System.out.println();
}
}
//矩阵的转置
Matrix transport( Matrix a ) {
Matrix b = new Matrix(a.column , a.row);
for(int i = 0;i < a.row;i++) {
for(int j = 0;j < a.column;j++) {
b.data[j][i] = a.data[i][j];
}
}
return b;
}
//计算n行n列的矩阵的行列式
double countMatrix( Matrix a ) {
if(a.row == a.column) {
int n = a.row;
if(n == 1)
return a.data[0][0];
else if(n == 2) {
return a.data[0][0] * a.data[1][1] - a.data[1][0] * a.data[0][1];
}
else {
double sum = 0;
Matrix[] b = new Matrix[n];
for(int i = 0;i < n;i++) {
b[i] = new Matrix(n - 1);
b[i] = a.cofactor(a, 1, i+1);
}
for(int i = 0;i < n;i++) {
sum = sum + Math.pow(-1, i) * b[i].countMatrix(b[i]) * a.data[0][i];
}
return sum;
}
}
else return 0;
}
//将矩阵中某两行的数值进行交换的函数
Matrix exchangeLine( Matrix a , int x , int y ) {
if(x == y) {
return a;
}
else if (x < a.row && y < a.row && x >= 0 && y >= 0) {
double[] s = new double[a.column];
for(int i = 0;i < a.column;i++) {
s[i] = a.data[x][i];
a.data[x][i] = a.data[y][i];
a.data[y][i] = s[i];
}
return a;
}
else return null;
}
//将矩阵中某两列的数值进行交换的函数
Matrix exchangeColumn( Matrix a , int x , int y ) {
if(x == y) {
return a;
}
else if (x <= a.column && y <= a.column && x > 0 && y > 0) {
double[] s = new double[a.row];
for(int i = 0;i < a.column;i++) {
s[i] = a.data[i][x];
a.data[i][x] = a.data[i][y];
a.data[i][y] = s[i];
}
return a;
}
else return null;
}
//计算行列式里某一个数的余子矩阵
Matrix cofactor( Matrix c , int i , int j ) {
Matrix a = copy(c);
if( i >= 0 && i <= a.row && j >= 0 && j <= a.column) {
Matrix b = new Matrix( a.row - 1 , a.column - 1);
for( int m = i - 1;m < a.row - 1;m++ ) {
for ( int n = 0;n < a.column;n++ ) {
a.data[m][n] = a.data[m+1][n];
}
}
for( int m = j - 1;m < a.column - 1;m++ ) {
for ( int n = 0;n < a.row;n++ ) {
a.data[n][m] = a.data[n][m+1];
}
}
for (int m = 0;m < a.row - 1;m++) {
for (int n = 0;n < a.column - 1;n++) {
b.data[m][n] = a.data[m][n];
}
}
return b;
}
else return null;
}
//复制一个矩阵的函数
Matrix copy( Matrix a ) {
Matrix b = new Matrix( a.row , a.column);
for(int i = 0;i < a.row;i++) {
for(int j = 0;j < a.column;j++) {
b.data[i][j] = a.data[i][j];
}
}
return b;
}
//矩阵数乘的函数
public static Matrix numMultiply( Matrix a , double k) {
for(int i = 0;i < a.row;i++) {
for(int j = 0;j < a.column;j++) {
a.data[i][j] = k * a.data[i][j];
}
}
return a;
}
//计算方阵n*n的时候的伴随矩阵的计算函数
Matrix companion( Matrix a ) {
if(a.row == a.column) {
int n = a.row;
Matrix c = copy(a);
Matrix b = new Matrix( n );
for( int i = 0;i < n;i++) {
for( int j = 0;j < n;j++) {
b.data[i][j] = Math.pow(-1, i+j) * countMatrix(cofactor(c, i+1, j+1));
}
}
return b;
}
else return null;
}
//计算矩阵的逆矩阵的函数
Matrix inverse( Matrix b ) {
Matrix a = copy( b );
if(b.row == 1) {
a.data[0][0] = 1 / a.data[0][0];
return a;
}
if(a.row == a.column) {
double k = 1 / countMatrix( a );
Matrix c = a.companion(a);
c = c.transport(c);
if( countMatrix( a ) != 0) {
a = numMultiply( c ,k);
return a;
}
else return null;
}
else
return null;
}
//返回增广矩阵的不包含解的那个方阵
Matrix returnPart( Matrix a ) {
Matrix b = new Matrix( a.row );
for( int i = 0;i < a.row;i++ ) {
for( int j = 0;j < a.row;j++ ) {
b.data[i][j] = a.data[i][j];
}
}
return b;
}
//返回增广矩阵的解向量
Matrix returnSolution( Matrix a ) {
Matrix b = new Matrix( a.row , 1 );
for( int i = 0;i < a.row;i++ ) {
b.data[i][0] = a.data[i][a.column - 1];
}
return b;
}
//列向量的内积公式函数
double vectorProduct(Matrix a) {
Matrix b = a.copy(a);
b.output(b);
Matrix c = transport(b);
Matrix d = Compute.mupltiply(c, b);
return d.data[0][0];
}
//计算列向量的模的公式
double vectorModule(Matrix a) {
Matrix b = a.copy(a);
Matrix c = Compute.mupltiply(b.transport(b), b);
double d = Math.sqrt(c.data[0][0]);
return d;
}
//列向量的取二范数单位化
Matrix vectorUnit(Matrix a) {
Matrix b = a.copy(a);
Matrix c = b.transport(b);
Matrix e = Compute.mupltiply(c , b);
double d = Math.sqrt(e.data[0][0]);
return numMultiply(b , 1.0 / d);
}
//创建一个单位矩阵
}
容易上手,适合小白,只是为了数学求算而写的代码。