[前][次][番号順一覧][スレッド一覧]

ruby-changes:33448

From: marcandre <ko1@a...>
Date: Mon, 7 Apr 2014 02:46:04 +0900 (JST)
Subject: [ruby-changes:33448] marcandRe: r45527 (trunk): * lib/matrix.rb: Add first_minor [fix GH-568]

marcandre	2014-04-07 02:45:54 +0900 (Mon, 07 Apr 2014)

  New Revision: 45527

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45527

  Log:
    * lib/matrix.rb: Add first_minor [fix GH-568]
      Patch by gogotanaka

  Modified files:
    trunk/NEWS
    trunk/lib/matrix.rb
    trunk/test/matrix/test_matrix.rb
Index: lib/matrix.rb
===================================================================
--- lib/matrix.rb	(revision 45526)
+++ lib/matrix.rb	(revision 45527)
@@ -58,6 +58,7 @@ end https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L58
 # * #each_with_index
 # * #find_index
 # * #minor(*param)
+# * #first_minor(row, column)
 #
 # Properties of a matrix:
 # * #diagonal?
@@ -590,6 +591,34 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L591
     new_matrix rows, [column_count - from_col, size_col].min
   end
 
+  #
+  # Returns the submatrix obtained by deleting the specified row and column.
+  #
+  #   Matrix.diagonal(9, 5, -3, 4).first_minor(1, 2)
+  #     => 9 0 0
+  #        0 0 0
+  #        0 0 4
+  #
+  def first_minor(row, column)
+    raise RuntimeError, "first_minor of empty matrix is not defined" if empty?
+
+    unless 0 <= row && row < row_count
+      raise ArgumentError, "invalid row (#{row.inspect} for 0..#{row_count - 1})"
+    end
+
+    unless 0 <= column && column < column_count
+      raise ArgumentError, "invalid column (#{column.inspect} for 0..#{column_count - 1})"
+    end
+
+    arrays = to_a
+    arrays.delete_at(row)
+    arrays.each do |array|
+      array.delete_at(column)
+    end
+
+    new_matrix arrays, column_count - 1
+  end
+
   #--
   # TESTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
   #++
Index: NEWS
===================================================================
--- NEWS	(revision 45526)
+++ NEWS	(revision 45527)
@@ -27,6 +27,11 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L27
     * Most symbols which are returned by String#to_sym and
       String#intern are GC-able.
 
+* Matrix
+  * New methods:
+    * Matrix#first_minor(row, column) returns the submatrix obtained
+      by deleting the specified row and column.
+
 === Core classes compatibility issues (excluding feature bug fixes)
 
 * IO
Index: test/matrix/test_matrix.rb
===================================================================
--- test/matrix/test_matrix.rb	(revision 45526)
+++ test/matrix/test_matrix.rb	(revision 45527)
@@ -250,6 +250,18 @@ class TestMatrix < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_matrix.rb#L250
     assert_raise(ArgumentError) { @m1.minor(0) }
   end
 
+  def test_first_minor
+    assert_equal(Matrix.empty(0, 0), Matrix[[1]].first_minor(0, 0))
+    assert_equal(Matrix.empty(0, 2), Matrix[[1, 4, 2]].first_minor(0, 1))
+    assert_equal(Matrix[[1, 3]], @m1.first_minor(1, 1))
+    assert_equal(Matrix[[4, 6]], @m1.first_minor(0, 1))
+    assert_equal(Matrix[[1, 2]], @m1.first_minor(1, 2))
+    assert_raise(RuntimeError) { Matrix.empty(0, 0).first_minor(0, 0) }
+    assert_raise(ArgumentError) { @m1.first_minor(4, 0) }
+    assert_raise(ArgumentError) { @m1.first_minor(0, -1) }
+    assert_raise(ArgumentError) { @m1.first_minor(-1, 4) }
+  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/

[前][次][番号順一覧][スレッド一覧]