Math::...:Vector(3pm)
π NAME π
This is the Math::GSL::Vector page from User Contributed Perl Documentation, bestie β part 1, and it's literally all about Functions concerning vectors. No cap. Vector math but make it Perl, and yes we're gonna keep every single detail. π«Ά
β¨ SYNOPSIS β¨
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);
# access raw GSL object to call low-level functions
my $raw = $vec1->raw;
my $element = gsl_vector_get($raw, 2);
Okay real talk: I copied every character, including the lil > moment in Math::GSL::Vector>new(10) β that's what the page says, so that's what we keep. Periodt. π
π¦ Objected Oriented Interface to GSL Math::GSL::Vector π¦
β¨ Math::GSL::Vector->new() β¨
Creates a new Vector of the given size. Slay.
my $vector = Math::GSL::Vector->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]);
π raw() π
Get the underlying GSL vector object created by SWIG, useful for using gsl_vector_* functions which do not have an OO counterpart. Raw access, main character energy, iykyk.
my $vector = Math::GSL::Vector->new(3);
my $gsl_vector = $vector->raw;
my $stuff = gsl_vector_get($gsl_vector, 1);
π swap() π
Exchanges the values in the vectors $v with $w by copying. Switcheroo bestie.
my $v = Math::GSL::Vector->new([1..5]);
my $w = Math::GSL::Vector->new([3..7]);
$v->swap( $w );
πΈ reverse() πΈ
Reverse the elements in the vector. It's giving backwards, but make it fashion.
$v->reverse;
β min() β
Returns the minimum value contained in the vector.
my $vector = Math::GSL::Vector->new([2,4,1]);
my $minimum = $vector->min;
π max() π
Returns the minimum value contained in the vector.
Yes bestie, the page literally says minimum here for max(). I am not fixing it, I am just reporting it. π
my $vector = Math::GSL::Vector->new([2,4,1]);
my $maximum = $vector->max;
π length() π
Returns the number of elements contained in the vector. The way it just counts and serves.
my $vector = Math::GSL::Vector->new([2,4,1]);
my $length = $vector->length;
π§ $v->norm($p) π§
Returns the p-norm of $v, which defaults to the Euclidean (p=2) norm when no argument is given. Obsessed with a good default.
my $euclidean_distance = $v->norm;
π«Ά normalize($p) π«Ά
Divide each element of a vector by its norm, hence creating a unit vector. Returns the vector for chaining. If you just want the value of the norm without changing the vector, use norm(). The default value for $p is 2, which gives the familiar Euclidean distance norm.
my $unit_vector = $vector->normalize(2);
is the same as
my $unit_vector = $vector->normalize;
π as_list() π
Gets the content of a Math::GSL::Vector object as a Perl list. Perl list reveal, iconic.
my $vector = Math::GSL::Vector->new(3);
...
my @values = $vector->as_list;
β¨ get() β¨
Gets the value of an of a Math::GSL::Vector object. (Yes, the page says "an of a" β we're keeping the lore, not editing it.)
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]);
π set() π
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.
π
copy() π
Returns a copy of the vector, which has the same length and values but resides at a different location in memory. Twin behavior but separate memory, so real.
my $vector = Math::GSL::Vector->new([10 .. 20]);
my $copy = $vector->copy;
gsl_vector_subvector($v, $offset, $n)
Return a vector_view type holding a subvector of $v, with a size of $size, starting from the $offset position.
gsl_vector_subvector_with_stride($v, $offset, $stride, $size)
Return a vector_view type holding a subvector of $v, with a size of $size, starting from the $offset position and with a $stride step in between each element of $v.
gsl_vector_const_subvectorβ listed here, no description in this page π«gsl_vector_get($v, $i)β return the$i-th element of a raw vector$v, where$vis aMath::GSL::Vectorobject. Like, for example:
my $third_element = gsl_vector_get($v->raw, 3);
gsl_vector_set($v, $i, $x)β return the vector$vwith his$i-th element set to$xgsl_vector_ptrβ listed here, no description in this page π«gsl_vector_const_ptrβ listed here, no description in this page π«gsl_vector_set_zero($v)β set all the elements of$vto0gsl_vector_set_all($v, $x)β set all the elements of$vto$xgsl_vector_set_basis($v, $i)β set all the elements of$vto0except for the$i-th element, which gets set to1, and return0if the operation succeeded,1otherwise.gsl_vector_fread($file, $v)β this one reads into the vector$vfrom the open stream$file(opened with thegsl_fopenfunction from theMath::GSLmodule) in binary format. Bestie,$vmust be preallocated with the correct length, because the function uses the size of$vto figure out how many bytes to read. Return value is0for success and1if there was a problem reading from the file.gsl_vector_fwrite($file, $v)β writes the elements of the vector$vto the stream$file(opened with thegsl_fopenfunction from theMath::GSLmodule) in binary format. Returns0for success and1if there was a problem writing to the file. Since the data is written in the native binary format it may not be portable between different architectures β so don't be shocked when it doesn't travel well, okay?gsl_vector_fscanf($file, $v)β reads formatted data from the stream$file(opened with thegsl_fopenfunction from theMath::GSLmodule) into the vector$v. Again:$vmust be preallocated with the correct length, since the function uses the size of$vto determine how many numbers to read. Returns0for success and1if there was a problem reading from the file.gsl_vector_fprintf($file, $v, $format)β writes the elements of the vector$vline-by-line to the stream$file(opened with thegsl_fopenfunction from theMath::GSLmodule) using the format specifier$format, which should be one of the"%g","%e"or"%f"formats for floating point numbers, and"%d"for integers. Returns0for success and1if there was a problem writing to the file.gsl_vector_memcpy($dest, $src)β copies the elements of the vector$srcinto the vector$destand returns0if the operation succeeded,1otherwise. The two vectors must have the same length. periodt.gsl_vector_reverse($v)β reverse the order of the elements of the vector$vand return0if the operation succeeded,1otherwise πgsl_vector_swap($v, $v2)β swap the values of the vectors$vand$v2and return0if the operation succeeded,1otherwisegsl_vector_swap_elements($v, $i, $j)β permute the elements at position$iand$jin the vector$vand return0if the operation succeeded,1otherwise.gsl_vector_max($v)β return the maximum value in the vector$vπgsl_vector_min($v)β return the minimum value in the vector$vgsl_vector_minmax($v)β return two values: the first is the minimum value in the vector$v, and the second is the maximum value.gsl_vector_max_index($v)β return the position of the maximum value in the vector$vgsl_vector_min_index($v)β return the position of the minimum value in the vector$vgsl_vector_minmax_indexβ return two values: the first is the position of the minimum value in the vector$v, and the second is the position of the maximum value.gsl_vector_add($v, $v2)β add the elements of$v2to the elements of$v. The two vectors must have the same length. Returns0if the operation succeeded,1otherwise.gsl_vector_sub($v, $v2)β subtract the elements of$v2from the elements of$v. The two vectors must have the same length. Returns0if the operation succeeded,1otherwise.gsl_vector_mul($v, $v2)β multiply the elements of$vby the elements of$v2. The two vectors must have the same length. Returns0if the operation succeeded,1otherwise.gsl_vector_div($v, $v2)β divides the elements of$vby the elements of$v2. The two vectors must have the same length. Returns0if the operation succeeded,1otherwise.gsl_vector_scale($v, $x)β multiply the elements of the vector$vby a constant$xand return0if the operation succeeded,1otherwise.gsl_vector_add_constant($v, $x)β add a constant$xto the elements of the vector$vand return0if the operation succeeded,1otherwise.gsl_vector_isnull($v)β verify if all the elements of the vector$vare null. Return0if it's the case,1otherwise.gsl_vector_ispos($v)β verify if all the elements of the vector$vare positive. Return0if it's the case,1otherwise.gsl_vector_isneg($v)β verify if all the elements of the vector$vare negative. Return0if it's the case,1otherwise.gsl_vector_isnonneg($v)β verify if all the elements of the vector$vare not negative. Return0if it's the case,1otherwise.
π Precision on the vector_view type π
Every single modification you make on a vector_view will also modify the original vector. That's the whole vibe, bestie β it's a view, not a copy, no cap. 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 information on the functions, we refer you to the GSL official documentation: http://www.gnu.org/software/gsl/manual/html_node/
π EXAMPLES π
Here is an example using both interfaces. Iconic of it, honestly:
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";
π AUTHORS π
Jonathan "Duke" Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
π COPYRIGHT AND LICENSE π
Copyright (C) 2008-2024 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.
okay bestie that's the whole page π«Ά go forth and allocate some vectors. this was Math::...:Vector(3pm) from the User Contributed Perl Documentation, all packaged up with perl v5.42.3 and dated 2026-08-20 β literally so slay, mwah π
πβ¨
π¦ where this page comes from
- ubuntu stonking: libmath-gsl-perl 0.49-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms