4

I am considering the complex extension of the Perl Data Language (PDL 2.19.0) for complex matrix operations, but operations as easy as transpose do not work as I would expect.

use strict;
use warnings;

use PDL;
use PDL::Complex;

my $m = cplx pdl [i, 1], [-1, -i];

printf "m=%s\n", $m; 
my $mt = $m->transpose;
printf "m=%s\n", $m; 
printf "mt=%s\n", $mt;
my $mx = $m->xchg(1,2);
printf "m=%s\n", $m; 
printf "mx=%s\n", $mx;

To me it seems that $m->transpose equals $m. Another supposedly easy operation which bugs me:

printf "m[0,0]=%s\n", $m->at(0,0);

does not work, only

printf "m[0,0,0]=%s\n", $m->at(0,0,0);

does. Am I using the API in a wrong way?

  • 1
    At https://metacpan.org/pod/PDL::Complex there is a section called "Questions? Chat with us". It will open irc chat dedicated to PDL and you may try to write there too. – zubenel Mar 04 '20 at 07:33

1 Answers1

2

The basic piddle operations don't behave as you expect because it looks like complex piddles are implemented using the 0th dimension to store the real and imaginary pair. You can see this if you check dims or real:

pdl> $m = cplx pdl [i, 1], [-1, -i];

pdl> p $m->dims
2 2 2
pdl> p $m->real

[
 [
  [0 1]
  [1 0]
 ]
 [
  [-1  0]
  [ 0 -1]
 ]
]

Thus, you can use xchg instead of transpose to transpose the correct dimensions of the "two-dimensional" complex piddle:

pdl> p $m

[
 [ 0 +1i   1 +0i]
 [-1 +0i   0 -1i]
]

pdl> p $m->xchg(1, 2)

[
 [ 0 +1i  -1 +0i]
 [ 1 +0i   0 -1i]
]

For at, you can get the real/imaginary parts separately and combine them:

pdl> p cplx pdl $m->at(0,0,0), $m->at(1,0,0)
0 +1i

Or slice the pair:

pdl> p $m->slice('', '(0)', '(0)')
0 +1i
Josh Brobst
  • 1,772
  • 10
  • 16
  • Thanks; do you have any insights why the API is designed this way? I find this somewhat counter intuitive and in the case of the at function probably also slower than optimal runtime. – Bernhard Bodenstorfer Mar 07 '20 at 10:27
  • Sorry I didn't see your comment earlier. I'm not too familiar with the library myself, so I can't really give any insight. I'm guessing that using a dimension for the complex parts was just the simplest implementation. You might want to try the IRC mentioned above, someone in there probably knows the thought process behind the API. – Josh Brobst Apr 15 '20 at 03:06
  • @BernhardBodenstorfer The library was implemented in the late 90s, and provided simple mathematical operations that functioned correctly on the complex numbers in the ndarrays. However, higher-dimensional stuff really wasn't done properly. For reasons of backwards compatibility, this cannot be fixed since a lot of code depends on it working as it does. As of 2.049, from May 2021, "native complex" numbers work as you'd expect. – Ed. Mar 10 '22 at 02:59