通过java实现矩阵的乘法、行列式求值、伴随矩阵和逆矩阵的运算
在考研备考的过程中复习到线性代数时,发现行列式的求法多种多样,并且矩阵的变换也五花八门,有时候偶然的一个思路或许能发现知识点直接的联系,但是如果每次都要为了验证自己的想法,并寻找规律时,花大量时间在行列式的计算上未免太过浪费,于是尝试写一些简陋的程序来减缓计算压力,同时练习自己的逻辑思维能力。
最终代码如下:
// 矩阵乘法
public static int[][] matrixmultiply(int[][] matrix1, int[][] matrix2) {
int m = matrix1.length;
int n1 = matrix1[0].length;
int n2 = matrix2.length;
if (n1 != n2) {
return null;
}
int s = matrix2[0].length;
int[][] result = new int[m][s];
int x = 0;
int y = 0;
for (x = 0; x < m; x++) {
for (y = 0; y < s; y++) {
int plus = 0;
for (int count1 = 0; count1 < n1; count1++) {
plus += matrix1[x][count1] * matrix2[count1][y];
}
result[x][y] = plus;
}
}
return result;
}
// 求逆矩阵
public static float[][] InverseMatrix(int[][] data) {
// 求矩阵的行列式值
int det = Determinantcount(data);
// 求伴随矩阵
int[][] adjoin = Adjointmatrix(data);
float[][] result = new float[data.length][data[0].length];
// 求逆
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
result[i][j] = ((float) adjoin[i][j] / (float) det);
}
}
return result;
}
// 伴随矩阵方法
public static int[][] Adjointmatrix(int[][] data) {
if (data.length != data[0].length) {
return null;
}
if (data.length == 1) {
return new int[][] { { 1 } };
}
int n = data.length;
int[][] result = new int[n][n];
// 分别求出data中aij的代数余子式值赋予result中的bji
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// 求出aij的余子式并求值
int cofactorDet = Determinantcount(Cofactor(data, i, j));
if ((i + j) % 2 == 1) {
cofactorDet *= -1;
}
// 赋值bji
result[j][i] = cofactorDet;
}
}
return result;
}
// 求余子式的方法
public static int[][] Cofactor(int[][] data, int i, int j) {
int m = data.length;
int n = data[0].length;
if (m < i || n < j) {
System.out.println("越界");
return null;
}
int[][] result = new int[m - 1][n - 1];
for (int row = 0; row < m; row++) {
if (row == i) {
// 不做操作
} else if (row < i) {
for (int colu = 0; colu < n; colu++) {
if (colu == j) {
// 不做操作
} else if (colu < j) {
result[row][colu] = data[row][colu];
} else if (colu > j) {
result[row][colu - 1] = data[row][colu];
}
}
} else if (row > i) {
for (int colu = 0; colu < n; colu++) {
if (colu == j) {
// 不做操作
} else if (colu < j) {
result[row - 1][colu] = data[row][colu];
} else if (colu > j) {
result[row - 1][colu - 1] = data[row][colu];
}
}
}
}
return result;
}
// 矩阵行列式计算
public static int Determinantcount(int[][] data) {
int rowcount = data.length;
int columncount = data[0].length;
if (rowcount != columncount) {
return -1;
}
if (rowcount == 1) {
return data[0][0];
}
// 创建用于容纳余子式的容器
int[][] tempMatrix = new int[rowcount - 1][columncount - 1];
int plus = 0;
// 确认传入的行列式大于等于2阶,按第一行展开
for (int i = 0; i < columncount; i++) {
int a = data[0][i];
// 通过递归求第一行第i列的余子式的行列式值
for (int j = 0; j < columncount; j++) {
// 当i=j时
if (i == j) {
}
// 当i < j的情况
else if (j < i) {
for (int c = 0; c < rowcount - 1; c++) {
tempMatrix[c][j] = data[c + 1][j];
}
} else if (j > i) {
for (int c = 0; c < rowcount - 1; c++) {
tempMatrix[c][j - 1] = data[c + 1][j];
}
}
}
// 求代数余子式的-1项
if (i % 2 == 1) {
a *= -1;
}
int re = Determinantcount(tempMatrix);
// 累加展开项与代数余子式的乘积
plus += a * re;
}
return plus;
}
由于练习题目中给出的大多是整数型矩阵,在矩阵乘法、求伴随矩阵和行列式值的方法中用的是整数型,只有求逆矩阵的运算用到了浮点型变量,涉及到的输入异常和越界异常也没怎么处理。