PDL::Tips (1)
Leading comments
Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) Standard preamble: ========================================================================
NAME
PDL::Tips - Small tidbits of useful arcana. Programming tidbits and such.SYNOPSIS
use PDL; # Whatever happens here.
DESCRIPTION
This page documents useful idioms, helpful hints and tips for using Perl Data Language v2.0.Help
Use "help help" within perldl or pdl2 or use the "pdldoc" program from the command line for access to the PerlDL documentation.
pdl> foreach ( map{"$_/PDL/HtmlDocs"}@INC ) { p "$_\n" if -d $_ }
Indexing idioms
The following code normalizes a bunch of vectors in $a. This works regardless of the dimensionality of $a.
$a /= $a->sumover->dummy(0);
What is actually happening?
If you want to see what the code is actually doing, try the command
PDL::Core::set_debugging(1);
somewhere. This spews out a huge amount of debug info for
Many of the messages come from "Basic/Core/pdlapi.c" and you can look at the source to see what is going on.
If you have any extra time to work on these mechanisms, inform the pdl-porters mailing list.
Memory savings
If you are running recursively something that selects certain indices of a large piddle, like
while(1) { $inds = where($a>0); $a = $a->index($inds); $b = $b->index($inds); func($b,$a); }
If you are not writing to $b, it saves a lot of memory to change this to
$b = $b->index($inds)->sever;
The new method "sever" is a causes the write-back relation to be forgotten. It is like copy except it changes the original piddle and returns it).
Of course, the probably best way to do the above is
$inds = xvals ($a->long); while(1) { $inds0 = where($a>0); $inds1 = $inds->index($inds)->sever; $a = $a0->index($inds1); $b = $b->index($inds1)->sever; func($b,$a); }
which doesn't save all the temporary instances of $a in memory. See "mandel.pl" in the Demos subdirectory of the PerlDL distribution for an example.
PP speed
If you really want to write speedy What this means is that you have to allow as many variables as possible to go into registers:
loop(a) %{ $a() += $COMP(foo_member) * $b() %}
expands to
for(i=0; i<10000; i++) { a[i] += __privtrans->foo_member * b[i]; }
is about the worst you can do, since your C compiler is not allowed to assume that "a" doesn't clobber "foo_member" which completely inhibits vectorization. Instead, do
float foo = $COMP(foo_member); loop(a) %{ $a() += foo * $b(); %}
This is not a restriction caused by
There are many other issues on organizing loops.
We are currently planning to make