Skip to content

Commit

Permalink
Merge pull request #1808 from JuliaReach/schillic/1446
Browse files Browse the repository at this point in the history
#1446 - Print error message if required external package is missing
  • Loading branch information
schillic authored Dec 12, 2019
2 parents 8f371b3 + cc4097f commit 3e9a6df
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 118 deletions.
7 changes: 7 additions & 0 deletions docs/src/lib/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ RecipesBase.apply_recipe(::Dict{Symbol,Any}, ::LazySet{N}, ::N=N(1e-3)) where {N
RecipesBase.apply_recipe(::Dict{Symbol,Any}, ::AbstractVector{VN}, ::N=N(1e-3), ::Int=40, ::Bool=false) where {N<:Real, VN<:LazySet{N}}
```

For three-dimensional sets, we support `Makie`:

```@docs
plot3d
plot3d!
```

### Set functions that override Base functions

```@docs
Expand Down
6 changes: 1 addition & 5 deletions src/Approximations/init.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
function __init__()
@require TaylorModels = "314ce334-5f6e-57ae-acf6-00b6e903104a" load_taylormodels()
@require TaylorModels = "314ce334-5f6e-57ae-acf6-00b6e903104a" include("init_TaylorModels.jl")
@require IntervalMatrices = "5c1f47dc-42dd-5697-8aaa-4d102d140ba9" include("init_IntervalMatrices.jl")
end

function load_taylormodels()
eval(load_taylormodels_overapproximation())
end
1 change: 1 addition & 0 deletions src/Approximations/init_TaylorModels.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eval(load_taylormodels_overapproximation())
8 changes: 1 addition & 7 deletions src/Initialization/init_Expokit.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
eval(quote
using .Expokit: expmv
end)

eval(load_expokit_sparsematrixexp())
eval(load_expokit_exponentialmap())
eval(load_expokit_exponentialprojectionmap())
eval(load_expokit())
2 changes: 1 addition & 1 deletion src/Initialization/init_Makie.jl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
eval(initialize_mesh())
eval(load_makie())
3 changes: 1 addition & 2 deletions src/Initialization/init_Polyhedra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ end)
eval(load_polyhedra_hpolytope())
eval(load_polyhedra_hpolyhedron())
eval(load_polyhedra_vpolytope())

eval(initialize_mesh())
eval(load_polyhedra_mesh())
40 changes: 25 additions & 15 deletions src/LazyOperations/ExponentialMap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ export SparseMatrixExp,
get_column,
get_columns

function load_expokit()
return quote

using .Expokit: expmv

end end # quote / load_expokit


# --- SparseMatrixExp & ExponentialMap ---

"""
Expand Down Expand Up @@ -84,10 +92,9 @@ function size(spmexp::SparseMatrixExp, ax::Int)::Int
return size(spmexp.M, ax)
end

function load_expokit_sparsematrixexp()
return quote

function get_column(spmexp::SparseMatrixExp{N}, j::Int)::Vector{N} where {N}
require(:Expokit; fun_name="get_column")

n = size(spmexp, 1)
aux = zeros(N, n)
aux[j] = one(N)
Expand All @@ -96,6 +103,7 @@ end

function get_columns(spmexp::SparseMatrixExp{N},
J::AbstractArray)::Matrix{N} where {N}
require(:Expokit; fun_name="get_columns")

n = size(spmexp, 1)
aux = zeros(N, n)
Expand Down Expand Up @@ -133,6 +141,8 @@ The result is of type `Transpose`; in Julia versions older than v0.7, the result
was of type `RowVector`.
"""
function get_row(spmexp::SparseMatrixExp{N}, i::Int) where {N}
require(:Expokit; fun_name="get_row")

n = size(spmexp, 1)
aux = zeros(N, n)
aux[i] = one(N)
Expand All @@ -141,6 +151,8 @@ end

function get_rows(spmexp::SparseMatrixExp{N},
I::AbstractArray{Int})::Matrix{N} where {N}
require(:Expokit; fun_name="get_rows")

