Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for SymEngine code; rename and fix free_symbols #3610

Merged
merged 4 commits into from
Jul 24, 2024
Merged
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
11 changes: 6 additions & 5 deletions src/Initialization/init_SymEngine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ julia> free_symbols(:(x1 + x2 <= 2*x4 + 6), HalfSpace)
x4
```
"""
function free_symbols(::Expr, ::Type{<:LazySet}) end
function free_symbols(::Expr, ::Type{<:LazySet}) end # COV_EXCL_LINE

function free_symbols(expr::Expr)
# Note: this convenience function is not used anywhere
function _free_symbols(expr::Expr)
if _is_hyperplane(expr)
return free_symbols(expr, HyperPlane)
return free_symbols(expr, Hyperplane)
elseif _is_halfspace(expr)
return free_symbols(expr, Halfspace)
return free_symbols(expr, HalfSpace)
else
error("the free symbols for the expression $(expr) is not implemented")
error("the `free_symbols` method for the expression $expr is not implemented")
end
end
2 changes: 1 addition & 1 deletion src/Sets/HalfSpace/init_SymEngine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import .SymEngine: free_symbols
function free_symbols(expr::Expr, ::Type{<:HalfSpace})
# get sides of the inequality
lhs, rhs = convert(SymEngine.Basic, expr.args[2]), convert(SymEngine.Basic, expr.args[3])
return free_symbols(lhs - rhs)
return SymEngine.free_symbols(lhs - rhs)
end

eval(load_SymEngine_ishalfspace())
Expand Down
2 changes: 1 addition & 1 deletion src/Sets/Hyperplane/init_SymEngine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function free_symbols(expr::Expr, ::Type{<:Hyperplane})
# treats the 4 in :(2*x1 = 4)
rhs = :args in fieldnames(typeof(expr.args[2])) ? convert(SymEngine.Basic, expr.args[2].args[2]) :
convert(SymEngine.Basic, expr.args[2])
return free_symbols(lhs - rhs)
return SymEngine.free_symbols(lhs - rhs)
end

eval(load_SymEngine_ishyperplanar())
Expand Down
10 changes: 10 additions & 0 deletions test/Interfaces/SymEngine.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# _is_linearcombination
@test LazySets._is_linearcombination(:(2*x1 - 4))
@test LazySets._is_linearcombination(:(6.1 - 5.3*f - 0.1*g))
@test !LazySets._is_linearcombination(:(2*x1^2))
@test !LazySets._is_linearcombination(:(x1^2 - 4*x2 + x3 + 2))

# _free_symbols
@test LazySets._free_symbols(:(x1 = 1)) == [SymEngine.Basic(:(x1))]
@test LazySets._free_symbols(:(2*x2 <= 4)) == [SymEngine.Basic(:(x2))]
@test_throws ErrorException LazySets._free_symbols(:(x3 != 4))
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SCS = "c946c3f1-0d1f-5ce8-9dea-7daa1f7e2d13"
SetProg = "39881422-4b75-5582-a5c7-0feb14562a65"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
SymEngine = "123dc426-2d89-5057-bbad-38513e3affd8"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
TaylorModels = "314ce334-5f6e-57ae-acf6-00b6e903104a"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down Expand Up @@ -52,6 +53,7 @@ RecipesBase = "0.6 - 0.8, 1"
SCS = "1, 2"
SetProg = "0.3"
StaticArrays = "0.12, 1"
SymEngine = "0.7 - 0.12"
Symbolics = "1 - 5"
TaylorModels = "0.0.1, 0.1 - 0.7"
WriteVTK = "1"
27 changes: 26 additions & 1 deletion test/Sets/HalfSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ for N in [Float64, Float32]
end

for N in [Float64]

# rationalization
H = HalfSpace([1.0, 2.0], 0.0)
Hr = rationalize(H)
Expand Down Expand Up @@ -252,6 +251,32 @@ for N in [Float64]
@test HalfSpace(2x[1] + 5x[4] >= -10.0 + x[3], x) ==
HalfSpace([-2.0, 0.0, 1.0, -5.0, 0.0], 10.0)
end

# tests that require SymEngine
@static if isdefined(@__MODULE__, :SymEngine)
# _is_halfspace
@test all(LazySets._is_halfspace.([:(x1 <= 0), :(x1 < 0), :(x1 > 0), :(x1 >= 0)]))
@test !LazySets._is_halfspace(:(x1 = 0))
@test LazySets._is_halfspace(:(2*x1 <= 4))
@test LazySets._is_halfspace(:(6.1 <= 5.3*f - 0.1*g))
@test !LazySets._is_halfspace(:(2*x1^2 <= 4))
@test !LazySets._is_halfspace(:(x1^2 > 4*x2 - x3))
@test LazySets._is_halfspace(:(x1 > 4*x2 - x3))

# convert
H = convert(HalfSpace, :(x1 <= -0.03))
@test H == HalfSpace([1.0], -0.03)
H = convert(HalfSpace, :(x1 < -0.03))
@test H == HalfSpace([1.0], -0.03)
H = convert(HalfSpace, :(x1 > -0.03))
@test H == HalfSpace([-1.0], 0.03)
H = convert(HalfSpace, :(x1 >= -0.03))
@test H == HalfSpace([-1.0], 0.03)
H = convert(HalfSpace, :(x1 + x2 <= 2*x4 + 6))
@test H == HalfSpace([1.0, 1.0, -2.0], 6.0)
H = convert(HalfSpace, :(x1 + x2 <= 2*x4 + 6), vars=SymEngine.Basic[:x1, :x2, :x3, :x4])
@test H == HalfSpace([1.0, 1.0, 0.0, -2.0], 6.0)
end
end

# isoperationtype
Expand Down
22 changes: 22 additions & 0 deletions test/Sets/Hyperplane.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,28 @@ for N in [Float64]
vars = @variables x[1:2] t
@test Hyperplane(x[1] == t, vars) == Hyperplane([1.0, 0.0, -1.0], 0.0)
end

# tests that require SymEngine
@static if isdefined(@__MODULE__, :SymEngine)
# _is_halfspace
@test LazySets._is_hyperplane(:(x1 = 0))
@test !LazySets._is_hyperplane(:(x1 <= 0))
@test LazySets._is_hyperplane(:(2*x1 = 4))
@test LazySets._is_hyperplane(:(6.1 = 5.3*f - 0.1*g))
@test !LazySets._is_hyperplane(:(2*x1^2 = 4))
@test !LazySets._is_hyperplane(:(x1^2 = 4*x2 - x3))
@test LazySets._is_hyperplane(:(x1 = 4*x2 - x3))

# convert
H = convert(Hyperplane, :(x1 = -0.03))
@test H == Hyperplane([1.0], -0.03)
H = convert(Hyperplane, :(x1 + 0.03 = 0))
@test H == Hyperplane([1.0], -0.03)
H = convert(Hyperplane, :(x1 + x2 = 2*x4 + 6))
@test H == Hyperplane([1.0, 1.0, -2.0], 6.0)
H = convert(Hyperplane, :(x1 + x2 = 2*x4 + 6), vars=SymEngine.Basic[:x1, :x2, :x3, :x4])
@test H == Hyperplane([1.0, 1.0, 0.0, -2.0], 6.0)
end
end

# isoperationtype
Expand Down
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Random.seed!(1234)
end
using IntervalMatrices: ±, IntervalMatrix
using TaylorModels: set_variables, TaylorModelN
using Symbolics
using SymEngine, Symbolics
end

# ==============================
Expand Down Expand Up @@ -107,6 +107,9 @@ if test_suite_basic
@testset "LazySets.CDDLib" begin
include("Interfaces/CDDLib.jl")
end
@testset "LazySets.SymEngine" begin
include("Interfaces/SymEngine.jl")
end

# =======================
# Testing basic set types
Expand Down
Loading