зеркало из https://github.com/github/ruby.git
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
This commit is contained in:
Родитель
a4ce3ab4fd
Коммит
7c2230bd8c
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче