/
ZeroSet.jl
202 lines (137 loc) · 4.11 KB
/
ZeroSet.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import Base: rand,
∈
export ZeroSet,
linear_map
"""
ZeroSet{N<:Real} <: AbstractSingleton{N}
Type that represents the zero set, i.e., the set that only contains the origin.
### Fields
- `dim` -- the ambient dimension of this zero set
"""
struct ZeroSet{N<:Real} <: AbstractSingleton{N}
dim::Int
end
# default constructor of type Float64
ZeroSet(dim::Int) = ZeroSet{Float64}(dim)
# --- AbstractSingleton interface functions ---
"""
element(S::ZeroSet{N})::Vector{N} where {N<:Real}
Return the element of a zero set.
### Input
- `S` -- zero set
### Output
The element of the zero set, i.e., a zero vector.
"""
function element(S::ZeroSet{N})::Vector{N} where {N<:Real}
return zeros(N, S.dim)
end
"""
element(S::ZeroSet{N}, ::Int)::N where {N<:Real}
Return the i-th entry of the element of a zero set.
### Input
- `S` -- zero set
- `i` -- dimension
### Output
The i-th entry of the element of the zero set, i.e., 0.
"""
function element(S::ZeroSet{N}, ::Int)::N where {N<:Real}
return zero(N)
end
# --- LazySet interface functions ---
"""
dim(Z::ZeroSet)::Int
Return the ambient dimension of this zero set.
### Input
- `Z` -- a zero set, i.e., a set that only contains the origin
### Output
The ambient dimension of the zero set.
"""
function dim(Z::ZeroSet)::Int
return Z.dim
end
"""
σ(d::AbstractVector{N}, Z::ZeroSet{N}) where {N<:Real}
Return the support vector of a zero set.
### Input
- `Z` -- a zero set, i.e., a set that only contains the origin
### Output
The returned value is the origin since it is the only point that belongs to this
set.
"""
function σ(d::AbstractVector{N}, Z::ZeroSet{N}) where {N<:Real}
@assert length(d) == dim(Z) "the direction has the wrong dimension"
return element(Z)
end
"""
∈(x::AbstractVector{N}, Z::ZeroSet{N})::Bool where {N<:Real}
Check whether a given point is contained in a zero set.
### Input
- `x` -- point/vector
- `Z` -- zero set
### Output
`true` iff ``x ∈ Z``.
### Examples
```jldoctest
julia> Z = ZeroSet(2);
julia> ∈([1.0, 0.0], Z)
false
julia> ∈([0.0, 0.0], Z)
true
```
"""
function ∈(x::AbstractVector{N}, Z::ZeroSet{N})::Bool where {N<:Real}
@assert length(x) == dim(Z)
zero_N = zero(N)
return all(i -> x[i] == zero_N, eachindex(x))
end
"""
rand(::Type{ZeroSet}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing
)::ZeroSet{N}
Create a zero set (note that there is nothing to randomize).
### Input
- `ZeroSet` -- 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
The (only) zero set of the given numeric type and dimension.
"""
function rand(::Type{ZeroSet};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int, Nothing}=nothing
)::ZeroSet{N}
rng = reseed(rng, seed)
return ZeroSet{N}(dim)
end
"""
linear_map(M::AbstractMatrix{N}, Z::ZeroSet{N}) where {N<:Real}
Concrete linear map of a zero set.
### Input
- `M` -- matrix
- `Z` -- zero set
### Output
The zero set whose dimension matches the output dimension of the given matrix.
"""
function linear_map(M::AbstractMatrix{N}, Z::ZeroSet{N}) where {N<:Real}
@assert dim(Z) == size(M, 2) "a linear map of size $(size(M)) cannot be " *
"applied to a set of dimension $(dim(Z))"
return ZeroSet(size(M, 1))
end
"""
translate(Z::ZeroSet{N}, v::AbstractVector{N}) where {N<:Real}
Translate (i.e., shift) a zero set by a given vector.
### Input
- `Z` -- zero set
- `v` -- translation vector
### Output
A singleton containing the vector `v`.
"""
function translate(Z::ZeroSet{N}, v::AbstractVector{N}) where {N<:Real}
@assert length(v) == dim(Z) "cannot translate a $(dim(Z))-dimensional " *
"set by a $(length(v))-dimensional vector"
return Singleton(v)
end