-
Notifications
You must be signed in to change notification settings - Fork 113
/
helpers.jl
737 lines (615 loc) · 22.1 KB
/
helpers.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
import Polymake: IncidenceMatrix
@doc raw"""
IncidenceMatrix
A matrix with boolean entries. Each row corresponds to a fixed element of a collection of mathematical objects and the same holds for the columns and a second (possibly equal) collection. A `1` at entry `(i, j)` is interpreted as an incidence between object `i` of the first collection and object `j` of the second one.
# Examples
Note that the input and print of an `IncidenceMatrix` lists the non-zero indices for each row.
```jldoctest
julia> IM = IncidenceMatrix([[1,2,3],[4,5,6]])
2×6 IncidenceMatrix
[1, 2, 3]
[4, 5, 6]
julia> IM[1, 2]
true
julia> IM[2, 3]
false
julia> IM[:, 4]
2-element SparseVectorBool
[2]
```
"""
IncidenceMatrix
number_of_rows(i::IncidenceMatrix) = Polymake.nrows(i)
number_of_columns(i::IncidenceMatrix) = Polymake.ncols(i)
number_of_rows(A::Polymake.Matrix) = Polymake.nrows(A)
number_of_columns(A::Polymake.Matrix) = Polymake.ncols(A)
@doc raw"""
row(i::IncidenceMatrix, n::Int)
Return the indices where the `n`-th row of `i` is `true`, as a `Set{Int}`.
# Examples
```jldoctest
julia> IM = IncidenceMatrix([[1,2,3],[4,5,6]])
2×6 IncidenceMatrix
[1, 2, 3]
[4, 5, 6]
julia> row(IM, 2)
Set{Int64} with 3 elements:
5
4
6
```
"""
row(i::IncidenceMatrix, n::Int) = convert(Set{Int}, Polymake.row(i, n))
@doc raw"""
column(i::IncidenceMatrix, n::Int)
Return the indices where the `n`-th column of `i` is `true`, as a `Set{Int}`.
# Examples
```jldoctest
julia> IM = IncidenceMatrix([[1,2,3],[4,5,6]])
2×6 IncidenceMatrix
[1, 2, 3]
[4, 5, 6]
julia> column(IM, 5)
Set{Int64} with 1 element:
2
```
"""
column(i::IncidenceMatrix, n::Int) = convert(Set{Int}, Polymake.col(i, n))
const _polymake_scalars = Union{
Polymake.Integer,
Polymake.Rational,
Polymake.QuadraticExtension,
Polymake.OscarNumber,
Float64,
Polymake.TropicalNumber,
}
const _polymake_compatible_scalars = Union{
QQFieldElem,ZZRingElem,Base.Integer,Base.Rational,_polymake_scalars
}
function assure_matrix_polymake(m::Union{AbstractMatrix{Any},AbstractMatrix{FieldElem}})
a, b = size(m)
if a > 0
i = findfirst(_cannot_convert_to_fmpq, m)
t = i === nothing ? QQFieldElem : typeof(m[i])
if t <: _polymake_scalars
m = Polymake.Matrix{Polymake.convert_to_pm_type(t)}(m)
else
m = Polymake.Matrix{_scalar_type_to_polymake(t)}(m)
end
else
m = Polymake.Matrix{Polymake.Rational}(undef, a, b)
end
return m
end
assure_matrix_polymake(m::AbstractMatrix{<:FieldElem}) =
Polymake.Matrix{Polymake.OscarNumber}(m)
assure_matrix_polymake(m::MatElem) = Polymake.Matrix{_scalar_type_to_polymake(eltype(m))}(m)
assure_matrix_polymake(
m::Union{Oscar.ZZMatrix,Oscar.QQMatrix,AbstractMatrix{<:_polymake_compatible_scalars}}
) = m
assure_matrix_polymake(m::SubArray{T,2,U,V,W}) where {T<:Union{_polymake_scalars},U,V,W} =
Polymake.Matrix{T}(m)
function assure_vector_polymake(v::Union{AbstractVector{Any},AbstractVector{FieldElem}})
i = findfirst(_cannot_convert_to_fmpq, v)
T = i === nothing ? QQFieldElem : typeof(v[i])
return Polymake.Vector{_scalar_type_to_polymake(T)}(v)
end
assure_vector_polymake(v::AbstractVector{<:FieldElem}) =
Polymake.Vector{Polymake.OscarNumber}(v)
assure_vector_polymake(v::AbstractVector{<:_polymake_compatible_scalars}) = v
affine_matrix_for_polymake(x::Tuple{<:AnyVecOrMat,<:AbstractVector}) =
augment(unhomogenized_matrix(x[1]), -Vector(assure_vector_polymake(x[2])))
affine_matrix_for_polymake(x::Tuple{<:AnyVecOrMat,<:Any}) = homogenized_matrix(x[1], -x[2])
_cannot_convert_to_fmpq(x::Any) = !hasmethod(convert, Tuple{Type{QQFieldElem},typeof(x)})
linear_matrix_for_polymake(x::Union{Oscar.ZZMatrix,Oscar.QQMatrix,AbstractMatrix}) =
assure_matrix_polymake(x)
linear_matrix_for_polymake(x::AbstractVector{<:AbstractVector}) =
assure_matrix_polymake(stack(x...))
matrix_for_polymake(x::Union{Oscar.ZZMatrix,Oscar.QQMatrix,AbstractMatrix}) =
assure_matrix_polymake(x)
number_of_rows(x::SubArray{T,2,U,V,W}) where {T,U,V,W} = size(x, 1)
_isempty_halfspace(x::Pair{<:Union{Oscar.MatElem,AbstractMatrix},Any}) = isempty(x[1])
_isempty_halfspace(x) = isempty(x)
function Polymake.Matrix{T}(
x::Union{MatElem,AbstractMatrix{<:FieldElem}}
) where {T<:_polymake_scalars}
res = Polymake.Matrix{T}(size(x)...)
return res .= x
end
Base.convert(::Type{Polymake.Matrix{T}}, x::MatElem) where {T} = Polymake.Matrix{T}(x)
Base.convert(::Type{Polymake.QuadraticExtension{Polymake.Rational}}, x::QQFieldElem) =
Polymake.QuadraticExtension(convert(Polymake.Rational, x))
Base.convert(::Type{<:Polymake.Integer}, x::ZZRingElem) =
GC.@preserve x return Polymake.new_integer_from_fmpz(x)
Base.convert(::Type{<:Polymake.Rational}, x::QQFieldElem) =
GC.@preserve x return Polymake.new_rational_from_fmpq(x)
Base.convert(::Type{<:Polymake.Rational}, x::ZZRingElem) =
GC.@preserve x return Polymake.new_rational_from_fmpz(x)
Base.convert(::Type{<:Polymake.Integer}, x::QQFieldElem) =
GC.@preserve x return Polymake.new_integer_from_fmpq(x)
function Base.convert(::Type{ZZRingElem}, x::Polymake.Integer)
res = ZZRingElem()
GC.@preserve x Polymake.new_fmpz_from_integer(x, pointer_from_objref(res))
return res
end
function Base.convert(::Type{QQFieldElem}, x::Polymake.Rational)
res = QQFieldElem()
GC.@preserve x Polymake.new_fmpq_from_rational(x, pointer_from_objref(res))
return res
end
function Base.convert(::Type{QQFieldElem}, x::Polymake.Integer)
res = QQFieldElem()
GC.@preserve x Polymake.new_fmpq_from_integer(x, pointer_from_objref(res))
return res
end
function Base.convert(::Type{ZZRingElem}, x::Polymake.Rational)
res = ZZRingElem()
GC.@preserve x Polymake.new_fmpz_from_rational(x, pointer_from_objref(res))
return res
end
Base.convert(::Type{Polymake.OscarNumber}, x::FieldElem) = Polymake.OscarNumber(x)
(::Type{T})(x::Polymake.OscarNumber) where {T<:FieldElem} = convert(T, Polymake.unwrap(x))
(R::QQField)(x::Polymake.Rational) = convert(QQFieldElem, x)
(Z::ZZRing)(x::Polymake.Rational) = convert(ZZRingElem, x)
function (NF::Hecke.EmbeddedNumField)(x::Polymake.QuadraticExtension{Polymake.Rational})
g = Polymake.generating_field_elements(x)
if g.r == 0 || g.b == 0
return NF(convert(QQFieldElem, g.a))
end
isq = Hecke.is_quadratic_type(number_field(NF))
@req isq[1] "Can not construct non-trivial QuadraticExtension in non-quadratic number field."
@req isq[2] == base_field(number_field(NF))(g.r) "Source and target fields do not match."
a = NF(basis(number_field(NF))[2])
return convert(QQFieldElem, g.a) + convert(QQFieldElem, g.b) * a
end
(F::Field)(x::Polymake.Rational) = F(QQ(x))
(F::Field)(x::Polymake.OscarNumber) = F(Polymake.unwrap(x))
# Disambiguation
(F::QQBarField)(x::Polymake.Rational) = F(QQ(x))
(F::QQBarField)(x::Polymake.OscarNumber) = F(Polymake.unwrap(x))
Polymake.convert_to_pm_type(::Type{typeof(min)}) = Polymake.Min
Polymake.convert_to_pm_type(::Type{typeof(max)}) = Polymake.Max
Polymake.convert_to_pm_type(::Type{ZZMatrix}) = Polymake.Matrix{Polymake.Integer}
Polymake.convert_to_pm_type(::Type{QQMatrix}) = Polymake.Matrix{Polymake.Rational}
Polymake.convert_to_pm_type(::Type{ZZRingElem}) = Polymake.Integer
Polymake.convert_to_pm_type(::Type{QQFieldElem}) = Polymake.Rational
Polymake.convert_to_pm_type(::Type{T}) where {T<:FieldElem} = Polymake.OscarNumber
Polymake.convert_to_pm_type(::Type{<:MatElem{T}}) where {T} =
Polymake.Matrix{Polymake.convert_to_pm_type(T)}
Polymake.convert_to_pm_type(::Type{<:Graph{T}}) where {T<:Union{Directed,Undirected}} =
Polymake.Graph{T}
Base.convert(
::Type{<:Polymake.Graph{T}}, g::Graph{T}
) where {T<:Union{Directed,Undirected}} = Oscar.pm_object(g)
function remove_zero_rows(A::AbstractMatrix)
A[findall(x -> !iszero(x), collect(eachrow(A))), :]
end
function remove_zero_rows(A::AbstractMatrix{Float64})
A[
findall(
x -> !isapprox(x, zero(x); atol=Polymake._get_global_epsilon()), collect(eachrow(A))
),
:,
]
end
function remove_zero_rows(A::Oscar.MatElem)
remove_zero_rows(Matrix(A))
end
# function remove_redundant_rows(A::Union{Oscar.MatElem,AbstractMatrix})
# rindices = Polymake.Set{Polymake.to_cxx_type(Int64)}(1:size(A, 1))
# for i in rindices
# for j in rindices
# if i == j
# continue
# end
# if A[i, :] == A[j, :]
# delete!(rindices, j)
# end
# end
# end
# return A[rindices]
# end
function augment(vec::AbstractVector, val)
s = size(vec)
@req s[1] > 0 "cannot homogenize empty vector"
res = similar(vec, (s[1] + 1,))
res[1] = val + zero(first(vec))
res[2:end] = vec
return assure_vector_polymake(res)
end
function augment(mat::AbstractMatrix, vec::AbstractVector)
s = size(mat)
res = similar(mat, promote_type(eltype(mat), eltype(vec)), (s[1], s[2] + 1))
res[:, 1] = vec
res[:, 2:end] = mat
return assure_matrix_polymake(res)
end
homogenize(vec::AbstractVector, val::Number=0) = augment(vec, val)
homogenize(mat::AbstractMatrix, val::Number=1) = augment(mat, fill(val, size(mat, 1)))
homogenize(mat::MatElem, val::Number=1) = homogenize(Matrix(mat), val)
homogenize(nothing, val::Number) = nothing
homogenized_matrix(x::Union{AbstractVecOrMat,MatElem,Nothing}, val::Number) =
homogenize(x, val)
homogenized_matrix(x::AbstractVector, val::Number) = permutedims(homogenize(x, val))
homogenized_matrix(x::AbstractVector{<:AbstractVector}, val::Number) =
stack((homogenize(x[i], val) for i in 1:length(x))...)
dehomogenize(vec::AbstractVector) = vec[2:end]
dehomogenize(mat::AbstractMatrix) = mat[:, 2:end]
unhomogenized_matrix(x::AbstractVector) = assure_matrix_polymake(stack(x))
unhomogenized_matrix(x::AbstractMatrix) = assure_matrix_polymake(x)
unhomogenized_matrix(x::MatElem) = Matrix(assure_matrix_polymake(x))
unhomogenized_matrix(x::AbstractVector{<:AbstractVector}) =
unhomogenized_matrix(stack(x...))
"""
stack(A::AbstractVecOrMat, B::AbstractVecOrMat)
Stacks `A` and `B` vertically. The difference to `vcat`is that `AbstractVector`s are always
interpreted as row vectors. Empty vectors are ignored.
# Examples
```
julia> stack([1, 2], [0, 0])
2×2 Matrix{Int64}:
1 2
0 0
julia> stack([1 2], [0 0])
2×2 Matrix{Int64}:
1 2
0 0
julia> stack([1 2], [0, 0])
2×2 Matrix{Int64}:
1 2
0 0
julia> stack([1, 2], [0 0])
2×2 Matrix{Int64}:
1 2
0 0
julia> stack([1 2], [])
1×2 Matrix{Int64}:
1 2
```
"""
stack(A::AbstractMatrix, ::Nothing) = A
stack(::Nothing, B::AbstractMatrix) = B
stack(A::AbstractMatrix, B::AbstractMatrix) = [A; B]
stack(A::AbstractMatrix, B::AbstractVector) = isempty(B) ? A : [A; permutedims(B)]
stack(A::AbstractVector, B::AbstractMatrix) = isempty(A) ? B : [permutedims(A); B]
stack(A::AbstractVector, B::AbstractVector) =
isempty(A) ? B : [permutedims(A); permutedims(B)]
stack(A::AbstractVector, ::Nothing) = permutedims(A)
stack(::Nothing, B::AbstractVector) = permutedims(B)
stack(x, y, z...) = stack(stack(x, y), z...)
stack(x) = stack(x, nothing)
# stack(x::Union{QQMatrix, ZZMatrix}, ::Nothing) = x
#=
function stack(A::Vector{Polymake.Vector{Polymake.Rational}})
if length(A)==2
return stack(A[1],A[2])
end
M=stack(A[1],A[2])
for i in 3:length(A)
M=stack(M,A[i])
end
return M
end
=#
_ambient_dim(x::AbstractVector) = length(x)
_ambient_dim(x::AbstractMatrix) = size(x, 2)
_ambient_dim(x::AbstractVector{<:AbstractVector}) = _ambient_dim(x[1])
_ambient_dim(x::MatElem) = ncols(x)
"""
decompose_vdata(A::AbstractMatrix)
Given a (homogeneous) polymake matrix split into vertices and rays and dehomogenize.
"""
function decompose_vdata(A::AbstractMatrix)
vertex_indices = findall(!iszero, view(A, :, 1))
ray_indices = findall(iszero, view(A, :, 1))
return (vertices=A[vertex_indices, 2:end], rays=A[ray_indices, 2:end])
end
function decompose_hdata(A)
(A=-A[:, 2:end], b=A[:, 1])
end
# TODO: different printing within oscar? if yes, implement the following method
# Base.show(io::IO, ::MIME"text/plain", I::IncidenceMatrix) = show(io, "text/plain", Matrix{Bool}(I))
####################################################################
# Prepare interface for objects to be defined later
####################################################################
abstract type PolyhedralObject{T} end
# several toric types need to inherit from abstract schemes and thus cannot
# inherit from PolyhedralObject, but they still need to behave like polyhedral objects
# for some operations.
const PolyhedralObjectUnion = Union{PolyhedralObject,NormalToricVarietyType}
@doc raw"""
coefficient_field(P::Union{Polyhedron{T}, Cone{T}, PolyhedralFan{T}, PolyhedralComplex{T}) where T<:scalar_types
Return the parent `Field` of the coefficients of `P`.
# Examples
```jldoctest
julia> c = cross_polytope(2)
Polytope in ambient dimension 2
julia> coefficient_field(c)
Rational field
```
"""
coefficient_field(x::PolyhedralObject) = x.parent_field
coefficient_field(x::PolyhedralObject{QQFieldElem}) = QQ
_get_scalar_type(::PolyhedralObject{T}) where {T} = T
_get_scalar_type(::NormalToricVarietyType) = QQFieldElem
################################################################################
######## Scalar types
################################################################################
const scalar_types = Union{FieldElem,Float64}
const scalar_type_to_oscar = Dict{String,Type}([
("Rational", QQFieldElem),
("QuadraticExtension<Rational>", Hecke.EmbeddedNumFieldElem{AbsSimpleNumFieldElem}),
("QuadraticExtension", Hecke.EmbeddedNumFieldElem{AbsSimpleNumFieldElem}),
("Float", Float64),
])
const scalar_types_extended = Union{scalar_types,ZZRingElem}
const scalar_type_or_field = Union{Type{<:scalar_types},Field}
_scalar_type_to_polymake(::Type{QQFieldElem}) = Polymake.Rational
_scalar_type_to_polymake(::Type{<:FieldElem}) = Polymake.OscarNumber
_scalar_type_to_polymake(::Type{Float64}) = Float64
####################################################################
# Parent Fields
####################################################################
function _embedded_quadratic_field(r::ZZRingElem)
if iszero(r)
R, = rationals_as_number_field()
return Hecke.embedded_field(R, real_embeddings(R)[])
else
R, = quadratic_field(r)
return Hecke.embedded_field(R, real_embeddings(R)[2])
end
end
function _check_field_polyhedral(::Type{T}) where {T}
@req !(T <: NumFieldElem) "Number fields must be embedded, e.g. via `embedded_number_field`."
@req hasmethod(isless, (T, T)) "Field must be ordered and have `isless` method."
end
_find_elem_type(x::Nothing) = Any
_find_elem_type(x::Any) = typeof(x)
_find_elem_type(x::Type) = x
_find_elem_type(x::Polymake.Rational) = QQFieldElem
_find_elem_type(x::Polymake.Integer) = ZZRingElem
_find_elem_type(x::AbstractArray) = reshape(_find_elem_type.(x), :)
_find_elem_type(x::Tuple) = vcat(_find_elem_type.(x)...)
_find_elem_type(x::AbstractArray{<:AbstractArray}) = vcat(_find_elem_type.(x)...)
_find_elem_type(x::MatElem) = elem_type(base_ring(x))
function _guess_fieldelem_type(x...)
types = filter(!=(Any), vcat(_find_elem_type.(x)...))
T = QQFieldElem
for t in types
if t == Float64
return Float64
elseif promote_type(t, T) != T
T = t
end
end
return T
end
_parent_or_coefficient_field(::Type{Float64}, x...) = AbstractAlgebra.Floats{Float64}()
_parent_or_coefficient_field(::Type{ZZRingElem}, x...) = ZZ
_parent_or_coefficient_field(r::Base.RefValue{<:Union{FieldElem,ZZRingElem}}, x...) =
parent(r.x)
_parent_or_coefficient_field(v::AbstractArray{T}) where {T<:Union{FieldElem,ZZRingElem}} =
_parent_or_coefficient_field(T, v)
# QQ is done as a special case here to avoid ambiguities
function _parent_or_coefficient_field(::Type{T}, e::Any) where {T<:FieldElem}
hasmethod(parent, (typeof(e),)) && elem_type(parent(e)) <: T ? parent(e) : missing
end
function _parent_or_coefficient_field(::Type{T}, c::MatElem) where {T<:FieldElem}
elem_type(base_ring(c)) <: T ? base_ring(c) : missing
end
function _parent_or_coefficient_field(::Type{T}, c::AbstractArray) where {T<:FieldElem}
first([collect(skipmissing(_parent_or_coefficient_field.(Ref(T), c))); missing])
end
function _parent_or_coefficient_field(::Type{T}, x, y...) where {T<:scalar_types}
for c in (x, y...)
p = _parent_or_coefficient_field(T, c)
if p !== missing && elem_type(p) <: T
return p
end
end
missing
end
function _determine_parent_and_scalar(f::Union{Field,ZZRing}, x...)
_check_field_polyhedral(elem_type(f))
return (f, elem_type(f))
end
function _determine_parent_and_scalar(::Type{T}, x...) where {T<:scalar_types}
T == QQFieldElem && return (QQ, QQFieldElem)
p = _parent_or_coefficient_field(T, x...)
@req p !== missing "Scalars of type $T require specification of a parent field. Please pass the desired Field instead of the type or have a $T contained in your input data."
_check_field_polyhedral(elem_type(p))
return (p, elem_type(p))
end
function _detect_default_field(
::Type{Hecke.EmbeddedNumFieldElem{AbsSimpleNumFieldElem}}, p::Polymake.BigObject
)
# we only want to check existing properties
f = x -> Polymake.exists(p, string(x))
propnames = intersect(
propertynames(p),
[
:INPUT_RAYS,
:POINTS,
:RAYS,
:VERTICES,
:VECTORS,
:INPUT_LINEALITY,
:LINEALITY_SPACE,
:FACETS,
:INEQUALITIES,
:EQUATIONS,
:LINEAR_SPAN,
:AFFINE_HULL,
],
)
i = findfirst(f, propnames)
# find first QuadraticExtension with root != 0
# or first OscarNumber wrapping an embedded number field element
while !isnothing(i)
prop = getproperty(p, propnames[i])
if eltype(prop) <: Polymake.QuadraticExtension
for el in prop
r = Polymake.generating_field_elements(el).r
iszero(r) || return _embedded_quadratic_field(ZZ(r))[1]
end
elseif eltype(prop) <: Polymake.OscarNumber
for el in prop
on = Polymake.unwrap(el)
if on isa Hecke.EmbeddedNumFieldElem{AbsSimpleNumFieldElem}
return parent(on)
end
end
end
i = findnext(f, propnames, i + 1)
end
return _embedded_quadratic_field(ZZ(0))[1]
end
_detect_default_field(::Type{QQFieldElem}, p::Polymake.BigObject) = QQ
_detect_default_field(::Type{Float64}, p::Polymake.BigObject) =
AbstractAlgebra.Floats{Float64}()
function _detect_default_field(::Type{T}, p::Polymake.BigObject) where {T<:FieldElem}
# we only want to check existing properties
propnames = intersect(
Polymake.list_properties(p),
[
"INPUT_RAYS",
"POINTS",
"RAYS",
"VERTICES",
"VECTORS",
"INPUT_LINEALITY",
"LINEALITY_SPACE",
"FACETS",
"INEQUALITIES",
"EQUATIONS",
"LINEAR_SPAN",
"AFFINE_HULL",
],
)
# find first OscarNumber wrapping a FieldElem
for pn in propnames
prop = getproperty(p, convert(String, pn))
for el in prop
on = Polymake.unwrap(el)
if on isa T
return parent(on)
end
end
end
throw(ArgumentError("BigObject does not contain information about a parent Field"))
end
function _detect_wrapped_type_and_field(p::Polymake.BigObject)
# we only want to check existing properties
propnames = intersect(
Polymake.list_properties(p),
[
"INPUT_RAYS",
"POINTS",
"RAYS",
"VERTICES",
"VECTORS",
"INPUT_LINEALITY",
"LINEALITY_SPACE",
"FACETS",
"INEQUALITIES",
"EQUATIONS",
"LINEAR_SPAN",
"AFFINE_HULL",
],
)
# find first OscarNumber wrapping a FieldElem
for pn in propnames
prop = getproperty(p, convert(String, pn))
for el in prop
on = Polymake.unwrap(el)
if on isa FieldElem
f = parent(on)
T = elem_type(f)
return (T, f)
end
end
end
throw(ArgumentError("BigObject does not contain information about a parent Field"))
end
function _detect_scalar_and_field(
::Type{U}, p::Polymake.BigObject
) where {U<:PolyhedralObject}
T = detect_scalar_type(U, p)
if isnothing(T)
return _detect_wrapped_type_and_field(p)
else
return (T, _detect_default_field(T, p))
end
end
# promotion helpers
function _promote_scalar_field(f::Union{Field,ZZRing}...)
try
x = sum([g(0) for g in f])
p = parent(x)
return (elem_type(p), p)
catch e
throw(ArgumentError("Can not find a mutual parent field for $f."))
end
end
function _promote_scalar_field(a::AbstractArray{<:FieldElem})
isempty(a) && return (QQFieldElem, QQ)
return _promote_scalar_field(parent.(a)...)
end
function _promoted_bigobject(
::Type{T}, obj::PolyhedralObject{U}
) where {T<:scalar_types,U<:scalar_types}
if T == U
pm_object(obj)
else
Polymake.common.convert_to{_scalar_type_to_polymake(T)}(pm_object(obj))
end
end
# oscarnumber helpers
function Polymake._fieldelem_to_rational(e::EmbeddedNumFieldElem)
return Rational{BigInt}(QQ(e))
end
function Polymake._fieldelem_is_rational(e::EmbeddedNumFieldElem)
return is_rational(e)
end
function Polymake._fieldelem_to_float(e::EmbeddedNumFieldElem)
return Float64(real(embedding(parent(e))(data(e), 32)))
end
function Polymake._fieldelem_to_float(e::QQBarFieldElem)
return Float64(ArbField(64)(e))
end
function Polymake._fieldelem_from_rational(::QQBarField, r::Rational{BigInt})
return QQBarFieldElem(QQFieldElem(r))
end
# convert a Polymake.BigObject's scalar from QuadraticExtension to OscarNumber (Polytope only)
function _polyhedron_qe_to_on(x::Polymake.BigObject, f::Field)
res = Polymake.polytope.Polytope{Polymake.OscarNumber}()
for pn in Polymake.list_properties(x)
prop = Polymake.give(x, pn)
Polymake.take(res, string(pn), _property_qe_to_on(prop, f))
end
return res
end
_property_qe_to_on(x::Polymake.BigObject, f::Field) =
Polymake.BigObject(Polymake.bigobject_type(x), x)
_property_qe_to_on(x::Polymake.PropertyValue, f::Field) = x
_property_qe_to_on(x::Polymake.QuadraticExtension{Polymake.Rational}, f::Field) = f(x)
function _property_qe_to_on(x, f::Field)
if hasmethod(length, (typeof(x),)) &&
eltype(x) <: Polymake.QuadraticExtension{Polymake.Rational}
return f.(x)
else
return x
end
end
# Helper function for conversion
# Cone -> Polyhedron
# PolyhedralFan -> PolyhedralComplex
# for transforming coordinate matrices.
function embed_at_height_one(M::Polymake.Matrix{T}, add_vert::Bool) where {T}
result = Polymake.Matrix{T}(add_vert + nrows(M), ncols(M) + 1)
if add_vert
result[1, 1] = 1
end
result[(add_vert + 1):end, 2:end] = M
return result
end