Skip to content

Commit

Permalink
Overload == and != for Matrix and update code in in MatrixObject pod
Browse files Browse the repository at this point in the history
  • Loading branch information
Moisan committed Jan 20, 2009
1 parent 63b2528 commit b2d551f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Changes
Expand Up @@ -14,7 +14,7 @@
- Added dot product to Matrix objects
- Fix various typos in documentation
- Fix warnings about overloaded operators in Matrix and BLAS
- Overload '==' and '!=' for MatrixComplex objects
- Overload '==' and '!=' for MatrixComplex and Matrix objects

=head1 v0.16 - December 14 2008 (Happy Snowpocalypse PDX!)

Expand Down
15 changes: 15 additions & 0 deletions pod/Matrix.pod
Expand Up @@ -29,6 +29,8 @@ use overload
'*' => \&_multiplication,
'+' => \&_addition,
'-' => \&_subtract,
'==' => \&_equal,
'!=' => \&_not_equal,
fallback => 1;

our @EXPORT_OK = qw/
Expand Down Expand Up @@ -292,6 +294,8 @@ Math::GSL::Matrix - Mathematical functions concerning Matrices

my $matrix6 = $matrix2 . 8; # Multiply every elements of $matrix2 by 8
my $matrix7 = $matrix2 * $matrix1; # scalar product of two matrices
if($matrix1 == $matrix4) ...
if($matrix1 != $matrix3) ...
my $matrix8 = gsl_matrix_alloc(5,5); # standard interface


Expand Down Expand Up @@ -744,6 +748,17 @@ sub _multiplication {
return $lcopy;
}

sub _equal {
my ($left, $right) = @_;
my $comp = Array::Compare->new;
return ($comp->compare([$left->as_list], [$right->as_list]));
}

sub _not_equal {
my ($left, $right) = @_;
return !(&_equal($left, $right));
}

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

Expand Down
7 changes: 3 additions & 4 deletions pod/MatrixComplex.pod
Expand Up @@ -164,8 +164,7 @@ sub _equal {

sub _not_equal {
my ($left, $right) = @_;
my $comp = Array::Compare->new;
return !($comp->compare([$left->as_list], [$right->as_list]));
return !(&_equal($left, $right));
}

sub copy {
Expand All @@ -188,8 +187,8 @@ Math::GSL::MatrixComplex - Complex Matrices
my $matrix1 = Math::GSL::MatrixComplex->new(5,5); # OO interface
my $matrix3 = $matrix1 + $matrix1;
my $matrix4 = $matrix1 - $matrix1;
if($matrix1 == $matrix1) ...
if($matrix1 != $matrix1) ...
if($matrix1 == $matrix4) ...
if($matrix1 != $matrix3) ...

my $matrix2 = gsl_matrix_complex_alloc(5,5); # standard interface

Expand Down
12 changes: 11 additions & 1 deletion t/Matrix.t
@@ -1,6 +1,6 @@
package Math::GSL::Matrix::Test;
use base q{Test::Class};
use Test::More tests => 235;
use Test::More tests => 237;
use strict;
use warnings;

Expand Down Expand Up @@ -753,4 +753,14 @@ sub MATRIX_INVERSE : Tests(3) {
dies_ok( sub { $B->inverse } , 'inverse of non square matrix dies' );
}

sub OVERLOAD_EQUAL : Tests(2) {
my $A = Math::GSL::Matrix->new(2,2)
->set_row(0, [1, 3] )
->set_row(1, [4, 2] );
my $B = $A->copy;
ok ( $A == $B, 'should be equal');
$B->set_row(0, [1,2]);
ok ( $A != $B, 'should not be equal');
}

Test::Class->runtests;

0 comments on commit b2d551f

Please sign in to comment.