Menu

#398 $pdl->hdr items are lost after $pdl->reshape

normal
closed-fixed
kmx
None
5
2015-09-28
2015-09-21
kmx
No
my $x = ones(25);
$x->hdr->{demo} = "yes";
say $x->info; # PDL: Double D [25]
say $x->sum; # 25
say $x->hdr->{demo} ? "exists" : "NOT FOUND"; # here it's OK

$x->reshape(5,5);
say $x->info; # PDL: Double D [5,5]
say $x->sum; # 25
say $x->hdr->{demo} ? "exists" : "NOT FOUND"; ### item does not exist!!!!!

Discussion

  • Chris Marshall

    Chris Marshall - 2015-09-22

    It appears that reshape() makes pdl of the input argument with pdl(). If this is replaced by PDL::Core::topdl() instead then the hdr appears to be preserved. pdl() makes a copy which is where the hdr is dropped.

    sub PDL::reshape{
    if (@ == 2 && $[1] == -1) { # a slicing reshape that drops 1-dims
    return $[0]->slice( map { $==1 ? [0,0,0] : [] } $[0]->dims);
    }
    my $pdl = pdl($
    [0]);
    my $nelem = $pdl->nelem;
    my @dims = grep defined, @[1..$#_];
    for my $dim(@dims) { barf "reshape: invalid dim size '$dim'" if $dim < 0 }
    @dims = grep($
    != 1, $pdl->dims) if @dims == 0; # get rid of dims of size 1
    $pdl->setdims([@dims]);
    $pdl->upd_data;
    if ($pdl->nelem > $nelem) {
    my $tmp=$pdl->clump(-1)->slice("$nelem:-1");
    $tmp .= 0;
    }
    $_[0] = $pdl;
    return $pdl;
    }

     
  •  kmx

    kmx - 2015-09-27

    I have pushed the fix you have proposed to branch kmx-fix-for-SF#398

     
  • Chris Marshall

    Chris Marshall - 2015-09-28
    • status: open --> closed-fixed
    • assigned_to: kmx
     
  • Chris Marshall

    Chris Marshall - 2015-09-28

    Fixed in git master and will appear in next CPAN release.

     

Log in to post a comment.