Array::APX

Array Programming eXtensions
Download

Array::APX Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Dr. Bernd Ulmann
  • Publisher web site:
  • http://search.cpan.org/~vaxman/

Array::APX Tags


Array::APX Description

Array::APX is a Perl module that extends Perl-5 with some basic functionality commonly found in array programming languages like APL, Lang5 etc. It is basically a wrapper of Array::Deeputils and overloads quite some basic Perl operators in a way that allows easy manipulation of nested data structures. These data structures are basically blessed n-dimensional arrays that can be handled in a way similar to APL or Lang5.A nice example is the computation of a list of prime numbers using an archetypical APL solution. The basic idea is this: Create an outer product of two vectors . The resulting matrix does not contain any primes since every number is the product of at least two integers. Then check for every number in the original vector if it is a member of this matrix. If not, it must be a prime number. The set theoretic method 'in' returns a selection vector consisting of 0 and 1 values which can be used in a second step to select only the prime values from the original vector. Using Array::APX this can be written in Perl like this: use strict; use warnings; use Array::APX qw(:all); my $f = sub { $_ * $_ }; # We need an outer product my $x; print $x->select(!($x = iota(199) + 2)->in($x |$f| $x));How does this work? First a vector is created: $x = iota(99) + 2This vector is then used to create an outer product (basically a multiplication table without the 1-column/row: my $f = sub { $_ * $_ }; # We need an outer product ... $x |$f| $x ...The |-operator is used here as the generalized outer-'product'-operator (if applied to two APX data structures it would act as the bitwise binary or) - it expects a function reference like $f in the example above. Thus it is possible to create any outer 'products' - not necessarily based on multiplication only. Using the vector stored in $x and this two dimensional matrix, the in-method is used to derive a boolean vector that contains a 1 at every place corresponding to an element on the left hand operand that is contained in the right hand operand. This boolean vector is then inverted using the overloaded !-operator: !($x = iota(99) + 2)->in($x |$f| $x)Using the select-method this boolean vector is used to select the elements corresponding to places marked with 1 from the original vector $x thus yielding a vector of prime numbers between 2 and 100: print $x->select(!($x = iota(199) + 2)->in($x |$f| $x));SYNOPSIS use strict; use warnings; use Array::APX qw(:all); # Create two vectors and : my $x = iota(3); my $y = iota(3) + 3; print "The first vector is $x"; print "The second vector is $y\n"; # Add these vectors and print the result: print 'The sum of these two vectors is ', $x + $y, "\n"; # Create a function to multiply two values: my $f = sub { $_ * $_ }; # Create an outer product and print it: print "The outer product of these two vectors is\n", $x |$f| $y;yields The first vector is The second vector is The sum of these two vectors is The outer product of these two vectors is ]Product's homepage


Array::APX Related Software