-
Notifications
You must be signed in to change notification settings - Fork 32
/
ZeroSet.jl
234 lines (162 loc) · 4.3 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
export ZeroSet
"""
ZeroSet{N} <: AbstractSingleton{N}
Type that represents the zero set, i.e., the set that only contains the origin.
### Fields
- `dim` -- the ambient dimension of the set
"""
struct ZeroSet{N} <: AbstractSingleton{N}
dim::Int
end
isoperationtype(::Type{<:ZeroSet}) = false
# default constructor of type Float64
ZeroSet(dim::Int) = ZeroSet{Float64}(dim)
"""
element(Z::ZeroSet{N}) where {N}
Return the element of a zero set.
### Input
- `Z` -- zero set
### Output
The element of the zero set, i.e., a zero vector.
"""
function element(Z::ZeroSet{N}) where {N}
return zeros(N, Z.dim)
end
"""
element(Z::ZeroSet{N}, ::Int) where {N}
Return the i-th entry of the element of a zero set.
### Input
- `Z` -- zero set
- `i` -- dimension
### Output
The i-th entry of the element of the zero set, i.e., 0.
"""
function element(Z::ZeroSet{N}, ::Int) where {N}
return zero(N)
end
"""
dim(Z::ZeroSet)
Return the ambient dimension of a zero set.
### Input
- `Z` -- zero set
### Output
The ambient dimension of the zero set.
"""
function dim(Z::ZeroSet)
return Z.dim
end
"""
ρ(d::AbstractVector, Z::ZeroSet)
Evaluate the support function of a zero set in a given direction.
### Input
- `d` -- direction
- `Z` -- zero set
### Output
`0`.
"""
function ρ(d::AbstractVector, Z::ZeroSet)
@assert length(d) == dim(Z) "a $(length(d))-dimensional vector is " *
"incompatible with a $(dim(Z))-dimensional set"
N = promote_type(eltype(d), eltype(Z))
return zero(N)
end
"""
∈(x::AbstractVector, Z::ZeroSet)
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, Z::ZeroSet)
@assert length(x) == dim(Z) "a $(length(x))-dimensional vector is " *
"incompatible with a $(dim(Z))-dimensional set"
return iszero(x)
end
"""
rand(::Type{ZeroSet}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing)
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)
rng = reseed!(rng, seed)
return ZeroSet{N}(dim)
end
"""
linear_map(M::AbstractMatrix, Z::ZeroSet)
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, Z::ZeroSet)
@assert dim(Z) == size(M, 2) "a linear map of size $(size(M)) cannot be " *
"applied to a set of dimension $(dim(Z))"
N = promote_type(eltype(M), eltype(Z))
return ZeroSet{N}(size(M, 1))
end
"""
translate(Z::ZeroSet, v::AbstractVector)
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, v::AbstractVector)
@assert length(v) == dim(Z) "cannot translate a $(dim(Z))-dimensional " *
"set by a $(length(v))-dimensional vector"
return Singleton(v)
end
"""
rectify(Z::ZeroSet)
Concrete rectification of a zero set.
### Input
- `Z` -- zero set
### Output
The same set.
"""
function rectify(Z::ZeroSet)
return Z
end
"""
reflect(Z::ZeroSet)
Concrete reflection of a zero set `Z`, resulting in the reflected set `-Z`.
### Input
- `Z` -- zero set
### Output
The same zero set.
"""
function reflect(Z::ZeroSet)
return Z
end
function scale(::Real, Z::ZeroSet)
return Z
end
function scale!(::Real, Z::ZeroSet)
return Z
end