Skip to content

Commit 8cd3710

Browse files
committed
Refactor substitution
1 parent 3bab383 commit 8cd3710

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/substitution.jl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const AbstractMultiSubstitution = Union{
2727
}
2828
const AbstractSubstitution = Union{Substitution,AbstractMultiSubstitution}
2929
const Substitutions = Tuple{Vararg{AbstractSubstitution}}
30+
const _Substitutions = Tuple{Vararg{Substitution}}
3031

3132
abstract type AbstractSubstitutionType end
3233
struct Subs <: AbstractSubstitutionType end
@@ -40,12 +41,20 @@ is equivalent to:
4041
4142
subs(polynomial, (x=>1, y=>2))
4243
"""
43-
function substitute(st::_AST, p::_APL, s::AbstractMultiSubstitution)
44-
return substitute(st, p, _flatten_subs(s))
44+
function substitute_fallback(st::_AST, p::_APL, s::Substitutions)
45+
return substitute_fallback(st, p, _flatten_subs(s...))
46+
end
47+
48+
function substitute(st::_AST, p::_APL, s::AbstractSubstitution...)
49+
return substitute(st, p, s)
50+
end
51+
52+
function substitute(st::_AST, p::_APL, s::Substitutions)
53+
return substitute_fallback(st, p, s)
4554
end
4655

4756
## Variables
48-
function substitute(st::_AST, v::AbstractVariable, s::Substitutions)
57+
function substitute_fallback(st::_AST, v::AbstractVariable, s::Substitutions)
4958
return substitute(st, v, s...)
5059
end
5160

@@ -127,7 +136,7 @@ function power_promote(
127136
)
128137
end
129138

130-
function substitute(st::_AST, m::AbstractMonomial, s::Substitutions)
139+
function substitute_fallback(st::_AST, m::AbstractMonomial, s::Substitutions)
131140
if isconstant(m)
132141
return one(power_promote(typeof(st), variables(m), s))
133142
else
@@ -136,7 +145,7 @@ function substitute(st::_AST, m::AbstractMonomial, s::Substitutions)
136145
end
137146

138147
## Terms
139-
function substitute(st::_AST, t::AbstractTerm, s::Substitutions)
148+
function substitute_fallback(st::_AST, t::AbstractTerm, s::Substitutions)
140149
return coefficient(t) * substitute(st, monomial(t), s)
141150
end
142151

@@ -163,7 +172,7 @@ end
163172
## Polynomials
164173
_polynomial(α) = α
165174
_polynomial(p::_APL) = polynomial(p)
166-
function substitute(st::_AST, p::AbstractPolynomial, s::Substitutions)
175+
function substitute_fallback(st::_AST, p::AbstractPolynomial, s::Substitutions)
167176
if iszero(p)
168177
_polynomial(substitute(st, zero_term(p), s))
169178
else
@@ -189,7 +198,7 @@ function MA.promote_operation(
189198
end
190199

191200
## Fallbacks
192-
function substitute(st::_AST, p::_APL, s::Substitutions)
201+
function substitute_fallback(st::_AST, p::_APL, s::Substitutions)
193202
return substitute(st, polynomial(p), s)
194203
end
195204
function substitute(st::_AST, q::RationalPoly, s::Substitutions)

test/substitution.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import Test: @inferred
33

44
import MutableArithmetics as MA
55

6-
@testset "Substitution" begin
6+
Mod = DynamicPolynomials
7+
8+
#@testset "Substitution" begin
79
Mod.@polyvar x[1:3]
810

911
@test subs(2, x[1] => 3) == 2
@@ -107,4 +109,17 @@ import MutableArithmetics as MA
107109
@test subs(F, x[3] => T(0)) == x[1] * x[2]^2
108110
@test subs(F, x[3] => 0) == x[1] * x[2]^2
109111
end
110-
end
112+
113+
function subs_alloc(x)
114+
p = sum(x)
115+
v = map(_ -> 1, x)
116+
@inferred subs(p, x => v)
117+
@time subs(p, x => v)
118+
@time subs(p, x => v)
119+
@time substitute(Eval(), p, x => v)
120+
@time substitute(Eval(), p, x => v)
121+
@time substitute(Eval(), p, v)
122+
@time p(v)
123+
end
124+
subs_alloc(x)
125+
#end

0 commit comments

Comments
 (0)