Skip to content

Commit

Permalink
added output types to all functions
Browse files Browse the repository at this point in the history
- turned shorthand function style into `function ... end` blocks (except for constructors)
- renamed `is_contained` -> `∈`
- checked #49
- minor doc fixes
  • Loading branch information
schillic committed Dec 9, 2017
1 parent 051b28c commit a4f01bd
Show file tree
Hide file tree
Showing 21 changed files with 190 additions and 142 deletions.
4 changes: 2 additions & 2 deletions docs/src/lib/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CartesianProduct
dim(::CartesianProduct)
σ(::AbstractVector{Float64}, ::CartesianProduct)
Base.:*(::LazySet, ::LazySet)
is_contained(::AbstractVector{Float64}, ::CartesianProduct)
(::AbstractVector{Float64}, ::CartesianProduct)
```

### ``n``-ary Cartesian Product
Expand All @@ -59,7 +59,7 @@ dim(::CartesianProductArray)
Base.:*(::CartesianProductArray, ::LazySet)
Base.:*(::LazySet, ::CartesianProductArray)
Base.:*(::CartesianProductArray, ::CartesianProductArray)
is_contained(::AbstractVector{Float64}, ::CartesianProductArray)
(::AbstractVector{Float64}, ::CartesianProductArray)
```

## Maps
Expand Down
4 changes: 2 additions & 2 deletions docs/src/lib/representations.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ HPolygon
addconstraint!(::HPolygon{Float64}, ::LinearConstraint{Float64})
dim(::HPolygon)
σ(::AbstractVector{Float64}, ::HPolygon)
is_contained(::AbstractVector{Float64}, ::HPolygon{Float64})
(::AbstractVector{Float64}, ::HPolygon{Float64})
tovrep(::HPolygon)
vertices_list(::HPolygon)
```
Expand All @@ -83,7 +83,7 @@ HPolygonOpt
addconstraint!(::HPolygonOpt{Float64}, ::LinearConstraint{Float64})
dim(::HPolygonOpt)
σ(::AbstractVector{Float64}, ::HPolygonOpt{Float64})
is_contained(::AbstractVector{Float64}, ::HPolygonOpt)
(::AbstractVector{Float64}, ::HPolygonOpt)
tovrep(::HPolygonOpt)
vertices_list(::HPolygonOpt)
```
Expand Down
4 changes: 2 additions & 2 deletions docs/src/lib/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ end
# Utility functions

```@docs
sign_cadlag(x::Float64)
sign_cadlag
jump2pi
Base.:<=(u::AbstractVector{Float64}, v::AbstractVector{Float64})
<=(::AbstractVector{Float64}, ::AbstractVector{Float64})
```
7 changes: 5 additions & 2 deletions src/Ball1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ Return the dimension of a `Ball1`.
The ambient dimension of the ball.
"""
dim(B::Ball1)::Int = length(B.center)
function dim(B::Ball1)::Int
return length(B.center)
end

"""
σ(d::AbstractVector{N}, B::Ball1)::AbstractVector{N} where {N<:AbstractFloat}
Expand All @@ -78,7 +80,8 @@ Return the support vector of a `Ball1` in a given direction.
Support vector in the given direction.
"""
function σ(d::AbstractVector{N}, B::Ball1)::AbstractVector{N} where {N<:AbstractFloat}
function σ(d::AbstractVector{N},
B::Ball1)::AbstractVector{N} where {N<:AbstractFloat}
res = copy(B.center)
imax = indmax(abs.(d))
res[imax] = sign(d[imax]) * B.radius
Expand Down
4 changes: 2 additions & 2 deletions src/Ball2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ Return the support vector of a 2-norm ball in a given direction.
### Output
The support vector in the given direction. If the direction has norm zero, the
origin is returned.
The support vector in the given direction.
If the direction has norm zero, the origin is returned.
### Notes
Expand Down
22 changes: 10 additions & 12 deletions src/BallInf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,10 @@ Return the support vector of an infinity norm ball in a given direction.
### Output
The support vector in the given direction. If the direction has norm zero, the
vertex with biggest values is returned.
The support vector in the given direction.
If the direction has norm zero, the vertex with biggest values is returned.
"""
function σ(d::AbstractVector{<:Real}, B::BallInf)::AbstractVector{<:Real}
#=
We cannot use `sign.(d)`, since the built-in `sign` function is such that
`sign(0) = 0`, instead of 1 as needed.
The function `sign_cadlag.(d)` does what we want.
=#
return @. B.center + sign_cadlag(d) * B.radius
end

Expand Down Expand Up @@ -155,7 +150,7 @@ function norm(B::BallInf, p::Real=Inf)::Real
end

