Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/substitution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const AbstractMultiSubstitution = Union{
}
const AbstractSubstitution = Union{Substitution,AbstractMultiSubstitution}
const Substitutions = Tuple{Vararg{AbstractSubstitution}}
const _Substitutions = Tuple{Vararg{Substitution}}

abstract type AbstractSubstitutionType end
struct Subs <: AbstractSubstitutionType end
Expand All @@ -40,12 +41,20 @@ is equivalent to:

subs(polynomial, (x=>1, y=>2))
"""
function substitute(st::_AST, p::_APL, s::AbstractMultiSubstitution)
return substitute(st, p, _flatten_subs(s))
function substitute_fallback(st::_AST, p::_APL, s::Substitutions)
return substitute_fallback(st, p, _flatten_subs(s...))
end

function substitute(st::_AST, p::_APL, s::AbstractSubstitution...)
return substitute(st, p, s)
end

function substitute(st::_AST, p::_APL, s::Substitutions)
return substitute_fallback(st, p, s)
end

## Variables
function substitute(st::_AST, v::AbstractVariable, s::Substitutions)
function substitute_fallback(st::_AST, v::AbstractVariable, s::Substitutions)
return substitute(st, v, s...)
end

Expand Down Expand Up @@ -127,7 +136,7 @@ function power_promote(
)
end

function substitute(st::_AST, m::AbstractMonomial, s::Substitutions)
function substitute_fallback(st::_AST, m::AbstractMonomial, s::Substitutions)
if isconstant(m)
return one(power_promote(typeof(st), variables(m), s))
else
Expand All @@ -136,7 +145,7 @@ function substitute(st::_AST, m::AbstractMonomial, s::Substitutions)
end

## Terms
function substitute(st::_AST, t::AbstractTerm, s::Substitutions)
function substitute_fallback(st::_AST, t::AbstractTerm, s::Substitutions)
return coefficient(t) * substitute(st, monomial(t), s)
end

Expand All @@ -163,7 +172,7 @@ end
## Polynomials
_polynomial(α) = α
_polynomial(p::_APL) = polynomial(p)
function substitute(st::_AST, p::AbstractPolynomial, s::Substitutions)
function substitute_fallback(st::_AST, p::AbstractPolynomial, s::Substitutions)
if iszero(p)
_polynomial(substitute(st, zero_term(p), s))
else
Expand All @@ -189,7 +198,7 @@ function MA.promote_operation(
end

## Fallbacks
function substitute(st::_AST, p::_APL, s::Substitutions)
function substitute_fallback(st::_AST, p::_APL, s::Substitutions)
return substitute(st, polynomial(p), s)
end
function substitute(st::_AST, q::RationalPoly, s::Substitutions)
Expand Down
19 changes: 17 additions & 2 deletions test/substitution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import Test: @inferred

import MutableArithmetics as MA

@testset "Substitution" begin
Mod = DynamicPolynomials

#@testset "Substitution" begin
Mod.@polyvar x[1:3]

@test subs(2, x[1] => 3) == 2
Expand Down Expand Up @@ -107,4 +109,17 @@ import MutableArithmetics as MA
@test subs(F, x[3] => T(0)) == x[1] * x[2]^2
@test subs(F, x[3] => 0) == x[1] * x[2]^2
end
end

function subs_alloc(x)
p = sum(x)
v = map(_ -> 1, x)
@inferred subs(p, x => v)
@time subs(p, x => v)
@time subs(p, x => v)
@time substitute(Eval(), p, x => v)
@time substitute(Eval(), p, x => v)
@time substitute(Eval(), p, v)
@time p(v)
end
subs_alloc(x)
#end
Loading