Skip to content

Commit ea719f6

Browse files
committed
Fix substitute promotion
1 parent f9d736c commit ea719f6

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/substitution.jl

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,37 @@ MP.substitute(st::MP.AbstractSubstitutionType, v::Variable, s::Substitution) = v
66
# subs(x, x=>y, y=>1) should be y, not 1 so as soon as we see the right variable, we stop, in subs(x, x=>y, x=>1), "x=>1" is ignored.
77
MP.substitute(st::MP.AbstractSubstitutionType, v::Variable{Name}, s::Substitution{Name}, ::MP.AbstractSubstitution...) where {Name} = s.second
88

9-
_remove_variable(t::Tuple{}, ::Type) = t
9+
_remove_variable(::Tuple{}, ::Type) = nothing
1010
function _remove_variable(t::Tuple{V,Vararg{Variable,N}}, ::Type{V}) where {V,N}
1111
Base.tail(t)
1212
end
1313
function _remove_variable(t::Tuple{V,Vararg{Variable,N}}, ::Type{W}) where {V,W,N}
14-
tuple(first(t), _remove_variable(Base.tail(t), W)...)
14+
tail = _remove_variable(Base.tail(t), W)
15+
if isnothing(tail)
16+
return
17+
else
18+
return tuple(first(t), tail...)
19+
end
1520
end
1621

1722
_mult_monomial_type(::Type{U}, ::Tuple{}) where {U} = U
1823
_mult_monomial_type(::Type{U}, V::Tuple) where {U} = MA.promote_operation(*, U, Monomial{V,length(V)})
1924

20-
function MA.promote_operation(::typeof(MP.substitute), ::Type{MP.Subs}, ::Type{Monomial{V,N}}, ::Type{Pair{Variable{Name},T}}) where {V,N,Name,T}
25+
function _promote_subs(::Type{Monomial{V,N}}, ::Type{Pair{Variable{Name},T}}) where {V,N,Name,T}
2126
U = MA.promote_operation(*, T, T)
2227
VV = _remove_variable(V, Variable{Name})
23-
return _mult_monomial_type(U, VV)
28+
if isnothing(VV)
29+
# Variable not present
30+
return Monomial{V,N}
31+
else
32+
return _mult_monomial_type(U, VV)
33+
end
34+
end
35+
36+
function MA.promote_operation(::typeof(MP.substitute), ::Type{MP.Subs}, ::Type{Monomial{V,N}}, ::Type{Pair{Variable{Name},T}}) where {V,N,Name,T}
37+
return _promote_subs(Monomial{V,N}, Pair{Variable{Name},T})
38+
end
39+
40+
function MA.promote_operation(::typeof(MP.substitute), ::Type{MP.Eval}, ::Type{Monomial{V,N}}, ::Type{Pair{Variable{Name},T}}) where {V,N,Name,T}
41+
return _promote_subs(Monomial{V,N}, Pair{Variable{Name},T})
2442
end

0 commit comments

Comments
 (0)