Manual

# Polynomials.jl

Polynomials.jl is a Julia package that provides basic arithmetic, integration, differentiation, evaluation, and root finding over dense univariate polynomials.

To install the package, run

``Pkg.add("Polynomials")``

The package can then be loaded into the current session using

``using Polynomials``

## Functions

``Poly{T<:Number}(a::AbstractVector{T}, [x])``

Construct a polynomial from its coefficients `a`, lowest order first, optionally in terms of the given variable `x`. `x` can be a character, symbol, or string.

If \$p = a_n x^n + \ldots + a_2 x^2 + a_1 x + a_0\$, we construct this through `Poly([a_0, a_1, ..., a_n])`.

The usual arithmetic operators are overloaded to work with polynomials as well as with combinations of polynomials and scalars. However, operations involving two polynomials of different variables causes an error.

Examples

``````julia> Poly([1, 0, 3, 4])
Poly(1 + 3⋅x^2 + 4⋅x^3)

julia> Poly([1, 2, 3], :s)
Poly(1 + 2⋅s + 3⋅s^2)

julia> a = Poly([1, 2, 3], :x); b = Poly([1, 2, 3], :s);

julia> a + b
ERROR: Polynomials must have same variable
...

julia> p = Poly([1, 2])
Poly(1 + 2⋅x)

julia> q = Poly([1, 0, -1])
Poly(1 - x^2)

julia> 2p
Poly(2 + 4⋅x)

julia> 2 + p
Poly(3 + 2⋅x)

julia> p - q
Poly(2⋅x + x^2)

julia> p * q
Poly(1 + 2⋅x - x^2 - 2⋅x^3)

julia> q / 2
Poly(0.5 - 0.5⋅x^2)``````
source
``poly(r)``

Construct a polynomial from its roots. Compare this to the `Poly` type constructor, which constructs a polynomial from its coefficients.

If `r` is a vector, the constructed polynomial is \$(x - r_1) (x - r_2) \cdots (x - r_n)\$. If `r` is a matrix, the constructed polynomial is \$(x - e_1) \cdots (x - e_n)\$, where \$e_i\$ is the \$i\$th eigenvalue of `r`.

Examples

``````julia> poly([1, 2, 3])   # The polynomial (x - 1)(x - 2)(x - 3)
Poly(-6 + 11⋅x - 6⋅x^2 + x^3)

julia> poly([1 2; 3 4])  # The polynomial (x - 5.37228)(x + 0.37228)
Poly(-1.9999999999999998 - 5.0⋅x + 1.0⋅x^2)``````
source
``degree(p::Poly)``

Return the degree of the polynomial `p`, i.e. the highest exponent in the polynomial that has a nonzero coefficient. The degree of the zero polynomial is defined to be -1.

source
``coeffs(p::Poly)``

Return the coefficient vector `[a_0, a_1, ..., a_n]` of a polynomial `p`.

source
``````variable(p::Poly)
variable([T::Type,] var)
variable()``````

Return the indeterminate of a polynomial, i.e. its variable, as a `Poly` object. When passed no arguments, this is equivalent to `variable(Float64, :x)`.

Examples

``````julia> variable(Poly([1, 2], :x))
Poly(x)

julia> variable(:y)
Poly(1.0⋅y)

julia> variable()
Poly(1.0⋅x)

julia> variable(Float32, :x)
Poly(1.0f0⋅x)``````
source
``printpoly(io::IO, p::Poly, mimetype = MIME"text/plain"(); descending_powers=false)``

Print a human-readable representation of the polynomial `p` to `io`. The MIME types "text/plain" (default), "text/latex", and "text/html" are supported. By default, the terms are in order of ascending powers, matching the order in `coeffs(p)`; specifying `descending_powers=true` reverses the order.

Examples

``````julia> printpoly(stdout, Poly([1,2,3], :y))
1 + 2*y + 3*y^2
julia> printpoly(stdout, Poly([1,2,3], :y), descending_powers=true)
3*y^2 + 2*y + 1``````
source
``polyval(p::Poly, x::Number)``

Evaluate the polynomial `p` at `x` using Horner's method. `Poly` objects are callable, using this function.

Examples

``````julia> p = Poly([1, 0, -1])
Poly(1 - x^2)

julia> polyval(p, 1)
0

julia> p(1)
0``````
source
``polyint(p::Poly, k::Number=0)``

Integrate the polynomial `p` term by term, optionally adding a constant term `k`. The order of the resulting polynomial is one higher than the order of `p`.

Examples

``````julia> polyint(Poly([1, 0, -1]))
Poly(1.0⋅x - 0.3333333333333333⋅x^3)

julia> polyint(Poly([1, 0, -1]), 2)
Poly(2.0 + 1.0⋅x - 0.3333333333333333⋅x^3)``````
source
``polyint(p::Poly, a::Number, b::Number)``

Compute the definite integral of the polynomial `p` over the interval `[a,b]`.

Examples

``````julia> polyint(Poly([1, 0, -1]), 0, 1)
0.6666666666666667``````
source
``polyder(p::Poly, k=1)``

Compute the `k`th derivative of the polynomial `p`.

Examples

``````julia> polyder(Poly([1, 3, -1]))
Poly(3 - 2⋅x)

julia> polyder(Poly([1, 3, -1]), 2)
Poly(-2)``````
source
``polyfit(x, y, n=length(x)-1, sym=:x)``

Fit a polynomial of degree `n` through the points specified by `x` and `y`, where `n <= length(x) - 1`, using least squares fit. When `n=length(x)-1` (the default), the interpolating polynomial is returned. The optional fourth argument can be used to specify the symbol for the returned polynomial.

Examples

``````julia> xs = linspace(0, pi, 5);

julia> ys = map(sin, xs);

julia> polyfit(xs, ys, 2)
Poly(-0.004902082150108854 + 1.242031920509868⋅x - 0.39535103925413095⋅x^2)``````
source
``roots(p::Poly)``

Return the roots (zeros) of `p`, with multiplicity. The number of roots returned is equal to the order of `p`. The returned roots may be real or complex.

Examples

``````julia> roots(Poly([1, 0, -1]))
2-element Array{Float64,1}:
-1.0
1.0

julia> roots(Poly([1, 0, 1]))
2-element Array{Complex{Float64},1}:
0.0+1.0im
0.0-1.0im

julia> roots(Poly([0, 0, 1]))
2-element Array{Float64,1}:
0.0
0.0

julia> roots(poly([1,2,3,4]))
4-element Array{Float64,1}:
4.0
3.0
2.0
1.0``````
source