mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
c33d5a80c7
added a test method for r24969. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25709 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
147 lines
3.8 KiB
Ruby
147 lines
3.8 KiB
Ruby
require 'test/unit'
|
|
require 'matrix'
|
|
|
|
class TestMatrix < Test::Unit::TestCase
|
|
def setup
|
|
@m1 = Matrix[[1,2,3], [4,5,6]]
|
|
@m2 = Matrix[[1,2,3], [4,5,6]]
|
|
@m3 = @m1.clone
|
|
@m4 = Matrix[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
|
|
@n1 = Matrix[[2,3,4], [5,6,7]]
|
|
end
|
|
|
|
def test_identity
|
|
assert_same @m1, @m1
|
|
assert_not_same @m1, @m2
|
|
assert_not_same @m1, @m3
|
|
assert_not_same @m1, @m4
|
|
assert_not_same @m1, @n1
|
|
end
|
|
|
|
def test_equality
|
|
assert_equal @m1, @m1
|
|
assert_equal @m1, @m2
|
|
assert_equal @m1, @m3
|
|
assert_equal @m1, @m4
|
|
assert_not_equal @m1, @n1
|
|
end
|
|
|
|
def test_hash_equality
|
|
assert @m1.eql?(@m1)
|
|
assert @m1.eql?(@m2)
|
|
assert @m1.eql?(@m3)
|
|
assert !@m1.eql?(@m4)
|
|
assert !@m1.eql?(@n1)
|
|
|
|
hash = { @m1 => :value }
|
|
assert hash.key?(@m1)
|
|
assert hash.key?(@m2)
|
|
assert hash.key?(@m3)
|
|
assert !hash.key?(@m4)
|
|
assert !hash.key?(@n1)
|
|
end
|
|
|
|
def test_hash
|
|
assert_equal @m1.hash, @m1.hash
|
|
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
|