How to Use Polynomials in Julia
Overview
Polynomials.jl is a package that includes the representation and computation of polynomial functions. Since polynomials are mathematically simple, there’s a tendency to think their coding should be straightforward, too. However, once you start implementing the necessary features, it can become quite cumbersome. Of course, it’s not extremely difficult, but it’s generally better to use a package whenever possible.
This is not an exhaustive list of all features of the package, but rather a selection of potentially useful ones, so check the repository for more details1.
General Polynomial Functions
Define a Polynomial Function by Coefficients Polynomial()
julia> using Polynomials
julia> p = Polynomial([1,0,0,1])
Polynomial(1 + x^3)
julia> q = Polynomial([1,1])
Polynomial(1 + x)
julia> r = Polynomial([1,1], :t)
Polynomial(1 + t)
julia> p(0)
1
julia> p(2)
9
Polynomial{T, X}(coeffs::AbstractVector{T}, [var = :x])
- It returns the polynomial function itself.
coeffsis an array of coefficients, with the constant term at the beginning and higher-order terms towards the end. varspecifies the polynomial’s variable. As shown in the example, entering the symbol:tmakes it a polynomial int.
Define a Polynomial Function by Data fit()
julia> fit([-1,0,1], [2,0,2])
Polynomial(2.0*x^2)
fit(::Type{RationalFunction}, xs::AbstractVector{S}, ys::AbstractVector{T}, m, n; var=:x)
- It returns the polynomial function that passes through points with coordinates $x$ at
xsand $y$ atys.
Define a Polynomial Function by Roots roots()
julia> fromroots([-2,2])
Polynomial(-4 + x^2)
fromroots(::AbstractVector{<:Number}; var=:x)
- It returns the polynomial function with the provided array’s elements as roots.
Operations +, -, *, ÷
julia> p + 1
Polynomial(2 + x^3)
julia> 2p
Polynomial(2 + 2*x^3)
Adding or multiplying by a scalar results in the intuitive operation as above.
julia> p + q
Polynomial(2 + x + x^3)
julia> p - q
Polynomial(-x + x^3)
julia> p * q
Polynomial(1 + x + x^3 + x^4)
julia> p ÷ q
Polynomial(1.0 - 1.0*x + 1.0*x^2)
Arithmetic operations between polynomial functions are overridden with +, -, *, ÷.
Finding Roots roots()
julia> roots(p)
3-element Vector{ComplexF64}:
-1.0 + 0.0im
0.4999999999999998 - 0.8660254037844383im
0.4999999999999998 + 0.8660254037844383im
roots(f::AbstractPolynomial)
Differentiation derivative()
julia> derivative(p, 3)
Polynomial(6)
derivative(f::AbstractPolynomial, order::Int = 1)
- It returns the
orderth derivative off.
Integration integrate()
julia> integrate(p, 7)
Polynomial(7.0 + 1.0*x + 0.25*x^4)
integrate(f::AbstractPolynomial, C = 0)
- It returns the indefinite integral of
fwith an integration constant ofC.
Special Polynomial Functions
Laurent Polynomial Functions LaurentPolynomial()
julia> LaurentPolynomial([4,3,2,1], -1)
LaurentPolynomial(4*x⁻¹ + 3 + 2*x + x²)
LaurentPolynomial{T,X}(coeffs::AbstractVector, [m::Integer = 0], [var = :x])
- It returns the Laurent polynomial function extended to integers for degree.
- The degree of the smallest term is given as
m.
Chebyshev Polynomial Functions ChebyshevT()
julia> ChebyshevT([3,2,1])
ChebyshevT(3⋅T_0(x) + 2⋅T_1(x) + 1⋅T_2(x))
ChebyshevT{T, X}(coeffs::AbstractVector)
- It returns the Type I Chebyshev polynomial function. The formula’s
T_n(x)is represented by $T_{n}(x) = \cos \left( n \cos^{-1} x \right)$.
Environment
- OS: Windows
- julia: v1.6.2
