From f67a116c2c8978b8a450efec788e785e8e7a9703 Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Sun, 21 Dec 2008 14:47:38 -0800 Subject: [PATCH] Refactoring row()/col() in Matrix/MatrixComplex as well trying to get set_row to work on MatrixComplex --- examples/matrix/nonsymmetric_eigen | 2 +- examples/matrix/nonsymmetric_eigen_old | 2 +- pod/Matrix.pod | 10 +++--- pod/MatrixComplex.pod | 63 +++++++++++++++++----------------- pod/VectorComplex.pod | 2 +- t/MatrixComplex.t | 13 +++++-- 6 files changed, 50 insertions(+), 42 deletions(-) diff --git a/examples/matrix/nonsymmetric_eigen b/examples/matrix/nonsymmetric_eigen index a15864f..83e1e26 100755 --- a/examples/matrix/nonsymmetric_eigen +++ b/examples/matrix/nonsymmetric_eigen @@ -1,4 +1,4 @@ -#!/usr/bin/perl5.10 -w +#!/usr/bin/perl -w use Math::GSL::Matrix; use strict; my $matrix = Math::GSL::Matrix->new(2,2) diff --git a/examples/matrix/nonsymmetric_eigen_old b/examples/matrix/nonsymmetric_eigen_old index 90f7749..490c696 100755 --- a/examples/matrix/nonsymmetric_eigen_old +++ b/examples/matrix/nonsymmetric_eigen_old @@ -1,4 +1,4 @@ -#!/usr/bin/perl5.10 -w +#!/usr/bin/perl -w use Math::GSL qw/:all/; use Math::GSL::Eigen qw/:all/; use Math::GSL::Matrix qw/:all/; diff --git a/pod/Matrix.pod b/pod/Matrix.pod index e7a60ee..35a1823 100644 --- a/pod/Matrix.pod +++ b/pod/Matrix.pod @@ -561,14 +561,12 @@ sub col croak (__PACKAGE__."::\$matrix->col(\$col) - $col not a valid column") unless ($col < $self->cols and $col >= 0); - my $colvec = Math::GSL::Vector->new($self->cols); my $colmat = Math::GSL::Matrix->new($self->rows, 1); - my @got; - for my $n (0 .. $self->rows-1) { - push (@got, gsl_matrix_get($self->raw, $n, $col)); - } - map { gsl_matrix_set($colmat->raw, $_, 0, $got[$_]) } (0 .. $self->rows-1); + map { gsl_matrix_set($colmat->raw, $_, 0, + gsl_matrix_get($self->raw, $_, $col), + ) + } (0 .. $self->rows-1); return $colmat; } diff --git a/pod/MatrixComplex.pod b/pod/MatrixComplex.pod index 5d37ff8..7a3efb0 100644 --- a/pod/MatrixComplex.pod +++ b/pod/MatrixComplex.pod @@ -1,9 +1,11 @@ -%perlcode %{ +%perlcode %{ use Carp qw/croak/; use Math::GSL qw/:all/; +use Math::GSL::Complex qw/:all/; use Math::GSL::Errno qw/:all/; use Math::Complex; +use Data::Dumper; @EXPORT_OK = qw/ gsl_matrix_complex_alloc @@ -182,12 +184,11 @@ sub row my $rowmat = Math::GSL::MatrixComplex->new(1,$self->cols); - my @got; for my $n (0 .. $self->cols-1) { - push (@got, gsl_matrix_complex_get($self->raw, $row, $n)); + gsl_matrix_complex_set( $rowmat->raw, 0, $n, + gsl_matrix_complex_get($self->raw, $row, $n) + ); } - for my $n (0 .. $self->cols-1) { - gsl_matrix_complex_set($rowmat->raw, 0, $n, $got[$n]); } return $rowmat; } @@ -202,21 +203,18 @@ Returns a col matrix of the column you enter. =cut -sub col +sub col { my ($self, $col) = @_; - croak (__PACKAGE__."::\$matrix->col(\$col) - $col not a valid column") - unless ($col < $self->cols and $col >= 0); + croak (__PACKAGE__."::\$matrix->col(\$col) - $col not a valid column") + unless ($col < $self->cols and $col >= 0); - my $colvec = Math::GSL::Vector->new($self->cols); my $colmat = Math::GSL::MatrixComplex->new($self->rows, 1); - my @got; - for my $n (0 .. $self->rows-1) { - push (@got, gsl_matrix_complex_get($self->raw, $n, $col)); - } - for my $n (0 .. $self->rows-1) { - gsl_matrix_complex_set($colmat->raw, $n, 0, $got[$n]); } + map { gsl_matrix_complex_set($colmat->raw, $_, 0, + gsl_matrix_complex_get($self->raw, $_, $col), + ) + } (0 .. $self->rows-1); return $colmat; } @@ -237,13 +235,16 @@ You can also set multiple rows at once with chained calls: =cut sub set_row { - my ($self, $row, $values) = @_; - my $length = $#$values; - die __PACKAGE__.'::new($x, $values) - $values must be a nonempty array reference' if $length == -1; - die __PACKAGE__.'::set_row($x, $values) - $x must be a valid row number' if ($row < 0 || $row >= $self->rows); - die __PACKAGE__.'::set_row($x, $values) - $values must contains the same number of elements as there is columns in the matrix' if($length != $self->cols-1); - map { gsl_matrix_complex_set($self->raw, $row, $_, $values->[$_]) } (0..$length); - return $self; + my ($self, $row, $values) = @_; + my $length = $#$values; + die __PACKAGE__.'::set_row($x, $values) - $values must be a nonempty array reference' if $length == -1; + die __PACKAGE__.'::set_row($x, $values) - $x must be a valid row number' if ($row < 0 || $row >= $self->rows); + die __PACKAGE__.'::set_row($x, $values) - $values must contains the same number of elements as there is columns in the matrix' if($length != $self->cols-1); + # $values may have Math::Complex objects + @$values = map { Math::GSL::Complex::gsl_complex_rect(Re($_), Im($_)) } @$values; + # warn Dumper [ @$values ]; + map { gsl_matrix_complex_set($self->raw, $row, $_, $values->[$_]) } (0..$length); + return $self; } =head2 set_col() @@ -262,19 +263,19 @@ You can also set multiple columns at once with chained calls: =cut sub set_col { - my ($self, $col, $values) = @_; - my $length = $#$values; - die __PACKAGE__.'::new($x, $values) - $values must be a nonempty array reference' if $length == -1; - die __PACKAGE__.'::set_row($x, $values) - $x must be a valid column number' if ($col < 0 || $col >= $self->cols); - die __PACKAGE__.'::set_row($x, $values) - $values must contains the same number of elements as there is rowss in the matrix' if($length != $self->rows-1); - map { gsl_matrix_complex_set($self->raw, $_, $col, $values->[$_]) } (0..$length); - return $self; + my ($self, $col, $values) = @_; + my $length = $#$values; + die __PACKAGE__.'::set_col($x, $values) - $values must be a nonempty array reference' if $length == -1; + die __PACKAGE__.'::set_col($x, $values) - $x must be a valid column number' if ($col < 0 || $col >= $self->cols); + die __PACKAGE__.'::set_col($x, $values) - $values must contains the same number of elements as there is rowss in the matrix' if($length != $self->rows-1); + # $values may have Math::Complex objects + @$values = map { gsl_complex_rect(Re($_), Im($_)) } @$values; + map { gsl_matrix_complex_set($self->raw, $_, $col, $values->[$_]) } (0..$length); + return $self; } =head1 DESCRIPTION -The following functions are specific to matrices containing complex numbers : - =over 1 =item C diff --git a/pod/VectorComplex.pod b/pod/VectorComplex.pod index a732e6b..488e484 100644 --- a/pod/VectorComplex.pod +++ b/pod/VectorComplex.pod @@ -74,7 +74,7 @@ sub new { my $this = {}; my $vector; - # we expected $values to have Math::Complex objects + # we expect $values to have Math::Complex objects @$values = map { gsl_complex_rect(Re($_), Im($_)) } @$values; if ( ref $values eq 'ARRAY' ){ diff --git a/t/MatrixComplex.t b/t/MatrixComplex.t index ae0f8ef..a588ca0 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 => 3; +use Test::More tests => 5; 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::Complex; use Data::Dumper; use strict; @@ -18,11 +19,19 @@ sub teardown : Test(teardown) { unlink 'matrix' if -f 'matrix'; } -sub GSL_MATRIX_COMPLEX_NEW: Tests { +sub GSL_MATRIX_COMPLEX_NEW: Tests(3) { my $u = Math::GSL::MatrixComplex->new(10,20); isa_ok($u, 'Math::GSL::MatrixComplex'); ok( $u->rows == 10, 'rows'); ok( $u->cols == 20, 'cols'); } +sub GSL_MATRIX_COMPLEX_SET : 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 ] ); + ok_similar( [ map { Re $_ } $u->as_list ], [ 1, 0, 0, -4 ] ); + ok_similar( [ map { Im $_ } $u->as_list ], [ 7, 2, 3, 0 ] ); +} + Test::Class->runtests; -- 2.11.4.GIT