我正在尝试使用伴随方法来确定矩阵的逆矩阵,即(首先计算矩阵的余因子,然后转置这个矩阵,最后将其乘以(1/行列式)作为行列式的值的逆数)我有确定转置和行列式的方法,但是我正在努力处理余因子矩阵-
显示如何通过手动http://www.mathwords.com/i/inverse_of_a_matrix.htm使用伴随方法确定逆数的链接显示如何通过手动http://www.mathwords.com/c/cofactor_matrix.htm计算余因>
我有一个计算行列式和转置的方法,但是我不能让cofactor方法给出我需要的输出。
输出示例为=> 24 5 -4
24 5 -4
24 5 -4但是第二行和第三行应该是不同的,有什么建议吗?谢谢!下面是我使用的=>方法,checkIfSquare和assigningSign方法也工作得很好
public static int[][] cofactor(int[][] matrix) {
int[][] cofactorMatrix = new int[matrix.length][matrix.length];
for (int j = 0; j < matrix.length; j++) {
if (checkIfSquare(matrix)) {
for (int location = 0; location < matrix.length; location++) {
int reducedMatrix[][] = new int[matrix.length - 1][matrix.length - 1];
for (int rows = 1; rows < matrix.length; rows++) {
for (int cols = 0; cols < matrix.length; cols++) {
if (cols > location) {
reducedMatrix[rows - 1][cols - 1] = matrix[rows][cols];
} else if (cols < location) {
reducedMatrix[rows - 1][cols] = matrix[rows][cols];
}
}
}
int sign = assigningSign(location);
cofactorMatrix[j][location] = determinantCalc(reducedMatrix) * sign;
}
}
}
return cofactorMatrix;
}
发布于 2013-02-13 17:45:02
在计算reducedMatrix
时,您总是跳过第一行。这适用于余因子矩阵的第一行,但对于下面的行,您需要跳过第j行。这就是为什么您的输出由重复的第一行组成。
顺便说一句,这是一种可怕的计算矩阵逆的方法。我认为这只是一个学术练习,并不是用在严肃的程序中。
发布于 2013-02-13 17:41:27
没有必要重新发明轮子。Jama是一个写得很好的包。Maven存储库
<dependency>
<groupId>Jama</groupId>
<artifactId>Jama</artifactId>
<version>1.0.2</version>
</dependency>
<repository>
<id>ebi-repo</id>
<name>The EBI internal repository</name>
<url>http://www.ebi.ac.uk/~maven/m2repo</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
或者,可以在http://math.nist.gov/javanumerics/jama/#Package上找到jar文件
https://stackoverflow.com/questions/14850173
复制相似问题