aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--lib/matrix.rb2
-rw-r--r--test/matrix/test_vector.rb15
3 files changed, 21 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index a7ac1dfceb..82aa55bfd3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Mon Sep 3 14:49:03 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * lib/matrix.rb (Vector#magnitude): accumulate squares of absolute
+ values to fix for complex vector. [ruby-dev:46100] [Bug #6966]
+
Mon Sep 3 10:09:36 2012 Martin Bosslet <Martin.Bosslet@googlemail.com>
* ext/openssl/extconf.rb: Detect OpenSSL_FIPS macro
diff --git a/lib/matrix.rb b/lib/matrix.rb
index 8f57c73904..00fa762da6 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -1770,7 +1770,7 @@ class Vector
# Vector[5,8,2].r => 9.643650761
#
def magnitude
- Math.sqrt(@elements.inject(0) {|v, e| v + e*e})
+ Math.sqrt(@elements.inject(0) {|v, e| v + e.abs2})
end
alias r magnitude
alias norm magnitude
diff --git a/test/matrix/test_vector.rb b/test/matrix/test_vector.rb
index 582509fd20..18660df574 100644
--- a/test/matrix/test_vector.rb
+++ b/test/matrix/test_vector.rb
@@ -131,4 +131,19 @@ class TestVector < Test::Unit::TestCase
assert_equal("Vector[1, 2, 3]", @v1.inspect)
end
+ def test_magnitude
+ assert_in_epsilon(3.7416573867739413, @v1.norm)
+ assert_in_epsilon(3.7416573867739413, @v4.norm)
+ end
+
+ def test_complex_magnitude
+ bug6966 = '[ruby-dev:46100]'
+ v = Vector[Complex(0,1), 0]
+ assert_equal(1.0, v.norm, bug6966)
+ end
+
+ def test_rational_magnitude
+ v = Vector[Rational(1,2), 0]
+ assert_equal(0.5, v.norm)
+ end
end