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

* lib/matrix: Handle empty diagonal matrix case [fix GH-576]

Patch by gogotanaka

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45524 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2014-04-06 17:45:07 +00:00
parent a237db5cbc
commit bcf8dd1096
2 changed files with 2 additions and 0 deletions

View file

@ -204,6 +204,7 @@ class Matrix
#
def Matrix.diagonal(*values)
size = values.size
return Matrix.empty if size == 0
rows = Array.new(size) {|j|
row = Array.new(size, 0)
row[j] = values[j]

View file

@ -179,6 +179,7 @@ class TestMatrix < Test::Unit::TestCase
end
def test_diagonal
assert_equal(Matrix.empty(0, 0), Matrix.diagonal( ))
assert_equal(Matrix[[3,0,0],[0,2,0],[0,0,1]], Matrix.diagonal(3, 2, 1))
assert_equal(Matrix[[4,0,0,0],[0,3,0,0],[0,0,2,0],[0,0,0,1]], Matrix.diagonal(4, 3, 2, 1))
end