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

* 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.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@26263 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2010-01-09 20:35:28 +00:00
parent 6051cf527b
commit 2ae2d9dd4d
3 changed files with 125 additions and 8 deletions

View file

@ -46,4 +46,111 @@ 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
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