Skip to content

Commit

Permalink
Fix substitute promotion
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jun 22, 2023
1 parent f9d736c commit ea719f6
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/substitution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,37 @@ MP.substitute(st::MP.AbstractSubstitutionType, v::Variable, s::Substitution) = v
# 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.
MP.substitute(st::MP.AbstractSubstitutionType, v::Variable{Name}, s::Substitution{Name}, ::MP.AbstractSubstitution...) where {Name} = s.second

_remove_variable(t::Tuple{}, ::Type) = t
_remove_variable(::Tuple{}, ::Type) = nothing
function _remove_variable(t::Tuple{V,Vararg{Variable,N}}, ::Type{V}) where {V,N}
Base.tail(t)
end
function _remove_variable(t::Tuple{V,Vararg{Variable,N}}, ::Type{W}) where {V,W,N}
tuple(first(t), _remove_variable(Base.tail(t), W)...)
tail = _remove_variable(Base.tail(t), W)
if isnothing(tail)
return
else
return tuple(first(t), tail...)
end
end

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

function MA.promote_operation(::typeof(MP.substitute), ::Type{MP.Subs}, ::Type{Monomial{V,N}}, ::Type{Pair{Variable{Name},T}}) where {V,N,Name,T}
function _promote_subs(::Type{Monomial{V,N}}, ::Type{Pair{Variable{Name},T}}) where {V,N,Name,T}
U = MA.promote_operation(*, T, T)
VV = _remove_variable(V, Variable{Name})
return _mult_monomial_type(U, VV)
if isnothing(VV)
# Variable not present
return Monomial{V,N}
else
return _mult_monomial_type(U, VV)
end
end

function MA.promote_operation(::typeof(MP.substitute), ::Type{MP.Subs}, ::Type{Monomial{V,N}}, ::Type{Pair{Variable{Name},T}}) where {V,N,Name,T}
return _promote_subs(Monomial{V,N}, Pair{Variable{Name},T})
end

function MA.promote_operation(::typeof(MP.substitute), ::Type{MP.Eval}, ::Type{Monomial{V,N}}, ::Type{Pair{Variable{Name},T}}) where {V,N,Name,T}
return _promote_subs(Monomial{V,N}, Pair{Variable{Name},T})
end

2 comments on commit ea719f6

@blegat
Copy link
Member Author

@blegat blegat commented on ea719f6 Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/86097

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.0 -m "<description of version>" ea719f69ab7acf568c7e9e1990c54b0cedcd3c54
git push origin v0.4.0

Please sign in to comment.