Skip to content

Commit

Permalink
Drop variables not in any substitution (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat authored Oct 2, 2021
1 parent c628e93 commit 3dfb15a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/subs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function _subs(::MP.Eval, p::Polynomial{C, T}, vals::AbstractVector{S}) where {C
end
function _subs(::MP.Subs, p::Polynomial{C, T}, vals::AbstractVector{S}) where {C, T, S}
Tout = MA.promote_operation(*, T, MP.coefficienttype(S))
q = zero_with_variables(Polynomial{C, Tout}, variables(p))
q = zero_with_variables(Polynomial{C, Tout}, mergevars_of(PolyVar{C}, vals)[1])
for i in 1:length(p.a)
MA.mutable_operate!(+, q, p.a[i] * monoeval(p.x.Z[i], vals))
end
Expand Down
5 changes: 5 additions & 0 deletions src/var.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,8 @@ function mergevars(varsvec::Vector{Vector{PV}}) where {PV<:PolyVar}
maps = mergevars_to!(vars, varsvec)
return vars, maps
end
function mergevars_of(::Type{PolyVar{C}}, polys::AbstractVector) where {C}
varsvec = Vector{PolyVar{C}}[variables(p) for p in polys if p isa PolyType]
# TODO avoid computing `maps`
return mergevars(varsvec)
end
6 changes: 5 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ end
@polyvar x[1:2]
p1 = x[1] * 0.0 + x[2] * 0
p2 = ( x[1] + x[2] ) * 0.0
@test variables(p1) == x
@test variables(p1) == x
@test variables(p1) == variables(p2)

@polyvar χ[1:4]
Expand All @@ -33,6 +33,10 @@ end
# This is for Issue #92
p3 = subs(p1, x => x)
@test variables(p3) == x

p4 = x[1] + x[2]
@test variables(subs(p4, x[2] => 1)) == [x[1]]
@test variables(subs(p4, x[1] => 1)) == [x[2]]
end

include("mono.jl")
Expand Down

0 comments on commit 3dfb15a

Please sign in to comment.