-
Notifications
You must be signed in to change notification settings - Fork 32
/
Line.jl
515 lines (368 loc) · 10.9 KB
/
Line.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
export Line
"""
Line{N, VN<:AbstractVector{N}} <: AbstractPolyhedron{N}
Type that represents a line of the form
```math
\\{y ∈ ℝ^n: y = p + λd, λ ∈ ℝ\\}
```
where ``p`` is a point on the line and ``d`` is its direction vector (not
necessarily normalized).
### Fields
- `p` -- point on the line
- `d` -- direction
### Examples
There are three constructors. The optional keyword argument `normalize`
(default: `false`) can be used to normalize the direction of the resulting line
to have norm 1 (w.r.t. the Euclidean norm).
1) The default constructor takes the fields `p` and `d`:
The line passing through the point ``[-1, 2, 3]`` and parallel to the vector
``[3, 0, -1]``:
```jldoctest
julia> Line([-1.0, 2, 3], [3.0, 0, -1])
Line{Float64, Vector{Float64}}([-1.0, 2.0, 3.0], [3.0, 0.0, -1.0])
julia> Line([-1.0, 2, 3], [3.0, 0, -1]; normalize=true)
Line{Float64, Vector{Float64}}([-1.0, 2.0, 3.0], [0.9486832980505138, 0.0, -0.31622776601683794])
```
2) The second constructor takes two points, `from` and `to`, as keyword
arguments, and returns the line through them. See the algorithm section for
details.
```jldoctest
julia> Line(from=[-1.0, 2, 3], to=[-4.0, 2, 4])
Line{Float64, Vector{Float64}}([-1.0, 2.0, 3.0], [3.0, 0.0, -1.0])
```
3) The third constructor resembles `Line2D` and only works for two-dimensional
lines. It takes two inputs, `a` and `b`, and constructs the line such that
``a ⋅ x = b``.
```jldoctest
julia> Line([2.0, 0], 1.)
Line{Float64, Vector{Float64}}([0.5, 0.0], [0.0, -1.0])
```
### Algorithm
Given two points ``p ∈ ℝ^n`` and ``q ∈ ℝ^n``, the line that
passes through these two points is
`L: `\\{y ∈ ℝ^n: y = p + λ(q - p), λ ∈ ℝ\\}``.
"""
struct Line{N,VN<:AbstractVector{N}} <: AbstractPolyhedron{N}
p::VN
d::VN
# default constructor with length constraint
function Line(p::VN, d::VN; check_direction::Bool=true,
normalize=false) where {N,VN<:AbstractVector{N}}
if check_direction
@assert !iszero(d) "a line needs a non-zero direction vector"
end
d_n = normalize ? LinearAlgebra.normalize(d) : d
return new{N,VN}(p, d_n)
end
end
function Line(; from::AbstractVector, to::AbstractVector, normalize=false)
d = from - to
@assert !iszero(d) "points `$from` and `$to` should be distinct"
return Line(from, d; normalize=normalize)
end
function Line(a::AbstractVector{N}, b::N; normalize=false) where {N}
@assert length(a) == 2 "expected a normal vector of length two, but it " *
"is $(length(a))-dimensional"
got_horizontal = iszero(a[1])
got_vertical = iszero(a[2])
if got_horizontal && got_vertical
throw(ArgumentError("the vector $a must be non-zero"))
end
if got_horizontal
α = b / a[2]
p = [zero(N), α]
q = [one(N), α]
elseif got_vertical
β = b / a[1]
p = [β, zero(N)]
q = [β, one(N)]
else
α = b / a[2]
μ = a[1] / a[2]
p = [zero(N), α]
q = [one(N), α - μ]
end
return Line(; from=p, to=q, normalize=normalize)
end
isoperationtype(::Type{<:Line}) = false
"""
direction(L::Line)
Return the direction of the line.
### Input
- `L` -- line
### Output
The direction of the line.
### Notes
The direction is not necessarily normalized.
See [`normalize(::Line, ::Real)`](@ref) / [`normalize!(::Line, ::Real)`](@ref).
"""
direction(L::Line) = L.d
"""
normalize!(L::Line{N}, p::Real=N(2)) where {N}
Normalize the direction of a line storing the result in `L`.
### Input
- `L` -- line
- `p` -- (optional, default: `2.0`) vector `p`-norm used in the normalization
### Output
A line whose direction has unit norm w.r.t. the given `p`-norm.
"""
function normalize!(L::Line{N}, p::Real=N(2)) where {N}
normalize!(L.d, p)
return L
end
"""
normalize(L::Line{N}, p::Real=N(2)) where {N}
Normalize the direction of a line.
### Input
- `L` -- line
- `p` -- (optional, default: `2.0`) vector `p`-norm used in the normalization
### Output
A line whose direction has unit norm w.r.t. the given `p`-norm.
### Notes
See also [`normalize!(::Line, ::Real)`](@ref) for the in-place version.
"""
function normalize(L::Line{N}, p::Real=N(2)) where {N}
return normalize!(copy(L), p)
end
"""
constraints_list(L::Line)
Return the list of constraints of a line.
### Input
- `L` -- line
### Output
A list containing `2n-2` half-spaces whose intersection is `L`, where `n` is the
ambient dimension of `L`.
"""
function constraints_list(L::Line)
p = L.p
n = length(p)
d = reshape(L.d, 1, n)
K = nullspace(d)
m = size(K, 2)
@assert m == n - 1 "expected $(n - 1) normal half-spaces, got $m"
N, VN = _parameters(L)
out = Vector{HalfSpace{N,VN}}(undef, 2m)
idx = 1
@inbounds for j in 1:m
Kj = K[:, j]
b = dot(Kj, p)
out[idx] = HalfSpace(Kj, b)
out[idx + 1] = HalfSpace(-Kj, -b)
idx += 2
end
return out
end
function _parameters(::Line{N,VN}) where {N,VN}
return (N, VN)
end
"""
dim(L::Line)
Return the ambient dimension of a line.
### Input
- `L` -- line
### Output
The ambient dimension of the line.
"""
dim(L::Line) = length(L.p)
"""
ρ(d::AbstractVector, L::Line)
Evaluate the support function of a line in a given direction.
### Input
- `d` -- direction
- `L` -- line
### Output
Evaluation of the support function in the given direction.
"""
function ρ(d::AbstractVector, L::Line)
if isapproxzero(dot(d, L.d))
return dot(d, L.p)
else
N = eltype(L)
return N(Inf)
end
end
"""
σ(d::AbstractVector, L::Line)
Return a support vector of a line in a given direction.
### Input
- `d` -- direction
- `L` -- line
### Output
A support vector in the given direction.
"""
function σ(d::AbstractVector, L::Line)
if isapproxzero(dot(d, L.d))
return L.p
else
throw(ArgumentError("the support vector is undefined because the " *
"line is unbounded in the given direction"))
end
end
"""
isbounded(L::Line)
Determine whether a line is bounded.
### Input
- `L` -- line
### Output
`false`.
"""
isbounded(::Line) = false
"""
isuniversal(L::Line; [witness::Bool]=false)
Check whether a line is universal.
### Input
- `P` -- line
- `witness` -- (optional, default: `false`) compute a witness if activated
### Output
* If `witness` is `false`: `true` if the ambient dimension is one, `false`
otherwise.
* If `witness` is `true`: `(true, [])` if the ambient dimension is one,
`(false, v)` where ``v ∉ P`` otherwise.
"""
isuniversal(L::Line; witness::Bool=false) = isuniversal(L, Val(witness))
# TODO implement case with witness
isuniversal(L::Line, ::Val{false}) = dim(L) == 1
"""
an_element(L::Line)
Return some element of a line.
### Input
- `L` -- line
### Output
An element on the line.
"""
an_element(L::Line) = L.p
"""
∈(x::AbstractVector, L::Line)
Check whether a given point is contained in a line.
### Input
- `x` -- point/vector
- `L` -- line
### Output
`true` iff `x ∈ L`.
### Algorithm
The point ``x`` belongs to the line ``L : p + λd`` if and only if ``x - p`` is
proportional to the direction ``d``.
"""
function ∈(x::AbstractVector, L::Line)
@assert length(x) == dim(L) "expected the point and the line to have the " *
"same dimension, but they are $(length(x)) and $(dim(L)) respectively"
# the following check is necessary because the zero vector is a special case
_isapprox(x, L.p) && return true
return first(ismultiple(x - L.p, L.d))
end
"""
rand(::Type{Line}; [N]::Type{<:Real}=Float64, [dim]::Int=2,
[rng]::AbstractRNG=GLOBAL_RNG, [seed]::Union{Int, Nothing}=nothing)
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.
"""
function rand(::Type{Line};
N::Type{<:Real}=Float64,
dim::Int=2,
rng::AbstractRNG=GLOBAL_RNG,
seed::Union{Int,Nothing}=nothing)
rng = reseed!(rng, seed)
d = randn(rng, N, dim)
while iszero(d)
d = randn(rng, N, dim)
end
p = randn(rng, N, dim)
return Line(p, d)
end
"""
isempty(L::Line)
Check whether a line is empty.
### Input
- `L` -- line
### Output
`false`.
"""
isempty(::Line) = false
"""
translate(L::Line, v::AbstractVector)
Translate (i.e., shift) a line by a given vector.
### Input
- `L` -- line
- `v` -- translation vector
### Output
A translated line.
### Notes
See also `translate!` for the in-place version.
"""
function translate(L::Line, v::AbstractVector)
return translate!(copy(L), v)
end
"""
translate!(L::Line, v::AbstractVector)
Translate (i.e., shift) a line by a given vector, in-place.
### Input
- `L` -- line
- `v` -- translation vector
### Output
The line `L` translated by `v`.
"""
function translate!(L::Line, v::AbstractVector)
@assert length(v) == dim(L) "cannot translate a $(dim(L))-dimensional " *
"set by a $(length(v))-dimensional vector"
L.p .+= v
return L
end
"""
distance(x::AbstractVector, L::Line; [p]::Real=2.0)
Compute the distance between point `x` and the line with respect to the given
`p`-norm.
### Input
- `x` -- point/vector
- `L` -- line
- `p` -- (optional, default: `2.0`) the `p`-norm used; `p = 2.0` corresponds to
the usual Euclidean norm
### Output
A scalar representing the distance between `x` and the line `L`.
"""
@commutative function distance(x::AbstractVector, L::Line; p::Real=2.0)
d = L.d # direction of the line
t = dot(x - L.p, d) / dot(d, d)
return distance(x, L.p + t * d; p=p)
end
"""
linear_map(M::AbstractMatrix, L::Line)
Concrete linear map of a line.
### Input
- `M` -- matrix
- `L` -- line
### Output
The line obtained by applying the linear map, if that still results in a line,
or a `Singleton` otherwise.
### Algorithm
We apply the linear map to the point and direction of `L`.
If the resulting direction is zero, the result is a singleton.
"""
function linear_map(M::AbstractMatrix, L::Line)
@assert dim(L) == size(M, 2) "a linear map of size $(size(M)) cannot be " *
"applied to a set of dimension $(dim(L))"
Mp = M * L.p
Md = M * L.d
if iszero(Md)
return Singleton(Mp)
end
return Line(Mp, Md)
end
function project(L::Line{N}, block::AbstractVector{Int}; kwargs...) where {N}
d = L.d[block]
if iszero(d)
return Singleton(L.p[block]) # projected out all nontrivial dimensions
elseif length(d) == 1
return Universe{N}(1) # special case: 1D line is a universe
else
return Line(L.p[block], d)
end
end