Simon Cozens wrote:
> 
> On Mon, Mar 19, 2001 at 08:30:31AM -0800, Peter Scott wrote:
> > Seen http://dev.perl.org/rfc/82.pod?
> 
> I hadn't. I'm surprised it didn't give the PDL people screaming fits.
> But no, I wouldn't do it like that. It has:
> 
>  @b = (1,2,3);
>  @c = (2,4,6);
>  @d = @b * @c;   # Returns (2,8,18)
> 
> Where I would have @d = (2,4,6,4,8,12,6,12,18);

The first example above is called the scalar product
of two vectors in APL, and can be generalised for
all arithmetic operators.

The second example is called the outer or cross-product
of two vectors.


> However, this isn't great language design; it's applying a specific solution
> to a specific problem. Better is to solve the general problem, and have all
> operators overloadable even on non-objects, so the user can define how this
> sort of thing works.

Precisely. The operators for arrays (lists) should be defined
orthogonally and completely in a mathematically consistent
manner to be of general purpose use.

Here is one example of such a definition, freely
adapted from the APL syntax:


1. scalar array operators:  @a ? @b

   For any numerical operator '?', two compatible arrays
   can be combined with that operator producing a new array
   as follows:

   @r = @a ? @b 

   where

   $r[i] = $a[i] ? $b[i]

   complexity: if @a and @b are not the same length, the shorter
   is extended with the appropriate scalar value, i.e. 0 for
   additive and 1 for multiplicative operators.

   example:
   @a = (1,2,3);
   @b = (2,4,6);
   @r = @a + @b;   # @r = (3,6,9)

   As well, mixed array and scalar operands are allowed:

   @a + 10 produces (11,12,13)
   20 - @b produces (18,16,14)


2. reduction:  ?/@a

   Reduction introduces the new diglyphs that end in '/',
   and are preceded by a numerical operator, i.e.

   +/  -/  */  //

   Any numerical array can be reduced to a scalar value over
   a given numerical operator '?' as follows:

   $r = ?/@a

   where

   $r = $a[0] ? $a[1] ? $a[2] ...

   example:
   @a = (2,4,6);
   $r = */@a;  # $r = 36
   

3. inner product:  @a ?/! @b

   Inner product introduces the new triglyphs with '/' as the
   middle character, and surrounded by numerical operator, i.e.

   +/* */- -/* -/- etc.

   For any numerical operators '?' and '!', two compatible arrays
   can be combined with those operators producing a scalar
   inner product, as follows:

   $r = @a ?/! @b 

   where

   $r = ?/ (@a ! @b)

   i.e.

   $r = ($a[0] ! $b[0]) ? ($a[1] ! $b[1]) ? ...

   example:
   @a = (1,2,3);
   @b = (2,4,6);
   $r = @a +/* @b;   # @r = +/(2,8,18) = 28


4. outer product:  @a @? @b

   Outer product introduces the new diglyphs that start with  '@',
   and end with a numerical operator, i.e.

   @+ @- @* @/

   For any numerical operator '?', any two arrays can be combined
   into the cross-product with those operators producing a new array
   containing the outer product. Each row of the outer product is
   concatenated to the next to produce the result, best illustrated
   by the following example:

   @a = (1,2,3);
   @b = (2,4,6);
   @r = @a @* @b;

   @r = (2,4,6), (4,8,12), (6,12,18) = (2,4,6,4,8,12,6,12,18)


Of course, there are probably syntactic nightmares in introducing
the diglyphs and triglyphs mentioned above into perl.   

--
Rick Welykochy || Praxis Services Pty Limited

Reply via email to