我正在编写一种方法来计算包含双精度的矩阵(此处为二维数组)的行列式。这是我写的:
/// <summary>
/// Checks to see if a matrix is square, and then computes its determinant
/// </summary>
/// <returns></returns>
public double Determinant()
{
// Check to make sure the matrix is square. Only square matrices
// have determinants.
if (!this.isSquare())
{
throw new Exception("The matrix does not have a determinant");
}
// Check to see if the matrix has dimensions 1x1.
// The determinant of a 1x1 matrix is equal to the value stored at (0,0).
if (this.NumberOfRows == 1)
{
return this.GetElement(0, 0);
}
double determinant = 0;
// Loop through the top row of the matrix.
for (int columnIndex = 0; columnIndex < this.NumberOfColumns; columnIndex++)
{
Matrix cofactor = new Matrix(this.NumberOfRows - 1, this.NumberOfColumns - 1);
//fill cofactor
//I dont Know what to do here?
determinant += this.GetElement(1, columnIndex) * cofactor.Determinant();
}
return determinant;
}
我缺少的是应该去的线fill cofactor
。
有人可以建议我应该在那里做什么吗?基本上,从原始矩阵添加元素到辅因子的最佳方法是什么,同时忽略出现在矩阵中我当前位置的行或列中的元素?