Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Composing operations on piddles

by lin0 (Curate)
on Jun 15, 2007 at 21:37 UTC ( #621528=snippet: print w/replies, xml ) Need Help??

What are Piddles?

They are a new data structure defined in the Perl Data Language. As indicated in RFC: Getting Started with PDL (the Perl Data Language):

Piddles are numerical arrays stored in column major order (meaning that the fastest varying dimension represent the columns following computational convention rather than the rows as mathematicians prefer). Even though, piddles look like Perl arrays, they are not. Unlike Perl arrays, piddles are stored in consecutive memory locations facilitating the passing of piddles to the C and FORTRAN code that handles the element by element arithmetic. One more thing to note about piddles is that they are referenced with a leading $

Now, given two piddles ($piddle1 and $piddle2) of the same size, this snippet shows you how you can easily compose operations in which the first operation is applied on an element by element basis. In this particular example, I was interested in finding the sum of the smaller elements. For the first operation, I used the minimum and for the second one, I used the sum. Note that for the first operation there are several options available (depending on what you want to do), such as minimum, maximum, average, and medover. For the second operation there are also several options available (depending, of course, on what you want to do), such as sum, prod, max, and min. For a more complete list of operations, please have a look at RFC: PDL Cheat Sheet.




  1. Added the definition of piddle. Thanks blazar for the suggestion
use PDL;

my $sum = sum( minimum( cat($piddle1->copy->flat, $piddle2->copy->flat
+)->xchg(0,1) ) );
Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://621528]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2023-12-10 10:20 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (39 votes). Check out past polls.