Types

Types

Variables

AbstractVariable <: AbstractMonomialLike

Abstract type for a variable.

source
variable(p::AbstractPolynomialLike)

Converts p to a variable. Throws an error if it is not possible.

Examples

Calling variable(x^2 + x - x^2) should return the variable x and calling variable(1.0y) should return the variable y however calling variable(2x) or variable(x + y) should throw an error.

Note

This operation is not type stable for the TypedPolynomials implementation if nvariables(p) > 1 but is type stable for DynamicPolynomials.

source
name(v::AbstractVariable)::AbstractString

Returns the name of a variable.

source
similarvariable(p::AbstractPolynomialLike, variable::Type{Val{V}})

Creates a new variable V based upon the the given source polynomial.

similarvariable(p::AbstractPolynomialLike, v::Symbol)

Creates a new variable based upon the given source polynomial and the given symbol v. Note that this can lead to type instabilities.

Examples

Calling similarvariable(typedpoly, Val{:x}) on a polynomial created with TypedPolynomials results in TypedPolynomials.Variable{:x}.

source
@similarvariable(p::AbstractPolynomialLike, variable)

Calls similarvariable(p, Val{variable}) and binds the result to a variable with the same name.

Examples

Calling @similarvariable typedpoly x on a polynomial created with TypedPolynomials binds TypedPolynomials.Variable{:x} to the variable x.

source

Monomials

AbstractMonomialLike

Abstract type for a value that can act like a monomial. For instance, an AbstractVariable is an AbstractMonomialLike since it can act as a monomial of one variable with degree 1.

source
AbstractMonomial <: AbstractMonomialLike

Abstract type for a monomial, i.e. a product of variables elevated to a nonnegative integer power.

source
monomialtype(p::AbstractPolynomialLike)

Return the type of the monomials of p.

termtype(::Type{PT}) where PT<:AbstractPolynomialLike

Returns the type of the monomials of a polynomial of type PT.

source
variables(p::AbstractPolynomialLike)

Returns the tuple of the variables of p in decreasing order. It could contain variables of zero degree, see the example section.

Examples

Calling variables(x^2*y) should return (x, y) and calling variables(x) should return (x,). Note that the variables of m does not necessarily have nonzero degree. For instance, variables([x^2*y, y*z][1]) is usually (x, y, z) since the two monomials have been promoted to a common type.

source
nvariables(p::AbstractPolynomialLike)

Returns the number of variables in p, i.e. length(variables(p)). It could be more than the number of variables with nonzero degree (see the Examples section of variables).

Examples

Calling nvariables(x^2*y) should return at least 2 and calling nvariables(x) should return at least 1.

source
exponents(t::AbstractTermLike)

Returns the exponent of the variables in the monomial of the term t.

Examples

Calling exponents(x^2*y) should return (2, 1).

source
degree(t::AbstractTermLike)

Returns the total degree of the monomial of the term t, i.e. sum(exponents(t)).

degree(t::AbstractTermLike, v::AbstractVariable)

Returns the exponent of the variable v in the monomial of the term t.

Examples

Calling degree(x^2*y) should return 3 which is $2 + 1$. Calling degree(x^2*y, x) should return 2 and calling degree(x^2*y, y) should return 1.

source
isconstant(t::AbstractTermLike)

Returns whether the monomial of t is constant.

source
powers(t::AbstractTermLike)

Returns an tuple of the powers of the monomial of t.

Examples

