Skip to content

Commit

Permalink
Put dot product into Matrix.pod but still failing for other reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
Moisan committed Dec 22, 2008
1 parent 2c1bc1b commit b21fd9d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 56 deletions.
29 changes: 0 additions & 29 deletions pod/BLAS.pod
@@ -1,21 +1,5 @@
%perlcode %{

#use Data::Dumper;
use Carp qw/croak/;
use Scalar::Util 'blessed';

#use Math::GSL qw/:all/;
#use Math::GSL::Errno qw/:all/;
#use Math::GSL::Eigen qw/:all/;
use Math::GSL::Vector qw/:all/;
#use Math::GSL::Complex qw/:all/;
use Math::GSL::Matrix qw/:all/;
use overload
'.' => \&_dotProduct,
fallback => 1;



@EXPORT_OK_level1 = qw/
gsl_blas_sdsdot gsl_blas_dsdot gsl_blas_sdot gsl_blas_ddot
gsl_blas_cdotu gsl_blas_cdotc gsl_blas_zdotu gsl_blas_zdotc
Expand Down Expand Up @@ -59,19 +43,6 @@ use overload
level3 => [ @EXPORT_OK_level3 ],
);

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

if (blessed $left && $left->isa('Math::GSL::Matrix') && blessed $right && $right->isa('Math::GSL::Matrix') ) {
if ( $left->rows == $right->cols ) {
#my $C = Math::GSL::Matrix->new($left->rows, $right->cols);
#gsl_blas_dgemm($CblasNoTrans, $CblasNoTrans, 1, $left->raw, $right->raw, 1, $C->raw);
} else {
croak "Math::GSL - dot product of matrices must be called with two objects matrices and left matrix must have the same number of rows than the number of columns of the right matrix";
} }
#return $C;
}

__END__

=head1 NAME
Expand Down
24 changes: 20 additions & 4 deletions pod/Matrix.pod
Expand Up @@ -8,14 +8,16 @@ use Math::GSL::Errno qw/:all/;
use Math::GSL::Eigen qw/:all/;
use Math::GSL::Vector qw/:all/;
use Math::GSL::Complex qw/:all/;
use Math::GSL::BLAS qw/gsl_blas_dgemm/;

# should only include needed methods
use Math::GSL::MatrixComplex qw/:all/;
use Math::GSL::VectorComplex qw/:all/;
use Data::Dumper;

use overload
'*' => \&_multiplication,
'*' => \&_dotProduct,
'.' => \&_multiplication,
'+' => \&_addition,
'-' => \&_subtract,
fallback => 1;
Expand Down Expand Up @@ -360,12 +362,13 @@ Math::GSL::Matrix - Mathematical functions concerning Matrices
my $matrix2 = $matrix1 + 4; # You can add or substract values or matrices to OO matrices
my $matrix3 = $matrix1 - 4;
my $matrix4 = $matrix2 + $matrix1;
my $matrix5 = $matrix2 * $matrix1; # This is NOT a scalar product, it simply multiply each element
my $matrix5 = $matrix2 . $matrix1; # This is a scalar product, it simply multiply each element
# with the element of $matrix1 that have the same position
# See Math::GSL::BLAS if you want scalar product

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


=head1 Objected Oriented Interface to GSL Math::GSL::Matrix
Expand Down Expand Up @@ -677,6 +680,19 @@ sub _multiplication {
return $lcopy;
}

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

if (blessed $left && $left->isa('Math::GSL::Matrix') && blessed $right && $right->isa('Math::GSL::Matrix') and $left->rows == $right->cols ) {
my $C = Math::GSL::Matrix->new($left->rows, $right->cols);
gsl_blas_dgemm($CblasNoTrans, $CblasNoTrans, 1, $left->raw, $right->raw, 1, $C->raw);
}
else {
croak "Math::GSL - dot product of matrices must be called with two objects matrices and left matrix must have the same number of rows than the number of columns of the right matrix";
}
return $C;
}

=head1 DESCRIPTION

