From 925cc94a2477be6f50192c7c21745c9f7d519bbf Mon Sep 17 00:00:00 2001 From: Thierry Moisan Date: Wed, 16 Jul 2008 19:01:54 -0400 Subject: [PATCH] Fixing ->row() and as_list inside Matrix. as_list now works in a far cleaner way. --- Matrix.i | 37 ++++++++++++++++++------------------- lib/Math/GSL/Matrix/Test.pm | 6 ++---- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/Matrix.i b/Matrix.i index b6aa7e6..80e08e3 100644 --- a/Matrix.i +++ b/Matrix.i @@ -439,38 +439,36 @@ Get the contents of a Math::GSL::Matrix object as a Perl list. sub as_list { my $self = shift; - my ($r,$c) = ($self->rows,$self->cols); - return - map { - gsl_matrix_get($self->raw, _index_to_row_col($_,$r,$c)) - } (0 .. $self->rows*$self->cols-1 ); -} - -# hackish -sub _index_to_row_col($$$) -{ - my ($k,$rows,$cols) = @_; - return ( ($rows == 1 ) ? 0 : int($k/$rows), $k % $cols ); + my $line; + my @part; + my @total; + for($line=0; $line<$self->rows; $line++){ + @part = map { + gsl_matrix_get($self->raw, $line, $_) + } (0 .. $self->cols-1 ); + push(@total, @part); + } + return @total; } sub row { my ($self, $row) = @_; croak (__PACKAGE__.'::$matrix->row($row) - invalid $row value') - unless (($row < $self->rows-1) and $row > 0); + unless (($row < $self->rows-1) and $row >= 0); - my $rowvec = gsl_vector_alloc($self->cols); + my $rowvec = Math::GSL::Vector->new($self->cols); my $rowmat = Math::GSL::Matrix->new(1,$self->cols); - my $status = gsl_matrix_get_row($rowvec, $self->raw, $row-1); + my $status = gsl_matrix_get_row($rowvec->raw, $self->raw, $row); croak (__PACKAGE__.'::gsl_matrix_get_row - ' . gsl_strerror($status) ) unless ( $status == $GSL_SUCCESS ); - $status = gsl_matrix_set_row($rowmat->raw, 0, $rowvec); + $status = gsl_matrix_set_row($rowmat->raw, 0, $rowvec->raw); croak (__PACKAGE__.'::gsl_matrix_set_row - ' . gsl_strerror($status) ) unless ( $status == $GSL_SUCCESS ); - + return $rowmat; } @@ -478,12 +476,13 @@ sub col { my ($self, $col) = @_; croak (__PACKAGE__."::\$matrix->col(\$col) - $col not a valid column") - unless ($col < $self->cols and $col > 0); + 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 $status = gsl_matrix_get_col($colvec->raw, $self->raw, $col-1); + my $status = gsl_matrix_get_col($colvec->raw, $self->raw, $col); +# return $colvec; $status = gsl_matrix_set_col($colmat->raw, 0, $colvec->raw); return $colmat; } diff --git a/lib/Math/GSL/Matrix/Test.pm b/lib/Math/GSL/Matrix/Test.pm index 042bf9c..eb40404 100644 --- a/lib/Math/GSL/Matrix/Test.pm +++ b/lib/Math/GSL/Matrix/Test.pm @@ -490,15 +490,13 @@ sub AS_LIST_ROW : Tests { sub ROW : Tests { my $matrix = Math::GSL::Matrix->new(5,5); map { gsl_matrix_set($matrix->raw, $_, $_, 5 + $_**2) } (0..4); - ok_similar( [qw/0 0 9 0 0/] , [$matrix->row(3)->as_list ] ); + ok_similar( [qw/0 0 9 0 0/] , [$matrix->row(2)->as_list ] ); } sub COL : Tests { - local $TODO = "borked"; my $matrix = Math::GSL::Matrix->new(3,3); map { gsl_matrix_set($matrix->raw, $_, $_, 7 + $_**2) } (0..2); - print Dumper [ $matrix->col(1)->as_list ]; - ok_similar( [ 7, 0, 0 ] , [$matrix->col(1)->as_list ] ); + ok_similar([7,0,0], [$matrix->col(0)->as_list]); } sub NEW_SETS_VALUES_TO_ZERO : Tests { -- 2.11.4.GIT