1Complex(3) User Contributed Perl Documentation Complex(3)
2
3
4
6 PDL::Complex - handle complex numbers
7
9 use PDL;
10 use PDL::Complex;
11
13 This module features a growing number of functions manipulating complex
14 numbers. These are usually represented as a pair "[ real imag ]" or "[
15 angle phase ]". If not explicitly mentioned, the functions can work
16 inplace (not yet implemented!!!) and require rectangular form.
17
18 While there is a procedural interface available ("$a/$b*$c <=" Cmul
19 (Cdiv $a, $b), $c)>), you can also opt to cast your pdl's into the
20 "PDL::Complex" datatype, which works just like your normal piddles, but
21 with all the normal perl operators overloaded.
22
23 The latter means that "sin($a) + $b/$c" will be evaluated using the
24 normal rules of complex numbers, while other pdl functions (like "max")
25 just treat the piddle as a real-valued piddle with a lowest dimension
26 of size 2, so "max" will return the maximum of all real and imaginary
27 parts, not the "highest" (for some definition)
28
30 · "i" is a constant exported by this module, which represents
31 "-1**0.5", i.e. the imaginary unit. it can be used to quickly and
32 conviniently write complex constants like this: "4+3*i".
33
34 · Use "r2C(real-values)" to convert from real to complex, as in "$r =
35 Cpow $cplx, r2C 2". The overloaded operators automatically do that
36 for you, all the other functions, do not. So "Croots 1, 5" will
37 return all the fifths roots of 1+1*i (due to threading).
38
39 · use "cplx(real-valued-piddle)" to cast from normal piddles intot he
40 complex datatype. Use "real(complex-valued-piddle)" to cast back.
41 This requires a copy, though.
42
43 · This module has received some testing by Vanuxem Grégory (g.vanuxem
44 at wanadoo dot fr). Please report any other errors you come across!
45
47 The complex constant five is equal to "pdl(1,0)":
48
49 perldl> p $x = r2C 5
50 [5 0]
51
52 Now calculate the three roots of of five:
53
54 perldl> p $r = Croots $x, 3
55
56 [
57 [ 1.7099759 0]
58 [-0.85498797 1.4808826]
59 [-0.85498797 -1.4808826]
60 ]
61
62 Check that these really are the roots of unity:
63
64 perldl> p $r ** 3
65
66 [
67 [ 5 0]
68 [ 5 -3.4450524e-15]
69 [ 5 -9.8776239e-15]
70 ]
71
72 Duh! Could be better. Now try by multiplying $r three times with
73 itself:
74
75 perldl> p $r*$r*$r
76
77 [
78 [ 5 0]
79 [ 5 -2.8052647e-15]
80 [ 5 -7.5369398e-15]
81 ]
82
83 Well... maybe "Cpow" (which is used by the "**" operator) isn't as bad
84 as I thought. Now multiply by "i" and negate, which is just a very
85 expensive way of swapping real and imaginary parts.
86
87 perldl> p -($r*i)
88
89 [
90 [ -0 1.7099759]
91 [ 1.4808826 -0.85498797]
92 [ -1.4808826 -0.85498797]
93 ]
94
95 Now plot the magnitude of (part of) the complex sine. First generate
96 the coefficients:
97
98 perldl> $sin = i * zeroes(50)->xlinvals(2,4)
99 + zeroes(50)->xlinvals(0,7)
100
101 Now plot the imaginary part, the real part and the magnitude of the
102 sine into the same diagram:
103
104 perldl> line im sin $sin; hold
105 perldl> line re sin $sin
106 perldl> line abs sin $sin
107
108 Sorry, but I didn't yet try to reproduce the diagram in this text. Just
109 run the commands yourself, making sure that you have loaded "PDL::Com‐
110 plex" (and "PDL::Graphics::PGPLOT").
111
113 cplx real-valued-pdl
114
115 Cast a real-valued piddle to the complex datatype. The first dimension
116 of the piddle must be of size 2. After this the usual (complex) arith‐
117 metic operators are applied to this pdl, rather than the normal elemen‐
118 twise pdl operators. Dataflow to the complex parent works. Use "sever"
119 on the result if you don't want this.
120
121 complex real-valued-pdl
122
123 Cast a real-valued piddle to the complex datatype without dataflow and
124 inplace. Achieved by merely reblessing a piddle. The first dimension of
125 the piddle must be of size 2.
126
127 real cplx-valued-pdl
128
129 Cast a complex valued pdl back to the "normal" pdl datatype. Afterwards
130 the normal elementwise pdl operators are used in operations. Dataflow
131 to the real parent works. Use "sever" on the result if you don't want
132 this.
133
134 r2C
135
136 Signature: (r(); [o]c(m=2))
137
138 convert real to complex, assuming an imaginary part of zero
139
140 i2C
141
142 Signature: (r(); [o]c(m=2))
143
144 convert imaginary to complex, assuming a real part of zero
145
146 Cr2p
147
148 Signature: (r(m=2); float+ [o]p(m=2))
149
150 convert complex numbers in rectangular form to polar (mod,arg) form
151
152 Cp2r
153
154 Signature: (r(m=2); [o]p(m=2))
155
156 convert complex numbers in polar (mod,arg) form to rectangular form
157
158 Cmul
159
160 Signature: (a(m=2); b(m=2); [o]c(m=2))
161
162 complex multiplication
163
164 Cprodover
165
166 Signature: (a(m=2,n); [o]c(m=2))
167
168 Project via product to N-1 dimension
169
170 Cscale
171
172 Signature: (a(m=2); b(); [o]c(m=2))
173
174 mixed complex/real multiplication
175
176 Cdiv
177
178 Signature: (a(m=2); b(m=2); [o]c(m=2))
179
180 complex division
181
182 Ccmp
183
184 Signature: (a(m=2); b(m=2); [o]c())
185
186 Complex comparison oeprator (spaceship). It orders by real first, then
187 by imaginary. Hm, but it is mathematical nonsense! Complex numbers can‐
188 not be ordered.
189
190 Cconj
191
192 Signature: (a(m=2); [o]c(m=2))
193
194 complex conjugation
195
196 Cabs
197
198 Signature: (a(m=2); [o]c())
199
200 complex "abs()" (also known as modulus)
201
202 Cabs2
203
204 Signature: (a(m=2); [o]c())
205
206 complex squared "abs()" (also known squared modulus)
207
208 Carg
209
210 Signature: (a(m=2); [o]c())
211
212 complex argument function ("angle")
213
214 Csin
215
216 Signature: (a(m=2); [o]c(m=2))
217
218 sin (a) = 1/(2*i) * (exp (a*i) - exp (-a*i))
219
220 Ccos
221
222 Signature: (a(m=2); [o]c(m=2))
223
224 cos (a) = 1/2 * (exp (a*i) + exp (-a*i))
225
226 Ctan a [not inplace]
227
228 tan (a) = -i * (exp (a*i) - exp (-a*i)) / (exp (a*i) + exp (-a*i))
229
230 Cexp
231
232 Signature: (a(m=2); [o]c(m=2))
233
234 exp (a) = exp (real (a)) * (cos (imag (a)) + i * sin (imag (a)))
235
236 Clog
237
238 Signature: (a(m=2); [o]c(m=2))
239
240 log (a) = log (cabs (a)) + i * carg (a)
241
242 Cpow
243
244 Signature: (a(m=2); b(m=2); [o]c(m=2))
245
246 complex "pow()" ("**"-operator)
247
248 Csqrt
249
250 Signature: (a(m=2); [o]c(m=2))
251
252 Casin
253
254 Signature: (a(m=2); [o]c(m=2))
255
256 Cacos
257
258 Signature: (a(m=2); [o]c(m=2))
259
260 Catan cplx [not inplace]
261
262 Return the complex "atan()".
263
264 Csinh
265
266 Signature: (a(m=2); [o]c(m=2))
267
268 sinh (a) = (exp (a) - exp (-a)) / 2
269
270 Ccosh
271
272 Signature: (a(m=2); [o]c(m=2))
273
274 cosh (a) = (exp (a) + exp (-a)) / 2
275
276 Ctanh
277
278 Signature: (a(m=2); [o]c(m=2))
279
280 Casinh
281
282 Signature: (a(m=2); [o]c(m=2))
283
284 Cacosh
285
286 Signature: (a(m=2); [o]c(m=2))
287
288 Catanh
289
290 Signature: (a(m=2); [o]c(m=2))
291
292 Cproj
293
294 Signature: (a(m=2); [o]c(m=2))
295
296 compute the projection of a complex number to the riemann sphere
297
298 Croots
299
300 Signature: (a(m=2); [o]c(m=2,n); int n => n)
301
302 Compute the "n" roots of "a". "n" must be a positive integer. The
303 result will always be a complex type!
304
305 re cplx, im cplx
306
307 Return the real or imaginary part of the complex number(s) given. These
308 are slicing operators, so data flow works. The real and imaginary parts
309 are returned as piddles (ref eq PDL).
310
311 rCpolynomial
312
313 Signature: (coeffs(n); x(c=2,m); [o]out(c=2,m))
314
315 evaluate the polynomial with (real) coefficients "coeffs" at the (com‐
316 plex) position(s) "x". "coeffs[0]" is the constant term.
317
319 Copyright (C) 2000 Marc Lehmann <pcg@goof.com>. All rights reserved.
320 There is no warranty. You are allowed to redistribute this software /
321 documentation as described in the file COPYING in the PDL distribution.
322
324 perl(1), PDL.
325
326
327
328perl v5.8.8 2006-12-02 Complex(3)