ruby-changes:33449
From: marcandre <ko1@a...>
Date: Mon, 7 Apr 2014 02:46:23 +0900 (JST)
Subject: [ruby-changes:33449] marcandRe: r45528 (trunk): * lib/matrix.rb: Add Matrix#cofactor [fix GH-568]
marcandre 2014-04-07 02:46:16 +0900 (Mon, 07 Apr 2014) New Revision: 45528 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45528 Log: * lib/matrix.rb: Add Matrix#cofactor [fix GH-568] Patch by gogotanaka Modified files: trunk/ChangeLog trunk/NEWS trunk/lib/matrix.rb trunk/test/matrix/test_matrix.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 45527) +++ ChangeLog (revision 45528) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon Apr 7 02:39:48 2014 Marc-Andre Lafortune <ruby-core@m...> + + * 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@h...> * dln.c (dln_load): use wchar version to load a library in Index: lib/matrix.rb =================================================================== --- lib/matrix.rb (revision 45527) +++ lib/matrix.rb (revision 45528) @@ -59,6 +59,7 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L59 # * #find_index # * #minor(*param) # * #first_minor(row, column) +# * #cofactor(row, column) # # Properties of a matrix: # * #diagonal? @@ -545,6 +546,7 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L546 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 https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L621 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 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ Index: NEWS =================================================================== --- NEWS (revision 45527) +++ NEWS (revision 45528) @@ -31,6 +31,8 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L31 * 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) Index: test/matrix/test_matrix.rb =================================================================== --- test/matrix/test_matrix.rb (revision 45527) +++ test/matrix/test_matrix.rb (revision 45528) @@ -262,6 +262,17 @@ class TestMatrix < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_matrix.rb#L262 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?) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/