Method: Matrix#cofactor

Defined in:
lib/matrix.rb

#cofactor(row, column) ⇒ Object

Returns the (row, column) cofactor which is obtained by multiplying the first minor by (-1)**(row + column).

Matrix.diagonal(9, 5, -3, 4).cofactor(1, 1)
#  => -108

Raises:

  • (RuntimeError)


778
779
780
781
782
783
784
# File 'lib/matrix.rb', line 778

def cofactor(row, column)
  raise RuntimeError, "cofactor of empty matrix is not defined" if empty?
  raise ErrDimensionMismatch unless square?

  det_of_minor = first_minor(row, column).determinant
  det_of_minor * (-1) ** (row + column)
end