Engee documentation
Notebook

Construction of complex numbers

In this demo, we will look at the basic techniques when graphing complex numbers in Engee.

A complex number $z$ is a number of the form $z=x+y\cdot i$, where $x, y$ are real numbers, $i$ is an imaginary unit $i=\sqrt{-1}$. The number $x$ is the real part of the complex number $z$ and is denoted as $Re(z)$. The number $y$ is the imaginary part of the complex number $z$ and is labelled $Im(z)$.

The complex number $z$ can be plotted in the Cartesian coordinate system, where its real part $Re(z) = x$ is plotted on the abscissa axis and its imaginary part $Im(z) = y$ is plotted on the ordinate axis.

Also $z$ can be plotted in polar coordinates. To do this, we should refer to its exponential or trigonometric notation $z=r \cdot e^{i \cdot \varphi} = r \cdot (cos{\varphi} + i \cdot sin{\varphi})$.
Hence: the modulus $r = \sqrt{Re(z)^2+Im(z)^2}$ and the argument $\varphi = arctg(\frac{Im(z)}{Re(z)})$ are respectively the distance from the origin and the radius-vector with the abscissa axis for the complex number $z$.

Construction of the vector of complex numbers

Let's define a vector of complex numbers.

In [ ]:
Pkg.add(["LinearAlgebra"])
In [ ]:
z = [
    3 + 4im,
    -4 - 3im,
    1 - 2im,
    -1 - 1im
    ];

Let's load the library and connect the backend for plotting.

In [ ]:
import Pkg
Pkg.add("Plots")
using Plots;
plotlyjs()
   Resolving package versions...
  No Changes to `/user/.project/Project.toml`
  No Changes to `/user/.project/Manifest.toml`
Out[0]:
Plots.PlotlyJSBackend()

Let's plot the given vector on the complex plane by substituting the real and imaginary parts extracted from z as coordinates.

In [ ]:
scatter(
    real.(z), imag.(z),
    title="Комплексные числа",
    xlabel="Re(z)",
    ylabel="Im(z)",
    markersize=4,
    markercolor=:red,
    legend=:topleft,
    label="z"
    )
Out[0]:

When mapping complex numbers, it is not necessary to extract the real and imaginary parts. When passing complex numbers to the functions scatter(z) or plot(z), their coordinates on the complex plane will be determined automatically, as we will see below.

Roots of nth degree from one on the complex plane

For a polynomial of the form $z^n-1$, where $n \in \mathbb N$, we can find the complex roots of $z$ according to the Moiré formula:

$z_k = cos\frac{2\pi k}{n}+i \cdot sin\frac{2\pi k}{n}$, where $k=0,1,\dots,n-1$

Let us define the function for calculating the roots:

In [ ]:
nthroots(n::Integer) = [ cospi(2k/n) + sinpi(2k/n)im for k = 0:n-1 ]
Out[0]:
nthroots (generic function with 1 method)

Find the complex roots of a polynomial of degree n = 5:

In [ ]:
z = nthroots(5)
Out[0]:
5-element Vector{ComplexF64}:
                 1.0 + 0.0im
 0.30901699437494734 + 0.9510565162951536im
 -0.8090169943749475 + 0.587785252292473im
 -0.8090169943749475 - 0.587785252292473im
  0.3090169943749477 - 0.9510565162951535im

Let's display the found roots on the complex plane in the Cartesian coordinate system. In this construction we pass to the function scatter() an array of complex numbers without extracting the real and imaginary parts.

In [ ]:
scatter(
    z,
    title="Корни полинома",
    xlabel="Re(z)",
    ylabel="Im(z)",
    markersize=4,
    markercolor=:blue,
    legend=:topleft,
    label="z"
    )
Out[0]:

According to the properties of roots of n-th degree from 1, the modulus of complex roots will be equal to 1. To be sure of this, let us proceed to the mapping of these roots in polar coordinates. In this case, the function scatter() must be passed the arrays of arguments and moduli of complex roots.

