Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Get complex subtraction operator overloading working

  • Loading branch information...
commit b69a5b4528a9e923965e43a94df6858cd2121548 1 parent c9908ff
@leto authored
Showing with 38 additions and 6 deletions.
  1. +14 −5 pod/Complex.pod
  2. +23 −0 t/Complex.t
  3. +1 −1  t/Matrix.t
View
19 pod/Complex.pod
@@ -11,7 +11,7 @@ use Scalar::Util 'blessed';
use overload
# '*' => \&_multiplication,
'+' => \&_addition,
-# '-' => \&_subtract,
+ '-' => \&_subtract,
# '==' => \&_equal,
# '!=' => \&_not_equal,
fallback => 1;
@@ -64,6 +64,16 @@ sub copy {
return $copy;
}
+sub _subtract {
+ my ($left, $right) = @_;
+ my $rcopy = $right->copy;
+ my $raw = gsl_complex_negative($right->raw);
+
+ $rcopy->set_raw($raw);
+
+ return _addition($left, $rcopy);
+}
+
sub _addition {
my ($left, $right) = @_;
@@ -86,24 +96,23 @@ sub set_raw {
sub new {
my ($class, @values) = @_;
- #die Dumper [ @values ];
my $this = {};
$this->{_complex} = gsl_complex_rect($values[0], $values[1]);
bless $this, $class;
}
sub real {
my ($self) = @_;
- gsl_real($self->{_complex}->{dat});
+ gsl_real($self->raw);
}
sub imag {
my ($self) = @_;
- gsl_imag($self->{_complex}->{dat});
+ gsl_imag($self->raw);
}
sub parts {
my ($self) = @_;
- gsl_parts($self->raw());
+ gsl_parts($self->raw);
}
sub raw { (shift)->{_complex} }
View
23 t/Complex.t
@@ -478,6 +478,21 @@ sub GSL_COMPLEX_ARCCOTH : Tests {
);
}
+sub GSL_COMPLEX_SUBTRACT_OVERLOAD : Tests {
+ my $a = Math::GSL::Complex->new(1,1);
+ my $b = Math::GSL::Complex->new(5,3);
+ my $c = $a - $b;
+ ok_similar( [ gsl_real($c->raw), gsl_imag($c->raw) ],
+ [ -4.0, -2.0 ],
+ 'complex addition OO using gsl_real/gsl_imag'
+ );
+
+ ok_similar( [ $c->parts() ],
+ [ -4.0, -2.0 ],
+ 'complex addition OO using parts()'
+ );
+}
+
sub GSL_COMPLEX_ADD_OVERLOAD : Tests {
my $a = Math::GSL::Complex->new(1,1);
my $b = Math::GSL::Complex->new(5,3);
@@ -493,4 +508,12 @@ sub GSL_COMPLEX_ADD_OVERLOAD : Tests {
);
}
+sub GSL_COMPLEX_REAL_IMAG : Tests {
+ my $z = Math::GSL::Complex->new(5,3);
+ ok_similar( [ $z->real, $z->imag ],
+ [ 5.0, 3.0 ],
+ '->real and ->imag work correctly'
+ );
+}
+
Test::Class->runtests;
View
2  t/Matrix.t
@@ -1,7 +1,7 @@
package Math::GSL::Matrix::Test;
use base q{Test::Class};
-use Test::More tests => 279;
+use Test::Most;
use strict;
use warnings;
Please sign in to comment.
Something went wrong with that request. Please try again.