[前][次][番号順一覧][スレッド一覧]

ruby-changes:60957

From: Marc-Andre <ko1@a...>
Date: Fri, 1 May 2020 16:31:21 +0900 (JST)
Subject: [ruby-changes:60957] 7d360efe92 (master): [ruby/matrix] Fix Matrix#unitary? [#14]

https://git.ruby-lang.org/ruby.git/commit/?id=7d360efe92

From 7d360efe92d2db11a4e51820ed2f52de36b3257f Mon Sep 17 00:00:00 2001
From: Marc-Andre Lafortune <github@m...>
Date: Thu, 30 Apr 2020 17:49:06 -0400
Subject: [ruby/matrix] Fix Matrix#unitary? [#14]


diff --git a/lib/matrix.rb b/lib/matrix.rb
index a0f1160..9d66503 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -983,11 +983,11 @@ class Matrix https://github.com/ruby/ruby/blob/trunk/lib/matrix.rb#L983
   #
   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 7dbb100..9206678 100644
--- a/test/matrix/test_matrix.rb
+++ b/test/matrix/test_matrix.rb
@@ -18,6 +18,7 @@ class TestMatrix < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/matrix/test_matrix.rb#L18
     @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 https://github.com/ruby/ruby/blob/trunk/test/matrix/test_matrix.rb#L793
     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
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]