Re: RFC 207 (v1) Array: Efficient Array Loops

[email protected] ("David L. Nicol") Tue, 12 Sep 2000 15:56:58 -0500
Newsgroups perl.perl6.language.data
Organization University of Missouri - Kansas City supercomputing infrastructure
Message-ID <[email protected]>

> Consider the problem of multiplying together two 2-dimensional tensors. In
> standard notation, this would be symbolized by
> 
>    Cijkl = Aij * Bkl
> 
> where the letters i, j, k and l are written as subscripts and represent
> the indices of their respective tensors. To accomplish that same
> multiplication in Perl, one needs to write (using RFC 204 notation):
> 
>   for my $i = (0..2) {    #assuming 3x3 tensors
>     for my $j = (0..2) {
>       for my $k = (0..2) {
>         for my $l = (0..2) {
>             $c[[$i,$j,$k,$l]] = $a[[$i,$j]] * $b[[$k,$l]] ;
>         }
>       }
>     }
>   }
> 
> While this is not particularly difficult, it is clumsy, and slow. 


Using $"-joined numbers as hash keys n-dim syntax, it becomes
something like

	my $outerkey;
	%C = map { $outerkey=$_; 
		map { ( $outerkey.$".$_ , $a{$outerkey} * $b{$_} ) }
			keys %b
	     } keys %a;


which isn't a whole lot cleaner, is it.