Calling powers(3x^4*y) should return((x, 4), (y, 1))`.

source
constantmonomial(p::AbstractPolynomialType)

Returns a constant monomial of the monomial type of p with the same variables as p.

constantmonomial(::Type{PT}) where {PT<:AbstractPolynomialType}

Returns a constant monomial of the monomial type of a polynomial of type PT.

source
mapexponents(f, m1::AbstractMonomialLike, m2::AbstractMonomialLike)

If $m_1 = \prod x^{\alpha_i}$ and $m_2 = \prod x^{\beta_i}$ then it returns the monomial $m = \prod x^{f(\alpha_i, \beta_i)}$.

Examples

The multiplication m1 * m2 is equivalent to mapexponents(+, m1, m2), the unsafe division _div(m1, m2) is equivalent to mapexponents(-, m1, m2), gcd(m1, m2) is equivalent to mapexponents(min, m1, m2), lcm(m1, m2) is equivalent to mapexponents(max, m1, m2).

source

Terms

AbstractTermLike{T}

Abstract type for a value that can act like a term. For instance, an AbstractMonomial is an AbstractTermLike{Int} since it can act as a term with coefficient 1.

source
AbstractTerm{T} <: AbstractTerm{T}

Abstract type for a term of coefficient type T, i.e. the product between a value of type T and a monomial.

source
term(p::AbstractPolynomialLike)

Converts the polynomial p to a term. When applied on a polynomial, it throws an error if it has more than one term. When applied to a term, it is the identity and does not copy it. When applied to a monomial, it create a term of type AbstractTerm{Int}.

source
termtype(p::AbstractPolynomialLike)

Returns the type of the terms of p.

termtype(::Type{PT}) where PT<:AbstractPolynomialLike

Returns the type of the terms of a polynomial of type PT.

termtype(p::AbstractPolynomialLike, ::Type{T}) where T

Returns the type of the terms of p but with coefficient type T.

termtype(::Type{PT}, ::Type{T}) where {PT<:AbstractPolynomialLike, T}

Returns the type of the terms of a polynomial of type PT but with coefficient type T.

source
coefficient(t::AbstractTermLike)

Returns the coefficient of the term t.

Examples

Calling coefficient on $4x^2y$ should return $4$.

source
coefficient(p::AbstractPolynomialLike)

Returns the coefficient type of p.

coefficient(::Type{PT}) where PT

Returns the coefficient type of a polynomial of type PT.

Examples

Calling coefficienttype on $(4//5)x^2y$ should return Rational{Int}, calling coefficienttype on $1.0x^2y + 2.0x$ should return Float64 and calling coefficienttype on $xy$ should return Int.

source
monomial(t::AbstractTermLike)

Returns the monomial of the term t.

Examples

Calling monomial on $4x^2y$ should return $x^2y$.

source
constantterm(α, p::AbstractPolynomialLike)

Creates a constant term with coefficient α and the same variables as p.

constantterm(α, ::Type{PT} where {PT<:AbstractPolynomialType}

Creates a constant term of the term type of a polynomial of type PT.

source
zeroterm(p::AbstractPolynomialLike{T}) where T

Equivalent to constantterm(zero(T), p).

zeroterm(α, ::Type{PT} where {T, PT<:AbstractPolynomialType{T}}

Equivalent to constantterm(zero(T), PT).

source

Polynomials

AbstractPolynomialLike{T}

Abstract type for a value that can act like a polynomial. For instance, an AbstractTerm{T} is an AbstractPolynomialType{T} since it can act as a polynomial of only one term.

source
AbstractPolynomial{T} <: AbstractPolynomialLike{T}

Abstract type for a polynomial of coefficient type T, i.e. a sum of AbstractTerm{T}s.

source
polynomial(p::AbstractPolynomialLike)

Converts p to a value with polynomial type.

polynomial(p::AbstractPolynomialLike, ::Type{T}) where T

Converts p to a value with polynomial type with coefficient type T.

polynomial(a::AbstractVector, mv::AbstractVector{<:AbstractMonomialLike})

Creates a polynomial equal to dot(a, mv).

polynomial(terms::AbstractVector{<:AbstractTerm}, s::ListState=MessyState())

Creates a polynomial equal to sum(terms) where terms are guaranteed to be in state s.

polynomial(f::Function, mv::AbstractVector{<:AbstractMonomialLike})

Creates a polynomial equal to sum(f(i) * mv[i] for i in 1:length(mv)).

Examples

Calling polynomial([2, 4, 1], [x, x^2*y, x*y]) should return $4x^2y + xy + 2x$.

source
polynomialtype(p::AbstractPolynomialLike)

Returns the type that p would have if it was converted into a polynomial.

polynomialtype(::Type{PT}) where PT<:AbstractPolynomialLike

Returns the same as polynomialtype(::PT).

polynomialtype(p::AbstractPolynomialLike, ::Type{T}) where T

Returns the type that p would have if it was converted into a polynomial of coefficient type T.

termtype(::Type{PT}, ::Type{T}) where {PT<:AbstractPolynomialLike, T}

Returns the same as polynomialtype(::PT, ::Type{T}).

source
terms(p::AbstractPolynomialLike)

Returns an iterator over the nonzero terms of the polynomial p sorted in the decreasing monomial order.

Examples

Calling terms on $4x^2y + xy + 2x$ should return an iterator of $[4x^2y, xy, 2x]$.

source
nterms(p::AbstractPolynomialLike)

Returns the number of nonzero terms in p, i.e. length(terms(p)).

Examples

Calling nterms on $4x^2y + xy + 2x$ should return 3.

source
coefficients(p::AbstractPolynomialLike)

Returns an iterator over the coefficients of p of the nonzero terms of the polynomial sorted in the decreasing monomial order.

coefficients(p::AbstractPolynomialLike, X::AbstractVector)

Returns an iterator over the coefficients of the monomials of X in p where X is a monomial vector not necessarily sorted but with no duplicate entry.

Examples

Calling coefficients on $4x^2y + xy + 2x$ should return an iterator of $[4, 1, 2]$. Calling coefficients(4x^2*y + x*y + 2x + 3, [x, 1, x*y, y]) should return an iterator of $[2, 3, 1, 0]$.

source
monomials(p::AbstractPolynomialLike)

Returns an iterator over the monomials of p of the nonzero terms of the polynomial sorted in the decreasing order.

monomials(vars::Tuple, degs::AbstractVector{Int}, filter::Function = m -> true)

Builds the vector of all the monovec m with variables vars such that the degree degree(m) is in degs and filter(m) is true.

Examples

Calling monomials on $4x^2y + xy + 2x$ should return an iterator of $[x^2y, xy, x]$.

Calling monomials((x, y), [1, 3], m -> degree(m, y) != 1) should return [x^3, x*y^2, y^3, x] where x^2*y and y have been excluded by the filter.

source
mindegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractTermLike}})

Returns the minimal total degree of the monomials of p, i.e. minimum(degree, terms(p)).

mindegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractTermLike}}, v::AbstractVariable)

Returns the minimal degree of the monomials of p in the variable v, i.e. minimum(degree.(terms(p), v)).

Examples

Calling mindegree on on $4x^2y + xy + 2x$ should return 1, mindegree(4x^2y + xy + 2x, x) should return 1 and mindegree(4x^2y + xy + 2x, y) should return 0.

source
maxdegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractTermLike}})

Returns the maximal total degree of the monomials of p, i.e. maximum(degree, terms(p)).

maxdegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractTermLike}}, v::AbstractVariable)

Returns the maximal degree of the monomials of p in the variable v, i.e. maximum(degree.(terms(p), v)).

Examples

Calling maxdegree on on $4x^2y + xy + 2x$ should return 3, maxdegree(4x^2y + xy + 2x, x) should return 2 and maxdegree(4x^2y + xy + 2x, y) should return 1.

source
extdegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractTermLike}})

Returns the extremal total degrees of the monomials of p, i.e. (mindegree(p), maxdegree(p)).

extdegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractTermLike}}, v::AbstractVariable)

Returns the extremal degrees of the monomials of p in the variable v, i.e. (mindegree(p, v), maxdegree(p, v)).

Examples

Calling extdegree on on $4x^2y + xy + 2x$ should return (1, 3), extdegree(4x^2y + xy + 2x, x) should return (1, 2) and maxdegree(4x^2y + xy + 2x, y) should return (0, 1).

source
leadingterm(p::AbstractPolynomialLike)

Returns the coefficient of the leading term, i.e. first(terms(p)).

Examples

Calling leadingterm on $4x^2y + xy + 2x$ should return $4x^2y$.

source
leadingcoefficient(p::AbstractPolynomialLike)

Returns the coefficient of the leading term of p, i.e. coefficient(leadingterm(p)).

Examples

Calling leadingcoefficient on $4x^2y + xy + 2x$ should return $4$ and calling it on $0$ should return $0$.

source
leadingmonomial(p::AbstractPolynomialLike)

Returns the monomial of the leading term of p, i.e. monomial(leadingterm(p)) or first(monomials(p)).

Examples

Calling leadingmonomial on $4x^2y + xy + 2x$ should return $x^2y$.

source
removeleadingterm(p::AbstractPolynomialLike)

Returns a polynomial with the leading term removed in the polynomial p.

Examples

Calling removeleadingterm on $4x^2y + xy + 2x$ should return $xy + 2x$.

source

Returns a polynomial with the terms having their monomial in the monomial vector mv removed in the polynomial p.

Examples

Calling removemonomials(4x^2*y + x*y + 2x, [x*y]) should return $4x^2*y + 2x$.

source
monic(p::AbstractPolynomialLike)

Returns p / leadingcoefficient(p) where the leading coefficient of the returned polynomials is made sure to be exactly one to avoid rounding error.

source

Rational Polynomial Function

A rational polynomial function can be constructed with the / operator. Common operations such as +, -, *, - have been implemented between rational functions. The numerator and denominator polynomials can be retrieved by the numerator and denominator functions.

Monomial Vectors

monovec(X::AbstractVector{MT}) where {MT<:AbstractMonomialLike}

Returns the vector of monomials X in decreasing order and without any duplicates.

Examples

Calling monovec on $[xy, x, xy, x^2y, x]$ should return $[x^2y, xy, x]$.

source
monovectype(X::AbstractVector{MT}) where {MT<:AbstractMonomialLike}

Returns the return type of monovec.

source
emptymonovec(p::AbstractPolynomialLike)

Returns an empty collection of the type of monomials(p).

source
sortmonovec(X::AbstractVector{MT}) where {MT<:AbstractMonomialLike}

Returns σ, the orders in which one must take the monomials in X to make them sorted and without any duplicate and the sorted vector of monomials, i.e. it returns (σ, X[σ]).

Examples

Calling sortmonovec on $[xy, x, xy, x^2y, x]$ should return $([4, 1, 2], [x^2y, xy, x])$.

source
mergemonovec{MT<:AbstractMonomialLike, MVT<:AbstractVector{MT}}(X::AbstractVector{MVT}}

Returns the vector of monomials in the entries of X in decreasing order and without any duplicates, i.e. monovec(vcat(X...))

Examples

Calling mergemonovec on $[[xy, x, xy], [x^2y, x]]$ should return $[x^2y, xy, x]$.

source