在下面列出的代码中,我能够正确地找到两个矩阵的和,乘法和转置。我不确定如何找到辅助因子和决定因子沿着我对其他矩阵的相同类型的设置。任何帮助,将不胜感激。谢谢!
public class MatrixMult {
public MatrixMult(int first[][], int second[][], int m, int n, int p, int q) {
doMatrixMultiply(first, second, m, n, p, q);
}
public void doMatrixMultiply(int first[][], int second[][], int m, int n,
int p, int q) {
if (n != p)
System.out
.println("Matrices with entered orders can't be multiplied with each other.");
else {
int multiply[][] = new int[m][q];
int addition[][] = new int[m][q];
int transpose[][] = new int[m][q];
int transpose2[][] = new int[m][q];
int cofactor[][] = new int[m][q];
int mult = 0;
int sum = 0;
int tran = 0;
int co = 0;
for (int c = 0; c < m; c++) {
for (int d = 0; d < q; d++) {
for (int k = 0; k < p; k++) {
mult = mult + first[c][k] * second[k][d];
}
multiply[c][d] = mult;
mult = 0;
}
}
System.out.println("Product of entered matrices:-");
for (int c = 0; c < m; c++) {
for (int d = 0; d < q; d++)
System.out.print(multiply[c][d] + "\t");
System.out.print("\n");
}
for (int c = 0; c < m; c++) {
for (int d = 0; d < q; d++) {
for (int k = 0; k < p; k++) {
sum = first[c][d] + second[c][d];
}
addition[c][d] = sum;
sum = 0;
}
}
System.out.println("Sum of entered matrices:-");
for (int c = 0; c < m; c++) {
for (int d = 0; d < q; d++)
System.out.print(addition[c][d] + "\t");
System.out.print("\n");
}
int c;
int d;
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
transpose[d][c] = first[c][d];
}
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
transpose2[d][c] = second[c][d];
}
System.out.println("Transpose of first entered matrix:-");
for (c = 0; c < n; c++) {
for (d = 0; d < m; d++)
System.out.print(transpose[c][d] + "\t");
System.out.print("\n");
}
System.out.println("Transpose of second entered matrix:-");
for (c = 0; c < n; c++) {
for (d = 0; d < m; d++)
System.out.print(transpose2[c][d] + "\t");
System.out.print("\n");
}
}
}
}
答案 0 :(得分:1)
以下是使用链接Determining Cofactor Matrix in Java中的代码使用您的结构进行矩阵represantation的行列式的实现:
public int determinant(int[][] result, int rows, int cols) {
if (rows == 2)
return result[0][0] * result[1][1] - result[0][1] * result[1][0];
int determinant1 = 0, determinant2 = 0;
for (int i = 0; i < rows; i++) {
int temp = 1, temp2 = 1;
for (int j = 0; j < cols; j++) {
temp *= result[(i + j) % cols][j];
temp2 *= result[(i + j) % cols][rows - 1 - j];
}
determinant1 += temp;
determinant2 += temp2;
}
return determinant1 - determinant2;
}
并使用提供的链接中的代码计算辅助因子:
public int[][] cofactor(int[][] matrix, int rows, int cols) {
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = (int) (Math.pow(-1, i + j) * determinant(
removeRowCol(matrix, rows, cols, i, j), rows - 1,
cols - 1));
}
}
return result;
}
public int[][] removeRowCol(int[][] matrix, int rows, int cols,
int row, int col) {
int[][] result = new int[rows - 1][cols - 1];
int k = 0, l = 0;
for (int i = 0; i < rows; i++) {
if (i == row)
continue;
for (int j = 0; j < cols; j++) {
if (j == col)
continue;
result[l][k] = matrix[i][j];
k = (k + 1) % (rows - 1);
if (k == 0)
l++;
}
}
return result;
}
答案 1 :(得分:0)
简单的Google搜索会找到很多例子。例如。 http://mrbool.com/how-to-use-java-for-performing-matrix-operations/26800