In [ ]:
scatter(
    angle.(z), abs.(z),
    proj=:polar,
    marker = :circle,
    m=4, #markersize
    markercolor=:green,
    legend=:topleft,
    label="z"
    )
Out[0]:

As follows from the geometric properties of roots of degree 5 of 1, their modules are equal to 1, on the complex plane they are located in the vertices of a regular pentagon with the centre at the origin of coordinates, and one of the roots is equal to the complex unit $1+0 \cdot i$.

Parametric curve on the complex plane

Let us construct a parametric curve on the complex plane, which is represented by the expression $f(t) = t \cdot exp(t \cdot i)$.
The parameter $t$ will be defined in the range $[0,\ 4\cdot \pi]$.
Let's set an array of 200 points of the parameter $t$ in the defined range and calculate the array of points of the parametric curve.

In [ ]:
t = collect(range(0, 4pi, length=200));
z = t.*exp.(t.*im);

Display the parametric curve on the complex plane in the Cartesian coordinate system:

In [ ]:
plot(
    z,
    title="Параметрическая кривая",
    xlabel="Re(z)",
    ylabel="Im(z)",
    markersize=4,
    markercolor=:blue,
    legend=:topright,
    label="z",
    width=3
    )
Out[0]:

Display the parametric curve on the complex plane in the polar coordinate system:

In [ ]:
plot(
    angle.(z), abs.(z),
    proj=:polar,
    legend=:topleft,
    label="z",
    width=3
    )
Out[0]:

Eigenvalues of the matrix on the complex plane

For a square matrix $n\times n$ there exist $n$ eigenvalues that are either valid or are complex-conjugate pairs.
Let us verify this property and display the eigenvalues on the complex plane.

To find the conjugate values, let us load and connect the library LinearAlgebra:

In [ ]:
Pkg.add("LinearAlgebra")
using LinearAlgebra;
   Resolving package versions...
  No Changes to `/user/.project/Project.toml`
  No Changes to `/user/.project/Manifest.toml`

Let us find the eigenvalues for the matrix $20\times 20$, generated randomly.

In [ ]:
z=eigen(rand(20,20));

Let us construct the eigenvalues of the matrix on the complex plane.

In [ ]:
scatter(
    z.values,
    title="Собственные значения",
    xlabel="Re(z)",
    ylabel="Im(z)",
    markersize=4,
    markercolor=:magenta,
    legend=:topleft,
    label="z"
    )
Out[0]:

As follows from the properties of eigenvalues for a square matrix $20\times 20$, we can observe 20 points on the complex plane, which are either located on the axis of real values or represent complex-conjugate pairs.

Several arrays of complex numbers in the plane

Consider the representation of two different sets of complex data on the complex plane. They will be defined by the following expressions:

$z_1 = x^{e^{-x^2}}$
$z_2 = 2 \cdot x^{e^{-x^2}}$
$x \in [-2,\ 2]$

We define the vector of values of $x$ in steps of $0,25$ and compute the arrays of values of $z_1$ and $z_2$.

In [ ]:
x = collect(-2:0.25:2);
z1=Complex.(x).^exp.(-x.^2);
z2=2*Complex.(x).^exp.(-x.^2);

Let's extract real and imaginary parts of numbers for both arrays:

In [ ]:
re_z1 = real.(z1);
im_z1 = imag.(z1);
re_z2 = real.(z2);
im_z2 = imag.(z2);

Let us plot two sets of complex numbers on the same plane:

In [ ]:
scatter(
    re_z1, im_z1,
    title="Массивы комплексных чисел",
    xlabel="Re(z)",
    ylabel="Im(z)",
    marker=:cross,
    markersize=5,
    markercolor=:violet,
    legend=:topleft,
    label="z1"
    )
scatter!(
    re_z2, im_z2,
    marker=:xcross,
    markersize=5,
    markercolor=:orange,
    label="z2"
    )
Out[0]:

Conclusion

In this demo, we have looked at various tools and techniques for graphically representing complex numbers in Engee.