Я пишу метод, который будет вычислять определитель матрицы (в данном случае двумерного массива), содержащей двойные числа. Вот что я написал:
/// <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
.
Может кто подскажет, что мне там делать? По сути, как лучше всего добавить элементы в кофактор из исходной матрицы, игнорируя те, которые появляются в строке или столбце моего текущего местоположения в матрице?