Skip to content

Commit

Permalink
Merge branch 'bleed' of http://thgate.net/git/Math-GSL into bleed
Browse files Browse the repository at this point in the history
  • Loading branch information
leto committed Dec 7, 2008
2 parents 7e96f9c + 2960b90 commit 715fc6e
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 4 deletions.
2 changes: 1 addition & 1 deletion inc/GSLBuilder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ sub compile_swig {
$self->do_system(@swig, '-o', $c_file ,
'-outdir', $gsldir,
'-perl5', @swig_flags, $file)
or die "error : $! while building ( @swig_flags ) $c_file in $gsl_dir from '$file'";
or die "error : $! while building ( @swig_flags ) $c_file in $gsldir from '$file'";
print "Copying from: $from, to: $to; it makes the CPAN indexer happy.\n";
copy($from,$to);
return $c_file;
Expand Down
1 change: 0 additions & 1 deletion pod/Histogram2D.pod
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,4 @@ under the same terms as Perl itself.
=cut

%}
%include "../pod/Histogram2D.pod"

66 changes: 65 additions & 1 deletion pod/Matrix.pod
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
%perlcode %{

use Scalar::Util 'blessed';
use Carp qw/croak/;
use Math::GSL qw/:all/;
use Math::GSL::Errno qw/:all/;
use overload
'+' => \&_addition,
'-' => \&_subtract,
fallback => 1,

@EXPORT_OK = qw/
gsl_matrix_alloc gsl_matrix_calloc gsl_matrix_alloc_from_block
Expand Down Expand Up @@ -331,6 +336,8 @@ int => [ qw/
gsl_matrix_complex_set_col
/]);



=head1 NAME

Math::GSL::Matrix - Mathematical functions concerning Matrices
Expand All @@ -339,7 +346,10 @@ Math::GSL::Matrix - Mathematical functions concerning Matrices

use Math::GSL::Matrix qw/:all/;
my $matrix1 = Math::GSL::Matrix->new(5,5); # OO interface
my $matrix2 = gsl_matrix_alloc(5,5); # standard interface
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 = gsl_matrix_alloc(5,5); # standard interface


=head1 Objected Oriented Interface to GSL Math::GSL::Matrix
Expand Down Expand Up @@ -379,6 +389,26 @@ Get the underlying GSL matrix object created by SWIG, useful for using gsl_matri

=cut
sub raw { (shift)->{_matrix} }

=head2 copy()

Returns a copy of the matrix, which has the same size and values but resides at a different location in memory.

my $matrix = Math::GSL::Matrix->new(5,5);
my $copy = $matrix->copy;

=cut

sub copy {
my $self = shift;
my $copy = Math::GSL::Matrix->new( $self->rows, $self->cols );
if ( gsl_matrix_memcpy($copy->raw, $self->raw) != $GSL_SUCCESS ) {
croak "Math::GSL - error copying memory, aborting";
}
return $copy;
}


=head2 rows()

Returns the number of rows in the matrix.
Expand Down Expand Up @@ -529,6 +559,40 @@ sub set_col {
return $self;
}

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

my $lcopy = $left->copy;

if ( blessed $right && $right->isa('Math::GSL::Matrix') && blessed $left && $left->isa('Math::GSL::Matrix') ) {
if ( $left->cols == $right->cols && $left->rows == $right->rows ) {
gsl_matrix_add($lcopy->raw, $right->raw);
} else {
croak "Math::GSL - addition of matrices must be called with two objects matrices and must have the same number of columnss and rows";
}
} else {
gsl_matrix_add_constant($lcopy->raw, $right);
}
return $lcopy;
}

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

my $lcopy = $left->copy;

if ( blessed $right && $right->isa('Math::GSL::Matrix') && blessed $left && $left->isa('Math::GSL::Matrix') ) {
if ( $left->cols == $right->cols && $left->rows == $right->rows ) {
gsl_matrix_sub($lcopy->raw, $right->raw);
} else {
croak "Math::GSL - addition of matrices must be called with two objects matrices and must have the same number of columnss and rows";
}
} else {
gsl_matrix_add_constant($lcopy->raw, $right*-1);
}
return $lcopy;
}

=head1 DESCRIPTION

Here is a list of all the functions included in this module :
Expand Down
66 changes: 65 additions & 1 deletion t/Matrix.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package Math::GSL::Matrix::Test;
use base q{Test::Class};
use Test::More tests => 167;
use Test::More tests => 191;
use Math::GSL qw/:all/;
use Math::GSL::Test qw/:all/;
use Math::GSL::Matrix qw/:all/;
Expand Down Expand Up @@ -564,4 +564,68 @@ sub GSL_MATRIX_VIEW_ARRAY_WITH_TDA : Tests {
ok_similar([map { gsl_matrix_get($matrix_view->{matrix}, 0, $_) } 0..1], [8, 4]);
ok_similar([map { gsl_matrix_get($matrix_view->{matrix}, 1, $_) } 0..1], [7, 5]);
}

sub GSL_MATRIX_OO_ADDITION_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;
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]);

ok_similar([$m2->col(1)->as_list], [8,9,10]);
ok_similar([$m2->col(2)->as_list], [13,12,11]);
ok_similar([$m2->col(0)->as_list], [4,4,4]);
}

sub GSL_MATRIX_OO_ADDITION_MATRICES : Tests {
my $m = Math::GSL::Matrix->new(3,3);
$m->set_col(1, [4,5,6])
->set_col(2, [9,8,7])
->set_col(0, [1,2,3]);
my $m2 = $m + $m;
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]);


ok_similar([$m2->col(0)->as_list], [2,4,6]);
ok_similar([$m2->col(1)->as_list], [8,10,12]);
ok_similar([$m2->col(2)->as_list], [18,16,14]);
}

sub GSL_MATRIX_OO_SUBSTRACTION_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;
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]);

ok_similar([$m2->col(1)->as_list], [0,1,2]);
ok_similar([$m2->col(2)->as_list], [5,4,3]);
ok_similar([$m2->col(0)->as_list], [-4,-4,-4]);
}

sub GSL_MATRIX_OO_SUBSTRACTION_MATRICES : Tests {
my $m = Math::GSL::Matrix->new(3,3);
my $m3 = Math::GSL::Matrix->new(3,3);
$m->set_col(1, [4,5,6])
->set_col(2, [9,8,7])
->set_col(0, [1,2,3]);
$m3->set_col(1, [1,2,3])
->set_col(2, [9,8,7])
->set_col(0, [1,2,3]);
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]);


ok_similar([$m2->col(0)->as_list], [0,0,0]);
ok_similar([$m2->col(1)->as_list], [3,3,3]);
ok_similar([$m2->col(2)->as_list], [0,0,0]);
}
Test::Class->runtests;

0 comments on commit 715fc6e

Please sign in to comment.