-
Notifications
You must be signed in to change notification settings - Fork 32
/
CartesianProduct.jl
519 lines (384 loc) · 12.5 KB
/
CartesianProduct.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
516
517
518
519
import Base: *
export CartesianProduct,
CartesianProduct!,
swap
"""
CartesianProduct{N, S1<:LazySet{N}, S2<:LazySet{N}} <: LazySet{N}
Type that represents the Cartesian product of two sets, i.e., the set
```math
Z = \\{ z ∈ ℝ^{n + m} : z = (x, y),\\qquad x ∈ X, y ∈ Y \\}.
```
If ``X ⊆ ℝ^n`` and ``Y ⊆ ℝ^m``, then ``Z`` is
``n+m``-dimensional.
### Fields
- `X` -- first set
- `Y` -- second set
### Notes
See also [`CartesianProductArray`](@ref) for an implementation of a Cartesian
product of more than two sets.
The `EmptySet` is the almost absorbing element for `CartesianProduct` (except
that the dimension is adapted).
The Cartesian product preserves convexity: if the set arguments are convex, then
their Cartesian product is convex as well.
In some docstrings the word "block" is used to denote each wrapped set, with the
natural order, i.e. we say that the first block of a Cartesian product `cp` is
`cp.X` and the second block is `cp.Y`.
### Examples
The Cartesian product of two sets `X` and `Y` can be constructed either using
`CartesianProduct(X, Y)` or the short-cut notation `X × Y` (to enter the *times*
symbol, write `\\times<tab>`).
```jldoctest cartesianproduct_constructor
julia> I1 = Interval(0, 1);
julia> I2 = Interval(2, 4);
julia> I12 = I1 × I2;
julia> typeof(I12)
CartesianProduct{Float64, Interval{Float64}, Interval{Float64}}
```
A hyperrectangle is the Cartesian product of intervals, so we can convert `I12`
to a `Hyperrectangle` type:
```jldoctest cartesianproduct_constructor
julia> convert(Hyperrectangle, I12)
Hyperrectangle{Float64, Vector{Float64}, Vector{Float64}}([0.5, 3.0], [0.5, 1.0])
```
"""
struct CartesianProduct{N,S1<:LazySet{N},S2<:LazySet{N}} <: LazySet{N}
X::S1
Y::S2
function CartesianProduct(X::LazySet{N}, Y::LazySet{N}) where {N}
return new{N,typeof(X),typeof(Y)}(X, Y)
end
end
isoperationtype(::Type{<:CartesianProduct}) = true
concrete_function(::Type{<:CartesianProduct}) = cartesian_product
function isconvextype(::Type{CartesianProduct{N,S1,S2}}) where {N,S1,S2}
return isconvextype(S1) && isconvextype(S2)
end
is_polyhedral(cp::CartesianProduct) = is_polyhedral(cp.X) && is_polyhedral(cp.Y)
# EmptySet is almost the absorbing element for CartesianProduct, but it should
# sum up the dimension of both set arguments
@commutative function CartesianProduct(X::LazySet, ∅::EmptySet)
N = promote_type(eltype(X), eltype(∅))
return EmptySet{N}(dim(X) + dim(∅))
end
function CartesianProduct(∅1::EmptySet, ∅2::EmptySet)
N = promote_type(eltype(∅1), eltype(∅2))
return EmptySet{N}(dim(∅1) + dim(∅2))
end
# interface for binary set operations
Base.first(cp::CartesianProduct) = cp.X
second(cp::CartesianProduct) = cp.Y
@declare_binary_operation(CartesianProduct)
"""
```
*(X::LazySet, Y::LazySet)
```
Alias for the binary Cartesian product.
"""
*(X::LazySet, Y::LazySet) = CartesianProduct(X, Y)
"""
×(X::LazySet, Y::LazySet)
Alias for the binary Cartesian product.
### Notes
The function symbol can be typed via `\\times<tab>`.
"""
×(X::LazySet, Y::LazySet) = CartesianProduct(X, Y)
"""
swap(cp::CartesianProduct)
Return a new `CartesianProduct` object with the arguments swapped.
### Input
- `cp` -- Cartesian product
### Output
A new `CartesianProduct` object with the arguments swapped.
"""
function swap(cp::CartesianProduct)
return CartesianProduct(cp.Y, cp.X)
end
"""
dim(cp::CartesianProduct)
Return the dimension of a Cartesian product.
### Input
- `cp` -- Cartesian product
### Output
The ambient dimension of the Cartesian product.
"""
function dim(cp::CartesianProduct)
return dim(cp.X) + dim(cp.Y)
end
"""
σ(d::AbstractVector, cp::CartesianProduct)
Return a support vector of a Cartesian product.
### Input
- `d` -- direction
- `cp` -- Cartesian product
### Output
A support vector in the given direction.
If the direction has norm zero, the result depends on the wrapped sets.
"""
function σ(d::AbstractVector, cp::CartesianProduct)
n1 = dim(cp.X)
return [σ(d[1:n1], cp.X); σ(d[(n1 + 1):length(d)], cp.Y)]
end
# faster version for single-entry vectors
function σ(d::SingleEntryVector, cp::CartesianProduct)
n1 = dim(cp.X)
idx = d.i
if idx <= n1
return [σ(SingleEntryVector(idx, n1, d.v), cp.X); an_element(cp.Y)]
else
n2 = length(d) - n1
return [an_element(cp.X); σ(SingleEntryVector(idx - n1, n2, d.v), cp.Y)]
end
end
"""
ρ(d::AbstractVector, cp::CartesianProduct)
Evaluate the support function of a Cartesian product.
### Input
- `d` -- direction
- `cp` -- Cartesian product
### Output
The evaluation of the support function in the given direction.
If the direction has norm zero, the result depends on the wrapped sets.
"""
function ρ(d::AbstractVector, cp::CartesianProduct)
n1 = dim(cp.X)
return ρ(d[1:n1], cp.X) + ρ(d[(n1 + 1):length(d)], cp.Y)
end
# faster version for single-entry vectors
function ρ(d::SingleEntryVector, cp::CartesianProduct)
n1 = dim(cp.X)
idx = d.i
if idx <= n1
return ρ(SingleEntryVector(idx, n1, d.v), cp.X)
else
n2 = length(d) - n1
return ρ(SingleEntryVector(idx - n1, n2, d.v), cp.Y)
end
end
"""
isbounded(cp::CartesianProduct)
Check whether a Cartesian product is bounded.
### Input
- `cp` -- Cartesian product
### Output
`true` iff both wrapped sets are bounded.
"""
function isbounded(cp::CartesianProduct)
return isbounded(cp.X) && isbounded(cp.Y)
end
function isboundedtype(::Type{<:CartesianProduct{N,S1,S2}}) where {N,S1,S2}
return isboundedtype(S1) && isboundedtype(S2)
end
"""
∈(x::AbstractVector, cp::CartesianProduct)
Check whether a given point is contained in a Cartesian product.
### Input
- `x` -- point/vector
- `cp` -- Cartesian product
### Output
`true` iff ``x ∈ cp``.
"""
function ∈(x::AbstractVector, cp::CartesianProduct)
@assert length(x) == dim(cp)
n1 = dim(cp.X)
return view(x, 1:n1) ∈ cp.X &&
view(x, (n1 + 1):length(x)) ∈ cp.Y
end
"""
isempty(cp::CartesianProduct)
Check whether a Cartesian product is empty.
### Input
- `cp` -- Cartesian product
### Output
`true` iff any of the sub-blocks is empty.
"""
function isempty(cp::CartesianProduct)
return isempty(cp.X) || isempty(cp.Y)
end
"""
center(cp::CartesianProduct)
Return the center of a Cartesian product of centrally-symmetric sets.
### Input
- `cp` -- Cartesian product of centrally-symmetric sets
### Output
The center of the Cartesian product.
"""
function center(cp::CartesianProduct)
return vcat(center(cp.X), center(cp.Y))
end
"""
constraints_list(cp::CartesianProduct)
Return the list of constraints of a (polyhedral) Cartesian product.
### Input
- `cp` -- polyhedral Cartesian product
### Output
A list of constraints.
"""
function constraints_list(cp::CartesianProduct)
return _constraints_list_cartesian_product(cp)
end
"""
vertices_list(cp::CartesianProduct)
Return the list of vertices of a (polytopic) Cartesian product.
### Input
- `cp` -- polytopic Cartesian product
### Output
A list of vertices.
### Algorithm
We assume that the underlying sets are polytopic.
Then the high-dimensional set of vertices is just the Cartesian product of the
low-dimensional sets of vertices.
"""
function vertices_list(cp::CartesianProduct)
# collect low-dimensional vertices lists
vlist1 = vertices_list(cp.X)
vlist2 = vertices_list(cp.Y)
# create high-dimensional vertices list
N = eltype(cp)
vlist = Vector{Vector{N}}()
m = length(vlist1) * length(vlist2)
sizehint!(vlist, m)
for v1 in vlist1
for v2 in vlist2
push!(vlist, vcat(v1, v2))
end
end
return vlist
end
"""
linear_map(M::AbstractMatrix, cp::CartesianProduct)
Concrete linear map of a (polyhedral) Cartesian product.
### Input
- `M` -- matrix
- `cp` -- Cartesian product
### Output
A polytope if `cp` is bounded and a polyhedron otherwise.
### Algorithm
We convert the Cartesian product to constraint representation and then call
`linear_map` on the corresponding polyhedron.
This is a fallback implementation and will fail if the wrapped sets are not
polyhedral.
"""
function linear_map(M::AbstractMatrix, cp::CartesianProduct)
return _linear_map_cartesian_product(M, cp)
end
function _linear_map_cartesian_product(M, cp)
@assert dim(cp) == size(M, 2) "a linear map of size $(size(M)) cannot " *
"be applied to a set of dimension $(dim(cp))"
# use constraint representation
T = isbounded(cp) ? HPolytope : HPolyhedron
P = T(constraints_list(cp))
return linear_map(M, P)
end
function project(cp::CartesianProduct, block::AbstractVector{Int}; kwargs...)
n1 = dim(cp.X)
if block[end] <= n1
# projection completely in the first block
return project(cp.X, block; kwargs...)
elseif block[1] > n1
# projection completely in the second block
return project(cp.Y, block .- n1; kwargs...)
end
# projection is a new Cartesian product of the block-wise projections
for (i, bi) in enumerate(block)
if bi > n1
X = project(cp.X, block[1:(i - 1)]; kwargs...)
Y = project(cp.Y, block[i:end] .- n1; kwargs...)
return CartesianProduct(X, Y)
end
end
end
"""
project(cp::CartesianProduct{N, IT, HT}, block::AbstractVector{Int};
[kwargs...]) where {N, IT<:Interval, HT<:AbstractHyperrectangle{N}}
Concrete projection of the Cartesian product of an interval and a
hyperrectangular set.
### Input
- `cp` -- Cartesian product of an interval and a hyperrectangle
- `block` -- block structure, a vector with the dimensions of interest
### Output
A hyperrectangle representing the projection of the Cartesian product `cp` on
the dimensions specified by `block`.
"""
function project(cp::CartesianProduct{N,IT,HT}, block::AbstractVector{Int};
kwargs...) where {N,IT<:Interval,HT<:AbstractHyperrectangle{N}}
I = cp.X
H = cp.Y
block_vec = collect(block)
if 1 ∉ block_vec
block_vec .-= 1
cH = center(H)
rH = radius_hyperrectangle(H)
else
cH = vcat(center(I), center(H))
rH = vcat(radius_hyperrectangle(I), radius_hyperrectangle(H))
end
return Hyperrectangle(cH[block_vec], rH[block_vec]; check_bounds=false)
end
"""
project(cp::CartesianProduct{N, IT, ZT}, block::AbstractVector{Int};
[kwargs...]) where {N, IT<:Interval, ZT<:AbstractZonotope{N}}
Concrete projection of the Cartesian product of an interval and a zonotopic set.
### Input
- `cp` -- Cartesian product of an interval and a zonotopic set
- `block` -- block structure, a vector with the dimensions of interest
### Output
A zonotope representing the projection of the Cartesian product `cp` on the
dimensions specified by `block`.
"""
function project(cp::CartesianProduct{N,IT,ZT}, block::AbstractVector{Int};
kwargs...) where {N,IT<:Interval,ZT<:AbstractZonotope{N}}
block_vec = collect(block)
Z = cp.Y
if 1 ∉ block_vec
block_vec .-= 1
else
Z = convert(Zonotope, cp)
end
M = projection_matrix(block_vec, dim(Z), N)
return linear_map(M, Z)
end
"""
project(cp::CartesianProduct{N,<:Interval,<:Union{VPolygon,VPolytope}
block::AbstractVector{Int};
[kwargs...]) where {N}
Concrete projection of the Cartesian product of an interval and a set in vertex
representation.
### Input
- `cp` -- Cartesian product of an interval and a `VPolygon` or a `VPolytope`
- `block` -- block structure, a vector with the dimensions of interest
### Output
A `VPolytope` representing the projection of the Cartesian product `cp` on the
dimensions specified by `block`.
"""
function project(cp::CartesianProduct{N,<:Interval,<:Union{VPolygon,VPolytope}},
block::AbstractVector{Int};
kwargs...) where {N}
I = cp.X
P = cp.Y
block_vec = collect(block)
if 1 ∉ block_vec
Pout = project(P, block_vec .- 1; kwargs...)
else
out = cartesian_product(I, P)
Pout = project(out, block_vec; kwargs...)
end
return Pout
end
"""
volume(cp::CartesianProduct)
Compute the volume of a Cartesian product.
### Input
- `cp` -- Cartesian product
### Output
The volume.
"""
function volume(cp::CartesianProduct)
return volume(cp.X) * volume(cp.Y)
end
function translate(cp::CartesianProduct, x::AbstractVector)
X = first(cp)
n1 = dim(X)
X = translate(X, @view(x[1:n1]))
Y = translate(second(cp), @view(x[(n1 + 1):end]))
return CartesianProduct(X, Y)
end