In my recent post on applying PDL threading to vectorize a graphics calculation, I ended up needing an explicit loop over higher dimensions to get the indadd accumulation to work. This session shows what I tried, expected, and actually got:
> pdl> p $N = sequence(5); # shape [5] > [0 1 2 3 4] > > pdl> $ind = floor(4*random(5))->long; # shape [5] > pdl> p $ind > [2 1 2 1 3] > > pdl> $sum = zeros(4); > pdl> indadd 1,3,$sum > pdl> p $sum > [0 0 0 1] > > pdl> $sum = zeros(4); > pdl> indadd $N,3,$sum > pdl> p $sum > [0 0 0 10] > > pdl> $sum = zeros(4); > pdl> indadd 1,$ind,$sum > pdl> p $sum > [0 3 2 0] > > pdl> $sum = zeros(4); > pdl> indadd $N,$ind,$sum > pdl> p $sum > [0 3 7 0] > > pdl> $sum = zeros(4,2) > pdl> indadd $N(:,*2),$ind(:,*2),$sum # shape [5,2] > Runtime error: PDL: PDL::Primitive::indadd(a,ind,sum): Parameter 'sum': > Mismatched implicit thread dimension 0: should be 5, is 0 > at (eval 575) line 5. I thought it would thread over dim(1) so I could do all the coordinates with the implicit looping. I wonder what the correct implementation would be and if it is even possible. Is there a way to get it to work? Here is the pod for indadd: > indadd > Signature: (a(); indx ind(); [o] sum(m)) > > Threaded Index Add: Add "a" to the "ind" element of "sum", i.e: > > sum(ind) += a > > Simple Example: > > $a = 2; > $ind = 3; > $sum = zeroes(10); > indadd($a,$ind, $sum); > print $sum > #Result: ( 2 added to element 3 of $sum) > # [0 0 0 2 0 0 0 0 0 0] > > Threaded Example: > > $a = pdl( 1,2,3); > $ind = pdl( 1,4,6); > $sum = zeroes(10); > indadd($a,$ind, $sum); > print $sum."\n"; > #Result: ( 1, 2, and 3 added to elements 1,4,6 $sum) > # [0 1 0 0 2 0 3 0 0 0] > > The routine barfs if any of the indices are bad. Suggestions and discussion welcome! Thanks, Chris _______________________________________________ Perldl mailing list [email protected] http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
