Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/promote.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const _PolynomialLike = Union{MP.AbstractPolynomialLike,SA.AlgebraElement}

function SA.promote_bases_with_maps(
::FullSpace,
p::Union{MP.AbstractPolynomialLike,AbstractSemialgebraicSet},
)
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},
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
52 changes: 52 additions & 0 deletions test/promote.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Test

import StarAlgebras as SA
import MultivariatePolynomials as MP
import MultivariateBases as MB

using SemialgebraicSets

Expand Down Expand Up @@ -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
Loading