cofactors method
Computes the matrix of cofactors for a square matrix. Each element in the cofactor matrix is the determinant of the subMatrix formed by removing the corresponding row and column from the original matrix, multiplied by the alternating sign pattern.
Example:
var A = Matrix('1 2 3; 4 5 6; 7 8 9');
var B = A.cofactors();
Throws an ArgumentError if the matrix is not square.
Returns a new Matrix object containing the cofactors.
Implementation
Matrix cofactors() {
if (!isSquareMatrix()) {
throw ArgumentError('Cofactors can only be computed for square matrices');
}
Matrix cofactorMatrix = Matrix.zeros(rowCount, columnCount, isDouble: true);
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < columnCount; j++) {
cofactorMatrix[i][j] = math.pow(-1, i + j) * slice(i, j).determinant();
}
}
return cofactorMatrix;
}