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 = $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 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 = $matrix2 * $matrix1; # scalar product of two matrices if($matrix1 == $matrix4) ... if($matrix1 != $matrix3) ... my $matrix8 = gsl_matrix_alloc(5,5); # standard interface

Math::GSL::Matrix->new()
Creates a new Matrix of the given size. my $matrix = Math::GSL::Matrix->new(10,10); =cut sub new { my ($class, $rows, $cols) = @_; my $this = {}; my $matrix; if ( defined $rows && defined $cols && $rows > 0 && $cols > 0 && (int $rows == $rows) && (int $cols == $cols)){ $matrix = gsl_matrix_alloc($rows,$cols); } else { croak( __PACKAGE__.'::new($x,$y) - $x and $y must be positive integers'); } gsl_matrix_set_zero($matrix); $this->{_matrix} = $matrix; ($this->{_rows}, $this->{_cols}) = ($rows,$cols); bless $this, $class; } =head2 raw() Get the underlying GSL matrix object created by SWIG, useful for using gsl_matrix_* functions which do not have an OO counterpart. my $matrix = Math::GSL::Matrix->new(3,3); my $gsl_matrix = $matrix->raw; my $stuff = gsl_matrix_get($gsl_matrix, 1, 2);

identity()
Set a matrix to the identity matrix, i.e. one on the diagonal and zero elsewhere. my $I = $matrix->identity;

zero()
Set a matrix to the zero matrix. $matrix->zero;

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;

rows()
Returns the number of rows in the matrix. my $rows = $matrix->rows; =cut sub rows { (shift)->{_rows} }

cols()
Returns the number of columns in the matrix. my $cols = $matrix->cols; =cut sub cols { (shift)->{_cols} }

as_list()
Get the contents of a Math::GSL::Matrix object as a Perl list. my $matrix = Math::GSL::Matrix->new(3,3); ... my @matrix = $matrix->as_list; =cut

is_square()
Returns true if a matrix is square, i.e. it has the same number of rows as columns, false otherwise.

det()
Returns the determinant of a matrix (computed by LU decomposition) or dies if called on a non-square matrix. my $det = $matrix->det();

lndet()
Returns the natural log of the absolute value of the determinant of a matrix (computed by LU decomposition) or dies if called on a non-square matrix. my $lndet = $matrix->lndet();

inverse()
Returns the inverse of a matrix or dies when called on a non-square matrix. my $inverse = $matrix->inverse;

eigenvalues()

as_vector()
Returns a 1xN or Nx1 matrix as a Math::GSL::Vector object. Dies if called on a matrix that is not a single row or column. Useful for turning the output of "col()" or "row()" into a vector, like so: my $vector1 = $matrix->col(0)->as_vector; my $vector2 = $matrix->row(1)->as_vector;

as_list()
Returns a matrix as a flattened Perl list. my @values = $matrix->as_list;

row()
Returns a row matrix of the row you enter. my $matrix = Math::GSL::Matrix->new(3,3); ... my $matrix_row = $matrix->row(0);

col()
Returns a col matrix of the column you enter. my $matrix = Math::GSL::Matrix->new(3,3); ... my $matrix_col = $matrix->col(0);

set_row()
Sets a the values of a row with the elements of an array. my $matrix = Math::GSL::Matrix->new(3,3); $matrix->set_row(0, [8, 6, 2]); You can also set multiple rows at once with chained calls: my $matrix = Math::GSL::Matrix->new(3,3); $matrix->set_row(0, [8, 6, 2]) ->set_row(1, [2, 4, 1]); ...

set_col()
Sets a the values of a column with the elements of an array. my $matrix = Math::GSL::Matrix->new(3,3); $matrix->set_col(0, [8, 6, 2]); You can also set multiple columns at once with chained calls: my $matrix = Math::GSL::Matrix->new(3,3); $matrix->set_col(0, [8, 6, 2]) ->set_col(1, [2, 4, 1]); ... Here is a list of all the functions included in this module : These are related to constant views of a matrix. The following functions are similar to those above but work with "char"'s and "int"'s. We are not quite sure if anyone wants these. Please speak up if you do and/or submit some patches to this documentation, please! You have to add the functions you want to use inside the qw /put_funtion_here /. You can also write use Math::GSL::Matrix qw/:all/ to use all avaible functions of the module. Other tags are also avaible, here is a complete list of all tags for this module : For more informations on the functions, we refer you to the GSL offcial documentation <http://www.gnu.org/software/gsl/manual/html_node/> Tip : search on google: site:http://www.gnu.org/software/gsl/manual/html_node/name_of_the_function_you_want Most of the examples from this section are perl versions of the examples at L<http://www.gnu.org/software/gsl/manual/html_node/Example-programs-for-matrices.html> The program below shows how to allocate, initialize and read from a matrix using the functions gsl_matrix_alloc, gsl_matrix_set and gsl_matrix_get. use Math::GSL::Matrix qw/:all/; my $m = gsl_matrix_alloc (10,3); for my $i (0..9){ for my $j (0..2){ gsl_matrix_set($m, $i, $j, 0.23 + 100*$i + $j); } } for my $i (0..99){ # OUT OF RANGE ERROR for my $j (0..2){ print "m($i, $j) = " . gsl_matrix_get ($m, $i, $j) . "\n"; } } gsl_matrix_free ($m); use Math::GSL::Matrix qw/:all/; my $m = gsl_matrix_alloc (100, 100); my $a = gsl_matrix_alloc (100, 100); for my $i (0..99){ for my $j (0..99){ gsl_matrix_set ($m, $i, $j, 0.23 + $i + $j); } } The next program shows how to write a matrix to a file. my $out = gsl_fopen("test.dat", "wb"); gsl_matrix_fwrite ($out, $m); gsl_fclose ($out); my $in = gsl_fopen("test.dat", "rb"); gsl_matrix_fread ($in, $a); gsl_fclose($in); my $k=0; for my $i (0..99){ for my $j (0..99){ $mij = gsl_matrix_get ($m, $i, $j); $aij = gsl_matrix_get ($a, $i, $j); $k++ if ($mij != $aij); } } gsl_matrix_free($m); gsl_matrix_free($a); print "differences = $k (should be zero)\n"; Jonathan Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com> Copyright (C) 2008-2009 Jonathan Leto and Thierry Moisan This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.