Types

Types

Variables

AbstractVariable <: AbstractMonomialLike

Abstract type for a variable.

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.

name(v::AbstractVariable)::AbstractString

Returns the name of a variable.

name_base_indices(v::AbstractVariable)

Returns the name of the variable (as a String or Symbol) and its indices as a Vector{Int} or tuple of Ints.

variable_union_type(p::AbstractPolynomialLike)

Return the supertype for variables of p. If p is a variable, it should not be the type of p but the supertype of all variables that could be created.

Examples

For TypedPolynomials, a variable of name x has type Variable{:x} so variable_union_type should return Variable. For DynamicPolynomials, all variables have the same type PolyVar{C} where C is true for commutative variables and false for non-commutative ones so variable_union_type should return PolyVar{C}.

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}.

@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.

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.

AbstractMonomial <: AbstractMonomialLike

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

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.

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.

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.

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).

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.

isconstant(t::AbstractTermLike)

Returns whether the monomial of t is constant.

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))`.

constantmonomial(p::AbstractPolynomialLike)

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

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

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

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).

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.

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.

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}.

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.

coefficient(t::AbstractTermLike)

Returns the coefficient of the term t.

coefficient(p::AbstractPolynomialLike, m::AbstractMonomialLike)

Returns the coefficient of the monomial m in p.

Examples

Calling coefficient on $4x^2y$ should return $4$. Calling coefficient(2x + 4y^2 + 3, y^2) should return $4$. Calling coefficient(2x + 4y^2 + 3, x^2) should return $0$.

coefficient(p::AbstractPolynomialLike, m::AbstractMonomialLike, vars)::AbstractPolynomialLike

Returns the coefficient of the monomial m of the polynomial p considered as a polynomial in variables vars.

Example

Calling coefficient((a+b)x^2+2x+y*x^2, x^2, [x,y]) should return a+b. Calling coefficient((a+b)x^2+2x+y*x^2, x^2, [x]) should return a+b+y.

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.

monomial(t::AbstractTermLike)

Returns the monomial of the term t.

Examples

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

constantterm(α, p::AbstractPolynomialLike)

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

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

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

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

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

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

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

Polynomials

AbstractPolynomialLike
AbstractPolynomial
polynomial
polynomialtype
terms
nterms
coefficients
coefficient(p::AbstractPolynomialLike, vars, m::AbstractMonomialLike)
monomials
mindegree
maxdegree
extdegree
leadingterm
leadingcoefficient
leadingmonomial
removeleadingterm
removemonomials
monic
mapcoefficientsnz

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]$.

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

Returns the return type of monovec.

emptymonovec(p::AbstractPolynomialLike)

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

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])$.

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]$.