Here is a list of all the functions included in this module :
Expand Down
21 changes: 2 additions & 19 deletions t/BLAS.t
@@ -1,6 +1,6 @@
package Math::GSL::BLAS::Test;
use base q{Test::Class};
use Test::More tests => 100;
use Test::More tests => 99;
use Math::GSL qw/:all/;
use Math::GSL::BLAS qw/:all/;
use Math::GSL::Vector qw/:all/;
Expand Down Expand Up @@ -630,22 +630,5 @@ sub GSL_BLAS_ZSYR2K : Tests {
ok_similar([gsl_parts(gsl_matrix_complex_get($C, 1, 1))], [24, 4]);
}

sub DOT_PRODUCT_OVERLOAD : Tests {
my $A = Math::GSL::Matrix->new(2,2);
gsl_matrix_set($A->raw, 0,0,1);
gsl_matrix_set($A->raw, 1,0,3);
gsl_matrix_set($A->raw, 0,1,4);
gsl_matrix_set($A->raw, 1,1,2);
my $B = Math::GSL::Matrix->new(2,2);
gsl_matrix_set($B->raw, 0,0,2);
gsl_matrix_set($B->raw, 1,0,5);
gsl_matrix_set($B->raw, 0,1,1);
gsl_matrix_set($B->raw, 1,1,3);
my $C = $A . $B;
print Dumper [ $C];
my @got = $C->row(0)->as_list;
ok_similar([@got], [22, 13]);
@got = $C->row(1)->as_list;
ok_similar([@got], [16, 9]);
}

Test::Class->runtests;
22 changes: 18 additions & 4 deletions t/Matrix.t
@@ -1,6 +1,6 @@
package Math::GSL::Matrix::Test;
use base q{Test::Class};
use Test::More tests => 226;
use Test::More tests => 228;
use Math::GSL qw/:all/;
use Math::GSL::Test qw/:all/;
use Math::GSL::Matrix qw/:all/;
Expand Down Expand Up @@ -637,7 +637,7 @@ sub GSL_MATRIX_OO_MULTIPLICATION_CONSTANT : Tests {
my $m = Math::GSL::Matrix->new(3,3);
$m->set_col(1, [4,5,6])
->set_col(2, [9,8,7]);
my $m2 = $m * 4;
my $m2 = $m . 4;
ok_similar([$m->col(2)->as_list], [9,8,7]);
ok_similar([$m->col(1)->as_list], [4,5,6]);
ok_similar([$m->col(0)->as_list], [0,0,0]);
Expand All @@ -646,7 +646,7 @@ sub GSL_MATRIX_OO_MULTIPLICATION_CONSTANT : Tests {
ok_similar([$m2->col(2)->as_list], [36,32,28]);
ok_similar([$m2->col(0)->as_list], [0,0,0]);

my $m3 = 4 * $m;
my $m3 = 4 . $m;
ok_similar([$m3->col(1)->as_list], [16,20,24]);
ok_similar([$m3->col(2)->as_list], [36,32,28]);
ok_similar([$m3->col(0)->as_list], [0,0,0]);
Expand All @@ -661,7 +661,7 @@ sub GSL_MATRIX_OO_MULTIPLICATION_MATRICES : Tests {
$m3->set_col(1, [1,2,3])
->set_col(2, [9,8,7])
->set_col(0, [1,2,3]);
my $m2 = $m * $m3;
my $m2 = $m . $m3;
ok_similar([$m->col(0)->as_list], [1,2,3]);
ok_similar([$m->col(2)->as_list], [9,8,7]);
ok_similar([$m->col(1)->as_list], [4,5,6]);
Expand Down Expand Up @@ -730,4 +730,18 @@ sub GSL_MATRIX_EIGENPAIR : Tests(11) {

}

sub DOT_PRODUCT_OVERLOAD : Tests {
my $A = Math::GSL::Matrix->new(2,2)
->set_row(0, [1,3] )
->set_row(1, [4, 2] );
my $B = Math::GSL::Matrix->new(2,2)
->set_row(0, [2,5] )
->set_row(1, [1, 3] );
my $C = $A * $B;
print Dumper [$C];
# my @got = $C->row(0)->as_list;
# ok_similar([@got], [22, 13]);
# @got = $C->row(1)->as_list;
# ok_similar([@got], [16, 9]);
}
Test::Class->runtests;

0 comments on commit b21fd9d

Please sign in to comment.