Skip to content

Commit 8dbe909

Browse files
committed
PolyhedralGeometry: simplified operations with PolyhedralFan and PolyhedralComplex
1 parent be24967 commit 8dbe909

File tree

3 files changed

+68
-60
lines changed

3 files changed

+68
-60
lines changed

Diff for: src/PolyhedralGeometry/PolyhedralComplex/standard_constructions.jl

+51-40
Original file line numberDiff line numberDiff line change
@@ -76,65 +76,76 @@ function k_skeleton(PC::PolyhedralComplex{T}, k::Int) where {T<:scalar_types}
7676
return PolyhedralComplex{T}(ksk, coefficient_field(PC))
7777
end
7878

79-
8079
###############################################################################
8180
## Scalar product
8281
###############################################################################
8382

84-
function *(c::QQFieldElem, Sigma::PolyhedralComplex)
85-
# if scalar is zero, return polyhedral complex consisting only of the origin
86-
if iszero(c)
87-
return polyhedral_complex(convex_hull(zero_matrix(QQ,1,ambient_dim(Sigma))))
88-
end
89-
90-
# if scalar is non-zero, multiple all vertices and rays by said scalar
91-
SigmaVertsAndRays = vertices_and_rays(Sigma)
92-
SigmaRayIndices = findall(vr -> vr isa RayVector, SigmaVertsAndRays)
93-
SigmaLineality = lineality_space(Sigma)
94-
SigmaIncidence = maximal_polyhedra(IncidenceMatrix,Sigma)
95-
return polyhedral_complex(SigmaIncidence, multiply_by_nonzero_scalar.(SigmaVertsAndRays,c), SigmaRayIndices, SigmaLineality)
83+
function *(c::scalar_types_extended, Sigma::PolyhedralComplex)
84+
# if scalar is zero, return polyhedral complex consisting only of the origin
85+
if iszero(c)
86+
return polyhedral_complex(convex_hull(zero_matrix(QQ, 1, ambient_dim(Sigma))))
87+
end
88+
89+
# if scalar is non-zero, multiple all vertices and rays by said scalar
90+
SigmaVertsAndRays = vertices_and_rays(Sigma)
91+
SigmaRayIndices = findall(vr -> vr isa RayVector, SigmaVertsAndRays)
92+
SigmaLineality = lineality_space(Sigma)
93+
SigmaIncidence = maximal_polyhedra(IncidenceMatrix, Sigma)
94+
return polyhedral_complex(
95+
coefficient_field(Sigma),
96+
SigmaIncidence,
97+
SigmaVertsAndRays .* c,
98+
SigmaRayIndices,
99+
SigmaLineality,
100+
)
96101
end
97-
*(c::RationalUnion, Sigma::PolyhedralComplex) = QQ(c)*Sigma
98-
*(Sigma::PolyhedralComplex, c::RationalUnion) = c*Sigma
99-
102+
*(Sigma::PolyhedralComplex, c::scalar_types_extended) = c * Sigma
100103

101104
###############################################################################
102105
## Negation
103106
###############################################################################
104107

105108
function -(Sigma::PolyhedralComplex)
106-
SigmaVertsAndRays = vertices_and_rays(Sigma)
107-
SigmaRayIndices = findall(vr -> vr isa RayVector, SigmaVertsAndRays)
108-
SigmaLineality = lineality_space(Sigma)
109-
SigmaIncidence = maximal_polyhedra(IncidenceMatrix,Sigma)
110-
return polyhedral_complex(SigmaIncidence, -SigmaVertsAndRays, SigmaRayIndices, SigmaLineality)
109+
SigmaVertsAndRays = vertices_and_rays(Sigma)
110+
SigmaRayIndices = findall(vr -> vr isa RayVector, SigmaVertsAndRays)
111+
SigmaLineality = lineality_space(Sigma)
112+
SigmaIncidence = maximal_polyhedra(IncidenceMatrix, Sigma)
113+
return polyhedral_complex(
114+
SigmaIncidence, -SigmaVertsAndRays, SigmaRayIndices, SigmaLineality
115+
)
111116
end
112117

113118
###############################################################################
114119
## Translation
115120
###############################################################################
116121

