ruby-changes:24836
From: nobu <ko1@a...>
Date: Mon, 3 Sep 2012 14:49:19 +0900 (JST)
Subject: [ruby-changes:24836] nobu:r36887 (trunk): matrix.rb: complex vector
nobu 2012-09-03 14:49:06 +0900 (Mon, 03 Sep 2012) New Revision: 36887 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=36887 Log: matrix.rb: complex vector * lib/matrix.rb (Vector#magnitude): accumulate squares of absolute values to fix for complex vector. [ruby-dev:46100] [Bug #6966] Modified files: trunk/ChangeLog trunk/lib/matrix.rb trunk/test/matrix/test_vector.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 36886) +++ ChangeLog (revision 36887) @@ -1,3 +1,8 @@ +Mon Sep 3 14:49:03 2012 Nobuyoshi Nakada <nobu@r...> + + * 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@g...> * ext/openssl/extconf.rb: Detect OpenSSL_FIPS macro Index: lib/matrix.rb =================================================================== --- lib/matrix.rb (revision 36886) +++ lib/matrix.rb (revision 36887) @@ -1770,7 +1770,7 @@ # 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 Index: test/matrix/test_vector.rb =================================================================== --- test/matrix/test_vector.rb (revision 36886) +++ test/matrix/test_vector.rb (revision 36887) @@ -131,4 +131,19 @@ 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/