ruby-changes:35753
From: marcandre <ko1@a...>
Date: Wed, 8 Oct 2014 04:30:54 +0900 (JST)
Subject: [ruby-changes:35753] marcandRe: r47835 (trunk): * lib/matrix.rb: Add Matrix#laplace_expansion.
marcandre 2014-10-08 04:30:48 +0900 (Wed, 08 Oct 2014) New Revision: 47835 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47835 Log: * lib/matrix.rb: Add Matrix#laplace_expansion. patch by gogo tanaka [#10073] Modified files: trunk/ChangeLog trunk/NEWS trunk/lib/matrix.rb trunk/test/matrix/test_matrix.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 47834) +++ ChangeLog (revision 47835) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Oct 8 04:30:29 2014 Marc-Andre Lafortune <ruby-core@m...> + + * lib/matrix.rb: Add Matrix#laplace_expansion. + patch by gogo tanaka [#10073] + Wed Oct 8 04:29:21 2014 Marc-Andre Lafortune <ruby-core@m...> * lib/matrix.rb: Add Vector.basis. Index: lib/matrix.rb =================================================================== --- lib/matrix.rb (revision 47834) +++ lib/matrix.rb (revision 47835) @@ -62,6 +62,8 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L62 # * #minor(*param) # * #first_minor(row, column) # * #cofactor(row, column) +# * #laplace_expansion(row_or_column: num) +# * #cofactor_expansion(row_or_column: num) # # Properties of a matrix: # * #diagonal? @@ -685,6 +687,37 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L687 det_of_minor * (-1) ** (row + column) end + # + # Returns the Laplace expansion along given row or column. + # + # Matrix[[7,6], [3,9]].laplace_expansion(column: 1) + # => 45 + # + # Matrix[[Vector[1, 0], Vector[0, 1]], [2, 3]].laplace_expansion(row: 0) + # => Vector[3, -2] + # + # + def laplace_expansion(row: nil, column: nil) + num = row || column + + if !num || (row && column) + raise ArgumentError, "exactly one the row or column arguments must be specified" + end + + Matrix.Raise ErrDimensionMismatch unless square? + raise RuntimeError, "laplace_expansion of empty matrix is not defined" if empty? + + unless 0 <= num && num < row_count + raise ArgumentError, "invalid num (#{num.inspect} for 0..#{row_count - 1})" + end + + send(row ? :row : :column, num).map.with_index { |e, k| + e * cofactor(*(row ? [num, k] : [k,num])) + }.inject(:+) + end + alias_method :cofactor_expansion, :laplace_expansion + + #-- # TESTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ Index: NEWS =================================================================== --- NEWS (revision 47834) +++ NEWS (revision 47835) @@ -84,6 +84,8 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L84 which is obtained by multiplying the first minor by (-1)**(row + column). * 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 + along the +num+ -th row or column. * Vector.basis(size:, index:) returns the specified basis vector * Method Index: test/matrix/test_matrix.rb =================================================================== --- test/matrix/test_matrix.rb (revision 47834) +++ test/matrix/test_matrix.rb (revision 47835) @@ -293,6 +293,24 @@ class TestMatrix < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_matrix.rb#L293 assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { Matrix[[2,0,1],[0,-2,2]].cofactor(0, 0) } 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)) + assert_equal(0, Matrix[[0,0],[0,0]].laplace_expansion(column: 0)) + assert_equal(-7, Matrix[[0,0,1],[0,7,6],[1,3,9]].laplace_expansion(column: 2)) + + assert_equal(Vector[3, -2], Matrix[[Vector[1, 0], Vector[0, 1]], [2, 3]].laplace_expansion(row: 0)) + + assert_raise(ExceptionForMatrix::ErrDimensionMismatch) { @m1.laplace_expansion(row: 1) } + assert_raise(ArgumentError) { Matrix[[7,6], [3,9]].laplace_expansion() } + assert_raise(ArgumentError) { Matrix[[7,6], [3,9]].laplace_expansion(foo: 1) } + assert_raise(ArgumentError) { Matrix[[7,6], [3,9]].laplace_expansion(row: 1, column: 1) } + assert_raise(ArgumentError) { Matrix[[7,6], [3,9]].laplace_expansion(row: 2) } + assert_raise(ArgumentError) { Matrix[[0,0,1],[0,7,6],[1,3,9]].laplace_expansion(column: -1) } + + assert_raise(RuntimeError) { Matrix.empty(0, 0).laplace_expansion(row: 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/