From 156426bb1d535846938357b95e200463a63a4dac Mon Sep 17 00:00:00 2001 From: Dave Whipp Date: Wed, 2 Mar 2011 19:34:57 -0800 Subject: [PATCH] Added conjugate method; .abs (.length) return real length for complex vectors. --- lib/Math/Vector.pm | 7 ++++++- t/01-basics.t | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/Math/Vector.pm b/lib/Math/Vector.pm index ee209ac..7a427a9 100644 --- a/lib/Math/Vector.pm +++ b/lib/Math/Vector.pm @@ -46,9 +46,14 @@ class Math::Vector $a ⋅ $b; } + method conjugate() + { + return Math::Vector.new(@.coordinates>>.conjugate); + } + method Length() { - sqrt(self ⋅ self); + sqrt(self ⋅ self.conjugate).abs; } multi method abs() diff --git a/t/01-basics.t b/t/01-basics.t index 75721ee..87cedff 100644 --- a/t/01-basics.t +++ b/t/01-basics.t @@ -207,4 +207,7 @@ isa_ok($vl, Math::VectorWithLength, "Variable is of type Math::VectorWithLength" my $vlc = eval($vl.perl); isa_ok($vlc, Math::VectorWithLength, "eval'd perl'd variable is of type Math::VectorWithLength"); +my $complex_vector = Math::Vector.new(0, 3+4i); +is($complex_vector.abs, 5, "length of vector with complex vector is real"); + done;