From 3dfb15ac19c88aec47f7f035131e4ce7aead570e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sat, 2 Oct 2021 11:36:16 -0400 Subject: [PATCH] Drop variables not in any substitution (#95) --- src/subs.jl | 2 +- src/var.jl | 5 +++++ test/runtests.jl | 6 +++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/subs.jl b/src/subs.jl index 8004192..51c0bb9 100644 --- a/src/subs.jl +++ b/src/subs.jl @@ -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 diff --git a/src/var.jl b/src/var.jl index 947ec14..0f66f8f 100644 --- a/src/var.jl +++ b/src/var.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 47b75dd..25d532b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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] @@ -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")