"""
radius(B::BallInf, [p]::Real=Inf)
radius(B::BallInf, [p]::Real=Inf)::Real
Return the radius of a ball in the infinity norm.
Expand All @@ -173,11 +168,12 @@ A real number representing the radius.
The radius is defined as the radius of the enclosing ball of the given
``p``-norm of minimal volume with the same center.
"""
radius(B::BallInf, p::Real=Inf) =
(p == Inf) ? B.radius : norm(fill(B.radius, dim(B)), p)
function radius(B::BallInf, p::Real=Inf)::Real
return (p == Inf) ? B.radius : norm(fill(B.radius, dim(B)), p)
end

"""
diameter(B::BallInf, [p]::Real=Inf)
diameter(B::BallInf, [p]::Real=Inf)::Real
Return the diameter of a ball in the infinity norm.
Expand All @@ -197,4 +193,6 @@ any two elements of the set.
Equivalently, it is the diameter of the enclosing ball of the given ``p``-norm
of minimal volume with the same center.
"""
diameter(B::BallInf, p::Real=Inf) = radius(B, p) * 2
function diameter(B::BallInf, p::Real=Inf)::Real
return radius(B, p) * 2
end
28 changes: 18 additions & 10 deletions src/Ballp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ Here ``‖ ⋅ ‖_p`` for ``1 ≤ p ≤ ∞`` denotes the vector ``p``-norm, de
### Fields
- `p` -- norm as a real scalar
- `center` -- center of the ball as a real vector
- `radius` -- radius of the ball as a scalar (``≥ 0``)
## Notes
The special cases ``p=1``, ``p=2`` and ``p=∞`` fall back to the specialized types
`Ball1`, `Ball2` and `BallInf` respectively.
The special cases ``p=1``, ``p=2`` and ``p=∞`` fall back to the specialized
types `Ball1`, `Ball2` and `BallInf`, respectively.
### Examples
A five-dimensional ball in the ``p=3/2`` norm centered at the origin of radius 0.5:
A five-dimensional ball in the ``p=3/2`` norm centered at the origin of radius
0.5:
```jldoctest ballp_constructor
julia> B = Ballp(3/2, zeros(5), 0.5)
Expand Down Expand Up @@ -71,7 +73,8 @@ struct Ballp{N<:Real} <: LazySet
end
end
# type-less convenience constructor
Ballp(p::Real, center::Vector{N}, radius::N) where {N<:Real} = Ballp{N}(p, center, radius)
Ballp(p::Real, center::Vector{N}, radius::N) where {N<:Real} =
Ballp{N}(p, center, radius)

"""
dim(B::Ballp)::Int
Expand All @@ -86,7 +89,9 @@ Return the dimension of a Ballp.
The ambient dimension of the ball.
"""
dim(B::Ballp)::Int = length(B.center)
function dim(B::Ballp)::Int
return length(B.center)
end

"""
σ(d::AbstractVector{N}, B::Ballp)::AbstractVector{N} where {N<:AbstractFloat}
Expand All @@ -101,6 +106,7 @@ Return the support vector of a `Ballp` in a given direction.
### Output
The support vector in the given direction.
If the direction has norm zero, the center of the ball is returned.
### Algorithm
Expand All @@ -109,18 +115,20 @@ The support vector of the unit ball in the ``p``-norm along direction ``d`` is:
```math
σ_{\\mathcal{B}_p^n(0, 1)}(d) = \\dfrac{\\tilde{v}}{‖\\tilde{v}‖_q},
```
where ``\\tilde{v}_i = \\frac{|d_i|^q}{d_i}`` if ``d_i ≠ 0`` and ``tilde{v}_i = 0``
otherwise, for all ``i=1,…,n``, and ``q`` is the conjugate number of ``p``.
where ``\\tilde{v}_i = \\frac{|d_i|^q}{d_i}`` if ``d_i ≠ 0`` and
``tilde{v}_i = 0`` otherwise, for all ``i=1,…,n``, and ``q`` is the conjugate
number of ``p``.
By the affine transformation ``x = r\\tilde{x} + c``, one obtains that
the support vector of ``\\mathcal{B}_p^n(c, r)`` is
```math
σ_{\\mathcal{B}_p^n(c, r)}(d) = \\dfrac{v}{‖v‖_q},
```
where ``v_i = c_i + r\\frac{|d_i|^q}{d_i}`` if ``d_i ≠ 0`` and ``v_i = 0`` otherwise,
for all ``i = 1, …, n``.
where ``v_i = c_i + r\\frac{|d_i|^q}{d_i}`` if ``d_i ≠ 0`` and ``v_i = 0``
otherwise, for all ``i = 1, …, n``.
"""
function σ(d::AbstractVector{N}, B::Ballp)::AbstractVector{N} where {N<:AbstractFloat}
function σ(d::AbstractVector{N},
B::Ballp)::AbstractVector{N} where {N<:AbstractFloat}
p = B.p
q = p/(p-1)
v = similar(d)
Expand Down
20 changes: 9 additions & 11 deletions src/CartesianProduct.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Base.*
import Base: *,

export CartesianProduct,
CartesianProductArray,
is_contained
CartesianProductArray

"""
CartesianProduct{S1<:LazySet,S2<:LazySet} <: LazySet
Expand Down Expand Up @@ -98,7 +97,7 @@ function σ(d::AbstractVector{<:Real},
end

"""
is_contained(x::AbstractVector{<:Real}, cp::CartesianProduct)::Bool
(x::AbstractVector{<:Real}, cp::CartesianProduct)::Bool
Return whether a given vector is contained in a Cartesian product set.
Expand All @@ -111,9 +110,9 @@ Return whether a given vector is contained in a Cartesian product set.
Return `true` iff ``x ∈ cp``.
"""
function is_contained(x::AbstractVector{<:Real}, cp::CartesianProduct)::Bool
return is_contained(view(x, 1:dim(cp.X)), cp.X) &&
is_contained(view(x, dim(cp.X)+1:length(x)), cp.Y)
function (x::AbstractVector{<:Real}, cp::CartesianProduct)::Bool
return (view(x, 1:dim(cp.X)), cp.X) &&
(view(x, dim(cp.X)+1:length(x)), cp.Y)
end

# ======================================
Expand Down Expand Up @@ -262,7 +261,7 @@ function σ(d::AbstractVector{<:Real},
end

"""
is_contained(x::AbstractVector{<:Real}, cpa::CartesianProductArray)::Bool
(x::AbstractVector{<:Real}, cpa::CartesianProductArray)::Bool
Return whether a given vector is contained in a Cartesian product of a finite
number of sets.
Expand All @@ -276,12 +275,11 @@ number of sets.
Return `true` iff ``x ∈ cpa``.
"""
function is_contained(x::AbstractVector{<:Real},
cpa::CartesianProductArray)::Bool
function (x::AbstractVector{<:Real}, cpa::CartesianProductArray)::Bool
jinit = 1
for sj in cpa
jend = jinit + dim(sj) - 1
if !is_contained(x[jinit:jend], sj)
if !(x[jinit:jend], sj)
return false
end
jinit = jend + 1
Expand Down
4 changes: 2 additions & 2 deletions src/ExponentialMap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ end

"""
```
*(spmexp::SparseMatrixExp, X::LazySet)
*(spmexp::SparseMatrixExp, X::LazySet)::ExponentialMap
```
Return the exponential map of a convex set from a sparse matrix exponential.
Expand All @@ -115,7 +115,7 @@ Return the exponential map of a convex set from a sparse matrix exponential.
The exponential map of the convex set.
"""
function *(spmexp::SparseMatrixExp, X::LazySet)
function *(spmexp::SparseMatrixExp, X::LazySet)::ExponentialMap
return ExponentialMap(spmexp, X)
end

Expand Down
17 changes: 11 additions & 6 deletions src/HPolygon.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Base.<=
import Base: <=,

export HPolygon,
addconstraint!,
is_contained,
,
tovrep,
vertices_list

Expand Down Expand Up @@ -36,7 +36,7 @@ HPolygon{N}() where {N<:Real} = HPolygon{N}(Vector{N}(0))
HPolygon() = HPolygon{Float64}()

"""
addconstraint!(P::HPolygon{N}, constraint::LinearConstraint{N}) where {N<:Real}
addconstraint!(P::HPolygon{N}, constraint::LinearConstraint{N})::Void where {N<:Real}
Add a linear constraint to a polygon in constraint representation, keeping the
constraints sorted by their normal directions.
Expand All @@ -45,15 +45,20 @@ constraints sorted by their normal directions.
- `P` -- polygon
- `constraint` -- linear constraint to add
### Output
Nothing.
"""
function addconstraint!(P::HPolygon{N},
constraint::LinearConstraint{N}) where {N<:Real}
constraint::LinearConstraint{N})::Void where {N<:Real}
i = length(P.constraints_list)
while i > 0 && constraint.a <= P.constraints_list[i].a
i -= 1
end
# here P.constraints_list[i] < constraint
insert!(P.constraints_list, i+1, constraint)
return nothing
end

"""
Expand Down Expand Up @@ -113,7 +118,7 @@ function σ(d::AbstractVector{<:Real}, P::HPolygon{N})::Vector{N} where {N<:Real
end

"""
is_contained(x::AbstractVector{<:Real}, P::HPolygon)::Bool
(x::AbstractVector{<:Real}, P::HPolygon)::Bool
Return whether a given vector is contained in a polygon.
Expand All @@ -126,7 +131,7 @@ Return whether a given vector is contained in a polygon.
Return `true` iff ``x ∈ P``.
"""
function is_contained(x::AbstractVector{<:Real}, P::HPolygon)::Bool
function (x::AbstractVector{<:Real}, P::HPolygon)::Bool
for c in P.constraints_list
if !(dot(c.a, x) <= c.b)
return false
Expand Down
Loading

0 comments on commit a4f01bd

Please sign in to comment.