BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

Can pdl efficiently(*) perform the equivalent of the following operation on a piddle?

$pdl[ $_ -1 ] -= $pdl[ $_ ] for reverse 1 .. $#pdl;

(*)Ie. Without converting every element to a perl var, perform the math and then convert the result back, and assign it back.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re: PDL: efficient self-referencing math?
by Corion (Patriarch) on Sep 04, 2011 at 08:02 UTC

    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;

      That's an interesting approach.

      My piddle is just a 1D vector so maybe if I create a second 1D vector that is a copy of the first minus the first element, I can just subtract one from the other and produce the result I need?

      Hm. Need to play.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: PDL: efficient self-referencing math?
by etj (Priest) on May 26, 2022 at 13:31 UTC
    The posted code actually has a sort of cascade effect; it starts at the highest-numbered (last) value, subtracts that from the second-last, then moves down one, subtracting that from the one below, etc:
    pdl> p $x = floor random(5) * 10 [9 4 3 2 8] pdl> p Dumper($xperl = $x->unpdl) $VAR1 = ['9','4','3','2','8']; pdl> $xperl->[ $_ -1 ] -= $xperl->[ $_ ] for reverse 1 .. $#$xperl; pdl> p Dumper($xperl) $VAR1 = [14,-5,9,-6,8];
    The way to achieve that would be to do the below, but multiplying $to_sub by sequence($x->dim(0)) before subtracting. On the assumption this was not intended and instead the idea was to rotate the vector one over, then subtract that from the original:
    pdl> p $x = pdl '[9 4 3 2 8]' [9 4 3 2 8] pdl> $to_sub = $x->rotate(-1)->sever # MUST sever, or next mutates $x pdl> $to_sub(-1) .= 0 pdl> p $to_sub [4 3 2 8 0] pdl> p $x - $to_sub [5 1 1 -6 8]
Re: PDL: efficient self-referencing math?
by Anonymous Monk on Sep 04, 2011 at 14:54 UTC
    If a piddle happens, put the puppy outside at once.