"John Porter" wrote:
> Sterin, Ilya wrote:
> > Don't really know which would be more helpful, since I first need to
find a
> > scenerio where I would use this facility, then what result would I
expect
> > once the shortest list runs out.
>
> Let us ask the PDL folks.
>
> In fact, I'm quite sure this has been done already.
>
Well, I'm not a PDL folk, but I'm a p6-data folk so perhaps I qualify.

The interest in non-matching indices comes in 'broadcasting', which,
assuming the element-wise operators mentioned by Larry, works like this:

  @b = (1,2,3);
  @c = (2,4,6);
  @d := @b :* @c;   # Returns (2,8,18)
  @e := 3 :* @c;    # Returns (6,12,18)

Notice that the scalar '3' is 'broadcast' across the length of @c just as if
it was the list (3,3,3).

Or if you prefer text-crunching examples to number crunching, it works like
this:

  @people = ('adam', 'eve ', 'bob ');
  @scores = (7,9,5);          # Score for each person
  @histogram := '#' :x @scores; # Returns ('xxxxxxx','xxxxxxxxx','xxxxx')
  print join("\n", @people . ' ' . @histogram);

Notice that the scalar '#' has been broadcast across the length of @scores.

For more information, see:
  http://dev.perl.org/rfc/82.html#Broadcasting
which explains the more interesting case of multidimensional broadcasting.
Note that this RFC is a little dated now, in that Larry has proposed the
adverb ':' to mean "apply element-wise", so the examples in the RFC really
need a ':' added before all the operators. Other than that nothing should
need to change.

For Pyton's implementation of this concept, see:
  http://starship.python.net/~da/numtut/array.html#SEC19

See also implementations in J and APL (which is the best role model), PDL,
functional languages like Haskell, and mathematical languages like
Mathematica.


Reply via email to