117-
function translate_by_vector(u::PointVector{QQFieldElem}, v::Vector{QQFieldElem})
118-
return u .+ v
122+
# requires separate function because to ensure that translation does not change ray vectors
123+
function translate_by_vector(
124+
u::PointVector{<:scalar_types_extended}, v::Vector{<:scalar_types_extended}
125+
)
126+
return u + v
119127
end
120-
function translate_by_vector(u::RayVector{QQFieldElem}, ::Vector{QQFieldElem})
121-
return u
128+
function translate_by_vector(
129+
u::RayVector{<:scalar_types_extended}, ::Vector{<:scalar_types_extended}
130+
)
131+
return u
122132
end
123-
function +(v::Vector{QQFieldElem}, Sigma::PolyhedralComplex)
124-
@req length(v)==ambient_dim(Sigma) "ambient dimension mismatch"
125-
SigmaVertsAndRays = vertices_and_rays(Sigma)
126-
SigmaRayIndices = findall(vr -> vr isa RayVector, SigmaVertsAndRays)
127-
SigmaLineality = lineality_space(Sigma)
128-
SigmaIncidence = maximal_polyhedra(IncidenceMatrix,Sigma)
129-
return polyhedral_complex(SigmaIncidence, translate_by_vector.(SigmaVertsAndRays,Ref(v)), SigmaRayIndices, SigmaLineality)
133+
function +(v::Vector{<:scalar_types_extended}, Sigma::PolyhedralComplex)
134+
@req length(v) == ambient_dim(Sigma) "ambient dimension mismatch"
135+
SigmaVertsAndRays = vertices_and_rays(Sigma)
136+
SigmaRayIndices = findall(vr -> vr isa RayVector, SigmaVertsAndRays)
137+
SigmaLineality = lineality_space(Sigma)
138+
SigmaIncidence = maximal_polyhedra(IncidenceMatrix, Sigma)
139+
return polyhedral_complex(
140+
coefficient_field(Sigma),
141+
SigmaIncidence,
142+
translate_by_vector.(SigmaVertsAndRays, Ref(v)),
143+
SigmaRayIndices,
144+
SigmaLineality,
145+
)
130146
end
131-
+(v::Vector{ZZRingElem}, Sigma::PolyhedralComplex) = QQ.(v)+Sigma
132-
+(v::Vector{Rational}, Sigma::PolyhedralComplex) = QQ.(v)+Sigma
133-
+(v::Vector{Int}, Sigma::PolyhedralComplex) = QQ.(v)+Sigma
134-
135-
+(Sigma::PolyhedralComplex, v::Vector{QQFieldElem}) = v+Sigma
136-
+(Sigma::PolyhedralComplex, v::Vector{<:RationalUnion}) = QQ.(v)+Sigma
147+
+(Sigma::PolyhedralComplex, v::Vector{<:scalar_types_extended}) = v + Sigma
137148

138149
# Vector addition for polyhedral fans
139-
+(Sigma::PolyhedralFan, v::Vector) = polyhedral_complex(Sigma)+v
140-
+(v::Vector, Sigma::PolyhedralFan) = v+polyhedral_complex(Sigma)
150+
+(Sigma::PolyhedralFan, v::Vector{<:scalar_types_extended}) = polyhedral_complex(Sigma) + v
151+
+(v::Vector{<:scalar_types_extended}, Sigma::PolyhedralFan) = v + polyhedral_complex(Sigma)

Diff for: src/PolyhedralGeometry/PolyhedralFan/standard_constructions.jl

+16-19
Original file line numberDiff line numberDiff line change
@@ -410,35 +410,32 @@ end
410410
## Scalar multiplication
411411
###############################################################################
412412

413-
function multiply_by_nonzero_scalar(u::PointVector{QQFieldElem}, c::QQFieldElem)
414-
return u .* c
415-
end
416-
function multiply_by_nonzero_scalar(u::RayVector{QQFieldElem}, c::QQFieldElem)
417-
return (c<0 ? -u : u)
418-
end
419-
420-
function *(c::QQFieldElem, Sigma::PolyhedralFan)
421-
# if scalar is zero, return polyhedral complex consisting only of the origin
413+
function *(c::scalar_types_extended, Sigma::PolyhedralFan)
414+
# if scalar is zero, return polyhedral fan consisting only of the origin
422415
if iszero(c)
423-
return polyhedral_complex(convex_hull(zero_matrix(QQ,1,ambient_dim(Sigma))))
416+
return polyhedral_fan(
417+
positive_hull(zero_matrix(coefficient_field(Sigma), 1, ambient_dim(Sigma)))
418+
)
424419
end
425420

426-
# if scalar is non-zero, multiple all vertices and rays by said scalar
421+
# if scalar is non-zero, multiple all rays by said scalar
427422
SigmaRays = first(rays_modulo_lineality(Sigma))
428423
SigmaLineality = lineality_space(Sigma)
429-
SigmaIncidence = maximal_cones(IncidenceMatrix,Sigma)
430-
return polyhedral_fan(SigmaIncidence, multiply_by_nonzero_scalar.(SigmaRays,c), SigmaLineality)
424+
SigmaIncidence = maximal_cones(IncidenceMatrix, Sigma)
425+
return polyhedral_fan(
426+
coefficient_field(Sigma), SigmaIncidence, SigmaRays .* c, SigmaLineality
427+
)
431428
end
432-
*(c::RationalUnion, Sigma::PolyhedralFan) = QQ(c)*Sigma
433-
*(Sigma::PolyhedralFan,c::RationalUnion) = c*Sigma
429+
*(Sigma::PolyhedralFan, c::scalar_types_extended) = c * Sigma
434430

435431
###############################################################################
436432
## Negation
437433
###############################################################################
438434

439435
function -(Sigma::PolyhedralFan)
440-
SigmaRays = first(rays_modulo_lineality(Sigma))
441-
SigmaLineality = lineality_space(Sigma)
442-
SigmaIncidence = maximal_cones(IncidenceMatrix,Sigma)
443-
return polyhedral_fan(SigmaIncidence, -SigmaRays, SigmaLineality)
436+
SigmaRays, SigmaLineality = first(rays_modulo_lineality(Sigma))
437+
SigmaIncidence = maximal_cones(IncidenceMatrix, Sigma)
438+
return polyhedral_fan(
439+
coefficient_field(Sigma), SigmaIncidence, -SigmaRays, SigmaLineality
440+
)
444441
end

Diff for: src/PolyhedralGeometry/helpers.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ const scalar_type_to_oscar = Dict{String,Type}([
530530
("Float", Float64),
531531
])
532532

533-
const scalar_types_extended = Union{scalar_types,ZZRingElem}
533+
const scalar_types_extended = Union{scalar_types,IntegerUnion}
534534

535535
const scalar_type_or_field = Union{Type{<:scalar_types},Field}
536536

0 commit comments

Comments
 (0)