develooper Front page | perl.perl6.language.data | Postings from September 2000

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

Thread Previous | Thread Next
From:
David L. Nicol
Date:
September 12, 2000 13:57
Subject:
Re: RFC 207 (v1) Array: Efficient Array Loops
Message ID:
39BE989A.A0698E8A@kasey.umkc.edu


> 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.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About