From aee5c8d55b9c403b044a46102e40aca5a729e7fb Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Sun, 21 Dec 2008 21:48:00 -0800 Subject: [PATCH] Refactor MatrixComplex::as_list and add tests for row()/col() --- pod/MatrixComplex.pod | 18 ++++++------------ t/MatrixComplex.t | 26 ++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/pod/MatrixComplex.pod b/pod/MatrixComplex.pod index 7a3efb0..f68e392 100644 --- a/pod/MatrixComplex.pod +++ b/pod/MatrixComplex.pod @@ -150,20 +150,14 @@ Get the contents of a Math::GSL::Matrix object as a Perl list. my @matrix = $matrix->as_list; =cut - -sub as_list +sub as_list($) { my $self = shift; - my $line; - my @part; - my @total; - for($line=0; $line<$self->rows; $line++){ - @part = map { - gsl_matrix_complex_get($self->raw, $line, $_) - } (0 .. $self->cols-1 ); - push(@total, @part); + my @matrix; + for my $row ( 0 .. $self->rows-1) { + push @matrix, map { gsl_matrix_complex_get($self->raw, $row, $_) } (0 .. $self->cols-1 ); } - return @total; + return @matrix; } =head2 row() @@ -207,7 +201,7 @@ sub col { my ($self, $col) = @_; croak (__PACKAGE__."::\$matrix->col(\$col) - $col not a valid column") - unless ($col < $self->cols and $col >= 0); + unless (defined $col && $col < $self->cols and $col >= 0); my $colmat = Math::GSL::MatrixComplex->new($self->rows, 1); diff --git a/t/MatrixComplex.t b/t/MatrixComplex.t index a588ca0..60eb350 100755 --- a/t/MatrixComplex.t +++ b/t/MatrixComplex.t @@ -1,10 +1,11 @@ package Math::GSL::MatrixComplex::Test; use base q{Test::Class}; -use Test::More tests => 5; +use Test::More tests => 12; use Math::GSL qw/:all/; use Math::GSL::Test qw/:all/; use Math::GSL::Errno qw/:all/; use Math::GSL::MatrixComplex qw/:all/; +use Math::GSL::Complex qw/:all/; use Math::Complex; use Data::Dumper; use strict; @@ -26,10 +27,31 @@ sub GSL_MATRIX_COMPLEX_NEW: Tests(3) { ok( $u->cols == 20, 'cols'); } -sub GSL_MATRIX_COMPLEX_SET : Tests(2) { +sub GSL_MATRIX_COMPLEX_SET : Tests(1) { + my $u = Math::GSL::MatrixComplex->new(2,2); + gsl_matrix_complex_set($u->raw, 0, 0, Math::GSL::Complex::gsl_complex_rect(3,5) ); + +} + +sub GSL_MATRIX_COMPLEX_COL : Tests(3) { + my $u = Math::GSL::MatrixComplex->new(2,2); + isa_ok( $u->col(0) , 'Math::GSL::MatrixComplex'); + cmp_ok( $u->col(0)->cols, '==', 1 ); + cmp_ok( $u->col(0)->rows, '==', 2 ); +} + +sub GSL_MATRIX_COMPLEX_ROW : Tests(3) { + my $u = Math::GSL::MatrixComplex->new(2,2); + isa_ok( $u->row(0) , 'Math::GSL::MatrixComplex'); + cmp_ok( $u->row(0)->cols, '==', 2 ); + cmp_ok( $u->row(0)->rows, '==', 1 ); +} + +sub GSL_MATRIX_COMPLEX_SET_OO : Tests(2) { my $u = Math::GSL::MatrixComplex->new(2,2); $u->set_row(0, [ 1+7*i, 2*i ] ) ->set_row(1, [ 3*i, -4 ] ); + warn Dumper [ $u->as_list ]; ok_similar( [ map { Re $_ } $u->as_list ], [ 1, 0, 0, -4 ] ); ok_similar( [ map { Im $_ } $u->as_list ], [ 7, 2, 3, 0 ] ); } -- 2.11.4.GIT