From addd96a0ffa81100adffec477959351909f98cd5 Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Sat, 13 Dec 2008 01:07:38 -0800 Subject: [PATCH] Implemented swap() for VecorComplex objects --- pod/VectorComplex.pod | 19 ++++++++++++++++++- t/VectorComplex.t | 24 +++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/pod/VectorComplex.pod b/pod/VectorComplex.pod index fb10144..fc43d5b 100644 --- a/pod/VectorComplex.pod +++ b/pod/VectorComplex.pod @@ -234,7 +234,6 @@ Returns a copy of the vector, which has the same length and values but resides a =cut - sub copy { my $self = shift; my $copy = Math::GSL::VectorComplex->new( $self->length ); @@ -244,6 +243,24 @@ sub copy { return $copy; } +=head2 swap() + +Exchanges the values in the vectors $v with $w by copying. + + my $v = Math::GSL::VectorComplex->new([1..5]); + my $w = Math::GSL::VectorComplex->new([3..7]); + $v->swap( $w ); + +=cut + +sub swap() { + my ($self,$other) = @_; + croak "Math::GSL::swap : other object must be a Math::GSL::VectorComplex" + unless ref $other eq 'Math::GSL::VectorComplex'; + gsl_vector_complex_swap( $self->raw, $other->raw ); + return $self; +} + sub _multiplication { my ($left,$right) = @_; my $lcopy = $left->copy; diff --git a/t/VectorComplex.t b/t/VectorComplex.t index 91859eb..eb64e5f 100755 --- a/t/VectorComplex.t +++ b/t/VectorComplex.t @@ -1,6 +1,6 @@ package Math::GSL::VectorComplex::Test; use base q{Test::Class}; -use Test::More tests => 17; +use Test::More tests => 23; use Math::GSL::Test qw/:all/; use Math::GSL qw/:all/; use Math::GSL::VectorComplex qw/:all/; @@ -63,4 +63,26 @@ sub GSL_VECTOR_COMPLEX_REVERSE : Tests(5) { } +sub GSL_VECTOR_COMPLEX_SWAP : Tests(6) { + my $self = shift; + my $y = Math::Complex->make(1,1); + my $z = Math::Complex->make(0,3); + my $v = $self->{vector}; + my $w = Math::GSL::VectorComplex->new([ $y , 1 , 3 ]); + + ok_status( gsl_vector_complex_swap( $v->raw, $w->raw ) ); + ok_similar ( [ map { Re $_ } $v->as_list ], + [ map { Re $_ } $y , 1 , 3 ] ); + ok_similar ( [ map { Im $_ } $w->as_list ], + [ map { Im $_ } $z , $z ** 2, 3 ] ); + + isa_ok( $v->swap( $w ), 'Math::GSL::VectorComplex' ); + + ok_similar ( [ map { Re $_ } $w->as_list ], + [ map { Re $_ } $y , 1 , 3 ] ); + ok_similar ( [ map { Im $_ } $v->as_list ], + [ map { Im $_ } $z , $z ** 2, 3 ] ); + +} + Test::Class->runtests; -- 2.11.4.GIT