Engee documentation
Notebook

Creating and calculating polynomials

This demo covers basic operations with polynomials: creating, calculating, and finding roots.

To work with polynomials - their creation, calculation, finding roots and mathematical operations on them in Engee you can use the library Polynomials.jl. Let's download and connect it for this example.

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

Creating a polynomial

In order to create a polynomial of the form $4 \cdot x^2 - 4 \cdot x + 1$ we will use the function Polynomial(), to which we pass the vector of its coefficients, starting from the free term.

In [ ]:
Polynomial([1, -4, 4])
Out[0]:
1 - 4∙x + 4∙x2

You can also create a polynomial by passing a variable of coefficients to the function - as a vector or matrix $1 \times (n+1)$. The result of the function (polynomial) can be passed to the variable for further operations.

In [ ]:
p = [4 0 0 -3 2 33];
y = Polynomial(p)
Out[0]:
4 - 3∙x3 + 2∙x4 + 33∙x5

The function of creating a polynomial can be calculated element by element. The result of such a call will be a vector of polynomials, and a vector consisting of vectors of coefficients will have to be passed to the function.

In [ ]:
P1 = [1, 2, 3, 0, 1];
P2 = [1, 0, -1];
P3 = [0, -1, 4, -2];
P = [P1, P2, P3];

Y = Polynomial.(P)
Out[0]:
3-element Vector{Polynomial{Int64, :x}}:
 Polynomial(1 + 2*x + 3*x^2 + x^4)
 Polynomial(1 - x^2)
 Polynomial(-x + 4*x^2 - 2*x^3)

By default, the polynomial variable is x. To change the variable, it is necessary to set it explicitly:

In [ ]:
Polynomial([1,2,3], :t)
Out[0]:
1 + 2∙t + 3∙t2

You can create a polynomial by the values of its roots, using the function fromroots(). The form of the polynomial assignment $(x-4) \cdot (x-1) \cdot (x-2)$ will be reproduced in the following form.

In [ ]:
u = fromroots([4,1,2])
Out[0]:
-8 + 14∙x - 7∙x2 + x3

Calculating the polynomial

The calculation of polynomials is done using the call notation. Let's calculate the previously created polynomial $y=4-3 \cdot x^3 + 2 \cdot x^4 + 33 \cdot x^5$ at the point $x = 1$.

In [ ]:
y(1)
Out[0]:
36

To calculate the polynomial created by the element-by-element call, it is necessary to refer to the element of the created vector. For example, let's calculate the polynomial $Y_2 = -x^2+1$ at the point $x = 0,1$.

In [ ]:
Y[2](0.1)
Out[0]:
0.99

Finding the roots of the polynomial

The function roots(). is used to find the roots of a polynomial. The polynomial must be passed to it - for example, through the create function:

In [ ]:
roots(Polynomial([1, 0, -1]))
Out[0]:
2-element Vector{Float64}:
 -1.0
  1.0

Let's calculate the roots of the function created earlier using the values of the roots: $u = (x-4) \cdot (x-1) \cdot (x-2)$.

In [ ]:
roots(u)
Out[0]:
3-element Vector{Float64}:
 0.9999999999999991
 2.0000000000000018
 3.9999999999999982

As can be seen from the result of the function calculation, the obtained root values are not equal to the given values, which is explained by the error in calculating floating point numbers.

The result of calculating the function of finding roots can be a vector of complex numbers. For example, let's calculate the roots for the previously given polynomial $y=4-3 \cdot x^3 + 2 \cdot x^4 + 33 \cdot x^5$.

In [ ]:
roots(y)
Out[0]:
5-element Vector{ComplexF64}:
  -0.6988527464788102 + 0.0im
 -0.22121608146338845 - 0.5967022127055316im
 -0.22121608146338845 + 0.5967022127055316im
   0.5403394243997632 - 0.3691905470638564im
   0.5403394243997632 + 0.3691905470638564im

Roots can also be calculated element by element, for a vector of polynomials $Y$:

In [ ]:
roots.(Y)
Out[0]:
3-element Vector{Vector}:
 ComplexF64[-0.34974582621172073 - 0.43899033747531147im, -0.34974582621172073 + 0.43899033747531147im, 0.34974582621172157 - 1.746977896113271im, 0.34974582621172157 + 1.746977896113271im]
 [-1.0, 1.0]
 [0.2928932188134525, 1.7071067811865475, 0.0]

Conclusion

In this example we have looked at the basic approaches of basic operations with polynomials: creating, calculating and finding roots. For a more detailed study of the topic and to improve your skills of working with polynomials in Engee, we recommend you to refer to the relevant sections of the documentation: Polynomials.jl Library.