Math::GSL::Matrix(3pm) | User Contributed Perl Documentation | Math::GSL::Matrix(3pm) |
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 subtract 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
This module is part of the Math::GSL distribution. It defines a Perl insterface to GNU Scientific Library matrices.
There are two different (but not exclusive) ways to use this module: using the OO API, built manually over the GSL functions, or using directly the functions defined by GSL library.
Math::GSL::Matrix->new()
Creates a new Matrix of the given size.
my $matrix = Math::GSL::Matrix->new(10,10);
If by any chance you already have a gsl_matrix and want to "objectify" it, you can use the following constructor:
my $m = gsl_matrix_alloc(10, 20); # ... something my $matrix = Math::GSL::Matrix->new($m);
Note that $m is NOT copied. The new object will just refer to the old gsl_matrix.
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);
dim()
Returns the number of rows and columns in a matrix as an array.
my ($rows, $cols) = $matrix->dim;
Basically a shortcut to "rows" and "cols" methods.
rows()
Returns the number of rows in the matrix.
my $rows = $matrix->rows;
cols()
Returns the number of columns in the matrix.
my $cols = $matrix->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;
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;
get_elem()
Returns an element of a matrix.
my $matrix = Math::GSL::Matrix->new(3,3); ... my $middle = $matrix->get_elem(1,1);
NOTE: just like any other method on this module, rows and arrays start with indice 0.
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);
max()
Computes the maximum value of a matrix. In array context returns the maximum value and the position of that element in the matrix. If the matrix is a vector (being it vertical or horizontal) only one coordinate is returned: the position of the element in the vector.
$max = $matrix->max(); ($max, $i, $j) = $matrix->max();
min()
Computes the minimum value of a matrix. In array context returns the minimum value and the position of that element in the matrix. If the matrix is a vector (being it vertical or horizontal) only one coordinate is returned: the position of the element in the vector.
$min = $matrix->min(); ($min, $i, $j) = $matrix->min();
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;
set_elem()
Sets a specific value in the matrix.
my $matrix = Math::GSL::Matrix->new(2,2); $matrix->set_elem(0, 0, $value);
You can set multiple elements at once with chained calls:
$matrix->set_elem(0,0,1)->set_elem(1,1,1);
NOTE: just like any other method on this module, rows and arrays start with indice 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]);
...
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;
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();
write
Saves ONE matrix into a file using the GSL binary format.
$matrix->save("matrix.dat");
Note that this method always overwrite the file if it exists. In case more than one GSL object will be written to the file, please use the "gsl_fopen", "gsl_matrix_fwrite" and "gsl_fclose" manually. Future versions might include helper for this task.
NOTE: in order to allow the read of one of these files without needing to know in advance the size of the matrix, an horizontal matrix with two elements is saved in the beginning of the file with the number of rows and columns of the saved matrix.
read
Loads a matrix from a GSL binary file (saved using the "save" method, as it stores some extra information on the file).
my $matrix = Math::GSL::Matrix->read("matrix.dat");
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;
transpose()
Returns the transpose of a matrix or dies when called on a non-square matrix.
my $transposed = $matrix->transpose;
eigenvalues()
Computes the a matrix eigen values.
eigenpair()
ieach()
Applied a function to each element of a matrix.
# compute exp to each element of matrix $matrix->ieach( sub { exp(shift) });
This method changes the original matrix. See "each" for a non destructive method.
each()
Applied a function to each element of a matrix.
# compute exp to each element of matrix $matrix->each( sub { exp(shift) });
This method returns a new matrix.
vconcat ()
Concatenates vertically a new matrix with the object matrix, where the object matrix is above in the final matrix. Note that both matrices need to have the same number of columns. The method returns a new matrix.
my $final = $m1->vconcat($m2); # $final is $m1 on top of $m2
hconcat ()
Concatenates horizontally a new matrix with the object matrix, where the object matrix is at the left in the final matrix. Note that both matrices need to have the same number of rows. The method returns a new matrix.
my $final = $m1->hconcat($m2); # $final is $m1 at the left of $m2
random ()
Fills a matrix with random values between 0 and 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_function_here /. You can also write use Math::GSL::Matrix qw/:all/ to use all available functions of the module. Other tags are also available, here is a complete list of all tags for this module :
For more information on the functions, we refer you to the GSL official documentation <http://www.gnu.org/software/gsl/manual/html_node/>
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 "Duke" Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
Copyright (C) 2008-2021 Jonathan "Duke" Leto and Thierry Moisan
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2022-10-20 | perl v5.36.0 |