1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
yugui 2009-11-10 17:11:36 +00:00
parent cc502757d0
commit 9ca073576c
3 changed files with 107 additions and 3 deletions

View file

@ -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

View file

@ -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]

View file

@ -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