PDL::Tips(3pm)

✨ PDL::Tips ✨

okay bestie, gather round πŸ’– this is PDL::Tips, part of the User Contributed Perl Documentation β€” it's the lil grab-bag of useful arcana, programming tidbits and such. no cap, it's giving "wise older cousin drops knowledge" energy. 🌸


🌈 SYNOPSIS 🌈

use PDL;

# Whatever happens here.

periodt. that's the whole vibe. πŸ’…


πŸ’« DESCRIPTION πŸ’«

so this page is where we document all the useful idioms, the helpful hints, and the tips for using Perl Data Language v2.0. it's the group chat of documentation. 🫢

πŸŽ€ Help πŸŽ€

if you're inside perldl, just type help help and you're set. or from the command line, hit up the pdldoc program to get at the PerlDL documentation. πŸ’–

want fuzzy searching while you're in perldl? use the apropos command β€” it finds every instance of your search term across the docs. for an example, go peek at PDL::FAQ Q:7.1. iykyk. πŸ”βœ¨

πŸ¦‹ Indexing idioms πŸ¦‹

okay so this next bit of code normalizes a whole bunch of vectors in $x. and the slay part? it works no matter what dimensionality $x is. literally any dimensionality. πŸ‘‘

$x /= $x->sumover->dummy(0);

πŸ’€ What is actually happening? πŸ’€

if you wanna see what the code is actually doing (like, behind the curtain, main character reveal moment), run this command

PDL::Core::set_debugging(1);

somewhere. warning tho: this spews out a huge amount of debug info for PDL into STDOUT. not me flooding your terminal 😭 it's a lot.

future plans include making it possible to redirect that output, and also making it possible to select messages with more precision. so like, it's on the vision board. ✨

a lot of those messages come from lib/PDL/Core/pdlapi.c, and you can look at the source to see what's going on. πŸ”

and if you have any extra time to work on these mechanisms, pls inform the pdl-porters mailing list. they'd be so happy 🫢

πŸ“ Memory savings πŸ“

if you're running something recursively that selects certain indices of a large ndarray, like

while (1) {
        $inds = where($x>0);
        $x = $x->index($inds);
        $y = $y->index($inds);
        func($y,$x);
}

then listen: if you are not writing to $y, you save a LOT of memory by changing it to

        $y = $y->index($inds)->sever;

the new method sever causes the write-back relation to be forgotten. it's like copy except it changes the original ndarray and returns it. πŸ’…

of course, the probably best way to do the above is

$inds = xvals ($x->long);
while (1) {
        $inds0 = where($x>0);
        $inds1 = $inds->index($inds)->sever;
        $x = $a0->index($inds1);
        $y = $y->index($inds1)->sever;
        func($y,$x);
}

which doesn't save all the temporary instances of $x in memory. 🧠✨

πŸ”₯ PP speed πŸ”₯

if you really wanna write speedy PP code, the first thing you need to do is make sure your C compiler is allowed to do the necessary optimizations. no optimization permission = no speed, and that's just sad. πŸ™ˆ

what this actually means: you have to allow as many variables as possible to go into registers. like

loop(a) %{
        $a() += $COMP(foo_member) * $b()
%}

expands to

for(i=0; i<10000; i++) {
        a[i] += __privtrans->foo_member * b[i];
}

and bestie, that is about the worst you can do, because 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();
%}

and now we're cooking. πŸ”₯

to be clear: this is not a restriction caused by PP. it's a restriction caused by ANSI C semantics. we could copy the struct into local variables and back, but that could cause very strange things sometimes. spooky. πŸ‘»

there are many other issues on organizing loops, fyi.

we are currently planning to make PP able to do fixed-width things as well as physical ndarrays β€” where looping over the first dimensions would be cheaper since there are less distinct increments, which might make a difference on machines with a small number of registers. πŸ“βœ¨


πŸ‘‘ AUTHOR πŸ‘‘

Copyright (C) Tuomas J. Lukka 1997. All rights reserved. Duplication in the same form and printing a copy for yourself allowed. πŸ’œ


so that's the whole page, bestie πŸŽ€ this documentation shipped as perl v5.42.3, dated 2026-08-20, and it's filed under PDL::Tips(3pm) in the User Contributed Perl Documentation. go forth and index responsibly. slay. πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from