Skip to content

Instantly share code, notes, and snippets.

@qoobaa
Created August 8, 2008 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qoobaa/4584 to your computer and use it in GitHub Desktop.
Save qoobaa/4584 to your computer and use it in GitHub Desktop.
require 'matrix'
class Matrix
alias old_minor minor
def minor(*param)
if param.size == 2 and param[0].kind_of?(Integer) and param[1].kind_of?(Integer)
rows = []
@rows.size.times do |i|
next if i == param[0]
rows << (@rows[i][0, param[1]] or []) +
(@rows[i][param[1] + 1, @rows.size - param[1] - 1] or [])
end
Matrix.rows(rows, false)
else
old_minor(param)
end
end
def adjugate
Matrix.Raise ErrDimensionMismatch unless square?
rows = Array.new(@rows.size) { [] }
rows.size.times do |row|
rows.size.times do |col|
rows[row][col] = (((row + col) % 2) == 0 ? 1 : -1) * minor(row, col).det
end
end
Matrix.rows(rows, false).t
end
alias adj adjugate
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment