/
properties.jl
1705 lines (1401 loc) · 43.9 KB
/
properties.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
###############################################################################
###############################################################################
### Iterators
###############################################################################
###############################################################################
#TODO: take into account lineality space
@doc raw"""
faces(P::Polyhedron, face_dim::Int)
Return an iterator over the faces of `P` of dimension `face_dim`.
# Examples
A `Vector` containing the six sides of the 3-dimensional cube can be obtained
via the following input:
```jldoctest
julia> F = faces(cube(3), 2)
6-element SubObjectIterator{Polyhedron{QQFieldElem}}:
Polytope in ambient dimension 3
Polytope in ambient dimension 3
Polytope in ambient dimension 3
Polytope in ambient dimension 3
Polytope in ambient dimension 3
Polytope in ambient dimension 3
```
"""
function faces(P::Polyhedron{T}, face_dim::Int) where {T<:scalar_types}
face_dim == dim(P) - 1 &&
return SubObjectIterator{Polyhedron{T}}(P, _face_polyhedron_facet, n_facets(P))
n = face_dim - length(lineality_space(P))
n < 0 && return nothing
pfaces = Polymake.to_one_based_indexing(Polymake.polytope.faces_of_dim(pm_object(P), n))
farf = Polymake.to_one_based_indexing(pm_object(P).FAR_FACE)
rfaces = Vector{Int}(filter(i -> !(pfaces[i] <= farf), range(1, length(pfaces); step=1)))
return SubObjectIterator{Polyhedron{T}}(
P, _face_polyhedron, length(rfaces), (f_dim=n, f_ind=rfaces)
)
end
function _face_polyhedron(
::Type{Polyhedron{T}},
P::Polyhedron{T},
i::Base.Integer;
f_dim::Int=-1,
f_ind::Vector{Int64}=Vector{Int64}(),
) where {T<:scalar_types}
pface = Polymake.to_one_based_indexing(
Polymake.polytope.faces_of_dim(pm_object(P), f_dim)
)[f_ind[i]]
V = pm_object(P).VERTICES[collect(pface), :]
L = pm_object(P).LINEALITY_SPACE
PT = _scalar_type_to_polymake(T)
return Polyhedron{T}(
Polymake.polytope.Polytope{PT}(; VERTICES=V, LINEALITY_SPACE=L), coefficient_field(P)
)
end
function _vertex_indices(
::Val{_face_polyhedron}, P::Polyhedron; f_dim=-1, f_ind::Vector{Int64}=Vector{Int64}()
)
return IncidenceMatrix(
collect.(
Polymake.to_one_based_indexing(
Polymake.polytope.faces_of_dim(pm_object(P), f_dim)[f_ind]
)
),
)[
:, _vertex_indices(pm_object(P))
]
end
function _ray_indices(
::Val{_face_polyhedron}, P::Polyhedron; f_dim=-1, f_ind::Vector{Int64}=Vector{Int64}()
)
return IncidenceMatrix(
collect.(
Polymake.to_one_based_indexing(
Polymake.polytope.faces_of_dim(pm_object(P), f_dim)[f_ind]
)
),
)[
:, _ray_indices(pm_object(P))
]
end
function _vertex_and_ray_indices(
::Val{_face_polyhedron}, P::Polyhedron; f_dim=-1, f_ind::Vector{Int64}=Vector{Int64}()
)
return IncidenceMatrix(
collect.(
Polymake.to_one_based_indexing(
Polymake.polytope.faces_of_dim(pm_object(P), f_dim)[f_ind]
)
),
)
end
_incidencematrix(::Val{_face_polyhedron}) = _vertex_and_ray_indices
function _face_polyhedron_facet(
::Type{Polyhedron{T}}, P::Polyhedron{T}, i::Base.Integer
) where {T<:scalar_types}
pface = pm_object(P).VERTICES_IN_FACETS[_facet_index(pm_object(P), i), :]
V = pm_object(P).VERTICES[collect(pface), :]
L = pm_object(P).LINEALITY_SPACE
PT = _scalar_type_to_polymake(T)
return Polyhedron{T}(
Polymake.polytope.Polytope{PT}(; VERTICES=V, LINEALITY_SPACE=L), coefficient_field(P)
)
end
_vertex_indices(::Val{_face_polyhedron_facet}, P::Polyhedron) = vcat(
pm_object(P).VERTICES_IN_FACETS[
1:(_facet_at_infinity(pm_object(P)) - 1), _vertex_indices(pm_object(P))
],
pm_object(P).VERTICES_IN_FACETS[
(_facet_at_infinity(pm_object(P)) + 1):end, _vertex_indices(pm_object(P))
],
)
_ray_indices(::Val{_face_polyhedron_facet}, P::Polyhedron) = vcat(
pm_object(P).VERTICES_IN_FACETS[
1:(_facet_at_infinity(pm_object(P)) - 1), _ray_indices(pm_object(P))
],
pm_object(P).VERTICES_IN_FACETS[
(_facet_at_infinity(pm_object(P)) + 1):end, _ray_indices(pm_object(P))
],
)
_vertex_and_ray_indices(::Val{_face_polyhedron_facet}, P::Polyhedron) = vcat(
pm_object(P).VERTICES_IN_FACETS[1:(_facet_at_infinity(pm_object(P)) - 1), :],
pm_object(P).VERTICES_IN_FACETS[(_facet_at_infinity(pm_object(P)) + 1):end, :],
)
_incidencematrix(::Val{_face_polyhedron_facet}) = _vertex_and_ray_indices
function _isray(P::Polyhedron, i::Base.Integer)
return in(i, _ray_indices(pm_object(P)))
end
function _vertex_indices(P::Polymake.BigObject)
vi = Polymake.get_attachment(P, "_vertex_indices")
if isnothing(vi)
A = P.VERTICES
vi = Polymake.Vector{Polymake.to_cxx_type(Int64)}(findall(!iszero, view(A, :, 1)))
Polymake.attach(P, "_vertex_indices", vi)
end
return vi
end
_ray_indices(P::Polymake.BigObject) = collect(Polymake.to_one_based_indexing(P.FAR_FACE))
function _polymake_to_oscar_vertex_index(P::Polymake.BigObject, i::Base.Integer)
return i - sum((>).(i, P.FAR_FACE))
end
function _polymake_to_oscar_vertex_index(P::Polymake.BigObject, v::AbstractVector)
return [_polymake_to_oscar_vertex_index(P, v[i]) for i in 1:length(v)]
end
function _polymake_to_oscar_ray_index(P::Polymake.BigObject, i::Base.Integer)
return sum((<).(i, P.FAR_FACE))
end
function _polymake_to_oscar_ray_index(P::Polymake.BigObject, v::AbstractVector)
return [_polymake_to_oscar_ray_index(P, v[i]) for i in 1:length(v)]
end
@doc raw"""
minimal_faces(as, P::Polyhedron)
Return the minimal faces of a polyhedron as a `NamedTuple` with two iterators.
For a polyhedron without lineality, the `base_points` are the vertices. If `P`
has lineality `L`, then every minimal face is an affine translation `p+L`,
where `p` is only unique modulo `L`. The return type is a dict, the key
`:base_points` gives an iterator over such `p`, and the key `:lineality_basis`
lets one access a basis for the lineality space `L` of `P`.
# Examples
The polyhedron `P` is just a line through the origin:
```jldoctest
julia> P = convex_hull([0 0], nothing, [1 0])
Polyhedron in ambient dimension 2
julia> lineality_dim(P)
1
julia> vertices(P)
0-element SubObjectIterator{PointVector{QQFieldElem}}
julia> minimal_faces(P)
(base_points = PointVector{QQFieldElem}[[0, 0]], lineality_basis = RayVector{QQFieldElem}[[1, 0]])
```
"""
minimal_faces(P::Polyhedron{T}) where {T<:scalar_types} = minimal_faces(
NamedTuple{
(:base_points, :lineality_basis),
Tuple{SubObjectIterator{PointVector{T}},SubObjectIterator{RayVector{T}}},
},
P,
)
function minimal_faces(
::Type{
NamedTuple{
(:base_points, :lineality_basis),
Tuple{SubObjectIterator{PointVector{T}},SubObjectIterator{RayVector{T}}},
},
},
P::Polyhedron{T},
) where {T<:scalar_types}
return (base_points=_vertices(PointVector{T}, P), lineality_basis=lineality_space(P))
end
minimal_faces(::Type{<:PointVector}, P::Polyhedron) = _vertices(P)
@doc raw"""
rays_modulo_lineality(as, P::Polyhedron)
Return the rays of the recession cone of `P` up to lineality as a `NamedTuple`
with two iterators. If `P` has lineality `L`, then the iterator
`rays_modulo_lineality` iterates over representatives of the rays of `P/L`.
The iterator `lineality_basis` gives a basis of the lineality space `L`.
# Examples
```jldoctest
julia> P = convex_hull([0 0 1], [0 1 0], [1 0 0])
Polyhedron in ambient dimension 3
julia> rmlP = rays_modulo_lineality(P)
(rays_modulo_lineality = RayVector{QQFieldElem}[[0, 1, 0]], lineality_basis = RayVector{QQFieldElem}[[1, 0, 0]])
julia> rmlP.rays_modulo_lineality
1-element SubObjectIterator{RayVector{QQFieldElem}}:
[0, 1, 0]
julia> rmlP.lineality_basis
1-element SubObjectIterator{RayVector{QQFieldElem}}:
[1, 0, 0]
```
"""
rays_modulo_lineality(P::Polyhedron{T}) where {T<:scalar_types} = rays_modulo_lineality(
NamedTuple{
(:rays_modulo_lineality, :lineality_basis),
Tuple{SubObjectIterator{RayVector{T}},SubObjectIterator{RayVector{T}}},
},
P,
)
function rays_modulo_lineality(
::Type{
NamedTuple{
(:rays_modulo_lineality, :lineality_basis),
Tuple{SubObjectIterator{RayVector{T}},SubObjectIterator{RayVector{T}}},
},
},
P::Polyhedron{T},
) where {T<:scalar_types}
return (rays_modulo_lineality=_rays(P), lineality_basis=lineality_space(P))
end
rays_modulo_lineality(::Type{<:RayVector}, P::Polyhedron) = _rays(P)
@doc raw"""
vertices(as, P)
Return an iterator over the vertices of `P` in the format defined by `as`. The
vertices are defined to be the zero-dimensional faces, so if `P` has lineality,
there are no vertices.
Optional arguments for `as` include
* `PointVector`.
# Examples
The following code computes the vertices of the Minkowski sum of a triangle and
a square:
```jldoctest
julia> P = simplex(2) + cube(2);
julia> vertices(PointVector, P)
5-element SubObjectIterator{PointVector{QQFieldElem}}:
[-1, -1]
[2, -1]
[2, 1]
[-1, 2]
[1, 2]
```
"""
vertices(as::Type{PointVector{T}}, P::Polyhedron{T}) where {T<:scalar_types} =
lineality_dim(P) == 0 ? _vertices(as, P) : _empty_subobjectiterator(as, P)
_vertices(as::Type{PointVector{T}}, P::Polyhedron{T}) where {T<:scalar_types} =
SubObjectIterator{as}(P, _vertex_polyhedron, length(_vertex_indices(pm_object(P))))
_vertex_polyhedron(
U::Type{PointVector{T}}, P::Polyhedron{T}, i::Base.Integer
) where {T<:scalar_types} = point_vector(
coefficient_field(P),
@view pm_object(P).VERTICES[_vertex_indices(pm_object(P))[i], 2:end]
)::U
_point_matrix(::Val{_vertex_polyhedron}, P::Polyhedron; homogenized=false) =
@view pm_object(P).VERTICES[_vertex_indices(pm_object(P)), (homogenized ? 1 : 2):end]
_matrix_for_polymake(::Val{_vertex_polyhedron}) = _point_matrix
vertices(::Type{<:PointVector}, P::Polyhedron{T}) where {T<:scalar_types} =
vertices(PointVector{T}, P)
_vertices(::Type{<:PointVector}, P::Polyhedron{T}) where {T<:scalar_types} =
_vertices(PointVector{T}, P)
_facet_indices(::Val{_vertex_polyhedron}, P::Polyhedron) =
pm_object(P).FACETS_THRU_VERTICES[
_vertex_indices(pm_object(P)), _facet_indices(pm_object(P))
]
_incidencematrix(::Val{_vertex_polyhedron}) = _facet_indices
function _facet_indices(P::Polymake.BigObject)
vi = Polymake.get_attachment(P, "_facet_indices")
if isnothing(vi)
vi = Polymake.Vector{Polymake.to_cxx_type(Int64)}(
[
collect(1:(_facet_at_infinity(P) - 1))
collect((_facet_at_infinity(P) + 1):size(P.FACETS, 1))
],
)
Polymake.attach(P, "_facet_indices", vi)
end
return vi
end
@doc raw"""
vertices(P::Polyhedron)
Return an iterator over the vertices of a polyhedron `P` as points.
# Examples
The following code computes the vertices of the Minkowski sum of a triangle and
a square:
```jldoctest
julia> P = simplex(2) + cube(2);
julia> vertices(P)
5-element SubObjectIterator{PointVector{QQFieldElem}}:
[-1, -1]
[2, -1]
[2, 1]
[-1, 2]
[1, 2]
```
"""
vertices(P::Polyhedron) = vertices(PointVector, P)
_vertices(P::Polyhedron) = _vertices(PointVector, P)
@doc raw"""
n_rays(P::Polyhedron)
Return the number of rays of `P`, i.e. the number of rays of the recession cone
of `P`.
# Examples
The two-dimensional positive orthant has two rays.
```jldoctest
julia> PO = convex_hull([0 0],[1 0; 0 1])
Polyhedron in ambient dimension 2
julia> n_rays(PO)
2
```
The upper half-plane has no ray, since it has lineality.
```jldoctest
julia> UH = convex_hull([0 0],[0 1],[1 0]);
julia> n_rays(UH)
0
```
"""
n_rays(P::Polyhedron)::Int = lineality_dim(P) == 0 ? _n_rays(P) : 0
_n_rays(P::Polyhedron) = length(pm_object(P).FAR_FACE)
@doc raw"""
n_vertices(P::Polyhedron)
Return the number of vertices of `P`.
# Examples
The 3-cube's number of vertices can be obtained with this input:
```jldoctest
julia> C = cube(3);
julia> n_vertices(C)
8
```
"""
n_vertices(P::Polyhedron)::Int = lineality_dim(P) == 0 ? _n_vertices(P) : 0
_n_vertices(P::Polyhedron) = size(pm_object(P).VERTICES, 1)::Int - _n_rays(P)
@doc raw"""
rays(as::Type{T} = RayVector, P::Polyhedron)
Return a minimal set of generators of the cone of unbounded directions of `P`
(i.e. its rays) in the format defined by `as`.
Optional arguments for `as` include
* `RayVector`.
# Examples
We can verify that the positive orthant of the plane is generated by the two
rays in positive unit direction:
```jldoctest
julia> PO = convex_hull([0 0], [1 0; 0 1]);
julia> rays(RayVector, PO)
2-element SubObjectIterator{RayVector{QQFieldElem}}:
[1, 0]
[0, 1]
```
"""
rays(as::Type{RayVector{T}}, P::Polyhedron{T}) where {T<:scalar_types} =
lineality_dim(P) == 0 ? _rays(as, P) : _empty_subobjectiterator(as, P)
_rays(as::Type{RayVector{T}}, P::Polyhedron{T}) where {T<:scalar_types} =
SubObjectIterator{as}(P, _ray_polyhedron, length(_ray_indices(pm_object(P))))
_ray_polyhedron(
U::Type{RayVector{T}}, P::Polyhedron{T}, i::Base.Integer
) where {T<:scalar_types} = ray_vector(
coefficient_field(P), @view pm_object(P).VERTICES[_ray_indices(pm_object(P))[i], 2:end]
)::U
_facet_indices(::Val{_ray_polyhedron}, P::Polyhedron) =
pm_object(P).FACETS_THRU_RAYS[_ray_indices(pm_object(P)), _facet_indices(pm_object(P))]
_vector_matrix(::Val{_ray_polyhedron}, P::Polyhedron; homogenized=false) =
@view pm_object(P).VERTICES[_ray_indices(pm_object(P)), (homogenized ? 1 : 2):end]
_matrix_for_polymake(::Val{_ray_polyhedron}) = _vector_matrix
_incidencematrix(::Val{_ray_polyhedron}) = _facet_indices
rays(::Type{<:RayVector}, P::Polyhedron{T}) where {T<:scalar_types} = rays(RayVector{T}, P)
_rays(::Type{<:RayVector}, P::Polyhedron{T}) where {T<:scalar_types} =
_rays(RayVector{T}, P)
@doc raw"""
rays(P::Polyhedron)
Return minimal set of generators of the cone of unbounded directions of `P`
(i.e. its rays) as points.
# Examples
We can verify that the positive orthant of the plane is generated by the two
rays in positive unit direction:
```jldoctest
julia> PO = convex_hull([0 0], [1 0; 0 1]);
julia> rays(PO)
2-element SubObjectIterator{RayVector{QQFieldElem}}:
[1, 0]
[0, 1]
julia> matrix(QQ, rays(PO))
[1 0]
[0 1]
julia> matrix(ZZ, rays(PO))
[1 0]
[0 1]
```
"""
rays(P::Polyhedron) = rays(RayVector, P)
_rays(P::Polyhedron) = _rays(RayVector, P)
@doc raw"""
n_facets(P::Polyhedron)
Return the number of facets of `P`.
# Examples
The number of facets of the 5-dimensional cross polytope can be retrieved via
the following line:
```jldoctest
julia> n_facets(cross_polytope(5))
32
```
"""
function n_facets(P::Polyhedron)
n = size(pm_object(P).FACETS, 1)::Int
return n - (_facet_at_infinity(pm_object(P)) != n + 1)
end
@doc raw"""
facets(as::Type{T} = AffineHalfspace, P::Polyhedron)
Return the facets of `P` in the format defined by `as`.
The allowed values for `as` are
* `Halfspace`,
* `Polyhedron`,
* `Pair`.
# Examples
We can retrieve the six facets of the 3-dimensional cube this way:
```jldoctest
julia> C = cube(3);
julia> facets(Polyhedron, C)
6-element SubObjectIterator{Polyhedron{QQFieldElem}}:
Polytope in ambient dimension 3
Polytope in ambient dimension 3
Polytope in ambient dimension 3
Polytope in ambient dimension 3
Polytope in ambient dimension 3
Polytope in ambient dimension 3
julia> facets(Halfspace, C)
6-element SubObjectIterator{AffineHalfspace{QQFieldElem}} over the Halfspaces of R^3 described by:
-x_1 <= 1
x_1 <= 1
-x_2 <= 1
x_2 <= 1
-x_3 <= 1
x_3 <= 1
```
"""
facets(
as::Type{T}, P::Polyhedron{S}
) where {S<:scalar_types,T<:Union{AffineHalfspace{S},Pair{R,S} where R,Polyhedron{S}}} =
SubObjectIterator{as}(P, _facet_polyhedron, n_facets(P))
function _facet_polyhedron(
U::Type{AffineHalfspace{S}}, P::Polyhedron{S}, i::Base.Integer
) where {S<:scalar_types}
h = decompose_hdata(view(pm_object(P).FACETS, [_facet_index(pm_object(P), i)], :))
return affine_halfspace(coefficient_field(P), h[1], h[2][])::U
end
function _facet_polyhedron(
U::Type{Pair{R,S}}, P::Polyhedron{S}, i::Base.Integer
) where {R,S<:scalar_types}
f = coefficient_field(P)
h = decompose_hdata(view(pm_object(P).FACETS, [_facet_index(pm_object(P), i)], :))
return U(f.(view(h[1], :, :)), f(h[2][])) # view_broadcast
end
function _facet_polyhedron(
::Type{Polyhedron{T}}, P::Polyhedron{T}, i::Base.Integer
) where {T<:scalar_types}
return Polyhedron{T}(
Polymake.polytope.facet(pm_object(P), _facet_index(pm_object(P), i) - 1),
coefficient_field(P),
)
end
_affine_inequality_matrix(::Val{_facet_polyhedron}, P::Polyhedron) =
-_remove_facet_at_infinity(pm_object(P))
_affine_matrix_for_polymake(::Val{_facet_polyhedron}) = _affine_inequality_matrix
_vertex_indices(::Val{_facet_polyhedron}, P::Polyhedron) = vcat(
pm_object(P).VERTICES_IN_FACETS[
1:(_facet_at_infinity(pm_object(P)) - 1), _vertex_indices(pm_object(P))
],
pm_object(P).VERTICES_IN_FACETS[
(_facet_at_infinity(pm_object(P)) + 1):end, _vertex_indices(pm_object(P))
],
)
_ray_indices(::Val{_facet_polyhedron}, P::Polyhedron) = vcat(
pm_object(P).VERTICES_IN_FACETS[
1:(_facet_at_infinity(pm_object(P)) - 1), _ray_indices(pm_object(P))
],
pm_object(P).VERTICES_IN_FACETS[
(_facet_at_infinity(pm_object(P)) + 1):end, _ray_indices(pm_object(P))
],
)
_vertex_and_ray_indices(::Val{_facet_polyhedron}, P::Polyhedron) = vcat(
pm_object(P).VERTICES_IN_FACETS[1:(_facet_at_infinity(pm_object(P)) - 1), :],
pm_object(P).VERTICES_IN_FACETS[(_facet_at_infinity(pm_object(P)) + 1):end, :],
)
_incidencematrix(::Val{_facet_polyhedron}) = _vertex_and_ray_indices
facets(::Type{<:Pair}, P::Polyhedron{T}) where {T<:scalar_types} =
facets(Pair{Matrix{T},T}, P)
facets(::Type{Polyhedron}, P::Polyhedron{T}) where {T<:scalar_types} =
facets(Polyhedron{T}, P)
@doc raw"""
facets(P::Polyhedron)
Return the facets of `P` as halfspaces.
# Examples
We can retrieve the six facets of the 3-dimensional cube this way:
```jldoctest
julia> C = cube(3);
julia> facets(C)
6-element SubObjectIterator{AffineHalfspace{QQFieldElem}} over the Halfspaces of R^3 described by:
-x_1 <= 1
x_1 <= 1
-x_2 <= 1
x_2 <= 1
-x_3 <= 1
x_3 <= 1
```
"""
facets(P::Polyhedron{T}) where {T<:scalar_types} = facets(AffineHalfspace{T}, P)
facets(::Type{<:Halfspace}, P::Polyhedron{T}) where {T<:scalar_types} =
facets(AffineHalfspace{T}, P)
function _facet_index(P::Polymake.BigObject, i::Base.Integer)
i < _facet_at_infinity(P) && return i
return i + 1
end
function _facet_at_infinity(P::Polymake.BigObject)
fai = Polymake.get_attachment(P, "_facet_at_infinity")
m = size(P.FACETS, 1)
if isnothing(fai)
i = 1
while i <= m
_is_facet_at_infinity(view(P.FACETS, i, :)) && break
i += 1
end
fai = i
Polymake.attach(P, "_facet_at_infinity", fai)
end
return fai::Int64
end
_is_facet_at_infinity(v::AbstractVector) = v[1] >= 0 && iszero(v[2:end])
_remove_facet_at_infinity(P::Polymake.BigObject) = view(
P.FACETS,
[
collect(1:(_facet_at_infinity(P) - 1))
collect((_facet_at_infinity(P) + 1):size(P.FACETS, 1))
],
:,
)
###############################################################################
###############################################################################
### Access properties
###############################################################################
###############################################################################
###############################################################################
## Scalar properties
###############################################################################
@doc raw"""
lineality_dim(P::Polyhedron)
Return the dimension of the lineality space, i.e. the dimension of the largest
affine subspace contained in `P`.
# Examples
Polyhedron with one lineality direction.
```jldoctest
julia> C = convex_hull([0 0], [1 0], [1 1])
Polyhedron in ambient dimension 2
julia> lineality_dim(C)
1
```
"""
lineality_dim(P::Polyhedron) = pm_object(P).LINEALITY_DIM::Int
@doc raw"""
volume(P::Polyhedron)
Return the (Euclidean) volume of `P`.
# Examples
```jldoctest
julia> C = cube(2);
julia> volume(C)
4
```
"""
volume(P::Polyhedron{T}) where {T<:scalar_types} =
coefficient_field(P)((pm_object(P)).VOLUME)
@doc raw"""
lattice_volume(P::Polyhedron{QQFieldElem})
Return the lattice volume of `P`.
# Examples
```jldoctest
julia> C = cube(2);
julia> lattice_volume(C)
8
```
"""
lattice_volume(P::Polyhedron{QQFieldElem})::ZZRingElem =
_assert_lattice(P) && pm_object(P).LATTICE_VOLUME
@doc raw"""
normalized_volume(P::Polyhedron)
Return the (normalized) volume of `P`.
# Examples
```jldoctest
julia> C = cube(2);
julia> normalized_volume(C)
8
```
"""
normalized_volume(P::Polyhedron) =
coefficient_field(P)(factorial(dim(P)) * (pm_object(P)).VOLUME)
@doc raw"""
dim(P::Polyhedron)
Return the dimension of `P`.
# Examples
```jldoctest
julia> V = [1 2 3; 1 3 2; 2 1 3; 2 3 1; 3 1 2; 3 2 1];
julia> P = convex_hull(V);
julia> dim(P)
2
```
"""
dim(P::Polyhedron) = Polymake.polytope.dim(pm_object(P))::Int
@doc raw"""
lattice_points(P::Polyhedron{QQFieldElem})
Return the integer points contained in the bounded polyhedron `P`.
# Examples
```jldoctest
julia> S = 2 * simplex(2);
julia> lattice_points(S)
6-element SubObjectIterator{PointVector{ZZRingElem}}:
[0, 0]
[0, 1]
[0, 2]
[1, 0]
[1, 1]
[2, 0]
julia> matrix(ZZ, lattice_points(S))
[0 0]
[0 1]
[0 2]
[1 0]
[1 1]
[2 0]
```
"""
function lattice_points(P::Polyhedron{QQFieldElem})
@req pm_object(P).BOUNDED "Polyhedron not bounded"
return SubObjectIterator{PointVector{ZZRingElem}}(
P, _lattice_point, size(pm_object(P).LATTICE_POINTS_GENERATORS[1], 1)
)
end
_lattice_point(
T::Type{PointVector{ZZRingElem}}, P::Polyhedron{QQFieldElem}, i::Base.Integer
) = point_vector(ZZ, @view pm_object(P).LATTICE_POINTS_GENERATORS[1][i, 2:end])::T
_point_matrix(::Val{_lattice_point}, P::Polyhedron; homogenized=false) =
@view pm_object(P).LATTICE_POINTS_GENERATORS[1][:, (homogenized ? 1 : 2):end]
_matrix_for_polymake(::Val{_lattice_point}) = _point_matrix
@doc raw"""
interior_lattice_points(P::Polyhedron{QQFieldElem})
Return the integer points contained in the interior of the bounded polyhedron
`P`.
# Examples
```jldoctest
julia> c = cube(3)
Polytope in ambient dimension 3
julia> interior_lattice_points(c)
1-element SubObjectIterator{PointVector{ZZRingElem}}:
[0, 0, 0]
julia> matrix(ZZ, interior_lattice_points(c))
[0 0 0]
```
"""
function interior_lattice_points(P::Polyhedron{QQFieldElem})
@req pm_object(P).BOUNDED "Polyhedron not bounded"
return SubObjectIterator{PointVector{ZZRingElem}}(
P, _interior_lattice_point, size(pm_object(P).INTERIOR_LATTICE_POINTS, 1)
)
end
_interior_lattice_point(
T::Type{PointVector{ZZRingElem}}, P::Polyhedron{QQFieldElem}, i::Base.Integer
) = point_vector(ZZ, @view pm_object(P).INTERIOR_LATTICE_POINTS[i, 2:end])::T
_point_matrix(::Val{_interior_lattice_point}, P::Polyhedron; homogenized=false) =
if homogenized
pm_object(P).INTERIOR_LATTICE_POINTS
else
@view pm_object(P).INTERIOR_LATTICE_POINTS[:, 2:end]
end
_matrix_for_polymake(::Val{_interior_lattice_point}) = _point_matrix
@doc raw"""
boundary_lattice_points(P::Polyhedron{QQFieldElem})
Return the integer points contained in the boundary of the bounded polyhedron
`P`.
# Examples
```jldoctest
julia> c = polarize(cube(3))
Polytope in ambient dimension 3
julia> boundary_lattice_points(c)
6-element SubObjectIterator{PointVector{ZZRingElem}}:
[-1, 0, 0]
[0, -1, 0]
[0, 0, -1]
[0, 0, 1]
[0, 1, 0]
[1, 0, 0]
julia> matrix(ZZ, boundary_lattice_points(c))
[-1 0 0]
[ 0 -1 0]
[ 0 0 -1]
[ 0 0 1]
[ 0 1 0]
[ 1 0 0]
```
"""
function boundary_lattice_points(P::Polyhedron{QQFieldElem})
@req pm_object(P).BOUNDED "Polyhedron not bounded"
return SubObjectIterator{PointVector{ZZRingElem}}(
P, _boundary_lattice_point, size(pm_object(P).BOUNDARY_LATTICE_POINTS, 1)
)
end
_boundary_lattice_point(
T::Type{PointVector{ZZRingElem}}, P::Polyhedron{QQFieldElem}, i::Base.Integer
) = point_vector(ZZ, @view pm_object(P).BOUNDARY_LATTICE_POINTS[i, 2:end])::T
_point_matrix(::Val{_boundary_lattice_point}, P::Polyhedron; homogenized=false) =
if homogenized
pm_object(P).BOUNDARY_LATTICE_POINTS
else
@view pm_object(P).BOUNDARY_LATTICE_POINTS[:, 2:end]
end
_matrix_for_polymake(::Val{_boundary_lattice_point}) = _point_matrix
@doc raw"""
ambient_dim(P::Polyhedron)
Return the ambient dimension of `P`.
# Examples
```jldoctest
julia> V = [1 2 3; 1 3 2; 2 1 3; 2 3 1; 3 1 2; 3 2 1];
julia> P = convex_hull(V);
julia> ambient_dim(P)
3
```
"""
ambient_dim(P::Polyhedron) = Polymake.polytope.ambient_dim(pm_object(P))::Int
@doc raw"""
codim(P::Polyhedron)
Return the codimension of `P`.
# Examples
```jldoctest
julia> V = [1 2 3; 1 3 2; 2 1 3; 2 3 1; 3 1 2; 3 2 1];
julia> P = convex_hull(V);
julia> codim(P)
1
```
"""
codim(P::Polyhedron) = ambient_dim(P) - dim(P)
@doc raw"""
facet_sizes(P::Polyhedron{T})
Number of vertices in each facet.
# Example
```jldoctest
julia> p = johnson_solid(4)
Polytope in ambient dimension 3 with EmbeddedAbsSimpleNumFieldElem type coefficients
julia> facet_sizes(p)
10-element Vector{Int64}:
8
4
3
4
4
3
4
3
3
4
```
"""
function facet_sizes(P::Polyhedron{T}) where {T<:scalar_types}
im = vertex_indices(facets(P))
return [length(row(im, i)) for i in 1:nrows(im)]
end
@doc raw"""
vertex_sizes(P::Polyhedron{T})
Number of incident facets for each vertex.
# Example
```jldoctest
julia> vertex_sizes(bipyramid(simplex(2)))
5-element Vector{Int64}:
4
4
4
3
3
```
"""
function vertex_sizes(P::Polyhedron{T}) where {T<:scalar_types}
pm_object(P).LINEALITY_DIM > 0 && return Vector{Int}()
res = Vector{Int}(pm_object(P).VERTEX_SIZES)
vertices = Polymake.to_one_based_indexing(
Polymake.polytope.bounded_vertices(pm_object(P))
)
return res[collect(vertices)]
end
###############################################################################
## Points properties
###############################################################################
# Previously: This implementation is not correct. Ask Taylor.
# Taylor: lineality space generators always look like [0, v] so
# v is a natural output.
@doc raw"""
lineality_space(P::Polyhedron)
Return a matrix whose row span is the lineality space of `P`.
# Examples
Despite not being reflected in this construction of the upper half-plane,
its lineality in $x$-direction is recognized:
```jldoctest
julia> UH = convex_hull([0 0],[0 1; 1 0; -1 0]);
julia> lineality_space(UH)
1-element SubObjectIterator{RayVector{QQFieldElem}}:
[1, 0]
```
"""
lineality_space(P::Polyhedron{T}) where {T<:scalar_types} =
SubObjectIterator{RayVector{T}}(P, _lineality_polyhedron, lineality_dim(P))
_lineality_polyhedron(
U::Type{RayVector{T}}, P::Polyhedron{T}, i::Base.Integer
) where {T<:scalar_types} =
ray_vector(coefficient_field(P), @view pm_object(P).LINEALITY_SPACE[i, 2:end])::U
_generator_matrix(::Val{_lineality_polyhedron}, P::Polyhedron; homogenized=false) =
homogenized ? pm_object(P).LINEALITY_SPACE : @view pm_object(P).LINEALITY_SPACE[:, 2:end]
_matrix_for_polymake(::Val{_lineality_polyhedron}) = _generator_matrix
@doc raw"""
affine_hull(P::Polytope)
Return the (affine) hyperplanes generating the affine hull of `P`.
# Examples
This triangle in $\mathbb{R}^4$ is contained in the plane defined by
$P = \{ (x_1, x_2, x_3, x_4) | x_3 = 2 ∧ x_4 = 5 \}$.
```jldoctest
julia> t = convex_hull([0 0 2 5; 1 0 2 5; 0 1 2 5]);
julia> affine_hull(t)