summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/matrix.rb6
-rw-r--r--test/matrix/test_matrix.rb8
2 files changed, 11 insertions, 3 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb
index a0f116095c..9d6650370d 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -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
diff --git a/test/matrix/test_matrix.rb b/test/matrix/test_matrix.rb
index 7dbb1000d9..9206678261 100644
--- a/test/matrix/test_matrix.rb
+++ b/test/matrix/test_matrix.rb
@@ -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