Using Complex Numbers in R
Overview
R includes the complex number data type. There’s no need to implement it yourself; you just need to use it. In addition to basic arithmetic, it also includes several functions essential for handling complex numbers.
Code
Let’s say $z_{1} : = 1- i$ and $z_{2} := 1+ i$.
z_1 = 1-1i
z_2 = 1+1i
z_1 + z_2
z_1 - z_2
z_1 * z_2
z_1 / z_2
Re(z_1)
Im(z_1)
Mod(z_1)
Arg(z_1)
Conj(z_1)
Executing the code above results in the following. Let’s verify it with the formulas.
$$ z_{1} + z_{2} = 2 \\ z_{1} - z_{2} = i 2 \\ z_{1} z_{2} = (1 - i) (1 + i) = 1 - i^2 = 2 \\ {{z_{1}} \over { z_{2} }} = {{ (1 - i) } \over { (1 + i) }} = {{ (1 - i)(1 + i) } \over { (1 + i)^2 }} = {{ 2 } \over { 2i }} = -i $$
$$ \operatorname{Re} z_{1} = 1 \\ \operatorname{Im} z_{1} = -1 \\ | z_{1} | = \sqrt{1^2 + (-1)^2 } = \sqrt{2} \\ \arg z_{1} = -{{ \pi } \over {4}} \\ \overline{ z_{1} } = 1 + i $$