summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-11-10 17:11:36 +0000
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-11-10 17:11:36 +0000
commit9ca073576ca27c4af316720aa0aec2da2230c021 (patch)
treeb103e051e1a91a04c64bcb1b75cc09af6e788111
parentcc502757d0f8dea1df628aa08facbf12c7ca87d4 (diff)
merges r24969 from trunk into ruby_1_9_1, and adds a test for the fix.
-- * lib/matrix.rb (Matrix#rank): Two bug fixes. One made Matrix[[0,0],[0,0],[1,0]].rank raise a NoMethodError while the other one had Matrix[[0,1],[0,0],[1,0]].rank raise a TypeError. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_1@25708 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--lib/matrix.rb6
-rw-r--r--test/matrix/test_matrix.rb98
3 files changed, 107 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 1349cf2d4f..6060ee5545 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Thu Sep 17 06:03:40 2009 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
+
+ * lib/matrix.rb (Matrix#rank): Two bug fixes. One made
+ Matrix[[0,0],[0,0],[1,0]].rank raise a NoMethodError while the other
+ one had Matrix[[0,1],[0,0],[1,0]].rank raise a TypeError.
+
Wed Sep 16 17:20:49 2009 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* lib/matrix.rb (Matrix#/): Fix obvious bug
diff --git a/lib/matrix.rb b/lib/matrix.rb
index 88ccbca6b0..04616d00d8 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -776,7 +776,7 @@ class Matrix
i = k
exists = true
loop do
- if (i += 1) > a_column_size - 1
+ if (i += 1) > a_row_size - 1
exists = false
break
end
@@ -789,14 +789,14 @@ class Matrix
i = k
exists = true
loop do
- if (i += 1) > a_row_size - 1
+ if (i += 1) > a_column_size - 1
exists = false
break
end
break unless a[k][i] == 0
end
if exists
- k.upto(a_column_size - 1) do |j|
+ k.upto(a_row_size - 1) do |j|
a[j][k], a[j][i] = a[j][i], a[j][k]
end
akk = a[k][k]
diff --git a/test/matrix/test_matrix.rb b/test/matrix/test_matrix.rb
index 0850117b86..8e0848de01 100644
--- a/test/matrix/test_matrix.rb
+++ b/test/matrix/test_matrix.rb
@@ -46,4 +46,102 @@ class TestMatrix < Test::Unit::TestCase
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
end