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

ruby-changes:14433

From: nobu <ko1@a...>
Date: Sun, 10 Jan 2010 05:35:43 +0900 (JST)
Subject: [ruby-changes:14433] Ruby:r26263 (ruby_1_8, trunk): * lib/matrix.rb (Matrix#inverse_from): use #quo. backported r9490.

nobu	2010-01-10 05:35:28 +0900 (Sun, 10 Jan 2010)

  New Revision: 26263

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

  Log:
    * lib/matrix.rb (Matrix#inverse_from): use #quo. backported r9490.
    
    * lib/matrix.rb (Matrix#determinant): ditto.  [ruby-core:27507]
    
    * lib/matrix.rb (Matrix#rank): ditto.
    
    * lib/matrix.rb (Matrix::Scalar#initialize): ditto.

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/lib/matrix.rb
    branches/ruby_1_8/test/matrix/test_matrix.rb
    trunk/lib/matrix.rb
    trunk/test/matrix/test_matrix.rb

Index: lib/matrix.rb
===================================================================
--- lib/matrix.rb	(revision 26262)
+++ lib/matrix.rb	(revision 26263)
@@ -569,7 +569,7 @@
 
   #
   # Returns the inverse of the matrix.
-  #   Matrix[[1, 2], [2, 1]].inverse
+  #   Matrix[[-1, -1], [0, -1]].inverse
   #     => -1  1
   #         0 -1
   #
Index: test/matrix/test_matrix.rb
===================================================================
--- test/matrix/test_matrix.rb	(revision 26262)
+++ test/matrix/test_matrix.rb	(revision 26263)
@@ -144,4 +144,13 @@
       assert_equal 3, m.transpose.rank
     end
   end
+
+  def test_inverse
+    assert_equal(Matrix[[-1, 1], [0, -1]], Matrix[[-1, -1], [0, -1]].inverse)
+  end
+
+  def test_determinant
+    assert_equal(45, Matrix[[7,6], [3,9]].determinant)
+    assert_equal(-18, Matrix[[2,0,1],[0,-2,2],[1,2,3]].determinant)
+  end
 end
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 26262)
+++ ruby_1_8/ChangeLog	(revision 26263)
@@ -1,3 +1,13 @@
+Sun Jan 10 05:35:26 2010  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/matrix.rb (Matrix#inverse_from): use #quo. backported r9490.
+
+	* lib/matrix.rb (Matrix#determinant): ditto.  [ruby-core:27507]
+
+	* lib/matrix.rb (Matrix#rank): ditto.
+
+	* lib/matrix.rb (Matrix::Scalar#initialize): ditto.
+
 Sun Jan 10 04:54:36 2010  Nobuyoshi Nakada  <nobu@r...>
 
 	* class.c (rb_define_class): raise TypeError same as class
Index: ruby_1_8/lib/matrix.rb
===================================================================
--- ruby_1_8/lib/matrix.rb	(revision 26262)
+++ ruby_1_8/lib/matrix.rb	(revision 26263)
@@ -549,7 +549,7 @@
 
   #
   # Returns the inverse of the matrix.
-  #   Matrix[[1, 2], [2, 1]].inverse
+  #   Matrix[[-1, -1], [0, -1]].inverse
   #     => -1  1
   #         0 -1
   #
@@ -585,7 +585,7 @@
 
       size.times do |i|
         next if i == k
-        q = a[i][k] / akk
+        q = a[i][k].quo(akk)
         a[i][k] = 0
 
         (k + 1 ... size).each do |j|
@@ -597,10 +597,10 @@
       end
 
       (k + 1 ... size).each do |j|
-        a[k][j] /= akk
+        a[k][j] = a[k][j].quo(akk)
       end
       size.times do |j|
-        @rows[k][j] /= akk
+        @rows[k][j] = @rows[k][j].quo(akk)
       end
     end
     self
@@ -649,7 +649,7 @@
   # Returns the determinant of the matrix.  If the matrix is not square, the
   # result is 0.
   #   Matrix[[7,6], [3,9]].determinant
-  #     => 63
+  #     => 45
   #
   def determinant
     return 0 unless square?
@@ -670,7 +670,7 @@
       end
 
       (k + 1 ... size).each do |i|
-        q = a[i][k] / akk
+        q = a[i][k].quo(akk)
         (k + 1 ... size).each do |j|
           a[i][j] -= a[k][j] * q
         end
@@ -720,7 +720,7 @@
       end
 
       (k + 1 ... a_row_size).each do |i|
-        q = a[i][k] / akk
+        q = a[i][k].quo(akk)
         (k + 1... a_column_size).each do |j|
           a[i][j] -= a[k][j] * q
         end
@@ -879,7 +879,7 @@
         self * other.inverse
       else
         x, y = other.coerce(self)
-        x / y
+        x.quo(y)
       end
     end
     
Index: ruby_1_8/test/matrix/test_matrix.rb
===================================================================
--- ruby_1_8/test/matrix/test_matrix.rb	(revision 26262)
+++ ruby_1_8/test/matrix/test_matrix.rb	(revision 26263)
@@ -46,4 +46,111 @@
     assert_equal @m1.hash, @m2.hash
     assert_equal @m1.hash, @m3.hash
   end
+
+  def test_rank
+    [
+      [[0]],
+      [[0], [0]],
+      [[0, 0], [0, 0]],
+      [[0, 0], [0, 0], [0, 0]],
+      [[0, 0, 0]],
+      [[0, 0, 0], [0, 0, 0]],
+      [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
+      [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
+    ].each do |rows|
+      assert_equal 0, Matrix[*rows].rank
+    end
+
+    [
+      [[1], [0]],
+      [[1, 0], [0, 0]],
+      [[1, 0], [1, 0]],
+      [[0, 0], [1, 0]],
+      [[1, 0], [0, 0], [0, 0]],
+      [[0, 0], [1, 0], [0, 0]],
+      [[0, 0], [0, 0], [1, 0]],
+      [[1, 0], [1, 0], [0, 0]],
+      [[0, 0], [1, 0], [1, 0]],
+      [[1, 0], [1, 0], [1, 0]],
+      [[1, 0, 0]],
+      [[1, 0, 0], [0, 0, 0]],
+      [[0, 0, 0], [1, 0, 0]],
+      [[1, 0, 0], [1, 0, 0]],
+      [[1, 0, 0], [1, 0, 0]],
+      [[1, 0, 0], [0, 0, 0], [0, 0, 0]],
+      [[0, 0, 0], [1, 0, 0], [0, 0, 0]],
+      [[0, 0, 0], [0, 0, 0], [1, 0, 0]],
+      [[1, 0, 0], [1, 0, 0], [0, 0, 0]],
+      [[0, 0, 0], [1, 0, 0], [1, 0, 0]],
+      [[1, 0, 0], [0, 0, 0], [1, 0, 0]],
+      [[1, 0, 0], [1, 0, 0], [1, 0, 0]],
+      [[1, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
+      [[1, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
+      [[1, 0, 0], [1, 0, 0], [0, 0, 0], [0, 0, 0]],
+      [[1, 0, 0], [0, 0, 0], [1, 0, 0], [0, 0, 0]],
+      [[1, 0, 0], [0, 0, 0], [0, 0, 0], [1, 0, 0]],
+      [[1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 0, 0]],
+      [[1, 0, 0], [0, 0, 0], [1, 0, 0], [1, 0, 0]],
+      [[1, 0, 0], [1, 0, 0], [0, 0, 0], [1, 0, 0]],
+      [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]],
+
+      [[1]],
+      [[1], [1]],
+      [[1, 1]],
+      [[1, 1], [1, 1]],
+      [[1, 1], [1, 1], [1, 1]],
+      [[1, 1, 1]],
+      [[1, 1, 1], [1, 1, 1]],
+      [[1, 1, 1], [1, 1, 1], [1, 1, 1]],
+      [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]],
+    ].each do |rows|
+      matrix = Matrix[*rows]
+      assert_equal 1, matrix.rank
+      assert_equal 1, matrix.transpose.rank
+    end
+
+    [
+      [[1, 0], [0, 1]],
+      [[1, 0], [0, 1], [0, 0]],
+      [[1, 0], [0, 1], [0, 1]],
+      [[1, 0], [0, 1], [1, 1]],
+      [[1, 0, 0], [0, 1, 0]],
+      [[1, 0, 0], [0, 0, 1]],
+      [[1, 0, 0], [0, 1, 0], [0, 0, 0]],
+      [[1, 0, 0], [0, 0, 1], [0, 0, 0]],
+
+      [[1, 0, 0], [0, 0, 0], [0, 1, 0]],
+      [[1, 0, 0], [0, 0, 0], [0, 0, 1]],
+
+      [[1, 0], [1, 1]],
+      [[1, 2], [1, 1]],
+      [[1, 2], [0, 1], [1, 1]],
+    ].each do |rows|
+      m = Matrix[*rows]
+      assert_equal 2, m.rank
+      assert_equal 2, m.transpose.rank
+    end
+
+    [
+      [[1, 0, 0], [0, 1, 0], [0, 0, 1]],
+      [[1, 1, 0], [0, 1, 1], [1, 0, 1]],
+      [[1, 1, 0], [0, 1, 1], [1, 0, 1]],
+      [[1, 1, 0], [0, 1, 1], [1, 0, 1], [0, 0, 0]],
+      [[1, 1, 0], [0, 1, 1], [1, 0, 1], [1, 1, 1]],
+      [[1, 1, 1], [1, 1, 2], [1, 3, 1], [4, 1, 1]],
+    ].each do |rows|
+      m = Matrix[*rows]
+      assert_equal 3, m.rank
+      assert_equal 3, m.transpose.rank
+    end
+  end
+
+  def test_inverse
+    assert_equal(Matrix[[-1, 1], [0, -1]], Matrix[[-1, -1], [0, -1]].inverse)
+  end
+
+  def test_determinant
+    assert_equal(45, Matrix[[7,6], [3,9]].determinant)
+    assert_equal(-18, Matrix[[2,0,1],[0,-2,2],[1,2,3]].determinant)
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

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