Subtitution
Given a polynomial, say $p(x, y) = 3x^2y + x + 2y + 1$, one can evaluate it at a given point, e.g. $p(2, 1) = 12 + 2 + 2 + 1 = 17$ or substitute one or more variable by a value or polynomial, e.g. $p(x, xy^2 + 1) = 3x^2(xy^2+1) + x + 2(xy^2+1) + 1 = 3x^3y^2 + 2xy^2 + 3x^2 + x + 3$. We distinguish the two operation as follows
- We call an evaluation an operation where every variable should be replace by a new value or polynomial, the syntax is
p(x => 2, y => 1)
. - We call a subsitution an operation where some (or all variables) are subtituted into a new value or polynomial, the syntax is
subs(p, y => x*y^2 + 1)
.
The distinction is important for type stability for some implementations (it is important for DynamicPolynomials but not for TypedPolynomials). Indeed consider a polynomial with Int
coefficients for which we ask to replace some variables with Int
values. If all the variables are replaced with Int
s, the return type should be Int
. However, if some variables only are replaced by Int
then the return type should be a polynomial with Int
coefficients.
MultivariatePolynomials.subs
— Functionsubs(p, s::AbstractSubstitution...)
Apply the substitutions s
to p
. Use p(s...)
if we are sure that all the variables are substited in s
.
The allowed substutions are:
v => p
wherev
is a variable andp
a polynomial, e.g.x => 1
orx => x^2*y + x + y
.V => P
whereV
is a tuple or vector of variables andP
a tuple or vector of polynomials, e.g.(x, y) => (y, x)
or(y, x) => (2, 1)
.
The order of the variables is lexicographic with the name with TypedPolynomials and by order of creation with DynamicPolynomials. Since there is no guarantee on the order of the variables, substitution directly with a tuple or a vector is not allowed. You can use p(variables(p) => (1, 2))
instead if you are sure of the order of the variables (e.g. the name order matches the creation order).
Examples
p = 3x^2*y + x + 2y + 1
p(x => 2, y => 1) # Return type is Int
subs(p, x => 2, y => 1) # Return type is Int in TypedPolynomials but is a polynomial of Int coefficients in DynamicPolynomials
subs(p, y => x*y^2 + 1)
p(y => 2) # Do not do that, this works fine with TypedPolynomials but it will not return a correct result with DynamicPolynomials since it thinks that the return type is `Int`.
MultivariatePolynomials.substitute
— Functionsubs(polynomial, (x, y)=>(1, 2))
is equivalent to:
subs(polynomial, (x=>1, y=>2))