-
Notifications
You must be signed in to change notification settings - Fork 120
/
standard_constructions.jl
2374 lines (1986 loc) · 67.3 KB
/
standard_constructions.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
###############################################################################
### Standard constructions
###############################################################################
###############################################################################
@doc raw"""
birkhoff_polytope(n::Integer, even::Bool = false)
Construct the Birkhoff polytope of dimension $n^2$.
This is the polytope of $n \times n$ stochastic matrices (encoded as row vectors of
length $n^2$), i.e., the matrices with non-negative real entries whose row and column
entries sum up to one. Its vertices are the permutation matrices.
Use `even = true` to get the vertices only for the even permutation matrices.
# Examples
```jldoctest
julia> b = birkhoff_polytope(3)
Polytope in ambient dimension 9
julia> vertices(b)
6-element SubObjectIterator{PointVector{QQFieldElem}}:
[1, 0, 0, 0, 1, 0, 0, 0, 1]
[0, 1, 0, 1, 0, 0, 0, 0, 1]
[0, 0, 1, 1, 0, 0, 0, 1, 0]
[1, 0, 0, 0, 0, 1, 0, 1, 0]
[0, 1, 0, 0, 0, 1, 1, 0, 0]
[0, 0, 1, 0, 1, 0, 1, 0, 0]
```
"""
birkhoff_polytope(n::Integer; even::Bool=false) =
polyhedron(Polymake.polytope.birkhoff(n, Int(even); group=true))
@doc raw"""
pyramid(P::Polyhedron, z::Union{Number, FieldElem} = 1)
Make a pyramid over the given polyhedron `P`.
The pyramid is the convex hull of the input polyhedron `P` and a point `v`
outside the affine span of `P`. For bounded polyhedra, the projection of `v` to
the affine span of `P` coincides with the vertex barycenter of `P`. The scalar `z`
is the distance between the vertex barycenter and `v`.
# Examples
```jldoctest
julia> c = cube(2)
Polytope in ambient dimension 2
julia> vertices(pyramid(c,5))
5-element SubObjectIterator{PointVector{QQFieldElem}}:
[-1, -1, 0]
[1, -1, 0]
[-1, 1, 0]
[1, 1, 0]
[0, 0, 5]
```
"""
function pyramid(P::Polyhedron{T}, z::Number=1) where {T<:scalar_types}
pm_in = pm_object(P)
has_group = Polymake.exists(pm_in, "GROUP")
return Polyhedron{T}(
Polymake.polytope.pyramid(pm_in, z; group=has_group), coefficient_field(P)
)
end
function pyramid(P::Polyhedron{T}, z::FieldElem) where {T<:scalar_types}
U, f = _promote_scalar_field(coefficient_field(P), parent(z))
pm_in = pm_object(P)
has_group = Polymake.exists(pm_in, "GROUP")
return Polyhedron{U}(Polymake.polytope.pyramid(pm_in, z; group=has_group), f)
end
@doc raw"""
bipyramid(P::Polyhedron, z::Union{Number, FieldElem} = 1, z_prime::Union{Number, FieldElem} = -z)
Make a bipyramid over a pointed polyhedron `P`.
The bipyramid is the convex hull of the input polyhedron `P` and two apexes
(`v`, `z`), (`v`, `z_prime`) on both sides of the affine span of `P`. For bounded
polyhedra, the projections of the apexes `v` to the affine span of `P` is the
vertex barycenter of `P`.
# Examples
```jldoctest
julia> c = cube(2)
Polytope in ambient dimension 2
julia> vertices(bipyramid(c,2))
6-element SubObjectIterator{PointVector{QQFieldElem}}:
[-1, -1, 0]
[1, -1, 0]
[-1, 1, 0]
[1, 1, 0]
[0, 0, 2]
[0, 0, -2]
```
"""
function bipyramid(
P::Polyhedron{T}, z::Number=1, z_prime::Number=-z
) where {T<:scalar_types}
pm_in = pm_object(P)
has_group = Polymake.exists(pm_in, "GROUP")
return Polyhedron{T}(
Polymake.polytope.bipyramid(pm_in, z, z_prime; group=has_group), coefficient_field(P)
)
end
function bipyramid(
P::Polyhedron{T}, z::FieldElem, z_prime::FieldElem=-z
) where {T<:scalar_types}
U, f = _promote_scalar_field(coefficient_field(P), parent(z), parent(z_prime))
pm_in = pm_object(P)
has_group = Polymake.exists(pm_in, "GROUP")
return Polyhedron{U}(Polymake.polytope.bipyramid(pm_in, z, z_prime; group=has_group), f)
end
bipyramid(P::Polyhedron{T}, z::FieldElem, z_prime::Number) where {T<:scalar_types} =
bipyramid(P, z, parent(z)(z_prime))
bipyramid(P::Polyhedron{T}, z::Number, z_prime::FieldElem) where {T<:scalar_types} =
bipyramid(P, parent(z_prime)(z), z_prime)
@doc raw"""
normal_cone(P::Polyhedron, i::Int64)
Construct the normal cone to `P` at the `i`-th vertex of `P`.
The normal cone at a face is generated by all the inner normals of `P` that
attain their minimum at the `i`-th vertex.
# Examples
Build the normal cones at the first vertex of the square (in this case [-1,-1]).
```jldoctest
julia> square = cube(2)
Polytope in ambient dimension 2
julia> vertices(square)
4-element SubObjectIterator{PointVector{QQFieldElem}}:
[-1, -1]
[1, -1]
[-1, 1]
[1, 1]
julia> nc = normal_cone(square, 1)
Polyhedral cone in ambient dimension 2
julia> rays(nc)
2-element SubObjectIterator{RayVector{QQFieldElem}}:
[1, 0]
[0, 1]
```
"""
function normal_cone(P::Polyhedron{T}, i::Int64) where {T<:scalar_types}
@req 1 <= i <= n_vertices(P) "Vertex index out of range"
bigobject = Polymake.polytope.normal_cone(pm_object(P), Set{Int64}([i - 1]))
return Cone{T}(bigobject, coefficient_field(P))
end
@doc raw"""
orbit_polytope(V::AbstractCollection[PointVector], G::PermGroup)
Construct the convex hull of the orbit of one or several points (given row-wise
in `V`) under the action of `G`.
# Examples
This will construct the $3$-dimensional permutahedron:
```jldoctest
julia> V = [1 2 3];
julia> G = symmetric_group(3);
julia> P = orbit_polytope(V, G)
Polyhedron in ambient dimension 3
julia> vertices(P)
6-element SubObjectIterator{PointVector{QQFieldElem}}:
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
```
"""
function orbit_polytope(V::AbstractCollection[PointVector], G::PermGroup)
Vhom = stack(homogenized_matrix(V, 1), nothing)
@req size(Vhom, 2) == degree(G) + 1 "Dimension of points and group degree need to be the same"
generators = PermGroup_to_polymake_array(G)
pmGroup = Polymake.group.PermutationAction(; GENERATORS=generators)
pmPolytope = Polymake.polytope.orbit_polytope(Vhom, pmGroup)
return Polyhedron{QQFieldElem}(pmPolytope)
end
@doc raw"""
cube([::Union{Type{T}, Field} = QQFieldElem,] d::Int , [l::Rational = -1, u::Rational = 1])
Construct the $[l,u]$-cube in dimension $d$.
The first argument either specifies the `Type` of its coefficients or their
parent `Field`.
# Examples
In this example the 5-dimensional unit cube is constructed to ask for one of its
properties:
```jldoctest
julia> C = cube(5,0,1);
julia> normalized_volume(C)
120
```
"""
function cube(f::scalar_type_or_field, d::Int)
parent_field, scalar_type = _determine_parent_and_scalar(f)
return Polyhedron{scalar_type}(
Polymake.polytope.cube{_scalar_type_to_polymake(scalar_type)}(d), parent_field
)
end
cube(d::Int) = cube(QQFieldElem, d)
function cube(f::scalar_type_or_field, d::Int, l, u)
parent_field, scalar_type = _determine_parent_and_scalar(f, l, u)
return Polyhedron{scalar_type}(
Polymake.polytope.cube{_scalar_type_to_polymake(scalar_type)}(d, u, l), parent_field
)
end
cube(d::Int, l, u) = cube(QQFieldElem, d, l, u)
@doc raw"""
tetrahedron()
Construct the regular tetrahedron, one of the Platonic solids.
"""
tetrahedron() = polyhedron(Polymake.polytope.tetrahedron());
@doc raw"""
dodecahedron()
Construct the regular dodecahedron, one out of two Platonic solids.
"""
dodecahedron() = polyhedron(Polymake.polytope.dodecahedron());
@doc raw"""
icosahedron()
Construct the regular icosahedron, one out of two exceptional Platonic solids.
"""
icosahedron() = polyhedron(Polymake.polytope.icosahedron());
const _johnson_indexes_from_oscar = Set{Int}([9, 10, 13, 16, 17, 18, 20, 21, 22, 23, 24,
25, 30, 32, 33, 34, 35, 36, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 64, 68, 69, 70, 71, 72, 73, 74, 75,
77, 78, 79, 82, 84, 85, 86, 87, 88, 89,
90, 92])
@doc raw"""
johnson_solid(i::Int)
Construct the `i`-th proper Johnson solid.
A Johnson solid is a 3-polytope whose facets are regular polygons, of various gonalities.
It is proper if it is not an Archimedean solid. Up to scaling there are exactly 92 proper Johnson solids.
See also [`is_johnson_solid`](@ref).
"""
function johnson_solid(index::Int)
if index in _johnson_indexes_from_oscar
# code used for generation of loaded files can be found at:
# https://github.com/dmg-lab/JohnsonSrc
str_index = lpad(index, 2, '0')
filename = "j$str_index" * ".mrdi"
return load(joinpath(oscardir, "data", "JohnsonSolids", filename))
end
pmp = Polymake.polytope.johnson_solid(index)
return polyhedron(pmp)
end
@doc raw"""
regular_24_cell()
Construct the regular 24-cell, one out of three exceptional regular 4-polytopes.
"""
regular_24_cell() = polyhedron(Polymake.polytope.regular_24_cell());
@doc raw"""
regular_120_cell()
Construct the regular 120-cell, one out of three exceptional regular 4-polytopes.
"""
regular_120_cell() = polyhedron(Polymake.polytope.regular_120_cell());
@doc raw"""
regular_600_cell()
Construct the regular 600-cell, one out of three exceptional regular 4-polytopes.
"""
regular_600_cell() = polyhedron(Polymake.polytope.regular_600_cell());
@doc raw"""
newton_polytope(poly::Polynomial)
Compute the Newton polytope of the multivariate polynomial `poly`.
# Examples
```jldoctest
julia> S, (x, y) = polynomial_ring(ZZ, ["x", "y"])
(Multivariate polynomial ring in 2 variables over ZZ, ZZMPolyRingElem[x, y])
julia> f = x^3*y + 3x*y^2 + 1
x^3*y + 3*x*y^2 + 1
julia> NP = newton_polytope(f)
Polyhedron in ambient dimension 2
julia> vertices(NP)
3-element SubObjectIterator{PointVector{QQFieldElem}}:
[3, 1]
[1, 2]
[0, 0]
```
"""
function newton_polytope(f)
exponents = reduce(hcat, Oscar.AbstractAlgebra.exponent_vectors(f))'
convex_hull(exponents)
end
polyhedron(H::Halfspace{T}) where {T<:scalar_types} =
polyhedron(coefficient_field(H), normal_vector(H), negbias(H))
polyhedron(H::Hyperplane{T}) where {T<:scalar_types} =
polyhedron(coefficient_field(H), nothing, (normal_vector(H), [negbias(H)]))
@doc raw"""
intersect(P::Polyhedron...)
Return the intersection $\bigcap\limits_{p \in P} p$.
# Examples
The positive orthant of the plane is the intersection of the two halfspaces with
$x≥0$ and $y≥0$ respectively.
```jldoctest
julia> UH1 = convex_hull([0 0],[1 0],[0 1]);
julia> UH2 = convex_hull([0 0],[0 1],[1 0]);
julia> PO = intersect(UH1, UH2)
Polyhedron in ambient dimension 2
julia> rays(PO)
2-element SubObjectIterator{RayVector{QQFieldElem}}:
[1, 0]
[0, 1]
```
"""
function intersect(P::Polyhedron...)
T, f = _promote_scalar_field((coefficient_field(p) for p in P)...)
pmo = [pm_object(p) for p in P]
return Polyhedron{T}(Polymake.polytope.intersection(pmo...), f)
end
intersect(P::AbstractVector{<:Polyhedron}) = intersect(P...)
@doc raw"""
minkowski_sum(P::Polyhedron, Q::Polyhedron)
Return the Minkowski sum $P + Q = \{ x+y\ |\ x∈P, y∈Q\}$ of `P` and `Q`.
# Examples
The Minkowski sum of a square and the 2-dimensional cross-polytope is an
octagon:
```jldoctest
julia> P = cube(2);
julia> Q = cross_polytope(2);
julia> M = minkowski_sum(P, Q)
Polyhedron in ambient dimension 2
julia> n_vertices(M)
8
```
"""
function minkowski_sum(
P::Polyhedron{T}, Q::Polyhedron{U}; algorithm::Symbol=:standard
) where {T<:scalar_types,U<:scalar_types}
V, f = _promote_scalar_field(coefficient_field(P), coefficient_field(Q))
po = _promoted_bigobject(V, P)
qo = _promoted_bigobject(V, Q)
if algorithm == :standard
return Polyhedron{V}(Polymake.polytope.minkowski_sum(po, qo), f)
elseif algorithm == :fukuda
return Polyhedron{V}(Polymake.polytope.minkowski_sum_fukuda(po, qo), f)
else
throw(ArgumentError("Unknown minkowski sum `algorithm` argument: $algorithm"))
end
end
@doc raw"""
product(P::Polyhedron, Q::Polyhedron)
Return the Cartesian product of `P` and `Q`.
# Examples
The Cartesian product of a triangle and a line segment is a triangular prism.
```jldoctest
julia> T=simplex(2)
Polytope in ambient dimension 2
julia> S=cube(1)
Polytope in ambient dimension 1
julia> length(vertices(product(T,S)))
6
```
"""
function product(P::Polyhedron{T}, Q::Polyhedron{U}) where {T<:scalar_types,U<:scalar_types}
V, f = _promote_scalar_field(coefficient_field(P), coefficient_field(Q))
return Polyhedron{V}(Polymake.polytope.product(pm_object(P), pm_object(Q)), f)
end
@doc raw"""
*(P::Polyhedron, Q::Polyhedron)
Return the Cartesian product of `P` and `Q` (see also `product`).
# Examples
The Cartesian product of a triangle and a line segment is a triangular prism.
```jldoctest
julia> T=simplex(2)
Polytope in ambient dimension 2
julia> S=cube(1)
Polytope in ambient dimension 1
julia> length(vertices(T*S))
6
```
"""
*(P::Polyhedron{T}, Q::Polyhedron{U}) where {T<:scalar_types,U<:scalar_types} =
product(P, Q)
@doc raw"""
convex_hull(P::Polyhedron, Q::Polyhedron)
Return the convex_hull of `P` and `Q`.
# Examples
The convex hull of the following two line segments in $R^3$ is a tetrahedron.
```jldoctest
julia> L₁ = convex_hull([-1 0 0; 1 0 0])
Polyhedron in ambient dimension 3
julia> L₂ = convex_hull([0 -1 0; 0 1 0])
Polyhedron in ambient dimension 3
julia> T=convex_hull(L₁,L₂);
julia> f_vector(T)
2-element Vector{ZZRingElem}:
4
4
```
"""
function convex_hull(P::Polyhedron...)
T, f = _promote_scalar_field((coefficient_field(p) for p in P)...)
pmo = [pm_object(p) for p in P]
return Polyhedron{T}(Polymake.polytope.conv(pmo...), f)
end
convex_hull(P::AbstractVector{<:Polyhedron}) = convex_hull(P...)
@doc raw"""
+(P::Polyhedron, Q::Polyhedron)
Return the Minkowski sum $P + Q = \{ x+y\ |\ x∈P, y∈Q\}$ of `P` and `Q` (see also `minkowski_sum`).
# Examples
The Minkowski sum of a square and the 2-dimensional cross-polytope is an
octagon:
```jldoctest
julia> P = cube(2);
julia> Q = cross_polytope(2);
julia> M = minkowski_sum(P, Q)
Polyhedron in ambient dimension 2
julia> n_vertices(M)
8
```
"""
+(P::Polyhedron{T}, Q::Polyhedron{U}) where {T<:scalar_types,U<:scalar_types} =
minkowski_sum(P, Q)
@doc raw"""
*(k::Union{Number, FieldElem}, Q::Polyhedron)
Return the scaled polyhedron $kQ = \{ kx\ |\ x∈Q\}$.
Note that `k*Q = Q*k`.
# Examples
Scaling an $n$-dimensional bounded polyhedron by the factor $k$ results in the
volume being scaled by $k^n$.
This example confirms the statement for the 6-dimensional cube and $k = 2$.
```jldoctest
julia> C = cube(6);
julia> SC = 2*C
Polyhedron in ambient dimension 6
julia> volume(SC)//volume(C)
64
```
"""
*(k::Number, P::Polyhedron{T}) where {T<:scalar_types} =
Polyhedron{T}(Polymake.polytope.scale(pm_object(P), k), coefficient_field(P))
function *(k::FieldElem, P::Polyhedron{T}) where {T<:scalar_types}
U, f = _promote_scalar_field(parent(k), coefficient_field(P))
return Polyhedron{U}(Polymake.polytope.scale(pm_object(P), k), f)
end
@doc raw"""
*(P::Polyhedron, k::Union{Number, FieldElem})
Return the scaled polyhedron $kP = \{ kx\ |\ x∈P\}$.
Note that `k*P = P*k`.
# Examples
Scaling an $n$-dimensional bounded polyhedron by the factor $k$ results in the
volume being scaled by $k^n$.
This example confirms the statement for the 6-dimensional cube and $k = 2$.
```jldoctest
julia> C = cube(6);
julia> SC = C*2
Polyhedron in ambient dimension 6
julia> volume(SC)//volume(C)
64
```
"""
*(P::Polyhedron{T}, k::Union{Number,FieldElem}) where {T<:scalar_types} = k * P
@doc raw"""
+(P::Polyhedron, v::AbstractVector)
Return the translation $P+v = \{ x+v\ |\ x∈P\}$ of `P` by `v`.
Note that `P+v = v+P`.
# Examples
We construct a polyhedron from its $V$-description. Shifting it by the right
vector reveals that its inner geometry corresponds to that of the 3-simplex.
```jldoctest
julia> P = convex_hull([100 200 300; 101 200 300; 100 201 300; 100 200 301]);
julia> v = [-100, -200, -300];
julia> S = P + v
Polyhedron in ambient dimension 3
julia> vertices(S)
4-element SubObjectIterator{PointVector{QQFieldElem}}:
[0, 0, 0]
[1, 0, 0]
[0, 1, 0]
[0, 0, 1]
```
"""
function +(P::Polyhedron{T}, v::AbstractVector) where {T<:scalar_types}
@req ambient_dim(P) == length(v) "Translation vector not correct dimension"
return Polyhedron{T}(
Polymake.polytope.translate(
pm_object(P), Polymake.Vector{_scalar_type_to_polymake(T)}(v)
),
coefficient_field(P),
)
end
@doc raw"""
+(v::AbstractVector, P::Polyhedron)
Return the translation $P+v = \{ x+v\ |\ x∈P\}$ of `P` by `v`.
Note that `P+v = v+P`.
# Examples
We construct a polyhedron from its $V$-description. Shifting it by the right
vector reveals that its inner geometry corresponds to that of the 3-simplex.
```jldoctest
julia> P = convex_hull([100 200 300; 101 200 300; 100 201 300; 100 200 301]);
julia> v = [-100, -200, -300];
julia> S = v + P
Polyhedron in ambient dimension 3
julia> vertices(S)
4-element SubObjectIterator{PointVector{QQFieldElem}}:
[0, 0, 0]
[1, 0, 0]
[0, 1, 0]
[0, 0, 1]
```
"""
+(v::AbstractVector, P::Polyhedron{T}) where {T<:scalar_types} = P + v
@doc raw"""
simplex([::Union{Type{T}, Field} = QQFieldElem,] d::Int [,n])
Construct the simplex which is the convex hull of the standard basis vectors
along with the origin in $\mathbb{R}^d$, scaled by $n$.
The first argument either specifies the `Type` of its coefficients or their
parent `Field`.
# Examples
Here we take a look at the facets of the 7-simplex and a scaled 7-simplex:
```jldoctest
julia> s = simplex(7)
Polytope in ambient dimension 7
julia> facets(s)
8-element SubObjectIterator{AffineHalfspace{QQFieldElem}} over the Halfspaces of R^7 described by:
-x_1 <= 0
-x_2 <= 0
-x_3 <= 0
-x_4 <= 0
-x_5 <= 0
-x_6 <= 0
-x_7 <= 0
x_1 + x_2 + x_3 + x_4 + x_5 + x_6 + x_7 <= 1
julia> t = simplex(7, 5)
Polytope in ambient dimension 7
julia> facets(t)
8-element SubObjectIterator{AffineHalfspace{QQFieldElem}} over the Halfspaces of R^7 described by:
-x_1 <= 0
-x_2 <= 0
-x_3 <= 0
-x_4 <= 0
-x_5 <= 0
-x_6 <= 0
-x_7 <= 0
x_1 + x_2 + x_3 + x_4 + x_5 + x_6 + x_7 <= 5
```
"""
function simplex(f::scalar_type_or_field, d::Int, n)
parent_field, scalar_type = _determine_parent_and_scalar(f, n)
return Polyhedron{scalar_type}(
Polymake.polytope.simplex{_scalar_type_to_polymake(scalar_type)}(d, n), parent_field
)
end
simplex(d::Int, n) = simplex(QQFieldElem, d, n)
function simplex(f::scalar_type_or_field, d::Int)
parent_field, scalar_type = _determine_parent_and_scalar(f)
return Polyhedron{scalar_type}(
Polymake.polytope.simplex{_scalar_type_to_polymake(scalar_type)}(d), parent_field
)
end
simplex(d::Int) = simplex(QQFieldElem, d)
@doc raw"""
cross_polytope([::Union{Type{T}, Field} = QQFieldElem,] d::Int [,n])
Construct a $d$-dimensional cross polytope around origin with vertices located
at $\pm e_i$ for each unit vector $e_i$ of $R^d$, scaled by $n$.
The first argument either specifies the `Type` of its coefficients or their
parent `Field`.
# Examples
Here we print the facets of a non-scaled and a scaled 3-dimensional cross
polytope:
```jldoctest
julia> C = cross_polytope(3)
Polytope in ambient dimension 3
julia> facets(C)
8-element SubObjectIterator{AffineHalfspace{QQFieldElem}} over the Halfspaces of R^3 described by:
x_1 + x_2 + x_3 <= 1
-x_1 + x_2 + x_3 <= 1
x_1 - x_2 + x_3 <= 1
-x_1 - x_2 + x_3 <= 1
x_1 + x_2 - x_3 <= 1
-x_1 + x_2 - x_3 <= 1
x_1 - x_2 - x_3 <= 1
-x_1 - x_2 - x_3 <= 1
julia> D = cross_polytope(3, 2)
Polytope in ambient dimension 3
julia> facets(D)
8-element SubObjectIterator{AffineHalfspace{QQFieldElem}} over the Halfspaces of R^3 described by:
x_1 + x_2 + x_3 <= 2
-x_1 + x_2 + x_3 <= 2
x_1 - x_2 + x_3 <= 2
-x_1 - x_2 + x_3 <= 2
x_1 + x_2 - x_3 <= 2
-x_1 + x_2 - x_3 <= 2
x_1 - x_2 - x_3 <= 2
-x_1 - x_2 - x_3 <= 2
```
"""
function cross_polytope(f::scalar_type_or_field, d::Int64, n)
parent_field, scalar_type = _determine_parent_and_scalar(f, n)
return Polyhedron{scalar_type}(
Polymake.polytope.cross{_scalar_type_to_polymake(scalar_type)}(d, n), parent_field
)
end
cross_polytope(d::Int64, n) = cross_polytope(QQFieldElem, d, n)
function cross_polytope(f::scalar_type_or_field, d::Int64)
parent_field, scalar_type = _determine_parent_and_scalar(f)
return Polyhedron{scalar_type}(
Polymake.polytope.cross{_scalar_type_to_polymake(scalar_type)}(d), parent_field
)
end
cross_polytope(d::Int64) = cross_polytope(QQFieldElem, d)
@doc raw"""
platonic_solid(s)
Construct a Platonic solid with the name given by String `s` from the list
below.
See also [`is_platonic_solid`](@ref).
# Arguments
- `s::String`: The name of the desired Platonic solid.
Possible values:
- "tetrahedron" : Tetrahedron.
Regular polytope with four triangular facets.
- "cube" : Cube.
Regular polytope with six square facets.
- "octahedron" : Octahedron.
Regular polytope with eight triangular facets.
- "dodecahedron" : Dodecahedron.
Regular polytope with 12 pentagonal facets.
- "icosahedron" : Icosahedron.
Regular polytope with 20 triangular facets.
# Examples
```jldoctest
julia> T = platonic_solid("icosahedron")
Polytope in ambient dimension 3 with EmbeddedAbsSimpleNumFieldElem type coefficients
julia> n_facets(T)
20
```
"""
platonic_solid(s::String) = polyhedron(Polymake.polytope.platonic_solid(s))
@doc raw"""
archimedean_solid(s)
Construct an Archimedean solid with the name given by String `s` from the list
below. Some of these polytopes are realized with floating point numbers and
thus not exact; Vertex-facet-incidences are correct in all cases.
See also [`is_archimedean_solid`](@ref).
# Arguments
- `s::String`: The name of the desired Archimedean solid.
Possible values:
- "truncated_tetrahedron" : Truncated tetrahedron.
Regular polytope with four triangular and four hexagonal facets.
- "cuboctahedron" : Cuboctahedron.
Regular polytope with eight triangular and six square facets.
- "truncated_cube" : Truncated cube.
Regular polytope with eight triangular and six octagonal facets.
- "truncated_octahedron" : Truncated Octahedron.
Regular polytope with six square and eight hexagonal facets.
- "rhombicuboctahedron" : Rhombicuboctahedron.
Regular polytope with eight triangular and 18 square facets.
- "truncated_cuboctahedron" : Truncated Cuboctahedron.
Regular polytope with 12 square, eight hexagonal and six octagonal
facets.
- "snub_cube" : Snub Cube.
Regular polytope with 32 triangular and six square facets.
The vertices are realized as floating point numbers.
This is a chiral polytope.
- "icosidodecahedron" : Icosidodecahedon.
Regular polytope with 20 triangular and 12 pentagonal facets.
- "truncated_dodecahedron" : Truncated Dodecahedron.
Regular polytope with 20 triangular and 12 decagonal facets.
- "truncated_icosahedron" : Truncated Icosahedron.
Regular polytope with 12 pentagonal and 20 hexagonal facets.
- "rhombicosidodecahedron" : Rhombicosidodecahedron.
Regular polytope with 20 triangular, 30 square and 12 pentagonal
facets.
- "truncated_icosidodecahedron" : Truncated Icosidodecahedron.
Regular polytope with 30 square, 20 hexagonal and 12 decagonal
facets.
- "snub_dodecahedron" : Snub Dodecahedron.
Regular polytope with 80 triangular and 12 pentagonal facets.
The vertices are realized as floating point numbers.
This is a chiral polytope.
# Examples
```jldoctest
julia> T = archimedean_solid("cuboctahedron")
Polytope in ambient dimension 3
julia> sum([n_vertices(F) for F in faces(T, 2)] .== 3)
8
julia> sum([n_vertices(F) for F in faces(T, 2)] .== 4)
6
julia> n_facets(T)
14
```
"""
archimedean_solid(s::String) = polyhedron(Polymake.polytope.archimedean_solid(s))
@doc raw"""
catalan_solid(s::String)
Construct a Catalan solid with the name `s` from the list below. Some of these
polytopes are realized with floating point coordinates and thus are not exact.
However, vertex-facet-incidences are correct in all cases.
# Arguments
- `s::String`: The name of the desired Archimedean solid.
Possible values:
- "triakis_tetrahedron" : Triakis Tetrahedron.
Dual polytope to the Truncated Tetrahedron, made of 12 isosceles
triangular facets.
- "triakis_octahedron" : Triakis Octahedron.
Dual polytope to the Truncated Cube, made of 24 isosceles triangular
facets.
- "rhombic_dodecahedron" : Rhombic dodecahedron.
Dual polytope to the cuboctahedron, made of 12 rhombic facets.
- "tetrakis_hexahedron" : Tetrakis hexahedron.
Dual polytope to the truncated octahedron, made of 24 isosceles
triangluar facets.
- "disdyakis_dodecahedron" : Disdyakis dodecahedron.
Dual polytope to the truncated cuboctahedron, made of 48 scalene
triangular facets.
- "pentagonal_icositetrahedron" : Pentagonal Icositetrahedron.
Dual polytope to the snub cube, made of 24 irregular pentagonal facets.
The vertices are realized as floating point numbers.
- "pentagonal_hexecontahedron" : Pentagonal Hexecontahedron.
Dual polytope to the snub dodecahedron, made of 60 irregular pentagonal
facets. The vertices are realized as floating point numbers.
- "rhombic_triacontahedron" : Rhombic triacontahedron.
Dual polytope to the icosidodecahedron, made of 30 rhombic facets.
- "triakis_icosahedron" : Triakis icosahedron.
Dual polytope to the icosidodecahedron, made of 30 rhombic facets.
- "deltoidal_icositetrahedron" : Deltoidal Icositetrahedron.
Dual polytope to the rhombicubaoctahedron, made of 24 kite facets.
- "pentakis_dodecahedron" : Pentakis dodecahedron.
Dual polytope to the truncated icosahedron, made of 60 isosceles
triangular facets.
- "deltoidal_hexecontahedron" : Deltoidal hexecontahedron.
Dual polytope to the rhombicosidodecahedron, made of 60 kite facets.
- "disdyakis_triacontahedron" : Disdyakis triacontahedron.
Dual polytope to the truncated icosidodecahedron, made of 120 scalene
triangular facets.
# Examples
```jldoctest
julia> T = catalan_solid("triakis_tetrahedron");
julia> count(F -> n_vertices(F) == 3, faces(T, 2))
12
julia> n_facets(T)
12
```
"""
catalan_solid(s::String) = polyhedron(Polymake.polytope.catalan_solid(s))
@doc raw"""
upper_bound_f_vector(d::Int, n::Int)
Return the maximal f-vector of a `d`-polytope with `n` vertices;
this is given by McMullen's Upper-Bound-Theorem.
"""
upper_bound_f_vector(d::Int, n::Int) =
Vector{Int}(Polymake.polytope.upper_bound_theorem(d, n).F_VECTOR)
@doc raw"""
upper_bound_g_vector(d::Int, n::Int)
Return the maximal g-vector of a `d`-polytope with `n` vertices;
this is given by McMullen's Upper-Bound-Theorem.
"""
upper_bound_g_vector(d::Int, n::Int) =
Vector{Int}(Polymake.polytope.upper_bound_theorem(d, n).G_VECTOR)
@doc raw"""
upper_bound_h_vector(d::Int, n::Int)
Return the maximal h-vector of a `d`-polytope with `n` vertices;
this is given by McMullen's Upper-Bound-Theorem.
"""
upper_bound_h_vector(d::Int, n::Int) =
Vector{Int}(Polymake.polytope.upper_bound_theorem(d, n).H_VECTOR)
@doc raw"""
billera_lee_polytope(h::AbstractVector)
Construct a simplicial polytope whose h-vector is $h$.
The corresponding g-vector must be an M-sequence.
The ambient dimension equals the length of $h$, and the polytope lives in codimension one.
- [BL81](@cite)
# Examples
```jldoctest
julia> BL = billera_lee_polytope([1,3,3,1])
Polyhedron in ambient dimension 4
julia> f_vector(BL)
3-element Vector{ZZRingElem}:
6
12
8
```
"""
billera_lee_polytope(h::AbstractVector) = Polyhedron{QQFieldElem}(
Polymake.polytope.billera_lee(Polymake.Vector{Polymake.Integer}(h)), QQ
)
@doc raw"""
polarize(P::Polyhedron)
Return the polar dual of the polyhedron `P`, consisting of all linear functions
whose evaluation on `P` does not exceed 1.
# Examples
```jldoctest
julia> square = cube(2)
Polytope in ambient dimension 2
julia> P = polarize(square)
Polytope in ambient dimension 2
julia> vertices(P)
4-element SubObjectIterator{PointVector{QQFieldElem}}:
[1, 0]
[-1, 0]
[0, 1]
[0, -1]
```
"""
function polarize(P::Polyhedron{T}) where {T<:scalar_types}
return Polyhedron{T}(Polymake.polytope.polarize(pm_object(P)), coefficient_field(P))
end
@doc raw"""
project_full(P::Polyhedron)
Project the polyhedron down such that it becomes full dimensional in the new
ambient space.
# Examples
```jldoctest
julia> P = convex_hull([1 0 0; 0 0 0])
Polyhedron in ambient dimension 3
julia> is_fulldimensional(P)
false
julia> p = project_full(P)
Polyhedron in ambient dimension 1
julia> is_fulldimensional(p)
true
```
"""
project_full(P::Polyhedron{T}) where {T<:scalar_types} =
Polyhedron{T}(Polymake.polytope.project_full(pm_object(P)), coefficient_field(P))
@doc raw"""
gelfand_tsetlin_polytope(lambda::AbstractVector)
Construct the Gelfand-Tsetlin polytope indexed by a weakly decreasing vector `lambda`.
# Examples
```jldoctest
julia> P = gelfand_tsetlin_polytope([5,3,2])
Polyhedron in ambient dimension 6
julia> is_fulldimensional(P)
false
julia> p = project_full(P)
Polyhedron in ambient dimension 3
julia> is_fulldimensional(p)
true
julia> volume(p)
3
```
"""
gelfand_tsetlin_polytope(lambda::AbstractVector) = Polyhedron{QQFieldElem}(
Polymake.polytope.gelfand_tsetlin(
Polymake.Vector{Polymake.Rational}(lambda); projected=false
),
)