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 GrA~Xgory
44 (g.vanuxem at wanadoo dot fr). Please report any other errors you
45 come across!
46
48 The complex constant five is equal to "pdl(1,0)":
49
50 perldl> p $x = r2C 5
51 [5 0]
52
53 Now calculate the three roots of of five:
54
55 perldl> p $r = Croots $x, 3
56
57 [
58 [ 1.7099759 0]
59 [-0.85498797 1.4808826]
60 [-0.85498797 -1.4808826]
61 ]
62
63 Check that these really are the roots of unity:
64
65 perldl> p $r ** 3
66
67 [
68 [ 5 0]
69 [ 5 -3.4450524e-15]
70 [ 5 -9.8776239e-15]
71 ]
72
73 Duh! Could be better. Now try by multiplying $r three times with
74 itself:
75
76 perldl> p $r*$r*$r
77
78 [
79 [ 5 0]
80 [ 5 -2.8052647e-15]
81 [ 5 -7.5369398e-15]
82 ]
83
84 Well... maybe "Cpow" (which is used by the "**" operator) isn't as bad
85 as I thought. Now multiply by "i" and negate, which is just a very
86 expensive way of swapping real and imaginary parts.
87
88 perldl> p -($r*i)
89
90 [
91 [ -0 1.7099759]
92 [ 1.4808826 -0.85498797]
93 [ -1.4808826 -0.85498797]
94 ]
95
96 Now plot the magnitude of (part of) the complex sine. First generate
97 the coefficients:
98
99 perldl> $sin = i * zeroes(50)->xlinvals(2,4)
100 + zeroes(50)->xlinvals(0,7)
101
102 Now plot the imaginary part, the real part and the magnitude of the
103 sine into the same diagram:
104
105 perldl> line im sin $sin; hold
106 perldl> line re sin $sin
107 perldl> line abs sin $sin
108
109 Sorry, but I didn't yet try to reproduce the diagram in this text. Just
110 run the commands yourself, making sure that you have loaded
111 "PDL::Complex" (and "PDL::Graphics::PGPLOT").
112
114 cplx real-valued-pdl
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)
117 arithmetic operators are applied to this pdl, rather than the normal
118 elementwise pdl operators. Dataflow to the complex parent works. Use
119 "sever" on the result if you don't want this.
120
121 complex real-valued-pdl
122 Cast a real-valued piddle to the complex datatype without dataflow and
123 inplace. Achieved by merely reblessing a piddle. The first dimension of
124 the piddle must be of size 2.
125
126 real cplx-valued-pdl
127 Cast a complex valued pdl back to the "normal" pdl datatype. Afterwards
128 the normal elementwise pdl operators are used in operations. Dataflow
129 to the real parent works. Use "sever" on the result if you don't want
130 this.
131
132 r2C
133 Signature: (r(); [o]c(m=2))
134
135 convert real to complex, assuming an imaginary part of zero
136
137 r2C does not process bad values. It will set the bad-value flag of all
138 output piddles if the flag is set for any of the input piddles.
139
140 i2C
141 Signature: (r(); [o]c(m=2))
142
143 convert imaginary to complex, assuming a real part of zero
144
145 i2C does not process bad values. It will set the bad-value flag of all
146 output piddles if the flag is set for any of the input piddles.
147
148 Cr2p
149 Signature: (r(m=2); float+ [o]p(m=2))
150
151 convert complex numbers in rectangular form to polar (mod,arg) form
152
153 Cr2p does not process bad values. It will set the bad-value flag of
154 all output piddles if the flag is set for any of the input piddles.
155
156 Cp2r
157 Signature: (r(m=2); [o]p(m=2))
158
159 convert complex numbers in polar (mod,arg) form to rectangular form
160
161 Cp2r does not process bad values. It will set the bad-value flag of
162 all output piddles if the flag is set for any of the input piddles.
163
164 Cmul
165 Signature: (a(m=2); b(m=2); [o]c(m=2))
166
167 complex multiplication
168
169 Cmul does not process bad values. It will set the bad-value flag of
170 all output piddles if the flag is set for any of the input piddles.
171
172 Cprodover
173 Signature: (a(m=2,n); [o]c(m=2))
174
175 Project via product to N-1 dimension
176
177 Cprodover does not process bad values. It will set the bad-value flag
178 of all output piddles if the flag is set for any of the input piddles.
179
180 Cscale
181 Signature: (a(m=2); b(); [o]c(m=2))
182
183 mixed complex/real multiplication
184
185 Cscale does not process bad values. It will set the bad-value flag of
186 all output piddles if the flag is set for any of the input piddles.
187
188 Cdiv
189 Signature: (a(m=2); b(m=2); [o]c(m=2))
190
191 complex division
192
193 Cdiv does not process bad values. It will set the bad-value flag of
194 all output piddles if the flag is set for any of the input piddles.
195
196 Ccmp
197 Signature: (a(m=2); b(m=2); [o]c())
198
199 Complex comparison oeprator (spaceship). It orders by real first, then
200 by imaginary. Hm, but it is mathematical nonsense! Complex numbers
201 cannot be ordered.
202
203 Ccmp does not process bad values. It will set the bad-value flag of
204 all output piddles if the flag is set for any of the input piddles.
205
206 Cconj
207 Signature: (a(m=2); [o]c(m=2))
208
209 complex conjugation
210
211 Cconj does not process bad values. It will set the bad-value flag of
212 all output piddles if the flag is set for any of the input piddles.
213
214 Cabs
215 Signature: (a(m=2); [o]c())
216
217 complex "abs()" (also known as modulus)
218
219 Cabs does not process bad values. It will set the bad-value flag of
220 all output piddles if the flag is set for any of the input piddles.
221
222 Cabs2
223 Signature: (a(m=2); [o]c())
224
225 complex squared "abs()" (also known squared modulus)
226
227 Cabs2 does not process bad values. It will set the bad-value flag of
228 all output piddles if the flag is set for any of the input piddles.
229
230 Carg
231 Signature: (a(m=2); [o]c())
232
233 complex argument function ("angle")
234
235 Carg does not process bad values. It will set the bad-value flag of
236 all output piddles if the flag is set for any of the input piddles.
237
238 Csin
239 Signature: (a(m=2); [o]c(m=2))
240
241 sin (a) = 1/(2*i) * (exp (a*i) - exp (-a*i))
242
243 Csin does not process bad values. It will set the bad-value flag of
244 all output piddles if the flag is set for any of the input piddles.
245
246 Ccos
247 Signature: (a(m=2); [o]c(m=2))
248
249 cos (a) = 1/2 * (exp (a*i) + exp (-a*i))
250
251 Ccos does not process bad values. It will set the bad-value flag of
252 all output piddles if the flag is set for any of the input piddles.
253
254 Ctan a [not inplace]
255 tan (a) = -i * (exp (a*i) - exp (-a*i)) / (exp (a*i) + exp (-a*i))
256
257 Cexp
258 Signature: (a(m=2); [o]c(m=2))
259
260 exp (a) = exp (real (a)) * (cos (imag (a)) + i * sin (imag (a)))
261
262 Cexp does not process bad values. It will set the bad-value flag of
263 all output piddles if the flag is set for any of the input piddles.
264
265 Clog
266 Signature: (a(m=2); [o]c(m=2))
267
268 log (a) = log (cabs (a)) + i * carg (a)
269
270 Clog does not process bad values. It will set the bad-value flag of
271 all output piddles if the flag is set for any of the input piddles.
272
273 Cpow
274 Signature: (a(m=2); b(m=2); [o]c(m=2))
275
276 complex "pow()" ("**"-operator)
277
278 Cpow does not process bad values. It will set the bad-value flag of
279 all output piddles if the flag is set for any of the input piddles.
280
281 Csqrt
282 Signature: (a(m=2); [o]c(m=2))
283
284 Csqrt does not process bad values. It will set the bad-value flag of
285 all output piddles if the flag is set for any of the input piddles.
286
287 Casin
288 Signature: (a(m=2); [o]c(m=2))
289
290 Casin does not process bad values. It will set the bad-value flag of
291 all output piddles if the flag is set for any of the input piddles.
292
293 Cacos
294 Signature: (a(m=2); [o]c(m=2))
295
296 Cacos does not process bad values. It will set the bad-value flag of
297 all output piddles if the flag is set for any of the input piddles.
298
299 Catan cplx [not inplace]
300 Return the complex "atan()".
301
302 Csinh
303 Signature: (a(m=2); [o]c(m=2))
304
305 sinh (a) = (exp (a) - exp (-a)) / 2
306
307 Csinh does not process bad values. It will set the bad-value flag of
308 all output piddles if the flag is set for any of the input piddles.
309
310 Ccosh
311 Signature: (a(m=2); [o]c(m=2))
312
313 cosh (a) = (exp (a) + exp (-a)) / 2
314
315 Ccosh does not process bad values. It will set the bad-value flag of
316 all output piddles if the flag is set for any of the input piddles.
317
318 Ctanh
319 Signature: (a(m=2); [o]c(m=2))
320
321 Ctanh does not process bad values. It will set the bad-value flag of
322 all output piddles if the flag is set for any of the input piddles.
323
324 Casinh
325 Signature: (a(m=2); [o]c(m=2))
326
327 Casinh does not process bad values. It will set the bad-value flag of
328 all output piddles if the flag is set for any of the input piddles.
329
330 Cacosh
331 Signature: (a(m=2); [o]c(m=2))
332
333 Cacosh does not process bad values. It will set the bad-value flag of
334 all output piddles if the flag is set for any of the input piddles.
335
336 Catanh
337 Signature: (a(m=2); [o]c(m=2))
338
339 Catanh does not process bad values. It will set the bad-value flag of
340 all output piddles if the flag is set for any of the input piddles.
341
342 Cproj
343 Signature: (a(m=2); [o]c(m=2))
344
345 compute the projection of a complex number to the riemann sphere
346
347 Cproj does not process bad values. It will set the bad-value flag of
348 all output piddles if the flag is set for any of the input piddles.
349
350 Croots
351 Signature: (a(m=2); [o]c(m=2,n); int n => n)
352
353 Compute the "n" roots of "a". "n" must be a positive integer. The
354 result will always be a complex type!
355
356 Croots does not process bad values. It will set the bad-value flag of
357 all output piddles if the flag is set for any of the input piddles.
358
359 re cplx, im cplx
360 Return the real or imaginary part of the complex number(s) given. These
361 are slicing operators, so data flow works. The real and imaginary parts
362 are returned as piddles (ref eq PDL).
363
364 rCpolynomial
365 Signature: (coeffs(n); x(c=2,m); [o]out(c=2,m))
366
367 evaluate the polynomial with (real) coefficients "coeffs" at the
368 (complex) position(s) "x". "coeffs[0]" is the constant term.
369
370 rCpolynomial does not process bad values. It will set the bad-value
371 flag of all output piddles if the flag is set for any of the input
372 piddles.
373
375 Copyright (C) 2000 Marc Lehmann <pcg@goof.com>. All rights reserved.
376 There is no warranty. You are allowed to redistribute this software /
377 documentation as described in the file COPYING in the PDL distribution.
378
380 perl(1), PDL.
381
382
383
384perl v5.12.3 2011-03-31 Complex(3)