Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

In some languages you can do vector arithmetic easily. Is there a way to do this in Perl? For example, (1, 2, 3) + (4, 5, 6) = ( 5, 7, 9)? Thank you very much.

Replies are listed 'Best First'.
Re: vector math
by TQuid (Sexton) on Jun 29, 2000 at 08:53 UTC
    If I am reading the Cookbook aright, you want to use the PDL module from CPAN. For your example:
    use PDL; $foo = pdl [ 1, 2, 3 ]; $bar = pdl [ 4, 5, 6 ]; $baz = $foo + $bar;
    I'm going to go verify this now, but it looks straightforward. --TQuid
(jcwren) Re: vector math
by jcwren (Prior) on Jun 29, 2000 at 08:46 UTC
    I've not used them, but you might check out the Math::MatrixReal module and see if that's what you're looking for. From the documentation:
    "Also features many important operations and methods: matrix norm, matrix transposition, matrix inverse, determinant of a matrix, order and numerical condition of a matrix, scalar product of vectors, vector product of vectors, vector length, projection of row and column vectors, a comfortable way for reading in a matrix from a file, the keyboard or your code, and many more."
    --Chris
(jeffa) Re: vector math
by jeffa (Bishop) on Jun 29, 2000 at 18:45 UTC
    The PDL module is very easy to use. Just remember to use Perl's overloaded x operator for multiplication, not *

    For example (borrowing from TQuid's answer):

    $baz = $foo x $bar;
    (well, this actually won't work with TQuid's matrices, but you get the point)

    "test"

      In fact, x in PDL is overloaded to achieve matrix multiplication, so the left- and right-hand operands need to be appropriately dimensioned (it will error out in a hopefully-informative way if not). But * operates on an element-by-element basis (using "broadcasting") over vectors, thus achieving part of a dot-product (there is in fact an inner operation that does an actual dot-product).
Re: vector math
by redmist (Deacon) on Jun 30, 2000 at 13:28 UTC
    Perhaps Math::VecStat would suit your needs (I happened upon it while looking for a module for calculating primes).

    It's description: "This package procides some basic statistics on numerical vectors. All the subroutines can take a copy of the vector, or, preferably for efficiency, a reference to the vector to be operated on.

    max(@vector), max(\@vector)

    return the maximum value of given values or vector. In an array context returns the value and the index in the array where it occurs.

    min(@vector), min(\@vector)

    return the minimum value of given values or vector, In an array context returns the value and the index in the array where it occurs.

    maxabs(@vector), maxabs(\@vector)

    return the maximum value of absolute of the given values or vector. In an array context returns the value and the index in the array where it occurs.

    minabs(@vector), minabs(\@vector)

    return the minimum value of the absolute of the given values or vector, In an array context returns the value and the index in the array where it occurs.

    sum($v1,$v2,...), sum(@vector), sum(\@vector)

    return the sum of the given values or vector average($v1,$v2,..), average(@vector), average(\@vector)

    return the average of the given values or vector."

    redmist