PDL::Ops#
PDL’s operators: arithmetic, comparison, bitwise and the unary functions, element by element, as the functions behind Perl’s operators on ndarrays.
use PDL;
my ($a, $b) = (pdl(1, 2, 3), pdl(4, 5, 6));
print $a + $b, " ", $a * $b, " ", $b ** 2, " ", $b % 4, "\n"; # [5 7 9] [4 10 18] [16 25 36] [0 1 2]
print $a > 1, " ", $a <=> 2, " ", -$a, "\n"; # [0 1 1] [-1 0 1] [-1 -2 -3]
print sqrt(pdl(4, 9)), " ", abs(pdl(-2, 3)), " ", log10(pdl(10, 100)), "\n"; # [2 3] [2 3] [1 2]
print pdl(6) & 3, " ", byte(250) + 10, "\n"; # 2 4
my $z = czip(3, 4);
print $z, " ", abs($z), " ", $z->re, " ", conj($z), "\n"; # 3+4i 5 3 3-4i
my $x = $a;
$x += 10;
print "$a\n"; # [11 12 13]
Every operation broadcasts: operands of different shapes are matched dimension by dimension, a dimension of size 1 (or a Perl number) repeated to fit. The result has the larger operand type - byte(250) + 10 stays a byte and wraps. A binary operation takes an output and a swap flag: minus($a, $b, $c, 1) writes $b - $a into $c. With three arguments the third is the swap flag, as perl’s overloading passes it - minus($a, $b, 1) returns $b - $a. With $a->inplace the result is written into $a.
The operators are overloaded with these functions: + - * / ** %, == != < > <= >= <=>, & | ^ << >> ~ !, atan2 sqrt abs sin cos exp log, every assignment form (+= and the rest rewrite the left ndarray, so $x += 1 reaches every name of it), ++, -- and .= (assignment into an ndarray or a view of one: assgn). Unary minus is perl’s 0 - $x. eq compares element-wise and warns when the other operand is neither a number nor an ndarray; the other string operators are not overloaded and die.
This is PDL::Ops of PDL 2.106. The kernels are PDL’s own, through pperl’s engine. The overload handlers are upstream’s Perl (PDL::overload_plus and the rest), each with a native fast path in its place that hands anything unusual back to it; abs (_rabs for real types, _cabs for complex), log10, r2C and i2C are upstream’s Perl unchanged.