Permalink
Browse files

* lib/matrix.rb: Add Matrix#cofactor [fix GH-568]

  Patch by gogotanaka

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45528 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information...
marcandre committed Apr 6, 2014
1 parent b8ba202 commit d11d87c3627e60a957f9313d2106f7a6d2b5f7a1
Showing with 37 additions and 0 deletions.
  1. +7 −0 ChangeLog
  2. +2 −0 NEWS
  3. +17 −0 lib/matrix.rb
  4. +11 −0 test/matrix/test_matrix.rb
View
@@ -1,3 +1,10 @@
Mon Apr 7 02:39:48 2014 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* lib/matrix.rb: Add Matrix#cofactor [fix GH-568]
Add first_minor [fix GH-568]
Handle empty diagonal matrix case [fix GH-576]
Patches by gogotanaka
Sun Apr 6 08:52:50 2014 Bugra Barin <bugrabarin@hotmail.com>
* dln.c (dln_load): use wchar version to load a library in
View
2 NEWS
@@ -31,6 +31,8 @@ with all sufficient information, see the ChangeLog file.
* New methods:
* Matrix#first_minor(row, column) returns the submatrix obtained
by deleting the specified row and column.
* Matrix#cofactor(row, column) returns the (row, column) cofactor
which is obtained by multiplying the first minor by (-1)**(row + column).
=== Core classes compatibility issues (excluding feature bug fixes)
View
@@ -59,6 +59,7 @@ module ExceptionForMatrix # :nodoc:
# * #find_index
# * #minor(*param)
# * #first_minor(row, column)
# * #cofactor(row, column)
#
# Properties of a matrix:
# * #diagonal?
@@ -545,6 +546,7 @@ def index(*args)
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 @@ def first_minor(row, column)
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 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#++
View
@@ -262,6 +262,17 @@ def test_first_minor
assert_raise(ArgumentError) { @m1.first_minor(-1, 4) }
end
def test_cofactor
assert_equal(1, Matrix[[1]].cofactor(0, 0))
assert_equal(9, Matrix[[7,6],[3,9]].cofactor(0, 0))
assert_equal(0, Matrix[[0,0],[0,0]].cofactor(0, 0))
assert_equal(3, Matrix[[0,0,1],[0,7,6],[1,3,9]].cofactor(1, 0))
assert_equal(-21, Matrix[[7,0,1,0,12],[8,1,1,9,1],[4,0,0,-7,17],[-1,0,0,-4,8],[10,1,1,8,6]].cofactor(2, 3))
assert_raise(RuntimeError) { Matrix.empty(0, 0).cofactor(0, 0) }
assert_raise(ArgumentError) { Matrix[[0,0],[0,0]].cofactor(-1, 4) }
assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { Matrix[[2,0,1],[0,-2,2]].cofactor(0, 0) }
end
def test_regular?
assert(Matrix[[1, 0], [0, 1]].regular?)
assert(Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]].regular?)

0 comments on commit d11d87c

Please sign in to comment.