Engee documentation
Notebook

Creating and calculating polynomials

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

To work with polynomials - their creation, calculation, root finding 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 `~/.project/Project.toml`
  No Changes to `~/.project/Manifest.toml`

Creating a polynomial

In order to create a polynomial of the form
let's use the function Polynomial(), to which we will 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 - in the form of a vector or matrix. . The result of the function (the polynomial) can be passed to a variable for further operations.

In [ ]:
p = [4 0 0 -3 2 33];
y = Polynomial(vec(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 need 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 variable of the polynomials is x. To change a variable, you must specify it explicitly.:

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

You can create a polynomial based on the values of its roots using the function fromroots(). The form of the polynomial assignment it will be reproduced in the following format.

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

Calculating the polynomial

The polynomials are calculated using the call notation. Calculate the previously created polynomial

at the point .

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

To calculate a polynomial created using an element-by-element call, you need to access an element of the created vector. For example, calculate the polynomial at the point .

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

Finding the roots of a polynomial

To find the roots of a polynomial, the function is used roots(). It needs to be passed a polynomial, for example, through the creation function.:

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

Calculate the roots of the function created earlier from the values of the roots:
.

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

As can be seen from the result of calculating the function, the obtained values of the roots are not equal to the specified ones, which is explained by the error in calculating floating-point numbers.

The result of calculating the root finding function can be a vector of complex numbers. For example, let's calculate the roots for a previously given polynomial
.

In [ ]:
Polynomials.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

The roots can also be calculated element by element, for a vector of polynomials. :

In [ ]:
Polynomials.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 looked at the basic approaches of basic operations with polynomials: creating, calculating, and finding roots. To study the topic in more detail and improve your skills working with polynomials in Engee, we recommend that you refer to the relevant sections of the documentation.:
Library of Polynomials.jl.