Math::Vector::Real(3pm) | User Contributed Perl Documentation | Math::Vector::Real(3pm) |
Math::Vector::Real - Real vector arithmetic in Perl
use Math::Vector::Real; my $v = V(1.1, 2.0, 3.1, -4.0, -12.0); my $u = V(2.0, 0.0, 0.0, 1.0, 0.3); printf "abs(%s) = %d\n", $v, abs($b); my $dot = $u * $v; my $sub = $u - $v; # etc...
A simple pure perl module to manipulate vectors of any dimension.
The function "V", always exported by the module, allows one to create new vectors:
my $v = V(0, 1, 3, -1);
Vectors are represented as blessed array references. It is allowed to manipulate the arrays directly as far as only real numbers are inserted (well, actually, integers are also allowed because from a mathematical point of view, integers are a subset of the real numbers).
Example:
my $v = V(0.0, 1.0); # extending the 2D vector to 3D: push @$v, 0.0; # setting some component value: $v->[0] = 23;
Vectors can be used in mathematical expressions:
my $u = V(3, 3, 0); $p = $u * $v; # dot product $f = 1.4 * $u + $v; # scalar product and vector addition $c = $u x $v; # cross product, only defined for 3D vectors # etc.
The currently supported operations are:
+ * / - (both unary and binary) x (cross product for 3D vectors) += -= *= /= x= == != "" (stringfication) abs (returns the norm) atan2 (returns the angle between two vectors)
That, AFAIK, are all the operations that can be applied to vectors.
When an array reference is used in an operation involving a vector, it is automatically upgraded to a vector. For instance:
my $v = V(1, 2); $v += [0, 2];
Besides the common mathematical operations described above, the following methods are available from the package.
Note that all these methods are non destructive returning new objects with the result.
For instance:
Math::Vector::Real->axis_versor(5, 3); # V(0, 0, 0, 1, 0) Math::Vector::Real->axis_versor(2, 0); # V(1, 0)
It is equivalent to:
$u = $v / abs($v);
Long description:
Given the vector "W" and the canonical base "U1, U2, ...Un" such that "W = w1*U1 + w2*U2 +...+ wn*Un". For every component "wi" we can consider the infinite set of affine hyperplanes perpendicular to "Ui" such that they contain the point "j * wi * Ui" being "j" an integer number.
The combination of all the hyperplanes defined by every component define a grid that divides the space into an infinite set of affine hypercubes. Every hypercube can be identified by its lower corner indexes "j1, j2, ..., jN" or its lower corner point "j1*w1*U1 + j2*w2*U2 +...+ jn*wn*Un".
Given the vector "V", wrapping it by "W" is equivalent to finding where it lays relative to the lower corner point of the hypercube inside the grid containing it:
Wrapped = V - (j1*w1*U1 + j2*w2*U2 +...+ jn*wn*Un) such that ji*wi <= vi < (ji+1)*wi
In scalar context returns the difference between the two corners (the box diagonal vector).
For instance, given a point $v and an axis-aligned rectangle defined by two opposite corners ($c0 and $c1), this method can be used to find the point nearest to $v from inside the rectangle:
my $n = $v->nearest_in_box($c0, $c1);
Note that if $v lays inside the box, the nearest point is $v itself. Otherwise it will be a point from the box hyper-surface.
Degenerated cases where the length of any segment is (too close to) 0 are not supported.
Note that this method is destructive.
See also [http://en.wikipedia.org/wiki/Reflection_%28mathematics%29|reflection] and [http://en.wikipedia.org/wiki/Orthant|orthant].
In scalar context returns the normal vector.
The vectors on @v must be linearly independent. Otherwise a division by zero error may pop up or probably due to rounding errors, just a wrong result may be generated.
In scalar context returns just some unitary vector normal to $v.
Note that this two expressions are equivalent:
@b = $v->normal_base; @b = Math::Vector::Real->complementary_base($v);
See <http://en.wikipedia.org/wiki/Rotation_operator_(vector_space)>.
Passing the zero vector to some methods (i.e. "versor", "decompose", "normal_base", etc.) is not acceptable. In those cases, the module will croak with an "Illegal division by zero" error.
"atan2" is an exceptional case that will return 0 when any of its arguments is the zero vector (for consistency with the "atan2" builtin operating over real numbers).
In any case note that, in practice, rounding errors frequently cause the check for the zero vector to fail resulting in numerical instabilities.
The correct way to handle this problem is to introduce in your code checks of this kind:
if ($v->norm2 < $epsilon2) { croak "$v is too small"; }
Or even better, reorder the operations to minimize the chance of instabilities if the algorithm allows it.
The module Math::Vector::Real::XS reimplements most of the methods available from this module in XS. "Math::Vector::Real" automatically loads and uses it when it is available.
Math::Vector::Real::Random extends this module with random vector generation methods.
Math::GSL::Vector, PDL.
There are other vector manipulation packages in CPAN (Math::Vec, Math::VectorReal, Math::Vector), but they can only handle 3 dimensional vectors.
In order to report bugs you can send me and email to the address that appears below or use the CPAN RT bug-tracking system available at <http://rt.cpan.org>.
The source for the development version of the module is hosted at GitHub: <https://github.com/salva/p5-Math-Vector-Real>.
If you like this module and you're feeling generous, take a look at my wishlist: <http://amzn.com/w/1WU1P6IR5QZ42>
Copyright (C) 2009-2012, 2014-2017 by Salvador Fandiño (sfandino@yahoo.com)
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.
2022-10-22 | perl v5.36.0 |