Engee documentation
Notebook

Building complex numbers

In this demo, we will look at the basic techniques for graphically displaying complex numbers in Engee.

A complex number - this is a number of the form , where - real numbers, - imaginary unit . Number - the real part of a complex number and is denoted as . Number - the imaginary part of a complex number and it bears the designation .

A complex number It can be constructed in the Cartesian coordinate system, where the real part of it is deposited along the abscissa axis. , and along the ordinate axis , the imaginary .

Also it can be constructed in polar coordinates. To do this, refer to its exponential or trigonometric notation. .
From here: the module and the argument - respectively, the distance from the origin and the radius vector with the abscissa axis for a complex number .

Constructing a 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
    ];

Download the library and connect the backend for charting.

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 construct a given vector on the complex plane by substituting the coordinates extracted from the z real and imaginary parts.

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

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

Roots of the nth degree from unity on the complex plane

For a polynomial of the form , where , complex roots can be found according to the Moivre formula:

, where

Let's define a 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)

Let's 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 will pass the functions 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 the nth degree of 1, the modulus of complex roots will be 1. To verify this, let's move on to mapping these roots in polar coordinates. Functions scatter() in this case, arrays of arguments and modules of complex roots must be passed.

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 the roots of the 5th degree of 1, their modules are equal to 1, on the complex plane they are located at the vertices of a regular pentagon centered at the origin, and one of the roots is equal to the complex unit .

Parametric curve on the complex plane

We construct a parametric curve on the complex plane, which is represented by the expression .
Parameter will be defined in the range .
Setting an array of 200 parameter points in the set range, we will calculate the array of points of the parametric curve.

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

Let's display a parametric curve on a complex plane in a 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]:

Let's 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 a matrix on the complex plane

For a square matrix There are eigenvalues that are either valid or complex conjugate pairs.
Let's make sure of this property and display the eigenvalues on the complex plane.

To find the associated values, download 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's find the eigenvalues for the matrix , randomly generated.

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

Let's 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 the eigenvalues for a square matrix On the complex plane, 20 points can be observed, which are either located on the axis of real values, or represent complex conjugate pairs.

Multiple arrays of complex numbers on a plane

Let's consider the representation on the complex plane of two different sets of complex data. They will be defined by the following expressions:



Define a vector of values with a step and calculate the arrays of values and .

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

Extract the real and imaginary parts of the numbers for both arrays.:

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

Let's build 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 reviewed various tools and techniques for graphically representing complex numbers in Engee.