Skip to content

Commit

Permalink
Fix with_variables for TypedPolynomials (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat authored Mar 7, 2022
1 parent 37edbcb commit 5747286
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
29 changes: 21 additions & 8 deletions src/Certificate/Certificate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,32 @@ function MP.monomials(v::WithVariables)
return MP.monomials(v.inner)
end


_cat(a::Vector, b::Vector) = vcat(a, b)
_cat(a::Vector, ::Tuple{}) = copy(a)
_cat(a::Tuple, b::Tuple) = [a..., b...]
_cat(a::Tuple, ::Tuple{}) = [a...]
_merge_sorted(a::Vector, ::Tuple{}) = a
function _merge_sorted(a::Vector, b::Vector)
vars = sort!(vcat(a, b), rev = true)
unique!(vars)
return vars
end
_merge_sorted(a::Tuple{}, ::Tuple{}) = a
_merge_sorted(a::Tuple, ::Tuple{}) = a
_merge_sorted(::Tuple{}, b::Tuple) = b
function _merge_sorted(a::Tuple, b::Tuple)
v = first(a)
w = first(b)
if v == w
return (v, _merge_sorted(Base.tail(a), Base.tail(b))...)
elseif v > w
return (v, _merge_sorted(Base.tail(a), b)...)
else
return (w, _merge_sorted(a, Base.tail(b))...)
end
end

_vars(::SemialgebraicSets.FullSpace) = tuple()
_vars(x) = MP.variables(x)

function with_variables(inner, outer)
vars = sort!(_cat(_vars(inner), _vars(outer)), rev = true)
unique!(vars)
return WithVariables(inner, vars)
return WithVariables(inner, _merge_sorted(_vars(inner), _vars(outer)))
end

function preprocessed_domain(::Putinar, domain::BasicSemialgebraicSet, p)
Expand Down
9 changes: 8 additions & 1 deletion test/certificate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ const MP = MultivariatePolynomials
import MultivariateBases
const MB = MultivariateBases

@testset "_merge_sorted" begin
@test SumOfSquares.Certificate._merge_sorted([4, 1], [3, 0]) == [4, 3, 1, 0]
@test SumOfSquares.Certificate._merge_sorted((4, 1), (3, 0)) == (4, 3, 1, 0)
@test SumOfSquares.Certificate._merge_sorted([4, 1], [3, 2]) == [4, 3, 2, 1]
@test SumOfSquares.Certificate._merge_sorted((4, 1), (3, 2)) == (4, 3, 2, 1)
end

@testset "with_variables" begin
@polyvar x y z
p = x + z
Expand All @@ -17,7 +24,7 @@ const MB = MultivariateBases
v = SumOfSquares.Certificate.with_variables(q, FullSpace())
@test v.inner === q
@test MP.variables(v.inner) == [a, b, a]
@test MP.variables(v) == [a, b]
@test MP.variables(v) == [a, b, a]
end

@testset "Monomial selection for certificate" begin
Expand Down

2 comments on commit 5747286

@blegat
Copy link
Member Author

@blegat blegat commented on 5747286 Mar 7, 2022

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/56097

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.6.0 -m "<description of version>" 574728697e8ad90c9cc1fe566e0032fc6e3ea554
git push origin v0.6.0

Please sign in to comment.