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 = 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);
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 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
gsl_matrix_view_array_with_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)
The following functions are specific to matrices containing complex numbers :
gsl_matrix_complex_alloc
gsl_matrix_complex_calloc
gsl_matrix_complex_alloc_from_block
gsl_matrix_complex_alloc_from_matrix
gsl_vector_complex_alloc_row_from_matrix
gsl_vector_complex_alloc_col_from_matrix
gsl_matrix_complex_free
gsl_matrix_complex_submatrix
gsl_matrix_complex_row
gsl_matrix_complex_column
gsl_matrix_complex_diagonal
gsl_matrix_complex_subdiagonal
gsl_matrix_complex_superdiagonal
gsl_matrix_complex_subrow
gsl_matrix_complex_subcolumn
gsl_matrix_complex_view_array
gsl_matrix_complex_view_array_with_tda
gsl_matrix_complex_view_vector
gsl_matrix_complex_view_vector_with_tda
gsl_matrix_complex_const_submatrix
gsl_matrix_complex_const_row
gsl_matrix_complex_const_column
gsl_matrix_complex_const_diagonal
gsl_matrix_complex_const_subdiagonal
gsl_matrix_complex_const_superdiagonal
gsl_matrix_complex_const_subrow
gsl_matrix_complex_const_subcolumn
gsl_matrix_complex_const_view_array
gsl_matrix_complex_const_view_array_with_tda
gsl_matrix_complex_const_view_vector
gsl_matrix_complex_const_view_vector_with_tda
gsl_matrix_complex_get
gsl_matrix_complex_set
gsl_matrix_complex_ptr
gsl_matrix_complex_const_ptr
gsl_matrix_complex_set_zero
gsl_matrix_complex_set_identity
gsl_matrix_complex_set_all
gsl_matrix_complex_fread
gsl_matrix_complex_fwrite
gsl_matrix_complex_fscanf
gsl_matrix_complex_fprintf
gsl_matrix_complex_memcpy
gsl_matrix_complex_swap
gsl_matrix_complex_swap_rows
gsl_matrix_complex_swap_columns
gsl_matrix_complex_swap_rowcol
gsl_matrix_complex_transpose
gsl_matrix_complex_transpose_memcpy
gsl_matrix_complex_isnull
gsl_matrix_complex_ispos
gsl_matrix_complex_isneg
gsl_matrix_complex_add
gsl_matrix_complex_sub
gsl_matrix_complex_mul_elements
gsl_matrix_complex_div_elements
gsl_matrix_complex_scale
gsl_matrix_complex_add_constant
gsl_matrix_complex_add_diagonal
gsl_matrix_complex_get_row
gsl_matrix_complex_get_col
gsl_matrix_complex_set_row
gsl_matrix_complex_set_col
The following functions are the same as the previous enonced ones except that they work with other data types than double.
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
int
double
char
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 there : 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/; $m = gsl_matrix_alloc (10,3); for($i = 0; $i< 10; $i++){ for($j = 0; $j<3; $j++){ gsl_matrix_set($m, $i, $j, 0.23 + 100*$i + $j); } } for($i = 0; $i< 100; $i++){ # OUT OF RANGE ERROR for($j=0; $j<3; $j++){ print "m($i, $j) = " . gsl_matrix_get ($m, $i, $j) . "\n"; } } gsl_matrix_free ($m); use Math::GSL::Matrix qw/:all/; $m = gsl_matrix_alloc (100, 100); $a = gsl_matrix_alloc (100, 100); for($i = 0; $i < 100; $i++){ for($j = 0; $j < 100; $j++){ gsl_matrix_set ($m, $i, $j, 0.23 + $i + $j); } } The next program shows how to write a matrix to a file. $out = fopen("test.dat", "wb"); gsl_matrix_fwrite ($out, $m); fclose ($out); $in = fopen("test.dat", "rb"); gsl_matrix_fread ($in, $a); fclose($in); $k=0; for($i = 0; $i < 100; $i++){ for($j = 0; $j < 100; $j++){ $mij = gsl_matrix_get ($m, $i, $j); $aij = gsl_matrix_get ($a, $i, $j); if ($mij != $aij){ $k++ }; } } 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 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.