/
LinearMap.jl
504 lines (364 loc) · 12.2 KB
/
LinearMap.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
import Base: *, ∈, isempty
export LinearMap,
an_element,
constraints_list,
Projection
"""
LinearMap{N, S<:LazySet{N}, NM, MAT<:AbstractMatrix{NM}}
<: AbstractAffineMap{N, S}
Type that represents a linear transformation ``M⋅X`` of a set ``X``.
### Fields
- `M` -- matrix/linear map
- `X` -- set
### Notes
This type is parametric in the elements of the linear map, `NM`, which is
independent of the numeric type of the wrapped set (`N`).
Typically `NM = N`, but there may be exceptions, e.g., if `NM` is an interval
that holds numbers of type `N`, where `N` is a floating point number type such
as `Float64`.
The linear map preserves convexity: if `X` is convex, then any linear map of `X`
is convex as well.
### Examples
For the examples we create a ``3×2`` matrix and a two-dimensional unit square.
```jldoctest constructors
julia> M = [1 2; 1 3; 1 4]; X = BallInf([0, 0], 1);
```
The function ``*`` can be used as an alias to construct a `LinearMap` object.
```jldoctest constructors
julia> lm = LinearMap(M, X)
LinearMap{Int64, BallInf{Int64, Vector{Int64}}, Int64, Matrix{Int64}}([1 2; 1 3; 1 4], BallInf{Int64, Vector{Int64}}([0, 0], 1))
julia> lm2 = M * X
LinearMap{Int64, BallInf{Int64, Vector{Int64}}, Int64, Matrix{Int64}}([1 2; 1 3; 1 4], BallInf{Int64, Vector{Int64}}([0, 0], 1))
julia> lm == lm2
true
```
For convenience, `M` does not need to be a matrix; we also allow to use vectors
(interpreted as an ``n×1`` matrix) and `UniformScaling`s resp. scalars
(interpreted as a scaling, i.e., a scaled identity matrix).
Scaling by ``1`` is ignored.
```jldoctest constructors
julia> using LinearAlgebra: I
julia> Y = BallInf([0], 1); # one-dimensional interval
julia> [2, 3] * Y
LinearMap{Int64, BallInf{Int64, Vector{Int64}}, Int64, Matrix{Int64}}([2; 3;;], BallInf{Int64, Vector{Int64}}([0], 1))
julia> lm3 = 2 * X
LinearMap{Int64, BallInf{Int64, Vector{Int64}}, Int64, SparseArrays.SparseMatrixCSC{Int64, Int64}}(sparse([1, 2], [1, 2], [2, 2], 2, 2), BallInf{Int64, Vector{Int64}}([0, 0], 1))
julia> 2I * X == lm3
true
julia> 1I * X == X
true
```
Applying a linear map to a `LinearMap` object combines the two maps into a
single `LinearMap` instance.
Again we can make use of the conversion for convenience.
```jldoctest constructors
julia> B = transpose(M); B * lm
LinearMap{Int64, BallInf{Int64, Vector{Int64}}, Int64, Matrix{Int64}}([3 9; 9 29], BallInf{Int64, Vector{Int64}}([0, 0], 1))
julia> B = [3, 4, 5]; B * lm
LinearMap{Int64, BallInf{Int64, Vector{Int64}}, Int64, Matrix{Int64}}([12 38], BallInf{Int64, Vector{Int64}}([0, 0], 1))
julia> B = 2; B * lm
LinearMap{Int64, BallInf{Int64, Vector{Int64}}, Int64, Matrix{Int64}}([2 4; 2 6; 2 8], BallInf{Int64, Vector{Int64}}([0, 0], 1))
```
The application of a `LinearMap` to a `ZeroSet` or an `EmptySet` is simplified
automatically.
```jldoctest constructors
julia> M * ZeroSet{Int}(2)
ZeroSet{Int64}(3)
julia> M * EmptySet{Int}(2)
EmptySet{Int64}(3)
```
"""
struct LinearMap{N,S<:LazySet{N},NM,
MAT<:AbstractMatrix{NM}} <: AbstractAffineMap{N,S}
M::MAT
X::S
# default constructor with dimension check
function LinearMap(M::MAT, X::S) where {N,S<:LazySet{N},NM,
MAT<:AbstractMatrix{NM}}
@assert dim(X) == size(M, 2) "a linear map of size $(size(M)) cannot " *
"be applied to a set of dimension $(dim(X))"
return new{N,S,NM,MAT}(M, X)
end
end
isoperationtype(::Type{<:LinearMap}) = true
isconvextype(::Type{<:LinearMap{N,S}}) where {N,S} = isconvextype(S)
"""
```
*(M::Union{AbstractMatrix, UniformScaling, AbstractVector, Real},
X::LazySet)
```
Alias to create a `LinearMap` object.
### Input
- `M` -- linear map
- `X` -- set
### Output
A lazy linear map, i.e., a `LinearMap` instance.
"""
function *(M::Union{AbstractMatrix,UniformScaling,AbstractVector,Real},
X::LazySet)
return LinearMap(M, X)
end
# scaling from the right
function *(X::LazySet, M::Real)
return LinearMap(M, X)
end
# convenience constructor from a vector
function LinearMap(v::AbstractVector, X::LazySet)
return _LinearMap_vector(v, X)
end
function _LinearMap_vector(v, X)
n = dim(X)
m = length(v)
if n == m
M = reshape(v, 1, length(v))
else
M = reshape(v, length(v), 1)
end
return LinearMap(M, X)
end
# convenience constructor from a UniformScaling
function LinearMap(M::UniformScaling, X::LazySet)
if isone(M.λ)
return X
end
return LinearMap(Diagonal(fill(M.λ, dim(X))), X)
end
# convenience constructor from a scalar
function LinearMap(α::Real, X::LazySet)
n = dim(X)
return LinearMap(sparse(α * I, n, n), X)
end
# combine two linear maps into a single linear map
function LinearMap(M::AbstractMatrix, lm::LinearMap)
return LinearMap(M * lm.M, lm.X)
end
# disambiguation
function LinearMap(v::AbstractVector, lm::LinearMap)
return _LinearMap_vector(v, lm)
end
# more efficient versions when combining `LinearMap`s
function LinearMap(M::UniformScaling, lm::LinearMap)
if isone(M.λ)
return lm
end
return LinearMap(M.λ * lm.M, lm.X)
end
function LinearMap(α::Real, lm::LinearMap)
return LinearMap(α * lm.M, lm.X)
end
# ZeroSet is "almost absorbing" for LinearMap (only the dimension changes)
function LinearMap(M::AbstractMatrix, Z::ZeroSet)
N = promote_type(eltype(M), eltype(Z))
@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{N}(size(M, 1))
end
# EmptySet is "almost absorbing" for LinearMap (only the dimension changes)
function LinearMap(M::AbstractMatrix, ∅::EmptySet)
N = promote_type(eltype(M), eltype(∅))
@assert dim(∅) == size(M, 2) "a linear map of size $(size(M)) cannot " *
"be applied to a set of dimension $(dim(∅))"
return EmptySet{N}(size(M, 1))
end
function matrix(lm::LinearMap)
return lm.M
end
function vector(lm::LinearMap{N}) where {N}
return spzeros(N, dim(lm))
end
function set(lm::LinearMap)
return lm.X
end
"""
dim(lm::LinearMap)
Return the dimension of a linear map.
### Input
- `lm` -- linear map
### Output
The ambient dimension of the linear map.
"""
function dim(lm::LinearMap)
return size(lm.M, 1)
end
"""
σ(d::AbstractVector, lm::LinearMap)
Return a support vector of the linear map.
### Input
- `d` -- direction
- `lm` -- linear map
### Output
A support vector in the given direction.
If the direction has norm zero, the result depends on the wrapped set.
### Notes
If ``L = M⋅S``, where ``M`` is a matrix and ``S`` is a set, it follows that
``σ(d, L) = M⋅σ(M^T d, S)`` for any direction ``d``.
"""
function σ(d::AbstractVector, lm::LinearMap)
return _σ_linear_map(d, lm.M, lm.X)
end
function _σ_linear_map(d::AbstractVector, M::AbstractMatrix, X::LazySet)
return M * σ(At_mul_B(M, d), X)
end
"""
ρ(d::AbstractVector, lm::LinearMap; kwargs...)
Evaluate the support function of the linear map.
### Input
- `d` -- direction
- `lm` -- linear map
- `kwargs` -- additional arguments that are passed to the support function
algorithm
### Output
The evaluation of the support function in the given direction.
If the direction has norm zero, the result depends on the wrapped set.
### Notes
If ``L = M⋅S``, where ``M`` is a matrix and ``S`` is a set, it follows that
``ρ(d, L) = ρ(M^T d, S)`` for any direction ``d``.
"""
function ρ(d::AbstractVector, lm::LinearMap; kwargs...)
return _ρ_linear_map(d, lm.M, lm.X; kwargs...)
end
function _ρ_linear_map(d::AbstractVector, M::AbstractMatrix, X::LazySet;
kwargs...)
return ρ(At_mul_B(M, d), X; kwargs...)
end
"""
∈(x::AbstractVector, lm::LinearMap)
Check whether a given point is contained in a linear map.
### Input
- `x` -- point/vector
- `lm` -- linear map
### Output
`true` iff ``x ∈ lm``.
### Algorithm
Note that ``x ∈ M⋅S`` iff ``M^{-1}⋅x ∈ S``.
This implementation does not explicitly invert the matrix: instead of
``M^{-1}⋅x`` it computes ``M \\ x``.
Hence it also works for non-square matrices.
### Examples
```jldoctest
julia> lm = LinearMap([2.0 0.0; 0.0 1.0], BallInf([1., 1.], 1.));
julia> [5.0, 1.0] ∈ lm
false
julia> [3.0, 1.0] ∈ lm
true
```
An example with non-square matrix:
```jldoctest
julia> B = BallInf(zeros(4), 1.);
julia> M = [1. 0 0 0; 0 1 0 0]/2;
julia> [0.5, 0.5] ∈ M*B
true
```
"""
function ∈(x::AbstractVector, lm::LinearMap)
if !iswellconditioned(matrix(lm))
# ill-conditioned matrix; use concrete set representation
return x ∈ linear_map(matrix(lm), set(lm))
end
return matrix(lm) \ x ∈ set(lm)
end
"""
an_element(lm::LinearMap)
Return some element of a linear map.
### Input
- `lm` -- linear map
### Output
An element in the linear map.
It relies on the `an_element` function of the wrapped set.
"""
function an_element(lm::LinearMap)
return lm.M * an_element(lm.X)
end
function isboundedtype(::Type{<:LinearMap{N,S}}) where {N,S}
return isboundedtype(S)
end
"""
vertices_list(lm::LinearMap; prune::Bool=true)
Return the list of vertices of a (polytopic) linear map.
### Input
- `lm` -- linear map
- `prune` -- (optional, default: `true`) if `true`, we remove redundant vertices
### Output
A list of vertices.
### Algorithm
We assume that the underlying set `X` is polytopic and compute the vertices of
`X`. The result is just the linear map applied to each vertex.
"""
function vertices_list(lm::LinearMap; prune::Bool=true)
# apply the linear map to each vertex
vlist = broadcast(x -> lm.M * x, vertices(lm.X))
return prune ? convex_hull(vlist) : vlist
end
"""
constraints_list(lm::LinearMap)
Return the list of constraints of a (polyhedral) linear map.
### Input
- `lm` -- linear map
### Output
The list of constraints of the linear map.
### Notes
We assume that the underlying set `X` is polyhedral, i.e., offers a method
`constraints_list(X)`.
### Algorithm
We fall back to a concrete set representation by applying `linear_map`.
"""
function constraints_list(lm::LinearMap)
return constraints_list(linear_map(lm.M, lm.X))
end
"""
linear_map(M::AbstractMatrix, lm::LinearMap)
Return the linear map of a lazy linear map.
### Input
- `M` -- matrix
- `lm` -- linear map
### Output
A set representing the linear map.
"""
function linear_map(M::AbstractMatrix, lm::LinearMap)
return linear_map(M * lm.M, lm.X)
end
function concretize(lm::LinearMap)
return linear_map(lm.M, concretize(lm.X))
end
"""
Projection(X::LazySet{N}, variables::AbstractVector{Int}) where {N}
Return a lazy projection of a set.
### Input
- `X` -- set
- `variables` -- variables of interest
### Output
A lazy `LinearMap` that corresponds to projecting `X` along the given variables
`variables`.
### Examples
The projection of a three-dimensional cube into the first two coordinates:
```jldoctest Projection
julia> B = BallInf([1.0, 2, 3], 1.0)
BallInf{Float64, Vector{Float64}}([1.0, 2.0, 3.0], 1.0)
julia> Bproj = Projection(B, [1, 2])
LinearMap{Float64, BallInf{Float64, Vector{Float64}}, Float64, SparseArrays.SparseMatrixCSC{Float64, Int64}}(sparse([1, 2], [1, 2], [1.0, 1.0], 2, 3), BallInf{Float64, Vector{Float64}}([1.0, 2.0, 3.0], 1.0))
julia> isequivalent(Bproj, BallInf([1.0, 2], 1.0))
true
```
"""
function Projection(X::LazySet{N}, variables::AbstractVector{Int}) where {N}
M = projection_matrix(variables, dim(X), N)
return LinearMap(M, X)
end
"""
project(S::LazySet{N}, block::AbstractVector{Int}, set_type::Type{LM},
[n]::Int=dim(S); [kwargs...]) where {N, LM<:LinearMap}
Project a high-dimensional set to a given block by using a lazy linear map.
### Input
- `S` -- set
- `block` -- block structure - a vector with the dimensions of interest
- `LinearMap` -- used for dispatch
- `n` -- (optional, default: `dim(S)`) ambient dimension of the set `S`
### Output
A lazy `LinearMap` representing the projection of the set `S` to block `block`.
"""
@inline function project(S::LazySet{N}, block::AbstractVector{Int},
set_type::Type{LM}, n::Int=dim(S);
kwargs...) where {N,LM<:LinearMap}
M = projection_matrix(block, n, N)
return M * S
end