gpt4 book ai didi

java - 求 3x3 矩阵的辅因子和行列式

转载 作者:行者123 更新时间:2023-11-30 08:15:06 25 4
gpt4 key购买 nike

在下面列出的代码中,我能够正确找到两个矩阵的总和、乘法和转置。我不确定如何找到与其他矩阵相同类型的设置的辅因子和行列式。任何帮助,将不胜感激。谢谢!!

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

最佳答案

以下是使用链接Determining Cofactor Matrix in Java中的代码使用您的结构进行矩阵表示的行列式的实现:

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;
}

关于java - 求 3x3 矩阵的辅因子和行列式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29775646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com