#241 dummy() does not add dimensions correctly under certain case

closed-fixed
core (120)
5
2010-07-10
2010-06-25
punkish
No

Using Craig DeForest's language, I seem to have discovered "a bug in dummy. It appears that dummy is meant to supply extra dummy dimensions of size 1 if you run off the end of the original dimension list. But the extra dummy dimension code appears to have a fencepost error, so that the one-dim-too-high case gives the same answer as the add-one-extra-dim-on-the-end case, (and the two-dims-too-high case gives the answer that should be given by the one-dim-too-high case)."

perldl> $a = sequence 5
perldl> p $a
[0 1 2 3 4]
$a Double D [5] P 0.04Kb

perldl> $b = $a->dummy(0, 3)
perldl> p $b
[
[0 0 0]
[1 1 1]
[2 2 2]
[3 3 3]
[4 4 4]
]
$b Double D [3,5] VC 0.00Kb

perldl> $c = $a->dummy(1, 3)
perldl> p $c
[
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
]
$c Double D [5,3] VC 0.00Kb

perldl> $d = $a->dummy(2, 3)
perldl> p $d
[
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
]
$d Double D [5,3] VC 0.00Kb

Discussion

  • Chris Marshall
    Chris Marshall
    2010-06-25

    There was an off-by-one error in PDL::dummy in Core.pm.
    I've fixed the problem and will push to git once the tests pass.

     
  • Chris Marshall
    Chris Marshall
    2010-06-25

    • assigned_to: nobody --> marshallch
    • status: open --> open-accepted
     
  • Chris Marshall
    Chris Marshall
    2010-06-25

    Here is the new version in case one would rather fix your Core.pm rather
    than update from git:

    sub PDL::dummy($$;$) {
    my ($pdl,$dim,$size) = @_;
    barf("Missing position argument to dummy()") unless defined $dim; # required argument
    $dim = $pdl->getndims+1+$dim if $dim < 0;
    $size = 1 unless defined($size);

    barf("For safety, <pos> < -(dims+1) forbidden in dummy. min="
    . -($pdl->getndims+1).", pos=". ($dim-1-$pdl->getndims) ) if($dim<0);

    my($s) = ',' x ( ($dim > $pdl->getndims) ? ($pdl->getndims) : ($dim) );
    $s .= '*1,' x ( $dim-$pdl->getndims );
    $s .= "*$size";

    $pdl->slice($s);
    }

    Thanks for the bug report. The ticket will close automatically in
    2 weeks if no further followup occurs.

     
  • Chris Marshall
    Chris Marshall
    2010-06-25

    • status: open-accepted --> pending-fixed
     
  • This Tracker item was closed automatically by the system. It was
    previously set to a Pending status, and the original submitter
    did not respond within 14 days (the time period specified by
    the administrator of this Tracker).

     
    • status: pending-fixed --> closed-fixed