Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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) ) );
Comment on Composing operations on piddles
Download Code

Back to Snippets Section

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2014-07-26 20:18 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (179 votes), past polls