Skip to content

Commit

Permalink
Fix complex addition OO
Browse files Browse the repository at this point in the history
  • Loading branch information
leto committed Aug 31, 2014
1 parent 9001181 commit c9908ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
22 changes: 17 additions & 5 deletions pod/Complex.pod
@@ -1,6 +1,7 @@
%perlcode %{
use Scalar::Util 'blessed';
use Math::GSL::Errno qw/$GSL_SUCCESS gsl_strerror/;
use Data::Dumper;

use strict;
use warnings;
Expand Down Expand Up @@ -56,25 +57,36 @@ memory.

sub copy {
my $self = shift;
my $copy = Math::GSL::Complex->new( [$self->real, $self->imag] );
my $copy = Math::GSL::Complex->new(
gsl_real($self->raw), gsl_imag($self->raw)
);

return $copy;
}

sub _addition {
my ($left, $right, $flip) = @_;
my ($left, $right) = @_;

my $lcopy = $left->copy;
my $raw;

if ( blessed $right && $right->isa('Math::GSL::Complex') && blessed $left && $left->isa('Math::GSL::Complex') ) {
gsl_complex_add($lcopy->raw, $right->raw);
$raw = gsl_complex_add($lcopy->raw, $right->raw);
} else {
gsl_complex_add_constant($lcopy->raw, $right);
$raw = gsl_complex_add_constant($lcopy->raw, $right);
}
$lcopy->set_raw($raw);
return $lcopy;
}

sub set_raw {
my ($self, $raw) = @_;
$self->{_complex} = $raw;
}

sub new {
my ($class, @values) = @_;
#die Dumper [ @values ];
my $this = {};
$this->{_complex} = gsl_complex_rect($values[0], $values[1]);
bless $this, $class;
Expand All @@ -91,7 +103,7 @@ sub imag {

sub parts {
my ($self) = @_;
gsl_parts($self->{_complex}->{dat});
gsl_parts($self->raw());
}

sub raw { (shift)->{_complex} }
Expand Down
33 changes: 18 additions & 15 deletions t/Complex.t
@@ -1,4 +1,6 @@
package Math::GSL::Complex::Test;
use strict;
use warnings;
use base q{Test::Class};
use Test::Most;
use Math::GSL::Complex qw/:all/;
Expand All @@ -7,7 +9,6 @@ use Math::GSL::Errno qw/:all/;
use Math::GSL::Const qw/:all/;
use Math::GSL qw/:all/;
use Data::Dumper;
use strict;
BEGIN { gsl_set_error_handler_off() }

sub make_fixture : Test(setup) {
Expand All @@ -25,9 +26,15 @@ sub GSL_COMPLEX_NEW : Tests {
isa_ok( Math::GSL::Complex->new(0,0), 'Math::GSL::Complex' );
}

sub GSL_COMPLEX_RECT : Tests {
sub GSL_COMPLEX_RECT : Tests(2) {
my $x = gsl_complex_rect(5,3);
ok_similar( [ gsl_parts($x) ], [ 5, 3 ], 'gsl_complex_rect' );

my $a = gsl_complex_rect(1.2, 3.4);
ok_similar( [ gsl_parts($a) ],
[ 1.2, 3.4 ],
'gsl_complex_rect with floats'
);
}

sub GSL_COMPLEX_POLAR : Tests {
Expand Down Expand Up @@ -102,14 +109,6 @@ sub GSL_COMPLEX_EXP_EULERS_IDENTITY : Tests { # e^(i pi) + 1 = 0
);
}

sub GSL_COMPLEX_ADD_OO : Tests {
my $x = gsl_complex_rect(5,3);
my $y = gsl_complex_rect(1,2);

my $z = $x + $y;
ok_similar( [ gsl_parts($z) ], [ 6, 5 ], 'gsl_complex_add with operator overloading' );
}

sub GSL_COMPLEX_ADD : Tests {
my $x = gsl_complex_rect(5,3);
my $y = gsl_complex_rect(1,2);
Expand Down Expand Up @@ -481,13 +480,17 @@ sub GSL_COMPLEX_ARCCOTH : Tests {

sub GSL_COMPLEX_ADD_OVERLOAD : Tests {
my $a = Math::GSL::Complex->new(1,1);
my $b = 42;
my $b = Math::GSL::Complex->new(5,3);
my $c = $a + $b;
ok_similar( [ gsl_parts($c) ],
[ 43.0, 1.0 ],
'complex op overloading'
ok_similar( [ gsl_real($c->raw), gsl_imag($c->raw) ],
[ 6.0, 4.0 ],
'complex addition OO using gsl_real/gsl_imag'
);

ok_similar( [ $c->parts() ],
[ 6.0, 4.0 ],
'complex addition OO using parts()'
);
}

Test::Class->runtests;
done_testing;

0 comments on commit c9908ff

Please sign in to comment.