Jarkko Hietaniemi wrote:
> I simply can't get over the feeling that the proposed
> zip/unzip/partition functions are far too specialized/simple,

That's certainly a possibility. They are such common operations though, it
might be a win to build them in. With zip/unzip/partition and good array
slicing syntax it is possible to construct many n-dim matrix transforms and
functions.

> and that
> something more general-purpose in the order of pack/unpack (with the
> transformation spec encoded in a template) for lists would be preferable.

That's one of the things RFC 81--Lazily evaluated list generation functions,
covers. Using a generated list as the indexes to an array provides
completely flexible array transformations.

> When someone said that matrix/unmatrix would be better I did not find
> that to be a joke: on the contrary, what we are talking here would be
> a mapping from n-dim arrays to p-dim arrays.  Just simply thinking in
> 1-dim lists/arrays doesn't cut it.
>
Of course. But RFCs 81, 82, 90, and 91 provide between them all the parts
required for matrix operations over any number of dimensions. Even although
the basic platform is a 1d array, n-dim operations are provided for through
generated lists, zip, unzip, and partition. For instance, let's take the
example from RFC 91 and modify it to calculate column sums from a 2d matrix:

  # Add all the elements of a list together, returning the result
  $sum = reduce (^total + ^element, @^elements);
  # Swap the rows and columns of a list of lists
  $transpose = partition(
    # Find the size of each column
    scalar @^list_of_lists,
    # Interleave the rows
    zip(@^lists_of_lists);
  )

  # Take a list of references to lists, and return an array of each
  # sub-list's sum
  $sum_cols = reduce (
    push (@^total, $sum->( @^next_list )),
    $transpose->(^list_of_lists),
  );

  # Example usage of $sum_mult
  @a = (1,3,5);
  @b = (2,4,6);
  @c = (-1,1,-1);
  @answer = @{$sum_cols->(\@a, \@b, \@c)};   #
1*2*-1,3*4*1,5*6*-1=(-2,12,-30)

Mind you, I don't think your average Perl hacker should have to worry about
all this--it would be nice if Perl also provided some easy way to use n-dim
arrays directly. However, with the building blocks I've described the n-dim
stuff could be written in pure Perl. I'm not sure that is the best way--but
it's certainly one way (and the way C++ took, when it introduced the 1d
valarray--see Stroustrup, "The C++ Programming Language, 3rd Edition",
pp662-679).

I'm still trying to work out what the alternative might look like--a set of
language constructs that operated on n-dim arrays directly. This is really
hard, but there are some good starting points in:
- PDL: pdl.perl.org
- Blitz++: http://oonumerics.org/blitz/
- POOMA: http://www.acl.lanl.gov/pooma/

PDL uses a special language ('PP') that lets the programmer explicitly
specify loops over specific dimensions. Blitz++ and POOMA are more
adventerous, providing advanced iterator/index classes that operate over
n-dim arrays in defined ways, but require much more work from the compiler.

Of course, if we go down this route, we would need to ensure that related
RFCs (like 'reduce') can handle using these kinds of arrays and iterators.


Reply via email to