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 $v is a Math::GSL::Vector object. Like, for example:
my $third_element = gsl_vector_get($v->raw, 3);
  • gsl_vector_set($v, $i, $x) β€” return the vector $v with his $i-th element set to $x
  • gsl_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 $v to 0
  • gsl_vector_set_all($v, $x) β€” set all the elements of $v to $x
  • gsl_vector_set_basis($v, $i) β€” set all the elements of $v to 0 except for the $i-th element, which gets set to 1, and return 0 if the operation succeeded, 1 otherwise.
  • gsl_vector_fread($file, $v) β€” this one reads into the vector $v from the open stream $file (opened with the gsl_fopen function from the Math::GSL module) in binary format. Bestie, $v must be preallocated with the correct length, because the function uses the size of $v to figure out how many bytes to read. Return value is 0 for success and 1 if there was a problem reading from the file.
  • gsl_vector_fwrite($file, $v) β€” writes the elements of the vector $v to the stream $file (opened with the gsl_fopen function from the Math::GSL module) in binary format. Returns 0 for success and 1 if 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 the gsl_fopen function from the Math::GSL module) into the vector $v. Again: $v must be preallocated with the correct length, since the function uses the size of $v to determine how many numbers to read. Returns 0 for success and 1 if there was a problem reading from the file.
  • gsl_vector_fprintf($file, $v, $format) β€” writes the elements of the vector $v line-by-line to the stream $file (opened with the gsl_fopen function from the Math::GSL module) using the format specifier $format, which should be one of the "%g", "%e" or "%f" formats for floating point numbers, and "%d" for integers. Returns 0 for success and 1 if there was a problem writing to the file.
  • gsl_vector_memcpy($dest, $src) β€” copies the elements of the vector $src into the vector $dest and returns 0 if the operation succeeded, 1 otherwise. The two vectors must have the same length. periodt.
  • gsl_vector_reverse($v) β€” reverse the order of the elements of the vector $v and return 0 if the operation succeeded, 1 otherwise πŸ”„
  • gsl_vector_swap($v, $v2) β€” swap the values of the vectors $v and $v2 and return 0 if the operation succeeded, 1 otherwise
  • gsl_vector_swap_elements($v, $i, $j) β€” permute the elements at position $i and $j in the vector $v and return 0 if the operation succeeded, 1 otherwise.
  • gsl_vector_max($v) β€” return the maximum value in the vector $v πŸ‘‘
  • gsl_vector_min($v) β€” return the minimum value in the vector $v
  • gsl_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 $v
  • gsl_vector_min_index($v) β€” return the position of the minimum value in the vector $v
  • gsl_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 $v2 to the elements of $v. The two vectors must have the same length. Returns 0 if the operation succeeded, 1 otherwise.
  • gsl_vector_sub($v, $v2) β€” subtract the elements of $v2 from the elements of $v. The two vectors must have the same length. Returns 0 if the operation succeeded, 1 otherwise.
  • gsl_vector_mul($v, $v2) β€” multiply the elements of $v by the elements of $v2. The two vectors must have the same length. Returns 0 if the operation succeeded, 1 otherwise.
  • gsl_vector_div($v, $v2) β€” divides the elements of $v by the elements of $v2. The two vectors must have the same length. Returns 0 if the operation succeeded, 1 otherwise.
  • gsl_vector_scale($v, $x) β€” multiply the elements of the vector $v by a constant $x and return 0 if the operation succeeded, 1 otherwise.
  • gsl_vector_add_constant($v, $x) β€” add a constant $x to the elements of the vector $v and return 0 if the operation succeeded, 1 otherwise.
  • gsl_vector_isnull($v) β€” verify if all the elements of the vector $v are null. Return 0 if it's the case, 1 otherwise.
  • gsl_vector_ispos($v) β€” verify if all the elements of the vector $v are positive. Return 0 if it's the case, 1 otherwise.
  • gsl_vector_isneg($v) β€” verify if all the elements of the vector $v are negative. Return 0 if it's the case, 1 otherwise.
  • gsl_vector_isnonneg($v) β€” verify if all the elements of the vector $v are not negative. Return 0 if it's the case, 1 otherwise.

πŸŽ€ 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 (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