Chris Marshall > PDL-2.006 > PDL::Complex


Annotate this POD


View/Report Bugs
Source   Latest Release: PDL-2.007


PDL::Complex - handle complex numbers


  use PDL;
  use PDL::Complex;


This module features a growing number of functions manipulating complex numbers. These are usually represented as a pair [ real imag ] or [ angle phase ]. If not explicitly mentioned, the functions can work inplace (not yet implemented!!!) and require rectangular form.

While there is a procedural interface available ($a/$b*$c <=> Cmul (Cdiv $a, $b), $c)), you can also opt to cast your pdl's into the PDL::Complex datatype, which works just like your normal piddles, but with all the normal perl operators overloaded.

The latter means that sin($a) + $b/$c will be evaluated using the normal rules of complex numbers, while other pdl functions (like max) just treat the piddle as a real-valued piddle with a lowest dimension of size 2, so max will return the maximum of all real and imaginary parts, not the "highest" (for some definition)



The complex constant five is equal to pdl(1,0):

   pdl> p $x = r2C 5
   5 +0i

Now calculate the three roots of of five:

   pdl> p $r = Croots $x, 3
   [1.70998 +0i  -0.854988 +1.48088i  -0.854988 -1.48088i]

Check that these really are the roots of unity:

   pdl> p $r ** 3
   [5 +0i  5 -1.22465e-15i  5 -7.65714e-15i]

Duh! Could be better. Now try by multiplying $r three times with itself:

   pdl> p $r*$r*$r
   [5 +0i  5 -4.72647e-15i  5 -7.53694e-15i]

Well... maybe Cpow (which is used by the ** operator) isn't as bad as I thought. Now multiply by i and negate, which is just a very expensive way of swapping real and imaginary parts.

   pdl> p -($r*i)
   [0 -1.70998i  1.48088 +0.854988i  -1.48088 +0.854988i]

Now plot the magnitude of (part of) the complex sine. First generate the coefficients:

   pdl> $sin = i * zeroes(50)->xlinvals(2,4) + zeroes(50)->xlinvals(0,7)

Now plot the imaginary part, the real part and the magnitude of the sine into the same diagram:

   pdl> line im sin $sin; hold
   pdl> line re sin $sin
   pdl> line abs sin $sin

Sorry, but I didn't yet try to reproduce the diagram in this text. Just run the commands yourself, making sure that you have loaded PDL::Complex (and PDL::Graphics::PGPLOT).

cplx real-valued-pdl

Cast a real-valued piddle to the complex datatype. The first dimension of the piddle must be of size 2. After this the usual (complex) arithmetic operators are applied to this pdl, rather than the normal elementwise pdl operators. Dataflow to the complex parent works. Use sever on the result if you don't want this.

complex real-valued-pdl

Cast a real-valued piddle to the complex datatype without dataflow and inplace. Achieved by merely reblessing a piddle. The first dimension of the piddle must be of size 2.

real cplx-valued-pdl

Cast a complex valued pdl back to the "normal" pdl datatype. Afterwards the normal elementwise pdl operators are used in operations. Dataflow to the real parent works. Use sever on the result if you don't want this.

Ctan a [not inplace]

  tan (a) = -i * (exp (a*i) - exp (-a*i)) / (exp (a*i) + exp (-a*i))

Catan cplx [not inplace]

Return the complex atan().

re cplx, im cplx

Return the real or imaginary part of the complex number(s) given. These are slicing operators, so data flow works. The real and imaginary parts are returned as piddles (ref eq PDL).


Copyright (C) 2000 Marc Lehmann <>. All rights reserved. There is no warranty. You are allowed to redistribute this software / documentation as described in the file COPYING in the PDL distribution.


perl(1), PDL.

syntax highlighting: