diff --git a/src/promote.jl b/src/promote.jl index 5473917..a6942e5 100644 --- a/src/promote.jl +++ b/src/promote.jl @@ -1,3 +1,5 @@ +const _PolynomialLike = Union{MP.AbstractPolynomialLike,SA.AlgebraElement} + function SA.promote_bases_with_maps( ::FullSpace, p::Union{MP.AbstractPolynomialLike,AbstractSemialgebraicSet}, @@ -5,6 +7,10 @@ function SA.promote_bases_with_maps( return (FullSpace(), nothing), (p, nothing) end +function SA.promote_bases_with_maps(::FullSpace, p::SA.AlgebraElement) + return (FullSpace(), nothing), (p, nothing) +end + function SA.promote_bases_with_maps( a::AbstractSemialgebraicSet, b::Union{MP.AbstractPolynomialLike,AbstractSemialgebraicSet}, @@ -13,6 +19,22 @@ function SA.promote_bases_with_maps( return SA.maybe_promote(a, _a...), SA.maybe_promote(b, _b...) end +function _promote_ae(b::SA.AlgebraElement, all_vars) + alg = SA.parent(b) + new_obj = typeof(SA.object(alg))(all_vars) + new_basis, _ = SA.promote_with_map(SA.basis(alg), new_obj, identity) + (new_alg, alg_map), _ = SA.promote_bases_with_maps(alg, new_basis) + return SA.maybe_promote(b, new_alg, alg_map) +end + +function SA.promote_bases_with_maps( + a::AbstractSemialgebraicSet, + b::SA.AlgebraElement, +) + _a, _b = MP.promote_variables_with_maps(MP.variables(a), MP.variables(b)) + return SA.maybe_promote(a, _a...), _promote_ae(b, _b[1]) +end + function _map_polys(p, vars, map) return first.(SA.promote_with_map.(p, Ref(vars), Ref(map))) end diff --git a/test/Project.toml b/test/Project.toml index 8903644..9c7700a 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,6 +1,7 @@ [deps] DynamicPolynomials = "7c1d4256-1411-5781-91ec-d7bc3513ac07" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +MultivariateBases = "be282fd4-ad43-11e9-1d11-8bd9d7e43378" MultivariatePolynomials = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3" MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" diff --git a/test/promote.jl b/test/promote.jl index d8386d1..cd28c6a 100644 --- a/test/promote.jl +++ b/test/promote.jl @@ -2,6 +2,7 @@ using Test import StarAlgebras as SA import MultivariatePolynomials as MP +import MultivariateBases as MB using SemialgebraicSets @@ -177,4 +178,55 @@ using SemialgebraicSets @test length(eq) == 1 @test Set(MP.variables(eq[1])) == Set([x, y, z]) end + + @testset "FullSpace with AlgebraElement" begin + f = FullSpace() + a = MB.algebra_element(x^2 + y) + (new_f, map_f), (new_a, map_a) = SA.promote_bases_with_maps(f, a) + @test new_f isa FullSpace + @test map_f === nothing + @test new_a === a + @test map_a === nothing + end + + @testset "AlgebraicSet with AlgebraElement - same variables" begin + V = @set x * y == 1 + a = MB.algebra_element(x + y) + (new_V, map_V), (new_a, map_a) = SA.promote_bases_with_maps(V, a) + @test new_V === V + @test map_V === nothing + @test new_a === a + @test map_a === nothing + end + + @testset "AlgebraicSet with AlgebraElement - different variables" begin + V = @set x^2 == 1 + a = MB.algebra_element(y + z) + (new_V, map_V), (new_a, map_a) = SA.promote_bases_with_maps(V, a) + @test map_V !== nothing + @test map_a !== nothing + @test Set(MP.variables(new_V)) == Set([x, y, z]) + @test Set(MP.variables(new_a)) == Set([x, y, z]) + end + + @testset "BasicSemialgebraicSet with AlgebraElement - same variables" begin + S = @set x - y == 0 && x^2 * y >= 1 + a = MB.algebra_element(x + y) + (new_S, map_S), (new_a, map_a) = SA.promote_bases_with_maps(S, a) + @test new_S === S + @test map_S === nothing + @test new_a === a + @test map_a === nothing + end + + @testset "BasicSemialgebraicSet with AlgebraElement - different variables" begin + S = @set x >= 1 + a = MB.algebra_element(y + z) + (new_S, map_S), (new_a, map_a) = SA.promote_bases_with_maps(S, a) + @test map_S !== nothing + @test map_a !== nothing + @test Set(MP.variables(new_a)) == Set([x, y, z]) + ineqs = inequalities(new_S) + @test length(ineqs) == 1 + end end