Home > OS >  Best pratice for accessing Data in PDL objects
Best pratice for accessing Data in PDL objects

Time:07-09

I have a 2 dimensional pdl,for example:

my $data  = random(4,4);

and I want to compute the sum of the 0,0 and the 1,0 element outside of the pdl context. (Not like, for example $data->slice('0,0') $data->slice('1,0), which still returns a pdl object) What is the correct way of doing this?

CodePudding user response:

If you're looking to extract individual elements as normal Perl scalars, use at instead of slice:

pdl> $pdl = random(4,4)
pdl> p $pdl->at(0,0)   $pdl->at(1,0)
0.288796754210711
pdl> p ref \($pdl->at(0,0)   $pdl->at(1,0))
SCALAR

To convert the entire ndarray object into nested Perl arrays, use unpdl

pdl> $aoa = random(4,4)->unpdl;
pdl> p ref $aoa
ARRAY
pdl> p ref $aoa->[0]
ARRAY

Note that the indexing of elements in the Perl arrays is the transverse of that done in the ndarray objects. For example,

pdl> p $pdl->at(1,0)
0.111869023064209

pdl> p $aoa->[1][0] # wrong
0.954887281829823

pdl> p $aoa->[0][1] # correct
0.111869023064209

CodePudding user response:

Diab's answer is perfect for the question as asked. However, since the question includes asking about "best practice", I would recommend that usually when using PDL, one should consider operating on everything at once, in array-programming style, and/or to consider how your code would operate when given higher-dimensional ndarrays (i.e. how it would "broadcast").

A way to make the given code broadcast so that it would give back a 1-element ndarray for a 4x4, but a 3-element if given a 4x4x3 ndarray:

use PDL;
sub sum_top_left { $_[0]->slice('(0),0:1')->sumover }
print $data = sequence(4,4)   3;
print sum_top_left($data);
print $data = sequence(4,4,3)   3;
print sum_top_left($data);
  • Related