n = size(spmexp, 1)
aux = zeros(N, n)
ans = zeros(N, length(I), n)
Expand All @@ -157,8 +169,6 @@ function get_rows(spmexp::SparseMatrixExp{N},
return ans
end

end end # quote / load_expokit_sparsematrixexp

"""
ExponentialMap{N<:Real, S<:LazySet{N}} <: LazySet{N}
Expand Down Expand Up @@ -261,9 +271,6 @@ function dim(em::ExponentialMap)::Int
return size(em.spmexp.M, 1)
end

function load_expokit_exponentialmap()
return quote

"""
σ(d::AbstractVector{N}, em::ExponentialMap{N}) where {N<:Real}
Expand All @@ -288,6 +295,8 @@ We allow sparse direction vectors, but will convert them to dense vectors to be
able to use `expmv`.
"""
function σ(d::AbstractVector{N}, em::ExponentialMap{N}) where {N<:Real}
require(:Expokit; fun_name="σ")

d_dense = d isa Vector ? d : Vector(d)
v = expmv(one(N), transpose(em.spmexp.M), d_dense) # v <- exp(M') * d
return expmv(one(N), em.spmexp.M, σ(v, em.X)) # res <- exp(M) * σ(v, S)
Expand Down Expand Up @@ -316,6 +325,8 @@ We allow sparse direction vectors, but will convert them to dense vectors to be
able to use `expmv`.
"""
function ρ(d::AbstractVector{N}, em::ExponentialMap{N}) where {N<:Real}
require(:Expokit; fun_name="ρ")

d_dense = d isa Vector ? d : Vector(d)
v = expmv(one(N), transpose(em.spmexp.M), d_dense) # v <- exp(M^T) * d
return ρ(v, em.X)
Expand Down Expand Up @@ -356,6 +367,8 @@ true
```
"""
function (x::AbstractVector{N}, em::ExponentialMap{N})::Bool where {N<:Real}
require(:Expokit; fun_name="")

@assert length(x) == dim(em)
return expmv(-one(N), em.spmexp.M, x) em.X
end
Expand All @@ -379,6 +392,8 @@ We assume that the underlying set `X` is polytopic.
Then the result is just the exponential map applied to the vertices of `X`.
"""
function vertices_list(em::ExponentialMap{N})::Vector{Vector{N}} where {N<:Real}
require(:Expokit; fun_name="vertices_list")

# collect low-dimensional vertices lists
vlist_X = vertices_list(em.X)

Expand All @@ -392,8 +407,6 @@ function vertices_list(em::ExponentialMap{N})::Vector{Vector{N}} where {N<:Real}
return vlist
end

end end # quote / load_expokit_exponentialmap

"""
isbounded(em::ExponentialMap)::Bool
Expand Down Expand Up @@ -507,9 +520,6 @@ function dim(eprojmap::ExponentialProjectionMap)::Int
return size(eprojmap.projspmexp.L, 1)
end

function load_expokit_exponentialprojectionmap()
return quote

"""
σ(d::AbstractVector{N},
eprojmap::ExponentialProjectionMap{N}) where {N<:Real}
Expand Down Expand Up @@ -537,6 +547,8 @@ able to use `expmv`.
"""
function σ(d::AbstractVector{N},
eprojmap::ExponentialProjectionMap{N}) where {N<:Real}
require(:Expokit; fun_name="σ")

d_dense = d isa Vector ? d : Vector(d)
daux = transpose(eprojmap.projspmexp.L) * d_dense
aux1 = expmv(one(N), transpose(eprojmap.projspmexp.spmexp.M), daux)
Expand All @@ -548,8 +560,6 @@ function σ(d::AbstractVector{N},
return eprojmap.projspmexp.L * daux
end

end end # quote / load_expokit_exponentialprojectionmap

"""
isbounded(eprojmap::ExponentialProjectionMap)::Bool
Expand Down
2 changes: 1 addition & 1 deletion src/LazySets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ using .Arrays
include("Utils/helper_functions.jl")
include("Utils/comparisons.jl")
include("Utils/macros.jl")
include("Utils/samples.jl")

# ==================
# Abstract set types
Expand Down Expand Up @@ -124,6 +123,7 @@ include("ConcreteOperations/issubset.jl")
include("ConcreteOperations/minkowski_difference.jl")
include("ConcreteOperations/minkowski_sum.jl")
include("ConcreteOperations/reflection.jl")
include("Utils/samples.jl")

# =====================
# Approximations module
Expand Down
53 changes: 36 additions & 17 deletions src/Plotting/mesh.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
function load_mesh()
export plot3d, plot3d!


function load_polyhedra_mesh()
return quote

using .Polyhedra: Mesh

end end # quote / function load_polyhedra_mesh()


function load_makie()
return quote

using .Makie: mesh, mesh!
import .Makie.AbstractPlotting: Automatic
using .Makie.AbstractPlotting: Automatic

end end # quote / function load_makie()

export plot3d, plot3d!

# helper function for 3D plotting; converts S to a polytope in H-representation
function plot3d_helper(S::LazySet{N}, backend) where {N}
function _plot3d_helper(S::LazySet{N}, backend) where {N}
@assert dim(S) <= 3 "plot3d can only be used to plot sets of dimension three (or lower); " *
"but the given set is $(dim(S))-dimensional"

Expand All @@ -24,11 +35,11 @@ end

"""
plot3d(S::LazySet{N}; backend=default_polyhedra_backend(S, N),
alpha=1.0, color=:blue, colormap=:viridis, colorrange=Automatic(),
alpha=1.0, color=:blue, colormap=:viridis, colorrange=nothing,
interpolate=false, linewidth=1, overdraw=false, shading=true,
transparency=true, visible=true) where {N}
Plot a three-dimensional convex set using Makie.
Plot a three-dimensional convex set using `Makie`.
### Input
Expand All @@ -43,9 +54,10 @@ Plot a three-dimensional convex set using Makie.
- `colormap` -- (optional, default: `:viridis`) the color map of the main plot;
call `available_gradients()` to see what gradients are available,
and it can also be used as `[:red, :black]`
- `colorrange` -- (optional, default: `Automatic()`) a tuple `(min, max)` where
`min` and `max` specify the data range to be used for indexing
the colormap
- `colorrange` -- (optional, default: `nothing`, which falls back to
`Makie.AbstractPlotting.Automatic()`) a tuple `(min, max)`
where `min` and `max` specify the data range to be used for
indexing the colormap
- `interpolate` -- (optional, default: `false`) a bool for heatmap and images,
it toggles color interpolation between nearby pixels
- `linewidth` -- (optional, default: `1`) a number that specifies the width of
Expand Down Expand Up @@ -103,17 +115,22 @@ julia> plot3d!(10. * rand(Hyperrectangle, dim=3), color=:red)
```
"""
function plot3d(S::LazySet{N}; backend=default_polyhedra_backend(S, N),
alpha=1.0, color=:blue, colormap=:viridis, colorrange=Automatic(), interpolate=false,
alpha=1.0, color=:blue, colormap=:viridis, colorrange=nothing, interpolate=false,
linewidth=1, overdraw=false, shading=true, transparency=true, visible=true) where {N}
require(:Makie; fun_name="plot3d")
require(:Polyhedra; fun_name="plot3d")

P_poly_mesh = plot3d_helper(S, backend)
if colorrange == nothing
colorrange = Automatic()
end
P_poly_mesh = _plot3d_helper(S, backend)
return mesh(P_poly_mesh, alpha=alpha, color=color, colormap=colormap, colorrange=colorrange,
interpolate=interpolate, linewidth=linewidth, transparency=transparency, visible=visible)
end

"""
plot3d!(S::LazySet{N}; backend=default_polyhedra_backend(S, N),
alpha=1.0, color=:blue, colormap=:viridis, colorrange=Automatic(), interpolate=false,
alpha=1.0, color=:blue, colormap=:viridis, colorrange=nothing, interpolate=false,
linewidth=1, overdraw=false, shading=true, transparency=true, visible=true) where {N}
Plot a three-dimensional convex set using Makie.
Expand All @@ -129,13 +146,15 @@ documentation](http://makie.juliaplots.org/stable/plot-attributes).
See the documentation of `plot3d` for examples.
"""
function plot3d!(S::LazySet{N}; backend=default_polyhedra_backend(S, N),
alpha=1.0, color=:blue, colormap=:viridis, colorrange=Automatic(), interpolate=false,
alpha=1.0, color=:blue, colormap=:viridis, colorrange=nothing, interpolate=false,
linewidth=1, overdraw=false, shading=true, transparency=true, visible=true) where {N}
require(:Makie; fun_name="plot3d!")
require(:Polyhedra; fun_name="plot3d!")

P_poly_mesh = plot3d_helper(S, backend)
if colorrange == nothing
colorrange = Automatic()
end
P_poly_mesh = _plot3d_helper(S, backend)
return mesh!(P_poly_mesh, alpha=alpha, color=color, colormap=colormap, colorrange=colorrange,
interpolate=interpolate, linewidth=linewidth, transparency=transparency, visible=visible)
end

end # quote
end # function load_mesh()
8 changes: 3 additions & 5 deletions src/Sets/VPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export VPolytope,
cartesian_product,
linear_map,
remove_redundant_vertices,
minkowski_sum
minkowski_sum,
tohrep,
tovrep

"""
VPolytope{N<:Real} <: AbstractPolytope{N}
Expand Down Expand Up @@ -555,10 +557,6 @@ function load_polyhedra_vpolytope() # function to be loaded by Requires
return quote
# see the interface file AbstractPolytope.jl for the imports

export vertices_list,
tohrep,
tovrep

# VPolytope from a VRep
function VPolytope(P::VRep{N}) where {N}
vertices = Vector{Vector{N}}()
Expand Down
Loading

0 comments on commit 3e9a6df

Please sign in to comment.