Skip to content

Commit

Permalink
Merge pull request #1528 from JuliaReach/schillic/422
Browse files Browse the repository at this point in the history
#422 - Require non-zero normal directions in Line/Hyperplane/HalfSpace
  • Loading branch information
schillic authored Jul 27, 2019
2 parents 786611d + 01aa37a commit 5cc0310
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/HalfSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Type that represents a (closed) half-space of the form ``a⋅x ≤ b``.
### Fields
- `a` -- normal direction
- `a` -- normal direction (non-zero)
- `b` -- constraint
### Examples
Expand All @@ -30,8 +30,18 @@ HalfSpace{Float64,Array{Float64,1}}([0.0, -1.0], 0.0)
struct HalfSpace{N<:Real, VN<:AbstractVector{N}} <: AbstractPolyhedron{N}
a::VN
b::N

function HalfSpace{N, VN}(a::VN, b::N
) where {N<:Real, VN<:AbstractVector{N}}
@assert !iszero(a) "a half-space needs a non-zero normal vector"
return new{N, VN}(a, b)
end
end

# convenience constructor without type parameter
HalfSpace(a::VN, b::N) where {N<:Real, VN<:AbstractVector{N}} =
HalfSpace{N, VN}(a, b)

"""
LinearConstraint
Expand Down
10 changes: 9 additions & 1 deletion src/Hyperplane.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Type that represents a hyperplane of the form ``a⋅x = b``.
### Fields
- `a` -- normal direction
- `a` -- normal direction (non-zero)
- `b` -- constraint
### Examples
Expand All @@ -27,8 +27,16 @@ Hyperplane{Float64}([0.0, 1.0], 0.0)
struct Hyperplane{N<:Real} <: AbstractPolyhedron{N}
a::AbstractVector{N}
b::N

function Hyperplane{N}(a::AbstractVector{N}, b::N) where {N<:Real}
@assert !iszero(a) "a hyperplane needs a non-zero normal vector"
return new{N}(a, b)
end
end

# convenience constructor without type parameter
Hyperplane(a::AbstractVector{N}, b::N) where {N<:Real} = Hyperplane{N}(a, b)


# --- polyhedron interface functions ---

Expand Down
4 changes: 2 additions & 2 deletions src/Line.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ of a `Hyperplane`).
### Fields
- `a` -- normal direction
- `a` -- normal direction (non-zero)
- `b` -- constraint
### Examples
Expand All @@ -32,7 +32,7 @@ struct Line{N<:Real, VN<:AbstractVector{N}} <: AbstractPolyhedron{N}
# default constructor with length constraint
function Line{N, VN}(a::VN, b::N) where {N<:Real, VN<:AbstractVector{N}}
@assert length(a) == 2 "lines must be two-dimensional"
@assert a != zeros(N, 2) "the normal vector of a line must not be zero"
@assert !iszero(a) "a line needs a non-zero normal vector"
return new{N, VN}(a, b)
end
end
Expand Down
3 changes: 3 additions & 0 deletions test/unit_HalfSpace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ for N in [Float64, Rational{Int}, Float32]
# normal constructor
hs = HalfSpace(ones(N, 3), N(5))

# corner case: zero normal vector
@test_throws AssertionError HalfSpace(N[0, 0], N(1))

# dimension
@test dim(hs) == 3

Expand Down
3 changes: 3 additions & 0 deletions test/unit_Hyperplane.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ for N in [Float64, Rational{Int}, Float32]
b = N(5)
hp = Hyperplane(a, b)

# corner case: zero normal vector
@test_throws AssertionError Hyperplane(N[0, 0], N(1))

# dimension
@test dim(hp) == 3

Expand Down
3 changes: 3 additions & 0 deletions test/unit_Line.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ for N in [Float64, Rational{Int}, Float32]
[1.0, 10.0] L
[10.0, 10.0] L

# corner case: zero normal vector
@test_throws AssertionError Line(N[0, 0], N(1))

# dimension
@test dim(l1) == 2

Expand Down

0 comments on commit 5cc0310

Please sign in to comment.