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

[ruby/matrix] Fix Matrix#unitary? [#14]

This commit is contained in:
Marc-Andre Lafortune 2020-04-30 17:49:06 -04:00
parent c925cc01c5
commit 7d360efe92
2 changed files with 11 additions and 3 deletions

View file

@ -983,11 +983,11 @@ class Matrix
#
def unitary?
raise ErrDimensionMismatch unless square?
rows.each_with_index do |row, i|
column_count.times do |j|
rows.each_with_index do |row_i, i|
rows.each_with_index do |row_j, j|
s = 0
row_count.times do |k|
s += row[k].conj * rows[k][j]
s += row_i[k].conj * row_j[k]
end
return false unless s == (i == j ? 1 : 0)
end

View file

@ -18,6 +18,7 @@ class TestMatrix < Test::Unit::TestCase
@a3 = Matrix[[4, 1, -3], [0, 3, 7], [11, -4, 2]]
@a5 = Matrix[[2, 0, 9, 3, 9], [8, 7, 0, 1, 9], [7, 5, 6, 6, 5], [0, 7, 8, 3, 0], [7, 8, 2, 3, 1]]
@b3 = Matrix[[-7, 7, -10], [9, -3, -2], [-1, 3, 9]]
@rot = Matrix[[0, -1, 0], [1, 0, 0], [0, 0, -1]]
end
def test_matrix
@ -792,4 +793,11 @@ class TestMatrix < Test::Unit::TestCase
assert_in_epsilon(vectors[0][0], vectors[0][1])
assert_in_epsilon(-4 * vectors[1][0], vectors[1][1])
end
def test_unitary?
assert_equal true, @rot.unitary?
assert_equal true, ((0+1i) * @rot).unitary?
assert_equal false, @a3.unitary?
assert_raise(Matrix::ErrDimensionMismatch) { @m1.unitary? }
end
end