矩阵在形式上和行列式是一样 的,这两天看Mat,想起之前学历的行列式,那么我们如何求解一个矩阵对应的行列式的求解方法
行列式计算:
最简单的二维的行列式:
哈哈,上面是一个简单的计算的规则,希望你勾起你的青春的回忆
当然上面是二维和三维的,还有四维的甚至更高维度的
我们大概总结出来就是:某一行或者是某一列中的每一个元素乘以其对应的代数余子式的和
如何来求解代数余子式:
某一个元素对应的代数余子式,就是去掉这个元素所在的行和列,得到的行列式,称为改元素的代数余子式
代码:
首先我们使用Mat类创建一个矩阵:
创建上面的一个3x3的矩阵
首先我们求解的代数余子式
private static Mat Detsub(Mat mat, int i, int j) {
// 删除行
int rows = mat.rows();
int cols = mat.cols();
Mat tmp = mat.submat(0, i, 0, cols);
Mat mat_result = mat.submat(i + 1, rows, 0, cols);
tmp.push_back(mat_result);
//删除列
tmp = tmp.t();
mat_result = tmp.submat(0, j, 0, rows - 1);
tmp = tmp.submat(j + 1, cols, 0, rows - 1);
mat_result.push_back(tmp);
tmp.release();
return mat_result.t();
}
然后递归计算:
// 矩阵行列式求值
public static double Det(Mat mat) {
// 单通道
if (mat.channels() > 1) {
throw new RuntimeException("Mat 对应的通道必须单通道");
}
// 并且是方阵
if (mat.rows() != mat.cols()) {
throw new RuntimeException("Mat 必须是方阵");
}
double sum = 0.0;
// 矩阵的维度,也就是矩阵对应的行列式化简之后的结果
if (mat.size().height == 2) {
return mat.get(0, 0)[0] * mat.get(1, 1)[0] - mat.get(0, 1)[0] * mat.get(1, 0)[0];
}
for (int col = 0; col < mat.size().height; col++) {
double a = mat.get(0, col)[0];
if (col % 2 == 0) {
double one = Det(Detsub(mat, 0, col));
sum += a * one;
} else {
double two = Det(Detsub(mat, 0, col));
sum -= a * two;
}
}
return sum;
}
开始计算:测试1
测试2:
为了检验结果的正确性,我从网上找了一个二维数组的列子,求解行列式值的例子:
class aij {
// A函数可用于求余子阵
int[][] A(int[][] a, int row, int column) {
int[][] ans = new int[a.length - 1][a.length - 1];// ans用于储存返回的最终结果
int[] temp = new int[(a.length - 1) * (a.length - 1)];// 临时一维数组temp用于按顺序储存剔除相应行和列元素后的数组
int k = 0;
// 剔除行和列并按顺序储存到temp内
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (i == row - 1) {
continue;
} else if (j == column - 1) {
continue;
}
temp[k++] = a[i][j];
}
}
// 按顺序从temp中读取数据并储存到ans内
k = 0;
for (int i = 0; i < ans.length; i++) {
for (int j = 0; j < ans[i].length; j++) {
ans[i][j] = temp[k++];
}
}
return ans;
}
// det用于求行列式
int det(int[][] a) {
if (a.length == 1) {
return a[0][0];
} else {
int ans = 0;
for (int i = 0; i < a.length; i++) {
ans += a[i][0] * (int) Math.pow(-1, i) * det(A(a, i + 1, 1));
}
return ans;
}
}
}
可以看到的是两次的计算结果是一样的
希望对你有所帮助