diff options
Diffstat (limited to 'lib/matrix.rb')
-rw-r--r-- | lib/matrix.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb index 7502e6ef8b..1a7519f408 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -59,6 +59,7 @@ end # * #find_index # * #minor(*param) # * #first_minor(row, column) +# * #cofactor(row, column) # # Properties of a matrix: # * #diagonal? @@ -545,6 +546,7 @@ class Matrix nil end alias_method :find_index, :index + # # Returns a section of the matrix. The parameters are either: # * start_row, nrows, start_col, ncols; OR @@ -619,6 +621,21 @@ class Matrix new_matrix arrays, column_count - 1 end + # + # 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 + # + def cofactor(row, column) + raise RuntimeError, "cofactor of empty matrix is not defined" if empty? + Matrix.Raise ErrDimensionMismatch unless square? + + det_of_minor = first_minor(row, column).determinant + det_of_minor * (-1) ** (row + column) + end + #-- # TESTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ |