I've been trying to slowly learn PDL over the last few months. While I'm aware of some available documentation (the PDL::* perldoc, The PDL Book, etc.) I've found the beginner documentation to be lacking. Therefore, I thought it would be a good idea to start 'porting' some numpy documentation to PDL for new users such as myself.
I've started with 100 numpy exercises, and this is the work in progress port to Perl/PDL.
As I'm still learning PDL, some solutions may be less than optimal, while others do not currently have solutions as they are outside my of level of competency. Therefore, I'm posting this WIP to PM to ask for comments and contributions.
As with most of Perl, there is more than one way to do it for most of these. I've decided to keep the $var>function() syntax as much as possible to easily be able to chain operations.
Thanks in advance!
use PDL;
use PDL::Version; print $PDL::Version::VERSION;
my $z = zeros(10); print $z;
use PDL::Core ':Internal' my $z = zeros(10); my $size = howbig($z>get_datatype) * $z>nelem; print $size;
# To get top level PDL help perldoc PDL
my $z = zeros(10); $z>slice(4) .= 1; print $z;
or with PDL::NiceSlice loaded:
my $z = zeros(10); $z(4) .= 1; print $z;
Note: It will be assumed that PDL::NiceSlice will be loaded from now on.
my $z = 10 + sequence(40); print $z;
my $z = sequence(10); $z = $z(1:0); print $z;
my $z = sequence(3,3); print $z;
my $vec = pdl [1,2,0,0,4,0]; my $nz = $vec>which; print $nz;
my $z = identity(3); print $z;
my $z = random(3,3,3); print $z;
my $z = random(10,10); print $z>min, $z>max;
my $z = random(30); print $z>avg;
my $z = ones(10,10); $z(1:8,1:8) .= 0; print $z;
my $a = random(8,8); my $x = zeros(2 + $a>dim(0), 2 + $a>dim(1)); $x(1:8,1:8) .= $a; print $x;
n/a
my $z = identity(5) * (1 + sequence(5)); $z>where($z > 4) .= 0; $z = $z>transpose>rotate(1)>transpose; print $z;
my $z = zeros(8,8); $z("0::2","0::2") .= 1 $z("1::2","1::2") .= 1 print $z;
my $z = random(6,7,8); my $hundreth = $z>clump($z>ndims)>(100); # TODO: find index of $hundreth in $z
n/a
my $z = random(5,5); $z = (($z  $z>min) / ($z>max  $z>min)); print $z;
n/a
my $x = ones(3,5); my $y = ones(2,3); print $x x $y;
my $z = sequence(10); $z>where($z <= 8 & $z>= 3) *= 1; print $z;
n/a
my $z = sequence(long, 10); $z ** $z; 2 << $z >> 2; $z < $z; 1j * $z; $z / 1 / 1; $z < $z > $z;
print pdl(0) / pdl(0); print pdl(0) // pdl(0); print float int pdl(NaN);
$z = 20 * random(10)  10; $z>where($z < 0) .=  $z>where($z < 0)>abs>ceil; $z>where($z > 0) .= $z>where($z > 0)>ceil; print $z;
my $z1 = long 256 * random(10); my $z2 = long 256 * random(10); print intersect $z1, $z2;
n/a for PDL
n/a
# No built in PDL time/date functions my $yesterday = time()  (60 * 60 * 24); my $today = time(); my $tomorrow = time() + (60 * 60 * 24);
n/a
my $a = ones(3); my $b = 2 * ones(3); my $c = 3 * ones(3); print ($a + $b) * ( $a/2)
my $z = 10 * random(10); print $z>ceil; print $z>floor; print byte $z; print long $z; print longlong $z;
my $z = xvals zeros(5,5); print $z;
n/a
my $z = (sequence(12) / 11)>slice("1:10"); print $z;
my $z = random(10)>qsort; print $z;
n/a
my $a = random(10); my $b = random(10); print $a == $b;
n/a
use PDL::Complex; my $z = random(2,10); my $p = Cr2p($z); print $p;
my $z = random(10); $z>where($z == $z>max) .= 0; print $z;
n/a
TODO
# This cannot be done directly, but you can extract the underlying # C type used for each PDL type: print byte>realctype; print short>realctype; print ushort>realctype; print long>realctype; print longlong>realctype; print indx>realctype; print float>realctype; print double>realctype;
# Set maximum print limit to one million elements $PDL::toolongtoprint = 1_000_000; $z = zeros(1000,1000); print $z;
TODO
n/a
my $z = random(10,2); my ($x,$y) = ($z(:,0), $z(:,1)); my $d = (($x  $x>transpose)>ipow(2)) + (($y  $y>transpose)>ipow(2)); print $d;
my $z = float 1000 * random(10); $z = long $z;
1,2,3,4,5 6,,,7,8 ,,9,10,11
How to read it?
my $z = rcols "data.csv", { COLSEP => ',' }, []; $z = $z>transpose; # optional (PDL is column major) print $z;
n/a
my $z = grandom(10,10); # correct? print $z;
my $p = 3; my $z = zeros(10,10); my $i = indx $z>nelem * random($p) $z>clump($z>ndims)>($i) .= 1; print $z;
my $z = random(5, 10); $z = $z  $z>avgover>transpose; print $z;
TODO
TODO
TODO
n/a
n/a
TODO
TODO
my ($w, $h) = (16, 16); my $i = byte 256 * random($w, $h, 3); my $uniqcol = $i>uniq>nelem; print $uniqcol;
TODO
TODO
my $z1 = random(10, 10); my $z2 = random(10, 10); print $z1>inner($z2);
my $z = pdl [1,2,3,4,5]; my $nz = 3; my $x = zeros($z>dim(0) * $nz); $x("0::$nz") .= $z; print $x;
my $z1 = ones(5,5,3); my $z2 = 2 * ones(5,5); print $z1 * $z2;
my $z = sequence(5,5); $z(0:1,) .= $z(1:0,)>sever; print $z;
TODO
TODO
TODO
TODO
my $z = long 2 * random(10); $z = not $z; print $z; $z = 5 + sequence(10); $z = 1 * $z; print $z;
TODO
TODO
TODO
$z = 10 * random(15); $len = 4; my @r = (); push @r, $z($_:$_ + $len1) for (0 .. $z>nelem  $len) $r = pdl @r; print $r;
my $z = 10 * random(10,10); my ($u, $s, $v) = $z>svd; my $rank = $s>where($s > 1e10); print $rank;
TODO
my $z = long 5 * random(10,10); my $dim = 3; my (@out, $out); for my $i ( 0 .. $z>dim(0)  $dim  1) { for my $j ( 0 .. $z>dim(1)  $dim  1) { push @out, $z($i:$i+$dim,$j:$j+$dim); } } $out = pdl @out; print $out;
TODO
TODO
TODO
TODO
my $z = 10 * random(20); my $n = 3; print $z>qsort>($n:);
TODO
n/a?
my $z = random(5e7); $z ** 3; $z>ipow(3); $z>power(3,0);
TODO
TODO
my $z = pdl [0,1,2,3,15,16,32,64,128]; my $bits = ($z>transpose & (2 ** xvals(9))); $bits>where($bits > 0) .= 1; print $bits;
my $z = long 2 * random(3,6); print $z>uniqvec;
TODO
TODO
TODO
TODO
edit: link to PDL for those who do not know what it is

