Math::GSL::Vector - Functions concerning vectors
use Math::GSL::Vector qw/:all/; my $vec1 = Math::GSL::Vector->new([1, 7, 94, 15 ]); my $vec2 = $vec1 * 5; my $vec3 = Math::GSL::Vector>new(10); # 10 element zero vector my $vec4 = $vec1 + $vec2; # set the element at index 1 to 9 # and the element at index 3 to 8 $vec3->set([ 1, 3 ], [ 9, 8 ]); my @vec = $vec2->as_list; # return elements as Perl list my $dot_product = $vec1 * $vec2; my $length = $vec2->length; my $first = $vec1->get(0);
Creates a new Vector of the given size.
my $vector = Math::GSL::Matrix->new(3);
You can also create and set directly the values of the vector like this :
my $vector = Math::GSL::Vector->new([2,4,1]);
Get the underlying GSL vector object created by SWIG, useful for using gsl_vector_* functions which do not have an OO counterpart.
my $vector = Math::GSL::Vector->new(3); my $gsl_vector = $vector->raw; my $stuff = gsl_vector_get($gsl_vector, 1);
Returns the minimum value contained in the vector.
my $vector = Math::GSL::Vector->new([2,4,1]); my $minimum = $vector->min;
my $vector = Math::GSL::Vector->new([2,4,1]); my $maximum = $vector->max;
Returns the number of elements contained in the vector.
my $vector = Math::GSL::Vector->new([2,4,1]); my $length = $vector->length;
Gets the content of a Math::GSL::Vector object as a Perl list.
my $vector = Math::GSL::Vector->new(3); ... my @values = $vector->as_list;
Gets the value of an of a Math::GSL::Vector object.
my $vector = Math::GSL::Vector->new(3); ... my @values = $vector->get(2);
You can also enter an array of indices to receive their corresponding values:
my $vector = Math::GSL::Vector->new(3); ... my @values = $vector->get([0,2]);
Sets values of an of a Math::GSL::Vector object.
my $vector = Math::GSL::Vector->new(3); $vector->set([1,2], [8,23]);
This sets the second and third value to 8 and 23.
Returns a copy of the vector, which has the same length and values but resides at a different location in memory.
my $vector = Math::GSL::Vector->new([10 .. 20]); my $copy = $vector->copy;
Here is a list of all the functions included in this module :
gsl_vector_alloc($x)
gsl_vector_calloc($x)
gsl_vector_alloc_from_block
gsl_vector_alloc_from_vector
gsl_vector_free($v)
gsl_vector_view_array($base, $n)
gsl_vector_const_view_array($base, $n)
gsl_vector_view_array_with_stride($base, $stride, $n)
gsl_vector_const_view_array_with_stride($base, $stride, $n)
gsl_vector_subvector($v, $offset, $n)
gsl_vector_subvector_with_stride($v, $offset, $stride, $size)
gsl_vector_const_subvector
gsl_vector_get($v, $i)
gsl_vector_set($v, $i, $x)
gsl_vector_ptr
gsl_vector_const_ptr
gsl_vector_set_zero($v)
gsl_vector_set_all($v, $x)
gsl_vector_set_basis($v, $i)
gsl_vector_fread($file, $v)
gsl_vector_fwrite($file, $v)
gsl_vector_fscanf($file, $v)
gsl_vector_fprintf($file, $v, $format)
gsl_vector_memcpy($dest, $src)
gsl_vector_reverse($v)
gsl_vector_swap($v, $v2)
gsl_vector_swap_elements($v, $i, $j)
gsl_vector_max($v)
gsl_vector_min($v)
gsl_vector_minmax($v)
gsl_vector_max_index($v)
gsl_vector_min_index($v)
gsl_vector_minmax_index
gsl_vector_add($v, $v2)
gsl_vector_sub($v, $v2)
gsl_vector_mul($v, $v2)
gsl_vector_div($v, $v2)
gsl_vector_scale($v, $x)
gsl_vector_add_constant($v, $x)
gsl_vector_isnull($v)
gsl_vector_ispos($v)
gsl_vector_isneg($v)
gsl_vector_isnonneg($v)
You have to add the functions you want to use inside the qw /put_funtion_here / with spaces between each function. You can also write use Math::GSL::Complex qw/:all/ to use all avaible functions of the module.
Precision on the vector_view type : every modification you'll make on a vector_view will also modify the original vector. For example, the following code will zero the even elements of the vector $v of length $size, while leaving the odd elements untouched :
$v_even= gsl_vector_subvector_with_stride ($v, 0, 2, $size/2);
gsl_vector_set_zero ($v_even->{vector});
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
Here is an example using both interfaces.
use Math::GSL::Vector qw/:all/; print "We'll create this vector : [0,1,4,9,16] \n"; my $vector = Math::GSL::Vector->new([0,1,4,9,16]); my ($min, $max) = gsl_vector_minmax_index($vector->raw); print "We then check the index value of the maximum and minimum values of the vector. \n"; print "The index of the maximum should be 4 and we received $max \n"; print "The index of the minimum should be 0 and we received $min \n"; print "We'll then swap the first and the third elements of the vector \n"; gsl_vector_swap_elements($vector->raw, 0, 3); my @got = $vector->as_list; print "The vector should now be like this : [9,1,4,0,16] \n"; print "and we received : [ @got ]\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.