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
Creates a new Matrix of the given size.
my $matrix = Math::GSL::Matrix->new(10,10);
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);
Set a matrix to the identity matrix, i.e. one on the diagonal and zero elsewhere.
my $I = $matrix->identity;
Set a matrix to the zero matrix.
$matrix->zero;
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;
Returns the number of rows in the matrix.
my $rows = $matrix->rows;
Returns the number of columns in the matrix.
my $cols = $matrix->cols;
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;
Returns true if a matrix is square, i.e. it has the same number of rows as columns, false otherwise.
Returns the determinant of a matrix (computed by LU decomposition) or dies if called on a non-square matrix.
my $det = $matrix->det();
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();
Returns the inverse of a matrix or dies when called on a non-square matrix.
my $inverse = $matrix->inverse;
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:
col()
row()
my $vector1 = $matrix->col(0)->as_vector; my $vector2 = $matrix->row(1)->as_vector;
Returns a matrix as a flattened Perl list.
my @values = $matrix->as_list;
Returns a row matrix of the row you enter.
my $matrix = Math::GSL::Matrix->new(3,3); ... my $matrix_row = $matrix->row(0);
Returns a col matrix of the column you enter.
my $matrix = Math::GSL::Matrix->new(3,3); ... my $matrix_col = $matrix->col(0);
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]); ...
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 :
gsl_matrix_alloc($i, $j)
gsl_matrix_calloc($i, $j)
gsl_matrix_alloc_from_block
gsl_matrix_free
gsl_matrix_alloc_from_matrix
gsl_vector_alloc_row_from_matrix
gsl_vector_alloc_col_from_matrix
gsl_matrix_submatrix($m, $k1, $k2, $n1, $n2)
gsl_matrix_row($m , $i)
gsl_matrix_column($m, $j)
gsl_matrix_diagonal($m)
gsl_matrix_subdiagonal($m, $k)
gsl_matrix_superdiagonal($m, $k)
gsl_matrix_subrow($m, $i, $offset, $n)
gsl_matrix_subcolumn($m, $j, $offset, $n)
gsl_matrix_view_array($base, $n1, $n2)
gsl_matrix_view_array_with_tda($base, $n1, $n2, $tda)
gsl_matrix_view_vector
gsl_matrix_view_vector_with_tda
gsl_matrix_const_submatrix
gsl_matrix_get($m, $i, $j)
gsl_matrix_set($m, $i, $j, $x)
gsl_matrix_ptr
gsl_matrix_const_ptr
gsl_matrix_set_zero($m)
gsl_matrix_set_identity($m)
gsl_matrix_set_all($m, $x)
gsl_matrix_fread($fh, $m)
gsl_matrix_fwrite($fh, $m)
gsl_matrix_fscanf($fh, $m)
gsl_matrix_fprintf($fh, $m, $format)
gsl_matrix_memcpy($dest, $src)
gsl_matrix_swap($m1, $m2)
gsl_matrix_swap_rows($m, $i, $j)
gsl_matrix_swap_columns($m, $i, $j)
gsl_matrix_swap_rowcol($m, $i, $j)
gsl_matrix_transpose($m)
gsl_matrix_transpose_memcpy($dest, $src)
gsl_matrix_max($m)
gsl_matrix_min($m)
gsl_matrix_minmax($m)
gsl_matrix_max_index($m)
gsl_matrix_min_index($m)
gsl_matrix_minmax_index($m)
gsl_matrix_isnull($m)
gsl_matrix_ispos($m)
gsl_matrix_isneg($m)
gsl_matrix_isnonneg($m)
gsl_matrix_add($a, $b)
gsl_matrix_sub($a, $b)
gsl_matrix_mul_elements($a, $b)
gsl_matrix_div_elements($a, $b)
gsl_matrix_scale($a, $x)
gsl_matrix_add_constant($a, $x)
gsl_matrix_add_diagonal($a, $x)
gsl_matrix_get_row($v, $m, $i)
gsl_matrix_get_col($v, $m, $i)
gsl_matrix_set_row($m, $i, $v)
gsl_matrix_set_col($m, $j, $v)
These are related to constant views of a matrix.
gsl_matrix_const_row
gsl_matrix_const_colum
gsl_matrix_const_diagonal
gsl_matrix_const_subdiagonal
gsl_matrix_const_superdiagonal
gsl_matrix_const_subrow
gsl_matrix_const_subcolumn
gsl_matrix_const_view_array
gsl_matrix_const_view_array_with_tda
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!
char
int
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 :
all
double
complex
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.
To install Math::GSL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Math::GSL
CPAN shell
perl -MCPAN -e shell install Math::GSL
For more information on module installation, please visit the detailed CPAN module installation guide.