## Representing complex numbers/Statistical analysis by PDL

Complex numbers are represented using the ** i ** function, which represents the imaginary unit that becomes available when you read ** PDL::Complex **.

For example, to represent the complex number ** 3 + 2i **:

use PDL; use PDL::Complex; my $num = 3 + 2 * i;

When this complex number is output, it will be displayed as follows.

3 + 2i

### Use the imaginary unit i

PDL can handle imaginary numbers. To handle imaginary numbers, load ** PDL::PDL::Complex **.

use PDL::Complex;

Then the ** i ** function that represents the imaginary unit is imported.

i

When the imaginary unit is output, it is output as follows.

0 + 1i

The imaginary unit i was a number that squared to -1. Let's square it.

my $ret = i * i;

When this is output, it will be displayed as follows.

-1 + 0i

### Four arithmetic operations on complex numbers

Perform four arithmetic operations on complex numbers using ** + **, **-**, ** * **, **/** as in normal operations. I can.

use PDL; use PDL::Complex; my $num1 = 1 + 2 * i; my $num2 = 3 + 4 * i;

#### Japanese

# 4 + 6i my $sum = $num1 + $num2;

#### Difference

# 2 + 2i my $sub = $num2- $num1;

#### Product

# -5 + 10i my $multi = $num1 * $num2;

#### Commerce

# 0.44 + 0.08i my $div = $num1/$num2;

### Calculate the nth root

Use the ** Croots ** function to calculate the nth root.

use PDL; use PDL::Complex; # 5 cube root my $nums = Croots (5, 3);

The output of the calculation result is as follows.

[1.85398 + 0.496773i -1.35721 + 1.35721i -0.496773 -1.85398i]

### Convert real numbers to complex numbers

Use the ** r2C ** function to convert a real number to a complex number.

use PDL; use PDL::Complex; my $num = r2C 5;

The output result is as follows.

5 + 0i