man PDL::Complex () - handle complex numbers

NAME

PDL::Complex - handle complex numbers

SYNOPSIS

  use PDL;
  use PDL::Complex;

DESCRIPTION

This module features a growing number of functions manipulating complex numbers. These are usually represented as a pair CW[ real imag ] or CW[ 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 (CW$a/$b*$c <= Cmul (Cdiv CW$a, CW$b), CW$c)>), you can also opt to cast your pdl's into the CWPDL::Complex datatype, which works just like your normal piddles, but with all the normal perl operators overloaded.

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

TIPS, TRICKS & CAVEATS

•
CWi is a constant exported by this module, which represents CW-1**0.5, i.e. the imaginary unit. it can be used to quickly and conviniently write complex constants like this: CW4+3*i.
•
Use CWr2C(real-values) to convert from real to complex, as in CW$r = Cpow $cplx, r2C 2. The overloaded operators automatically do that for you, all the other functions, do not. So CWCroots 1, 5 will return all the fifths roots of 1+1*i (due to threading).
•
use CWcplx(real-valued-piddle) to cast from normal piddles intot he complex datatype. Use CWreal(complex-valued-piddle) to cast back. This requires a copy, though.
•
This module has received some testing by Vanuxem Grégory (g.vanuxem at wanadoo dot fr). Please report any other errors you come across!

EXAMPLE WALK-THROUGH

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

   perldl> p $x = r2C 5
   [5 0]

Now calculate the three roots of of five:

   perldl> p $r = Croots $x, 3

   [
    [  1.7099759           0]
    [-0.85498797   1.4808826]
    [-0.85498797  -1.4808826]
   ]

Check that these really are the roots of unity:

   perldl> p $r ** 3

   [
    [             5              0]
    [             5 -3.4450524e-15]
    [             5 -9.8776239e-15]
   ]

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

   perldl> p $r*$r*$r

   [
    [             5              0]
    [             5 -2.8052647e-15]
    [             5 -7.5369398e-15]
   ]

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

   perldl> p -($r*i)

   [
    [         -0   1.7099759]
    [  1.4808826 -0.85498797]
    [ -1.4808826 -0.85498797]
   ]

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

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

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

   perldl> line im sin $sin; hold
   perldl> line re sin $sin
   perldl> 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 CWPDL::Complex (and CWPDL::Graphics::PGPLOT).

FUNCTIONS

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 CWsever 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 CWsever on the result if you don't want this.

r2C

  Signature: (r(); [o]c(m=2))

convert real to complex, assuming an imaginary part of zero

r2C does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

i2C

  Signature: (r(); [o]c(m=2))

convert imaginary to complex, assuming a real part of zero

i2C does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cr2p

  Signature: (r(m=2); float+ [o]p(m=2))

convert complex numbers in rectangular form to polar (mod,arg) form

Cr2p does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cp2r

  Signature: (r(m=2); [o]p(m=2))

convert complex numbers in polar (mod,arg) form to rectangular form

Cp2r does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cmul

  Signature: (a(m=2); b(m=2); [o]c(m=2))

complex multiplication

Cmul does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cprodover

  Signature: (a(m=2,n); [o]c(m=2))

Project via product to N-1 dimension

Cprodover does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cscale

  Signature: (a(m=2); b(); [o]c(m=2))

mixed complex/real multiplication

Cscale does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cdiv

  Signature: (a(m=2); b(m=2); [o]c(m=2))

complex division

Cdiv does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Ccmp

  Signature: (a(m=2); b(m=2); [o]c())

Complex comparison oeprator (spaceship). It orders by real first, then by imaginary. Hm, but it is mathematical nonsense! Complex numbers cannot be ordered.

Ccmp does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cconj

  Signature: (a(m=2); [o]c(m=2))

complex conjugation

Cconj does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cabs

  Signature: (a(m=2); [o]c())

complex CWabs() (also known as modulus)

Cabs does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cabs2

  Signature: (a(m=2); [o]c())

complex squared CWabs() (also known squared modulus)

Cabs2 does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Carg

  Signature: (a(m=2); [o]c())

complex argument function (angle)

Carg does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Csin

  Signature: (a(m=2); [o]c(m=2))

  sin (a) = 1/(2*i) * (exp (a*i) - exp (-a*i))

Csin does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Ccos

  Signature: (a(m=2); [o]c(m=2))

  cos (a) = 1/2 * (exp (a*i) + exp (-a*i))

Ccos does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Ctan a [not inplace]

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

Cexp

  Signature: (a(m=2); [o]c(m=2))

exp (a) = exp (real (a)) * (cos (imag (a)) + i * sin (imag (a)))

Cexp does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Clog

  Signature: (a(m=2); [o]c(m=2))

log (a) = log (cabs (a)) + i * carg (a)

Clog does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cpow

  Signature: (a(m=2); b(m=2); [o]c(m=2))

complex CWpow() (CW**-operator)

Cpow does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Csqrt

  Signature: (a(m=2); [o]c(m=2))

Csqrt does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Casin

  Signature: (a(m=2); [o]c(m=2))

Casin does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cacos

  Signature: (a(m=2); [o]c(m=2))

Cacos does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Catan cplx [not inplace]

Return the complex CWatan().

Csinh

  Signature: (a(m=2); [o]c(m=2))

  sinh (a) = (exp (a) - exp (-a)) / 2

Csinh does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Ccosh

  Signature: (a(m=2); [o]c(m=2))

  cosh (a) = (exp (a) + exp (-a)) / 2

Ccosh does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Ctanh

  Signature: (a(m=2); [o]c(m=2))

Ctanh does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Casinh

  Signature: (a(m=2); [o]c(m=2))

Casinh does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cacosh

  Signature: (a(m=2); [o]c(m=2))

Cacosh does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Catanh

  Signature: (a(m=2); [o]c(m=2))

Catanh does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Cproj

  Signature: (a(m=2); [o]c(m=2))

compute the projection of a complex number to the riemann sphere

Cproj does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

Croots

  Signature: (a(m=2); [o]c(m=2,n); int n => n)

Compute the CWn roots of CWa. CWn must be a positive integer. The result will always be a complex type!

Croots does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

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).

rCpolynomial

  Signature: (coeffs(n); x(c=2,m); [o]out(c=2,m))

evaluate the polynomial with (real) coefficients CWcoeffs at the (complex) position(s) CWx. CWcoeffs[0] is the constant term.

rCpolynomial does not process bad values. It will set the bad-value flag of all output piddles if the flag is set for any of the input piddles.

AUTHOR

Copyright (C) 2000 Marc Lehmann <pcg@goof.com>. 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.

SEE ALSO

perl(1), PDL.