diff --git a/docs/src/lib/representations.md b/docs/src/lib/representations.md index b5d05ea555..dee2cd8da4 100644 --- a/docs/src/lib/representations.md +++ b/docs/src/lib/representations.md @@ -158,6 +158,7 @@ dim(::HalfSpace) σ(::AbstractVector{Real}, ::HalfSpace{Real}) ∈(::AbstractVector{Real}, ::HalfSpace{Real}) an_element(::HalfSpace{N}) where {N<:Real} +rand(::Type{HalfSpace}) isempty(::HalfSpace) constraints_list(::HalfSpace{N}) where {N<:Real} constrained_dimensions(::HalfSpace{N}) where {N<:Real} @@ -177,6 +178,7 @@ dim(::Hyperplane) σ(::AbstractVector{Real}, ::Hyperplane{Real}) ∈(::AbstractVector{Real}, ::Hyperplane{Real}) an_element(::Hyperplane{N}) where {N<:Real} +rand(::Type{Hyperplane}) isempty(::Hyperplane) constrained_dimensions(::Hyperplane{N}) where {N<:Real} ``` @@ -255,6 +257,7 @@ dim(::Line) σ(::AbstractVector{Real}, ::Line{Real}) ∈(::AbstractVector{Real}, ::Line{Real}) an_element(::Line{N}) where {N<:Real} +rand(::Type{Line}) isempty(::Line) constrained_dimensions(::Line{N}) where {N<:Real} ``` diff --git a/src/HalfSpace.jl b/src/HalfSpace.jl index acbf2bf7df..65bf5604ea 100644 --- a/src/HalfSpace.jl +++ b/src/HalfSpace.jl @@ -1,4 +1,6 @@ -import Base: ∈, isempty +import Base: rand, + ∈, + isempty export HalfSpace, LinearConstraint, an_element, @@ -119,6 +121,45 @@ function ∈(x::AbstractVector{N}, hs::HalfSpace{N})::Bool where {N<:Real} return dot(x, hs.a) <= hs.b end +""" + rand(::Type{HalfSpace}; [N]::Type{<:Real}=Float64, [dim]::Int=2, + [rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing + )::HalfSpace{N} + +Create a random half-space. + +### Input + +- `HalfSpace` -- type for dispatch +- `N` -- (optional, default: `Float64`) numeric type +- `dim` -- (optional, default: 2) dimension +- `rng` -- (optional, default: `GLOBAL_RNG`) random number generator +- `seed` -- (optional, default: `nothing`) seed for reseeding + +### Output + +A random half-space. + +### Algorithm + +All numbers are normally distributed with mean 0 and standard deviation 1. +Additionally, the constraint `a` is nonzero. +""" +function rand(::Type{HalfSpace}; + N::Type{<:Real}=Float64, + dim::Int=2, + rng::AbstractRNG=GLOBAL_RNG, + seed::Union{Int, Nothing}=nothing + )::HalfSpace{N} + rng = reseed(rng, seed) + a = randn(rng, N, dim) + while iszero(a) + a = randn(rng, N, dim) + end + b = randn(rng, N) + return HalfSpace(a, b) +end + """ isempty(hs::HalfSpace)::Bool diff --git a/src/Hyperplane.jl b/src/Hyperplane.jl index 6e5fa20622..b13df55f5b 100644 --- a/src/Hyperplane.jl +++ b/src/Hyperplane.jl @@ -1,4 +1,6 @@ -import Base: ∈, isempty +import Base: rand, + ∈, + isempty export Hyperplane, an_element @@ -110,6 +112,45 @@ function ∈(x::AbstractVector{N}, hp::Hyperplane{N})::Bool where {N<:Real} return dot(x, hp.a) == hp.b end +""" + rand(::Type{Hyperplane}; [N]::Type{<:Real}=Float64, [dim]::Int=2, + [rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing + )::Hyperplane{N} + +Create a random hyperplane. + +### Input + +- `Hyperplane` -- type for dispatch +- `N` -- (optional, default: `Float64`) numeric type +- `dim` -- (optional, default: 2) dimension +- `rng` -- (optional, default: `GLOBAL_RNG`) random number generator +- `seed` -- (optional, default: `nothing`) seed for reseeding + +### Output + +A random hyperplane. + +### Algorithm + +All numbers are normally distributed with mean 0 and standard deviation 1. +Additionally, the constraint `a` is nonzero. +""" +function rand(::Type{Hyperplane}; + N::Type{<:Real}=Float64, + dim::Int=2, + rng::AbstractRNG=GLOBAL_RNG, + seed::Union{Int, Nothing}=nothing + )::Hyperplane{N} + rng = reseed(rng, seed) + a = randn(rng, N, dim) + while iszero(a) + a = randn(rng, N, dim) + end + b = randn(rng, N) + return Hyperplane(a, b) +end + """ isempty(hp::Hyperplane)::Bool diff --git a/src/Line.jl b/src/Line.jl index 26cfd0737c..44075ba8c3 100644 --- a/src/Line.jl +++ b/src/Line.jl @@ -1,4 +1,6 @@ -import Base: ∈, isempty +import Base: rand, + ∈, + isempty export Line, an_element @@ -175,6 +177,46 @@ function ∈(x::AbstractVector{N}, L::Line{N})::Bool where {N<:Real} return dot(L.a, x) == L.b end +""" + rand(::Type{Line}; [N]::Type{<:Real}=Float64, [dim]::Int=2, + [rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing + )::Line{N} + +Create a random line. + +### Input + +- `Line` -- type for dispatch +- `N` -- (optional, default: `Float64`) numeric type +- `dim` -- (optional, default: 2) dimension +- `rng` -- (optional, default: `GLOBAL_RNG`) random number generator +- `seed` -- (optional, default: `nothing`) seed for reseeding + +### Output + +A random line. + +### Algorithm + +All numbers are normally distributed with mean 0 and standard deviation 1. +Additionally, the constraint `a` is nonzero. +""" +function rand(::Type{Line}; + N::Type{<:Real}=Float64, + dim::Int=2, + rng::AbstractRNG=GLOBAL_RNG, + seed::Union{Int, Nothing}=nothing + )::Line{N} + @assert dim == 2 "a Line must have dimension 2" + rng = reseed(rng, seed) + a = randn(rng, N, dim) + while iszero(a) + a = randn(rng, N, dim) + end + b = randn(rng, N) + return Line(a, b) +end + """ isempty(L::Line)::Bool diff --git a/test/unit_HalfSpace.jl b/test/unit_HalfSpace.jl index 1e88ee77e6..c782b1930c 100644 --- a/test/unit_HalfSpace.jl +++ b/test/unit_HalfSpace.jl @@ -1,4 +1,7 @@ for N in [Float64, Rational{Int}, Float32] + # random half-space + rand(HalfSpace) + # normal constructor normal = ones(N, 3) hs = HalfSpace(normal, N(5)) diff --git a/test/unit_Hyperplane.jl b/test/unit_Hyperplane.jl index 793c401260..cf91e036bb 100644 --- a/test/unit_Hyperplane.jl +++ b/test/unit_Hyperplane.jl @@ -1,4 +1,7 @@ for N in [Float64, Rational{Int}, Float32] + # random hyperplane + rand(Hyperplane) + # normal constructor a = ones(N, 3) hp = Hyperplane(a, N(5)) diff --git a/test/unit_Line.jl b/test/unit_Line.jl index b40c93686c..c9465a40f7 100644 --- a/test/unit_Line.jl +++ b/test/unit_Line.jl @@ -1,4 +1,7 @@ for N in [Float64, Rational{Int}, Float32] + # random line + rand(Line) + # construction l1 = Line(N[0, 1], N(1)) l2 = Line(N[1, 0], N(1))