http://www.perlmonks.org?node_id=924048


in reply to PDL: efficient self-referencing math?

I haven't used PDL at all, but maybe you have enough dimensions in your piddle to make the following approach efficient:

  1. Create a matrix to calculate the shifted, negative piddle (can be cached)
  2. Multiply the source piddle by the matrix to create the shifted negative source piddle
  3. Add the source piddle and the new piddle

The matrix would be (or, would be)

( 0 0 0 ...) ( -1 0 0 ...) ( 0 -1 0 ...) ( 0 0 -1 ...) ...

Reading from the PDL documentation, basically the following code should work (while PDL is installing):

# 4 dimensions my $matrix = pdl([[0,0,0,0],[-1,0,0,0],[0,-1,0,0],[0,0,-1,0]]); my $pdl = pdl(...); my $diff = $pdl x $matrix; # not sure about whether to use left- or ri +ght-multiplication here my $res = $pdl + $diff;