Deprecated.
NAME
PDL::Complex - handle complex numbers (DEPRECATED - use native complex)
SYNOPSIS
use PDL;
use PDL::Complex;
DESCRIPTION
This module is deprecated in favour of using "native complex" data types, e.g.:
use PDL;
my $complex_pdl = cdouble('[1+3i]');
print $complex_pdl * pdl('i'); # [-3+i]
This module features a growing number of functions manipulating complex numbers. These are usually represented as a pair [ real imag ] or [ magnitude phase ]. If not explicitly mentioned, the functions can work inplace (not yet implemented!!!) and require rectangular form.
While there is a procedural interface available ($x/$y*$c <=> Cmul (Cdiv ($x, $y), $c)), you can also opt to cast your pdl's into the PDL::Complex datatype, which works just like your normal ndarrays, but with all the normal perl operators overloaded.
The latter means that sin($x) + $y/$c will be evaluated using the normal rules of complex numbers, while other pdl functions (like max) just treat the ndarray as a real-valued ndarray 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)
Native complex support
2.027 added changes in complex number handling, with support for C99 complex floating-point types, and most functions and modules in the core distribution support these as well.
PDL can now handle complex numbers natively as scalars. This has the advantage that real and complex valued ndarrays have the same dimensions. Consider this when writing code in the future.
See "re" in PDL::Ops, "im" in PDL::Ops, "abs" in PDL::Ops, "carg" in PDL::Ops, "conj" in PDL::Ops for more.
TIPS, TRICKS & CAVEATS
iis a function (not, as of 2.047, a constant) exported by this module, which represents-1**0.5, i.e. the imaginary unit. it can be used to quickly and conveniently write complex constants like this:4+3*i.NB This will override the PDL::Core function of the same name, which returns a native complex value.
Use
r2C(real-values)to convert from real to complex, as in$r = Cpow $cplx, r2C 2. The overloaded operators automatically do that for you, all the other functions, do not. SoCroots 1, 5will return all the fifths roots of 1+1*i (due to broadcasting).use
cplx(real-valued-ndarray)to cast from normal ndarrays into the complex datatype. Usereal(complex-valued-ndarray)to cast back. This requires a copy, though.
EXAMPLE WALK-THROUGH
The complex constant five is equal to pdl(1,0):
pdl> p $x = r2C 5
5 +0i
Now calculate the three cubic roots 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:
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, then take the complex conjugate, which is just a very expensive way of swapping real and imaginary parts.
pdl> p Cconj(-($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> use PDL::Graphics::Gnuplot
pdl> gplot( with => 'lines',
PDL::cat(im ( sin $sin ),
re ( sin $sin ),
abs( sin $sin ) ))
An ASCII version of this plot looks like this:
30 ++-----+------+------+------+------+------+------+------+------+-----++
+ + + + + + + + + + +
| $$|
| $ |
25 ++ $$ ++
| *** |
| ** *** |
| $$* *|
20 ++ $** ++
| $$$* #|
| $$$ * # |
| $$ * # |
15 ++ $$$ * # ++
| $$$ ** # |
| $$$$ * # |
| $$$$ * # |
10 ++ $$$$$ * # ++
| $$$$$ * # |
| $$$$$$$ * # |
5 ++ $$$############ * # ++
|*****$$$### ### * # |
* #***** # * # |
| ### *** ### ** # |
0 ## *** # * # ++
| * # * # |
| *** # ** # |
| * # * # |
-5 ++ ** # * # ++
| *** ## ** # |
| * #* # |
| **** ***## # |
-10 ++ **** # # ++
| # # |
| ## ## |
+ + + + + + + ### + ### + + +
-15 ++-----+------+------+------+------+------+-----###-----+------+-----++
0 5 10 15 20 25 30 35 40 45 50
OPERATORS
The following operators are overloaded:
- +, += (addition)
- -, -= (subtraction)
- *, *= (multiplication; "Cmul")
- /, /= (division; "Cdiv")
- **, **= (exponentiation; "Cpow")
- atan2 (4-quadrant arc tangent)
- sin ("Csin")
- cos ("Ccos")
- exp ("Cexp")
- abs ("Cabs")
- log ("Clog")
- sqrt ("Csqrt")
- ++, -- (increment, decrement; they affect the real part of the complex number only)
- "" (stringification)
Comparing complex numbers other than for equality is a fatal error.
FUNCTIONS
from_native
Class method to convert a native-complex ndarray to a PDL::Complex object.
PDL::Complex->from_native($native_complex_ndarray)
as_native
Object method to convert a PDL::Complex object to a native-complex ndarray.
$pdl_complex_obj->as_native
cplx
Cast a real-valued ndarray to the complex datatype.
The first dimension of the ndarray 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.
cplx($real_valued_pdl)
complex
Cast a real-valued ndarray to the complex datatype without dataflow and inplace.
Achieved by merely reblessing an ndarray. The first dimension of the ndarray must be of size 2.
complex($real_valued_pdl)
real
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.
real($cplx_valued_pdl)
t
$pdl = $pdl->t(SCALAR(conj))
conj : Conjugate Transpose = 1 | Transpose = 0, default = 0;
Convenient function for transposing real or complex 2D array(s). For complex data, if conj is true returns conjugate transposed array(s). Supports broadcasting. Not exported.
Originally by Grégory Vanuxem.
r2C
Signature: (r(); [o]c(m=2))
Types: (sbyte byte short ushort long ulong indx ulonglong longlong
float double ldouble)
$c = r2C($r);
r2C($r, $c); # all arguments given
$c = $r->r2C; # method call
$r->r2C($c);
convert real to complex, assuming an imaginary part of zero
Broadcasts over its inputs.
r2C does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
i2C
Signature: (r(); [o]c(m=2))
Types: (sbyte byte short ushort long ulong indx ulonglong longlong
float double ldouble)
$c = i2C($r);
i2C($r, $c); # all arguments given
$c = $r->i2C; # method call
$r->i2C($c);
convert imaginary to complex, assuming a real part of zero
Broadcasts over its inputs.
i2C does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cr2p
Signature: (r(m=2); [o]p(m=2))
Types: (float double ldouble)
$p = Cr2p($r);
Cr2p($r, $p); # all arguments given
$p = $r->Cr2p; # method call
$r->Cr2p($p);
$r->inplace->Cr2p; # can be used inplace
Cr2p($r->inplace);
convert complex numbers in rectangular form to polar (mod,arg) form. Works inplace
Can operate inplace with r as output p. Broadcasts over its inputs.
Cr2p does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cp2r
Signature: (r(m=2); [o]p(m=2))
Types: (float double ldouble)
$p = Cp2r($r);
Cp2r($r, $p); # all arguments given
$p = $r->Cp2r; # method call
$r->Cp2r($p);
$r->inplace->Cp2r; # can be used inplace
Cp2r($r->inplace);
convert complex numbers in polar (mod,arg) form to rectangular form. Works inplace
Can operate inplace with r as output p. Broadcasts over its inputs.
Cp2r does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cmul
Signature: (a(m=2); b(m=2); [o]c(m=2))
Types: (sbyte byte short ushort long ulong indx ulonglong longlong
float double ldouble)
$c = Cmul($a, $b);
Cmul($a, $b, $c); # all arguments given
$c = $a->Cmul($b); # method call
$a->Cmul($b, $c);
complex multiplication
Broadcasts over its inputs.
Cmul does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cprodover
Signature: (a(m=2,n); [o]c(m=2))
Types: (sbyte byte short ushort long ulong indx ulonglong longlong
float double ldouble)
$c = Cprodover($a);
Cprodover($a, $c); # all arguments given
$c = $a->Cprodover; # method call
$a->Cprodover($c);
Project via product to N-1 dimension
Broadcasts over its inputs.
Cprodover does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cscale
Signature: (a(m=2); b(); [o]c(m=2))
Types: (sbyte byte short ushort long ulong indx ulonglong longlong
float double ldouble)
$c = Cscale($a, $b);
Cscale($a, $b, $c); # all arguments given
$c = $a->Cscale($b); # method call
$a->Cscale($b, $c);
mixed complex/real multiplication
Broadcasts over its inputs.
Cscale does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cdiv
Signature: (a(m=2); b(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Cdiv($a, $b);
Cdiv($a, $b, $c); # all arguments given
$c = $a->Cdiv($b); # method call
$a->Cdiv($b, $c);
complex division
Broadcasts over its inputs.
Cdiv does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Ceq
Signature: (a(m=2); b(m=2); [o]c())
Types: (float double ldouble)
$c = Ceq($a, $b);
Ceq($a, $b, $c); # all arguments given
$c = $a->Ceq($b); # method call
$a->Ceq($b, $c);
Complex equality operator.
Broadcasts over its inputs.
Ceq does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cconj
Signature: (a(m=2); [o]c(m=2))
Types: (sbyte byte short ushort long ulong indx ulonglong longlong
float double ldouble)
$c = Cconj($a);
Cconj($a, $c); # all arguments given
$c = $a->Cconj; # method call
$a->Cconj($c);
$a->inplace->Cconj; # can be used inplace
Cconj($a->inplace);
complex conjugation. Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Cconj does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cabs
Signature: (a(m=2); [o]c())
Types: (float double ldouble)
$c = Cabs($a);
Cabs($a, $c); # all arguments given
$c = $a->Cabs; # method call
$a->Cabs($c);
complex abs() (also known as modulus)
Broadcasts over its inputs.
Cabs does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cabs2
Signature: (a(m=2); [o]c())
Types: (sbyte byte short ushort long ulong indx ulonglong longlong
float double ldouble)
$c = Cabs2($a);
Cabs2($a, $c); # all arguments given
$c = $a->Cabs2; # method call
$a->Cabs2($c);
complex squared abs() (also known squared modulus)
Broadcasts over its inputs.
Cabs2 does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Carg
Signature: (a(m=2); [o]c())
Types: (float double ldouble)
$c = Carg($a);
Carg($a, $c); # all arguments given
$c = $a->Carg; # method call
$a->Carg($c);
complex argument function ("angle")
Broadcasts over its inputs.
Carg does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Csin
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Csin($a);
Csin($a, $c); # all arguments given
$c = $a->Csin; # method call
$a->Csin($c);
$a->inplace->Csin; # can be used inplace
Csin($a->inplace);
sin (a) = 1/(2*i) * (exp (a*i) - exp (-a*i)). Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Csin does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Ccos
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Ccos($a);
Ccos($a, $c); # all arguments given
$c = $a->Ccos; # method call
$a->Ccos($c);
$a->inplace->Ccos; # can be used inplace
Ccos($a->inplace);
cos (a) = 1/2 * (exp (a*i) + exp (-a*i)). Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Ccos does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Ctan
Complex tangent
tan (a) = -i * (exp (a*i) - exp (-a*i)) / (exp (a*i) + exp (-a*i))
Does not work inplace.
Cexp
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Cexp($a);
Cexp($a, $c); # all arguments given
$c = $a->Cexp; # method call
$a->Cexp($c);
$a->inplace->Cexp; # can be used inplace
Cexp($a->inplace);
exp (a) = exp (real (a)) * (cos (imag (a)) + i * sin (imag (a))). Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Cexp does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Clog
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Clog($a);
Clog($a, $c); # all arguments given
$c = $a->Clog; # method call
$a->Clog($c);
$a->inplace->Clog; # can be used inplace
Clog($a->inplace);
log (a) = log (cabs (a)) + i * carg (a). Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Clog does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cpow
Signature: (a(m=2); b(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Cpow($a, $b);
Cpow($a, $b, $c); # all arguments given
$c = $a->Cpow($b); # method call
$a->Cpow($b, $c);
$a->inplace->Cpow($b); # can be used inplace
Cpow($a->inplace, $b);
complex pow() (**-operator)
Can operate inplace with a as output c. Broadcasts over its inputs.
Cpow does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Csqrt
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Csqrt($a);
Csqrt($a, $c); # all arguments given
$c = $a->Csqrt; # method call
$a->Csqrt($c);
$a->inplace->Csqrt; # can be used inplace
Csqrt($a->inplace);
Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Csqrt does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Casin
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Casin($a);
Casin($a, $c); # all arguments given
$c = $a->Casin; # method call
$a->Casin($c);
$a->inplace->Casin; # can be used inplace
Casin($a->inplace);
Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Casin does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cacos
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Cacos($a);
Cacos($a, $c); # all arguments given
$c = $a->Cacos; # method call
$a->Cacos($c);
$a->inplace->Cacos; # can be used inplace
Cacos($a->inplace);
Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Cacos does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Catan
Return the complex atan().
Does not work inplace.
Csinh
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Csinh($a);
Csinh($a, $c); # all arguments given
$c = $a->Csinh; # method call
$a->Csinh($c);
$a->inplace->Csinh; # can be used inplace
Csinh($a->inplace);
sinh (a) = (exp (a) - exp (-a)) / 2. Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Csinh does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Ccosh
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Ccosh($a);
Ccosh($a, $c); # all arguments given
$c = $a->Ccosh; # method call
$a->Ccosh($c);
$a->inplace->Ccosh; # can be used inplace
Ccosh($a->inplace);
cosh (a) = (exp (a) + exp (-a)) / 2. Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Ccosh does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Ctanh
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Ctanh($a);
Ctanh($a, $c); # all arguments given
$c = $a->Ctanh; # method call
$a->Ctanh($c);
$a->inplace->Ctanh; # can be used inplace
Ctanh($a->inplace);
Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Ctanh does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Casinh
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Casinh($a);
Casinh($a, $c); # all arguments given
$c = $a->Casinh; # method call
$a->Casinh($c);
$a->inplace->Casinh; # can be used inplace
Casinh($a->inplace);
Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Casinh does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cacosh
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Cacosh($a);
Cacosh($a, $c); # all arguments given
$c = $a->Cacosh; # method call
$a->Cacosh($c);
$a->inplace->Cacosh; # can be used inplace
Cacosh($a->inplace);
Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Cacosh does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Catanh
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Catanh($a);
Catanh($a, $c); # all arguments given
$c = $a->Catanh; # method call
$a->Catanh($c);
$a->inplace->Catanh; # can be used inplace
Catanh($a->inplace);
Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Catanh does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cproj
Signature: (a(m=2); [o]c(m=2))
Types: (float double ldouble)
$c = Cproj($a);
Cproj($a, $c); # all arguments given
$c = $a->Cproj; # method call
$a->Cproj($c);
$a->inplace->Cproj; # can be used inplace
Cproj($a->inplace);
compute the projection of a complex number to the riemann sphere. Works inplace
Can operate inplace with a as output c. Broadcasts over its inputs.
Cproj does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Croots
Signature: (a(m=2); [o]c(m=2,n); int n => n)
Types: (float double ldouble)
$c = Croots($a, $n);
Croots($a, $c, $n); # all arguments given
$c = $a->Croots($n); # method call
$a->Croots($c, $n);
Compute the n roots of a. n must be a positive integer. The result will always be a complex type!
Broadcasts over its inputs.
Croots does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
re, im
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 ndarrays (ref eq PDL).
rCpolynomial
Signature: (coeffs(n); x(c=2,m); [o]out(c=2,m))
Types: (float double ldouble)
$out = rCpolynomial($coeffs, $x);
rCpolynomial($coeffs, $x, $out); # all arguments given
$out = $coeffs->rCpolynomial($x); # method call
$coeffs->rCpolynomial($x, $out);
evaluate the polynomial with (real) coefficients coeffs at the (complex) position(s) x. coeffs[0] is the constant term.
Broadcasts over its inputs.
rCpolynomial does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Ctricpy
Signature: (A(c=2,m,n);[o] C(c=2,m,n); int uplo)
Types: (sbyte byte short ushort long ulong indx ulonglong longlong
float double ldouble)
tricpy(PDL(A), int(uplo), PDL(C))
$c = $a->tricpy($uplo); # explicit uplo
$c = $a->tricpy; # default upper
or
tricpy($a, $uplo, $c); # modify c
Copy triangular part to another matrix. If uplo == 0 copy upper triangular part.
Originally by Grégory Vanuxem.
Broadcasts over its inputs.
Ctricpy does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Cmstack
Signature: (x(c=2,n,m);y(c,n,p);[o]out(c,n,q=CALC($SIZE(m)+$SIZE(p))))
Types: (sbyte byte short ushort long ulong indx ulonglong longlong
float double ldouble)
$out = Cmstack($x, $y);
Cmstack($x, $y, $out); # all arguments given
$out = $x->Cmstack($y); # method call
$x->Cmstack($y, $out);
$x->Cmstack($y) .= $data; # usable as lvalue
Combine two 2D ndarrays into a single ndarray, along the second ("vertical") dim. This routine does backward and forward dataflow automatically.
Originally by Grégory Vanuxem.
Broadcasts over its inputs. Creates data-flow back and forth by default.
Cmstack does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
Caugment
Signature: (x(c=2,n);y(c,p);[o]out(c,q=CALC($SIZE(n)+$SIZE(p))))
Types: (sbyte byte short ushort long ulong indx ulonglong longlong
float double ldouble)
$out = Caugment($x, $y);
Caugment($x, $y, $out); # all arguments given
$out = $x->Caugment($y); # method call
$x->Caugment($y, $out);
$x->Caugment($y) .= $data; # usable as lvalue
Combine two ndarrays into a single ndarray along the 0-th ("horizontal") dim. This routine does backward and forward dataflow automatically.
Originally by Grégory Vanuxem.
Broadcasts over its inputs. Creates data-flow back and forth by default.
Caugment does not process bad values. It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
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.