1Math::Complex(3pm)     Perl Programmers Reference Guide     Math::Complex(3pm)
2
3
4

NAME

6       Math::Complex - complex numbers and associated mathematical functions
7

SYNOPSIS

9               use Math::Complex;
10
11               $z = Math::Complex->make(5, 6);
12               $t = 4 - 3*i + $z;
13               $j = cplxe(1, 2*pi/3);
14

DESCRIPTION

16       This package lets you create and manipulate complex numbers. By
17       default, Perl limits itself to real numbers, but an extra "use" state‐
18       ment brings full complex support, along with a full set of mathematical
19       functions typically associated with and/or extended to complex numbers.
20
21       If you wonder what complex numbers are, they were invented to be able
22       to solve the following equation:
23
24               x*x = -1
25
26       and by definition, the solution is noted i (engineers use j instead
27       since i usually denotes an intensity, but the name does not matter).
28       The number i is a pure imaginary number.
29
30       The arithmetics with pure imaginary numbers works just like you would
31       expect it with real numbers... you just have to remember that
32
33               i*i = -1
34
35       so you have:
36
37               5i + 7i = i * (5 + 7) = 12i
38               4i - 3i = i * (4 - 3) = i
39               4i * 2i = -8
40               6i / 2i = 3
41               1 / i = -i
42
43       Complex numbers are numbers that have both a real part and an imaginary
44       part, and are usually noted:
45
46               a + bi
47
48       where "a" is the real part and "b" is the imaginary part. The arith‐
49       metic with complex numbers is straightforward. You have to keep track
50       of the real and the imaginary parts, but otherwise the rules used for
51       real numbers just apply:
52
53               (4 + 3i) + (5 - 2i) = (4 + 5) + i(3 - 2) = 9 + i
54               (2 + i) * (4 - i) = 2*4 + 4i -2i -i*i = 8 + 2i + 1 = 9 + 2i
55
56       A graphical representation of complex numbers is possible in a plane
57       (also called the complex plane, but it's really a 2D plane).  The num‐
58       ber
59
60               z = a + bi
61
62       is the point whose coordinates are (a, b). Actually, it would be the
63       vector originating from (0, 0) to (a, b). It follows that the addition
64       of two complex numbers is a vectorial addition.
65
66       Since there is a bijection between a point in the 2D plane and a com‐
67       plex number (i.e. the mapping is unique and reciprocal), a complex num‐
68       ber can also be uniquely identified with polar coordinates:
69
70               [rho, theta]
71
72       where "rho" is the distance to the origin, and "theta" the angle
73       between the vector and the x axis. There is a notation for this using
74       the exponential form, which is:
75
76               rho * exp(i * theta)
77
78       where i is the famous imaginary number introduced above. Conversion
79       between this form and the cartesian form "a + bi" is immediate:
80
81               a = rho * cos(theta)
82               b = rho * sin(theta)
83
84       which is also expressed by this formula:
85
86               z = rho * exp(i * theta) = rho * (cos theta + i * sin theta)
87
88       In other words, it's the projection of the vector onto the x and y
89       axes. Mathematicians call rho the norm or modulus and theta the argu‐
90       ment of the complex number. The norm of "z" will be noted abs(z).
91
92       The polar notation (also known as the trigonometric representation) is
93       much more handy for performing multiplications and divisions of complex
94       numbers, whilst the cartesian notation is better suited for additions
95       and subtractions. Real numbers are on the x axis, and therefore theta
96       is zero or pi.
97
98       All the common operations that can be performed on a real number have
99       been defined to work on complex numbers as well, and are merely exten‐
100       sions of the operations defined on real numbers. This means they keep
101       their natural meaning when there is no imaginary part, provided the
102       number is within their definition set.
103
104       For instance, the "sqrt" routine which computes the square root of its
105       argument is only defined for non-negative real numbers and yields a
106       non-negative real number (it is an application from R+ to R+).  If we
107       allow it to return a complex number, then it can be extended to nega‐
108       tive real numbers to become an application from R to C (the set of com‐
109       plex numbers):
110
111               sqrt(x) = x >= 0 ? sqrt(x) : sqrt(-x)*i
112
113       It can also be extended to be an application from C to C, whilst its
114       restriction to R behaves as defined above by using the following defi‐
115       nition:
116
117               sqrt(z = [r,t]) = sqrt(r) * exp(i * t/2)
118
119       Indeed, a negative real number can be noted "[x,pi]" (the modulus x is
120       always non-negative, so "[x,pi]" is really "-x", a negative number) and
121       the above definition states that
122
123               sqrt([x,pi]) = sqrt(x) * exp(i*pi/2) = [sqrt(x),pi/2] = sqrt(x)*i
124
125       which is exactly what we had defined for negative real numbers above.
126       The "sqrt" returns only one of the solutions: if you want the both, use
127       the "root" function.
128
129       All the common mathematical functions defined on real numbers that are
130       extended to complex numbers share that same property of working as
131       usual when the imaginary part is zero (otherwise, it would not be
132       called an extension, would it?).
133
134       A new operation possible on a complex number that is the identity for
135       real numbers is called the conjugate, and is noted with a horizontal
136       bar above the number, or "~z" here.
137
138                z = a + bi
139               ~z = a - bi
140
141       Simple... Now look:
142
143               z * ~z = (a + bi) * (a - bi) = a*a + b*b
144
145       We saw that the norm of "z" was noted abs(z) and was defined as the
146       distance to the origin, also known as:
147
148               rho = abs(z) = sqrt(a*a + b*b)
149
150       so
151
152               z * ~z = abs(z) ** 2
153
154       If z is a pure real number (i.e. "b == 0"), then the above yields:
155
156               a * a = abs(a) ** 2
157
158       which is true ("abs" has the regular meaning for real number, i.e.
159       stands for the absolute value). This example explains why the norm of
160       "z" is noted abs(z): it extends the "abs" function to complex numbers,
161       yet is the regular "abs" we know when the complex number actually has
162       no imaginary part... This justifies a posteriori our use of the "abs"
163       notation for the norm.
164

OPERATIONS

166       Given the following notations:
167
168               z1 = a + bi = r1 * exp(i * t1)
169               z2 = c + di = r2 * exp(i * t2)
170               z = <any complex or real number>
171
172       the following (overloaded) operations are supported on complex numbers:
173
174               z1 + z2 = (a + c) + i(b + d)
175               z1 - z2 = (a - c) + i(b - d)
176               z1 * z2 = (r1 * r2) * exp(i * (t1 + t2))
177               z1 / z2 = (r1 / r2) * exp(i * (t1 - t2))
178               z1 ** z2 = exp(z2 * log z1)
179               ~z = a - bi
180               abs(z) = r1 = sqrt(a*a + b*b)
181               sqrt(z) = sqrt(r1) * exp(i * t/2)
182               exp(z) = exp(a) * exp(i * b)
183               log(z) = log(r1) + i*t
184               sin(z) = 1/2i (exp(i * z1) - exp(-i * z))
185               cos(z) = 1/2 (exp(i * z1) + exp(-i * z))
186               atan2(y, x) = atan(y / x) # Minding the right quadrant, note the order.
187
188       The definition used for complex arguments of atan2() is
189
190              -i log((x + iy)/sqrt(x*x+y*y))
191
192       The following extra operations are supported on both real and complex
193       numbers:
194
195               Re(z) = a
196               Im(z) = b
197               arg(z) = t
198               abs(z) = r
199
200               cbrt(z) = z ** (1/3)
201               log10(z) = log(z) / log(10)
202               logn(z, n) = log(z) / log(n)
203
204               tan(z) = sin(z) / cos(z)
205
206               csc(z) = 1 / sin(z)
207               sec(z) = 1 / cos(z)
208               cot(z) = 1 / tan(z)
209
210               asin(z) = -i * log(i*z + sqrt(1-z*z))
211               acos(z) = -i * log(z + i*sqrt(1-z*z))
212               atan(z) = i/2 * log((i+z) / (i-z))
213
214               acsc(z) = asin(1 / z)
215               asec(z) = acos(1 / z)
216               acot(z) = atan(1 / z) = -i/2 * log((i+z) / (z-i))
217
218               sinh(z) = 1/2 (exp(z) - exp(-z))
219               cosh(z) = 1/2 (exp(z) + exp(-z))
220               tanh(z) = sinh(z) / cosh(z) = (exp(z) - exp(-z)) / (exp(z) + exp(-z))
221
222               csch(z) = 1 / sinh(z)
223               sech(z) = 1 / cosh(z)
224               coth(z) = 1 / tanh(z)
225
226               asinh(z) = log(z + sqrt(z*z+1))
227               acosh(z) = log(z + sqrt(z*z-1))
228               atanh(z) = 1/2 * log((1+z) / (1-z))
229
230               acsch(z) = asinh(1 / z)
231               asech(z) = acosh(1 / z)
232               acoth(z) = atanh(1 / z) = 1/2 * log((1+z) / (z-1))
233
234       arg, abs, log, csc, cot, acsc, acot, csch, coth, acosech, acotanh, have
235       aliases rho, theta, ln, cosec, cotan, acosec, acotan, cosech, cotanh,
236       acosech, acotanh, respectively.  "Re", "Im", "arg", "abs", "rho", and
237       "theta" can be used also as mutators.  The "cbrt" returns only one of
238       the solutions: if you want all three, use the "root" function.
239
240       The root function is available to compute all the n roots of some com‐
241       plex, where n is a strictly positive integer.  There are exactly n such
242       roots, returned as a list. Getting the number mathematicians call "j"
243       such that:
244
245               1 + j + j*j = 0;
246
247       is a simple matter of writing:
248
249               $j = ((root(1, 3))[1];
250
251       The kth root for "z = [r,t]" is given by:
252
253               (root(z, n))[k] = r**(1/n) * exp(i * (t + 2*k*pi)/n)
254
255       You can return the kth root directly by "root(z, n, k)", indexing
256       starting from zero and ending at n - 1.
257
258       The spaceship comparison operator, <=>, is also defined. In order to
259       ensure its restriction to real numbers is conform to what you would
260       expect, the comparison is run on the real part of the complex number
261       first, and imaginary parts are compared only when the real parts match.
262

CREATION

264       To create a complex number, use either:
265
266               $z = Math::Complex->make(3, 4);
267               $z = cplx(3, 4);
268
269       if you know the cartesian form of the number, or
270
271               $z = 3 + 4*i;
272
273       if you like. To create a number using the polar form, use either:
274
275               $z = Math::Complex->emake(5, pi/3);
276               $x = cplxe(5, pi/3);
277
278       instead. The first argument is the modulus, the second is the angle (in
279       radians, the full circle is 2*pi).  (Mnemonic: "e" is used as a nota‐
280       tion for complex numbers in the polar form).
281
282       It is possible to write:
283
284               $x = cplxe(-3, pi/4);
285
286       but that will be silently converted into "[3,-3pi/4]", since the modu‐
287       lus must be non-negative (it represents the distance to the origin in
288       the complex plane).
289
290       It is also possible to have a complex number as either argument of the
291       "make", "emake", "cplx", and "cplxe": the appropriate component of the
292       argument will be used.
293
294               $z1 = cplx(-2,  1);
295               $z2 = cplx($z1, 4);
296
297       The "new", "make", "emake", "cplx", and "cplxe" will also understand a
298       single (string) argument of the forms
299
300               2-3i
301               -3i
302               [2,3]
303               [2,-3pi/4]
304               [2]
305
306       in which case the appropriate cartesian and exponential components will
307       be parsed from the string and used to create new complex numbers.  The
308       imaginary component and the theta, respectively, will default to zero.
309
310       The "new", "make", "emake", "cplx", and "cplxe" will also understand
311       the case of no arguments: this means plain zero or (0, 0).
312

DISPLAYING

314       When printed, a complex number is usually shown under its cartesian
315       style a+bi, but there are legitimate cases where the polar style [r,t]
316       is more appropriate.  The process of converting the complex number into
317       a string that can be displayed is known as stringification.
318
319       By calling the class method "Math::Complex::display_format" and supply‐
320       ing either "polar" or "cartesian" as an argument, you override the
321       default display style, which is "cartesian". Not supplying any argument
322       returns the current settings.
323
324       This default can be overridden on a per-number basis by calling the
325       "display_format" method instead. As before, not supplying any argument
326       returns the current display style for this number. Otherwise whatever
327       you specify will be the new display style for this particular number.
328
329       For instance:
330
331               use Math::Complex;
332
333               Math::Complex::display_format('polar');
334               $j = (root(1, 3))[1];
335               print "j = $j\n";               # Prints "j = [1,2pi/3]"
336               $j->display_format('cartesian');
337               print "j = $j\n";               # Prints "j = -0.5+0.866025403784439i"
338
339       The polar style attempts to emphasize arguments like k*pi/n (where n is
340       a positive integer and k an integer within [-9, +9]), this is called
341       polar pretty-printing.
342
343       For the reverse of stringifying, see the "make" and "emake".
344
345       CHANGED IN PERL 5.6
346
347       The "display_format" class method and the corresponding "display_for‐
348       mat" object method can now be called using a parameter hash instead of
349       just a one parameter.
350
351       The old display format style, which can have values "cartesian" or
352       "polar", can be changed using the "style" parameter.
353
354               $j->display_format(style => "polar");
355
356       The one parameter calling convention also still works.
357
358               $j->display_format("polar");
359
360       There are two new display parameters.
361
362       The first one is "format", which is a sprintf()-style format string to
363       be used for both numeric parts of the complex number(s).  The is some‐
364       what system-dependent but most often it corresponds to "%.15g".  You
365       can revert to the default by setting the "format" to "undef".
366
367               # the $j from the above example
368
369               $j->display_format('format' => '%.5f');
370               print "j = $j\n";               # Prints "j = -0.50000+0.86603i"
371               $j->display_format('format' => undef);
372               print "j = $j\n";               # Prints "j = -0.5+0.86603i"
373
374       Notice that this affects also the return values of the "display_format"
375       methods: in list context the whole parameter hash will be returned, as
376       opposed to only the style parameter value.  This is a potential incom‐
377       patibility with earlier versions if you have been calling the "dis‐
378       play_format" method in list context.
379
380       The second new display parameter is "polar_pretty_print", which can be
381       set to true or false, the default being true.  See the previous section
382       for what this means.
383

USAGE

385       Thanks to overloading, the handling of arithmetics with complex numbers
386       is simple and almost transparent.
387
388       Here are some examples:
389
390               use Math::Complex;
391
392               $j = cplxe(1, 2*pi/3);  # $j ** 3 == 1
393               print "j = $j, j**3 = ", $j ** 3, "\n";
394               print "1 + j + j**2 = ", 1 + $j + $j**2, "\n";
395
396               $z = -16 + 0*i;                 # Force it to be a complex
397               print "sqrt($z) = ", sqrt($z), "\n";
398
399               $k = exp(i * 2*pi/3);
400               print "$j - $k = ", $j - $k, "\n";
401
402               $z->Re(3);                      # Re, Im, arg, abs,
403               $j->arg(2);                     # (the last two aka rho, theta)
404                                               # can be used also as mutators.
405

ERRORS DUE TO DIVISION BY ZERO OR LOGARITHM OF ZERO

407       The division (/) and the following functions
408
409               log     ln      log10   logn
410               tan     sec     csc     cot
411               atan    asec    acsc    acot
412               tanh    sech    csch    coth
413               atanh   asech   acsch   acoth
414
415       cannot be computed for all arguments because that would mean dividing
416       by zero or taking logarithm of zero. These situations cause fatal run‐
417       time errors looking like this
418
419               cot(0): Division by zero.
420               (Because in the definition of cot(0), the divisor sin(0) is 0)
421               Died at ...
422
423       or
424
425               atanh(-1): Logarithm of zero.
426               Died at...
427
428       For the "csc", "cot", "asec", "acsc", "acot", "csch", "coth", "asech",
429       "acsch", the argument cannot be 0 (zero).  For the logarithmic func‐
430       tions and the "atanh", "acoth", the argument cannot be 1 (one).  For
431       the "atanh", "acoth", the argument cannot be "-1" (minus one).  For the
432       "atan", "acot", the argument cannot be "i" (the imaginary unit).  For
433       the "atan", "acoth", the argument cannot be "-i" (the negative imagi‐
434       nary unit).  For the "tan", "sec", "tanh", the argument cannot be pi/2
435       + k * pi, where k is any integer.  atan2(0, 0) is undefined, and if the
436       complex arguments are used for atan2(), a division by zero will happen
437       if z1**2+z2**2 == 0.
438
439       Note that because we are operating on approximations of real numbers,
440       these errors can happen when merely `too close' to the singularities
441       listed above.
442

ERRORS DUE TO INDIGESTIBLE ARGUMENTS

444       The "make" and "emake" accept both real and complex arguments.  When
445       they cannot recognize the arguments they will die with error messages
446       like the following
447
448           Math::Complex::make: Cannot take real part of ...
449           Math::Complex::make: Cannot take real part of ...
450           Math::Complex::emake: Cannot take rho of ...
451           Math::Complex::emake: Cannot take theta of ...
452

BUGS

454       Saying "use Math::Complex;" exports many mathematical routines in the
455       caller environment and even overrides some ("sqrt", "log", "atan2").
456       This is construed as a feature by the Authors, actually... ;-)
457
458       All routines expect to be given real or complex numbers. Don't attempt
459       to use BigFloat, since Perl has currently no rule to disambiguate a '+'
460       operation (for instance) between two overloaded entities.
461
462       In Cray UNICOS there is some strange numerical instability that results
463       in root(), cos(), sin(), cosh(), sinh(), losing accuracy fast.  Beware.
464       The bug may be in UNICOS math libs, in UNICOS C compiler, in Math::Com‐
465       plex.  Whatever it is, it does not manifest itself anywhere else where
466       Perl runs.
467

AUTHORS

469       Daniel S. Lewart <d-lewart@uiuc.edu>
470
471       Original authors Raphael Manfredi <Raphael_Manfredi@pobox.com> and
472       Jarkko Hietaniemi <jhi@iki.fi>
473
474
475
476perl v5.8.8                       2001-09-21                Math::Complex(3pm)
Impressum