用Java确定辅因子矩阵 [英] Determining Cofactor Matrix in Java

查看:116
本文介绍了用Java确定辅因子矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定辅因子矩阵.我的代码正确生成了所有辅助因子.但是,在某些情况下,结果矩阵将旋转90度(很好,cols/rows是切换的).

I'm trying to determine a cofactor matrix. My code is correctly generating all the cofactors; however, in some cases, the resulting matrix is rotated by 90 degrees (well, the cols/rows are switched).

例如,矩阵:

For example, the matrix: {{8, 5, 1}, {3, 6, 7}, {5, 6, 6}} produced the correct result.

输出>

a
8 3 5 
5 6 6 
1 7 6 

a
-6 17 -12 
-24 43 -23 
29 -53 33 

但是,矩阵:

however, the matrix: {{1, 0, 5}, {9, 3, 0}, {0, 9, 3}} switches rows and columns.

输出>

b
1 0 5 
9 3 0 
0 9 3 

b
9 45 -15 
-27 3 45 
81 -9 3 

正确的结果是:

9 -27 81
45 3 -9
-15 45 3

矩阵因此被存储:

Matrix:
    int matrix[][]
    int rows
    int cols

行和列确实是必要的,但比每次尝试确定要使用的值多于使用matrix.length更好.

rows and cols are really necessary, but it's nicer than using matrix.length every time I'm trying to determine how many values I'm working with.

以下是生成这些矩阵的代码:

here is the code that generates these matrices:

public Matrix cofactor() {
    Matrix result = new Matrix(this.rows, this.cols);
    for (int i = 0; i < result.rows; i++) {
        for (int j = 0; j < result.cols; j++) {
            result.matrix[j][i] = (int)(Math.pow(-1, i + j) * removeRowCol(i, j).determinant());
        }
    }

    return result;
}

public Matrix removeRowCol(int row, int col) {
    Matrix result = new Matrix(this.rows - 1, this.cols - 1);

    int k = 0, l = 0;
    for (int i = 0; i < this.rows; i++) {
        if (i == row) continue;
        for (int j = 0; j < this.cols; j++) {
            if (j == col) continue;
            result.matrix[l][k] = this.matrix[i][j];

            k = (k + 1) % (this.rows - 1);
            if (k == 0) l++;
        }
    }

    return result;
}

行列式部分现在有点难受,但它适用于3x3和2x2矩阵.

the determinant part is a bit of a hack, now, but it works for 3x3 and 2x2 matrices.

public int determinant() {
    if (this.rows == 2) return this.matrix[0][0] * this.matrix[1][1] - this.matrix[0][1] * this.matrix[1][0];

    int determinant1 = 0, determinant2 = 0;
    for (int i = 0; i < this.rows; i++) {
        int temp = 1, temp2 = 1;
        for (int j = 0; j < this.cols; j++) {
            temp *= this.matrix[(i + j) % this.cols][j];
            temp2 *= this.matrix[(i + j) % this.cols][this.rows - 1 - j];
        }

        determinant1 += temp; 
        determinant2 += temp2;
    }

    return determinant1 - determinant2;
}

无论如何,我一直试图找出为什么只有某些矩阵被旋转"的原因.

Anyways, I'm stuck trying to figure out why only certain matrices are "rotated."

推荐答案

坦白说,我有点白痴.

Frankly, I'm a bit of an idiot.

我用于测试的矩阵是{{8, 3, 5}, {5, 6, 6}, {1, 7, 6}},但是我在Wolfram上检查的值是{{8, 5, 1}, {3, 6, 7}, {5, 6, 6}} ...

The matrix I'm using for testing is {{8, 3, 5}, {5, 6, 6}, {1, 7, 6}} but the value I'm checking on Wolfram is {{8, 5, 1}, {3, 6, 7}, {5, 6, 6}}...

这篇关于用Java确定辅因子矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