ComplexSection: User Contributed Perl Documentation (3)Updated: 2004-06-15 |
ComplexSection: User Contributed Perl Documentation (3)Updated: 2004-06-15 |
use PDL; use PDL::Complex;
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)
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 $r three times with itself:
perldl> p $r*$r*$r
[
[ 5 0]
[ 5 -2.8052647e-15]
[ 5 -7.5369398e-15]
]
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.
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(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:
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 "PDL::Complex" (and "PDL::Graphics::PGPLOT").
Signature: (r(); [o]c(m=2))
convert real to complex, assuming an imaginary part of zero
Signature: (r(); [o]c(m=2))
convert imaginary to complex, assuming a real part of zero
Signature: (r(m=2); float+ [o]p(m=2))
convert complex numbers in rectangular form to polar (mod,arg) form
Signature: (r(m=2); [o]p(m=2))
convert complex numbers in polar (mod,arg) form to rectangular form
Signature: (a(m=2); b(m=2); [o]c(m=2))
Signature: (a(m=2); b(); [o]c(m=2))
mixed complex/real multiplication
Signature: (a(m=2); b(m=2); [o]c(m=2))
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.
Signature: (a(m=2); [o]c(m=2))
Signature: (a(m=2); [o]c())
complex "abs()" (also known as modulus)
Signature: (a(m=2); [o]c())
complex squared "abs()" (also known squared modulus)
Signature: (a(m=2); [o]c())
complex argument function (``angle'')
Signature: (a(m=2); [o]c(m=2))
sin (a) = 1/(2*i) * (exp (a*i) - exp (-a*i))
Signature: (a(m=2); [o]c(m=2))
cos (a) = 1/2 * (exp (a*i) + exp (-a*i))
tan (a) = -i * (exp (a*i) - exp (-a*i)) / (exp (a*i) + exp (-a*i))
Signature: (a(m=2); [o]c(m=2))
exp (a) = exp (real (a)) * (cos (imag (a)) + i * sin (imag (a)))
Signature: (a(m=2); [o]c(m=2))
log (a) = log (cabs (a)) + i * carg (a)
Signature: (a(m=2); b(m=2); [o]c(m=2))
complex "pow()" ("**"-operator)
Signature: (a(m=2); [o]c(m=2))
Signature: (a(m=2); [o]c(m=2))
Signature: (a(m=2); [o]c(m=2))
Signature: (a(m=2); [o]c(m=2))
sinh (a) = (exp (a) - exp (-a)) / 2
Signature: (a(m=2); [o]c(m=2))
cosh (a) = (exp (a) + exp (-a)) / 2
Signature: (a(m=2); [o]c(m=2))
Signature: (a(m=2); [o]c(m=2))
Signature: (a(m=2); [o]c(m=2))
Signature: (a(m=2); [o]c(m=2))
Signature: (a(m=2); [o]c(m=2))
compute the projection of a complex number to the riemann sphere
Signature: (a(m=2); [o]c(m=2,n); int n => n)
Compute the "n" roots of "a". "n" must be a positive integer. The result will always be a complex type!
Signature: (coeffs(n); x(c=2,m); [o]out(c=2,m))
evaluate the polynomial with (real) coefficients "coeffs" at the (complex) position(s) "x". "coeffs[0]" is the constant term.