ruby-changes:36101
From: marcandre <ko1@a...>
Date: Wed, 29 Oct 2014 11:43:33 +0900 (JST)
Subject: [ruby-changes:36101] marcandRe: r48182 (trunk): * lib/matrix.rb: Add Matrix#adjucate
marcandre 2014-10-29 11:43:28 +0900 (Wed, 29 Oct 2014) New Revision: 48182 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48182 Log: * lib/matrix.rb: Add Matrix#adjucate patch by gogo tanaka [#10056] Modified files: trunk/ChangeLog trunk/NEWS trunk/lib/matrix.rb trunk/test/matrix/test_matrix.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 48181) +++ ChangeLog (revision 48182) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Oct 29 11:43:11 2014 Marc-Andre Lafortune <ruby-core@m...> + + * lib/matrix.rb: Add Matrix#adjucate + patch by gogo tanaka [#10056] + Wed Oct 29 11:42:33 2014 Marc-Andre Lafortune <ruby-core@m...> * lib/matrix.rb: Add aliases for Vector#cross & dot Index: lib/matrix.rb =================================================================== --- lib/matrix.rb (revision 48181) +++ lib/matrix.rb (revision 48182) @@ -62,6 +62,7 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L62 # * #minor(*param) # * #first_minor(row, column) # * #cofactor(row, column) +# * #adjugate # * #laplace_expansion(row_or_column: num) # * #cofactor_expansion(row_or_column: num) # @@ -690,6 +691,20 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L691 end # + # Returns the adjugate of the matrix. + # + # Matrix[ [7,6],[3,9] ].adjugate + # => 9 -6 + # -3 7 + # + def adjugate + Matrix.Raise ErrDimensionMismatch unless square? + Matrix.build(row_count, column_count) do |row, column| + cofactor(column, row) + end + end + + # # Returns the Laplace expansion along given row or column. # # Matrix[[7,6], [3,9]].laplace_expansion(column: 1) Index: NEWS =================================================================== --- NEWS (revision 48181) +++ NEWS (revision 48182) @@ -148,6 +148,7 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L148 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). + * Matrix#adjugate returns the adjugate of the matrix. * hstack and vstack are new instance and class methods to stack matrices horizontally and vertically. * Matrix#laplace_expansion(row_or_column: num) returns the laplace_expansion Index: test/matrix/test_matrix.rb =================================================================== --- test/matrix/test_matrix.rb (revision 48181) +++ test/matrix/test_matrix.rb (revision 48182) @@ -14,6 +14,9 @@ class TestMatrix < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_matrix.rb#L14 @c1 = Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]] @e1 = Matrix.empty(2,0) @e2 = Matrix.empty(0,3) + @a3 = Matrix[[4, 1, -3], [0, 3, 7], [11, -4, 2]] + @a5 = Matrix[[2, 0, 9, 3, 9], [8, 7, 0, 1, 9], [7, 5, 6, 6, 5], [0, 7, 8, 3, 0], [7, 8, 2, 3, 1]] + @b3 = Matrix[[-7, 7, -10], [9, -3, -2], [-1, 3, 9]] end def test_matrix @@ -302,6 +305,18 @@ class TestMatrix < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_matrix.rb#L305 assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { Matrix[[2,0,1],[0,-2,2]].cofactor(0, 0) } end + def test_adjugate + assert_equal(Matrix.empty, Matrix.empty.adjugate) + assert_equal(Matrix[[1]], Matrix[[5]].adjugate) + assert_equal(Matrix[[9,-6],[-3,7]], Matrix[[7,6],[3,9]].adjugate) + assert_equal(Matrix[[45,3,-7],[6,-1,0],[-7,0,0]], Matrix[[0,0,1],[0,7,6],[1,3,9]].adjugate) + assert_equal(Matrix.identity(5), (@a5.adjugate * @a5) / @a5.det) + assert_equal(Matrix.I(3), Matrix.I(3).adjugate) + assert_equal((@a3 * @b3).adjugate, @b3.adjugate * @a3.adjugate) + assert_equal(4**(@a3.row_count-1) * @a3.adjugate, (4 * @a3).adjugate) + assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { @m1.adjugate } + end + def test_laplace_expansion assert_equal(1, Matrix[[1]].laplace_expansion(row: 0)) assert_equal(45, Matrix[[7,6], [3,9]].laplace_expansion(row: 1)) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/