The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Math::Vector::Real - Real vector arithmetic in Perl

SYNOPSIS

  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...

DESCRIPTION

A simple pure perl module to manipulate vectors of any dimension.

The function V, always exported by the module, allows 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];

Extra methods

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.

$v = Math::Vector::Real->new(@components)

Equivalent to V(@components).

$zero = Math::Vector::Real->zero($dim)

Returns the zero vector of the given dimension.

$u = Math::Vector::Real->axis_versor($dim, $ix)

Returns a unitary vector of the given dimension parallel to the axis with index $ix (0-based).

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)
$u = $v->versor

Returns the versor for the given vector.

$wrapped = $v->wrap($w)

If $w is a vector, returns the result of wrapping the given vector in the box defined by $w.

If $w is a scalar, the hypercube of size $w is used as the wrapping box.

SEE ALSO

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.

COPYRIGHT AND LICENSE

Copyright (C) 2009, 2011 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.