Recipe 2.14 Using Complex Numbers

2.14.1 Problem

Your application must manipulate complex numbers, as are often needed in engineering, science, and mathematics.

2.14.2 Solution

Either keep track of the real and imaginary components yourself:

```# \$c = \$a * \$b manually
\$c_real = ( \$a_real * \$b_real ) - ( \$a_imaginary * \$b_imaginary );
\$c_imaginary = ( \$a_real * \$b_imaginary ) + ( \$b_real * \$a_imaginary );```

or use the Math::Complex module (part of the standard Perl distribution):

```# \$c = \$a * \$b using Math::Complex
use Math::Complex;
\$c = \$a * \$b;```

2.14.3 Discussion

Here's how you'd manually multiply 3+5i and 2-2i:

```\$a_real = 3; \$a_imaginary = 5;              # 3 + 5i;
\$b_real = 2; \$b_imaginary = -2;             # 2 - 2i;
\$c_real = ( \$a_real * \$b_real ) - ( \$a_imaginary * \$b_imaginary );
\$c_imaginary = ( \$a_real * \$b_imaginary ) + ( \$b_real * \$a_imaginary );
print "c = \${c_real}+\${c_imaginary}i\n";

c = 16+4i```

and with Math::Complex:

```use Math::Complex;
\$a = Math::Complex->new(3,5);               # or Math::Complex->new(3,5);
\$b = Math::Complex->new(2,-2);
\$c = \$a * \$b;
print "c = \$c\n";

c = 16+4i```

You may create complex numbers via the cplx constructor or via the exported constant i:

```use Math::Complex;
\$c = cplx(3,5) * cplx(2,-2);                # easier on the eye
\$d = 3 + 4*i;                               # 3 + 4i
printf "sqrt(\$d) = %s\n", sqrt(\$d);

sqrt(3+4i) = 2+i```

The Math::Trig module uses the Math::Complex module internally because some functions can break out from the real axis into the complex planefor example, the inverse sine of 2.

The documentation for the standard Math::Complex module (also in Chapter 32 of Programming Perl)  Chapter 1. Strings  Chapter 2. Numbers  Introduction  Recipe 2.1 Checking Whether a String Is a Valid Number  Recipe 2.2 Rounding Floating-Point Numbers  Recipe 2.3 Comparing Floating-Point Numbers  Recipe 2.4 Operating on a Series of Integers  Recipe 2.5 Working with Roman Numerals  Recipe 2.6 Generating Random Numbers  Recipe 2.7 Generating Repeatable Random Number Sequences  Recipe 2.8 Making Numbers Even More Random  Recipe 2.9 Generating Biased Random Numbers  Recipe 2.10 Doing Trigonometry in Degrees, Not Radians  Recipe 2.11 Calculating More Trigonometric Functions  Recipe 2.12 Taking Logarithms  Recipe 2.13 Multiplying Matrices  Recipe 2.14 Using Complex Numbers  Recipe 2.15 Converting Binary, Octal, and Hexadecimal Numbers  Recipe 2.16 Putting Commas in Numbers  Recipe 2.17 Printing Correct Plurals  Recipe 2.18 Program: Calculating Prime Factors  Chapter 3. Dates and Times  Chapter 4. Arrays  Chapter 5. Hashes  Chapter 6. Pattern Matching  Chapter 7. File Access  Chapter 8. File Contents  Chapter 9. Directories  Chapter 10. Subroutines  Chapter 11. References and Records  Chapter 12. Packages, Libraries, and Modules  Chapter 13. Classes, Objects, and Ties  Chapter 14. Database Access  Chapter 15. Interactivity  Chapter 16. Process Management and Communication  Chapter 17. Sockets  Chapter 18. Internet Services  Chapter 19. CGI Programming  Chapter 20. Web Automation  Chapter 21. mod_perl  Chapter 22. XML