Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

matrix.rb: complex vector

* lib/matrix.rb (Vector#magnitude): accumulate squares of absolute
  values to fix for complex vector.  [ruby-dev:46100] [Bug #6966]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36887 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information...
commit 7c2230bd8c8a16a976091f983737d94ce52959d1 1 parent a4ce3ab
@nobu nobu authored
Showing with 21 additions and 1 deletion.
  1. +5 −0 ChangeLog
  2. +1 −1  lib/matrix.rb
  3. +15 −0 test/matrix/test_vector.rb
View
5 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
View
2  lib/matrix.rb
@@ -1770,7 +1770,7 @@ def collect(&block) # :yield: e
# 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
View
15 test/matrix/test_vector.rb
@@ -131,4 +131,19 @@ def test_inspect
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
Please sign in to comment.
Something went wrong with that request. Please try again.