Skip to content

Commit

Permalink
PolyhedralGeometry: simplified operations with PolyhedralFan and Poly…
Browse files Browse the repository at this point in the history
…hedralComplex
  • Loading branch information
YueRen committed Jan 24, 2025
1 parent be24967 commit 8dbe909
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 60 deletions.
91 changes: 51 additions & 40 deletions src/PolyhedralGeometry/PolyhedralComplex/standard_constructions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,65 +76,76 @@ function k_skeleton(PC::PolyhedralComplex{T}, k::Int) where {T<:scalar_types}
return PolyhedralComplex{T}(ksk, coefficient_field(PC))
end


###############################################################################
## Scalar product
###############################################################################

function *(c::QQFieldElem, Sigma::PolyhedralComplex)
# if scalar is zero, return polyhedral complex consisting only of the origin
if iszero(c)
return polyhedral_complex(convex_hull(zero_matrix(QQ,1,ambient_dim(Sigma))))
end

# if scalar is non-zero, multiple all vertices and rays by said scalar
SigmaVertsAndRays = vertices_and_rays(Sigma)
SigmaRayIndices = findall(vr -> vr isa RayVector, SigmaVertsAndRays)
SigmaLineality = lineality_space(Sigma)
SigmaIncidence = maximal_polyhedra(IncidenceMatrix,Sigma)
return polyhedral_complex(SigmaIncidence, multiply_by_nonzero_scalar.(SigmaVertsAndRays,c), SigmaRayIndices, SigmaLineality)
function *(c::scalar_types_extended, Sigma::PolyhedralComplex)
# if scalar is zero, return polyhedral complex consisting only of the origin
if iszero(c)
return polyhedral_complex(convex_hull(zero_matrix(QQ, 1, ambient_dim(Sigma))))
end

# if scalar is non-zero, multiple all vertices and rays by said scalar
SigmaVertsAndRays = vertices_and_rays(Sigma)
SigmaRayIndices = findall(vr -> vr isa RayVector, SigmaVertsAndRays)
SigmaLineality = lineality_space(Sigma)
SigmaIncidence = maximal_polyhedra(IncidenceMatrix, Sigma)
return polyhedral_complex(
coefficient_field(Sigma),
SigmaIncidence,
SigmaVertsAndRays .* c,
SigmaRayIndices,
SigmaLineality,
)
end
*(c::RationalUnion, Sigma::PolyhedralComplex) = QQ(c)*Sigma
*(Sigma::PolyhedralComplex, c::RationalUnion) = c*Sigma

*(Sigma::PolyhedralComplex, c::scalar_types_extended) = c * Sigma

###############################################################################
## Negation
###############################################################################

function -(Sigma::PolyhedralComplex)
SigmaVertsAndRays = vertices_and_rays(Sigma)
SigmaRayIndices = findall(vr -> vr isa RayVector, SigmaVertsAndRays)
SigmaLineality = lineality_space(Sigma)
SigmaIncidence = maximal_polyhedra(IncidenceMatrix,Sigma)
return polyhedral_complex(SigmaIncidence, -SigmaVertsAndRays, SigmaRayIndices, SigmaLineality)
SigmaVertsAndRays = vertices_and_rays(Sigma)
SigmaRayIndices = findall(vr -> vr isa RayVector, SigmaVertsAndRays)
SigmaLineality = lineality_space(Sigma)
SigmaIncidence = maximal_polyhedra(IncidenceMatrix, Sigma)
return polyhedral_complex(
SigmaIncidence, -SigmaVertsAndRays, SigmaRayIndices, SigmaLineality
)
end

###############################################################################
## Translation
###############################################################################

function translate_by_vector(u::PointVector{QQFieldElem}, v::Vector{QQFieldElem})
return u .+ v
# requires separate function because to ensure that translation does not change ray vectors
function translate_by_vector(
u::PointVector{<:scalar_types_extended}, v::Vector{<:scalar_types_extended}
)
return u + v
end
function translate_by_vector(u::RayVector{QQFieldElem}, ::Vector{QQFieldElem})
return u
function translate_by_vector(
u::RayVector{<:scalar_types_extended}, ::Vector{<:scalar_types_extended}
)
return u
end
function +(v::Vector{QQFieldElem}, Sigma::PolyhedralComplex)
@req length(v)==ambient_dim(Sigma) "ambient dimension mismatch"
SigmaVertsAndRays = vertices_and_rays(Sigma)
SigmaRayIndices = findall(vr -> vr isa RayVector, SigmaVertsAndRays)
SigmaLineality = lineality_space(Sigma)
SigmaIncidence = maximal_polyhedra(IncidenceMatrix,Sigma)
return polyhedral_complex(SigmaIncidence, translate_by_vector.(SigmaVertsAndRays,Ref(v)), SigmaRayIndices, SigmaLineality)
function +(v::Vector{<:scalar_types_extended}, Sigma::PolyhedralComplex)
@req length(v) == ambient_dim(Sigma) "ambient dimension mismatch"
SigmaVertsAndRays = vertices_and_rays(Sigma)
SigmaRayIndices = findall(vr -> vr isa RayVector, SigmaVertsAndRays)
SigmaLineality = lineality_space(Sigma)
SigmaIncidence = maximal_polyhedra(IncidenceMatrix, Sigma)
return polyhedral_complex(
coefficient_field(Sigma),
SigmaIncidence,
translate_by_vector.(SigmaVertsAndRays, Ref(v)),
SigmaRayIndices,
SigmaLineality,
)
end
+(v::Vector{ZZRingElem}, Sigma::PolyhedralComplex) = QQ.(v)+Sigma
+(v::Vector{Rational}, Sigma::PolyhedralComplex) = QQ.(v)+Sigma
+(v::Vector{Int}, Sigma::PolyhedralComplex) = QQ.(v)+Sigma

+(Sigma::PolyhedralComplex, v::Vector{QQFieldElem}) = v+Sigma
+(Sigma::PolyhedralComplex, v::Vector{<:RationalUnion}) = QQ.(v)+Sigma
+(Sigma::PolyhedralComplex, v::Vector{<:scalar_types_extended}) = v + Sigma

# Vector addition for polyhedral fans
+(Sigma::PolyhedralFan, v::Vector) = polyhedral_complex(Sigma)+v
+(v::Vector, Sigma::PolyhedralFan) = v+polyhedral_complex(Sigma)
+(Sigma::PolyhedralFan, v::Vector{<:scalar_types_extended}) = polyhedral_complex(Sigma) + v
+(v::Vector{<:scalar_types_extended}, Sigma::PolyhedralFan) = v + polyhedral_complex(Sigma)
35 changes: 16 additions & 19 deletions src/PolyhedralGeometry/PolyhedralFan/standard_constructions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,35 +410,32 @@ end
## Scalar multiplication
###############################################################################

function multiply_by_nonzero_scalar(u::PointVector{QQFieldElem}, c::QQFieldElem)
return u .* c
end
function multiply_by_nonzero_scalar(u::RayVector{QQFieldElem}, c::QQFieldElem)
return (c<0 ? -u : u)
end

function *(c::QQFieldElem, Sigma::PolyhedralFan)
# if scalar is zero, return polyhedral complex consisting only of the origin
function *(c::scalar_types_extended, Sigma::PolyhedralFan)
# if scalar is zero, return polyhedral fan consisting only of the origin
if iszero(c)
return polyhedral_complex(convex_hull(zero_matrix(QQ,1,ambient_dim(Sigma))))
return polyhedral_fan(
positive_hull(zero_matrix(coefficient_field(Sigma), 1, ambient_dim(Sigma)))
)
end

# if scalar is non-zero, multiple all vertices and rays by said scalar
# if scalar is non-zero, multiple all rays by said scalar
SigmaRays = first(rays_modulo_lineality(Sigma))
SigmaLineality = lineality_space(Sigma)
SigmaIncidence = maximal_cones(IncidenceMatrix,Sigma)
return polyhedral_fan(SigmaIncidence, multiply_by_nonzero_scalar.(SigmaRays,c), SigmaLineality)
SigmaIncidence = maximal_cones(IncidenceMatrix, Sigma)
return polyhedral_fan(
coefficient_field(Sigma), SigmaIncidence, SigmaRays .* c, SigmaLineality
)
end
*(c::RationalUnion, Sigma::PolyhedralFan) = QQ(c)*Sigma
*(Sigma::PolyhedralFan,c::RationalUnion) = c*Sigma
*(Sigma::PolyhedralFan, c::scalar_types_extended) = c * Sigma

###############################################################################
## Negation
###############################################################################

function -(Sigma::PolyhedralFan)
SigmaRays = first(rays_modulo_lineality(Sigma))
SigmaLineality = lineality_space(Sigma)
SigmaIncidence = maximal_cones(IncidenceMatrix,Sigma)
return polyhedral_fan(SigmaIncidence, -SigmaRays, SigmaLineality)
SigmaRays, SigmaLineality = first(rays_modulo_lineality(Sigma))
SigmaIncidence = maximal_cones(IncidenceMatrix, Sigma)
return polyhedral_fan(
coefficient_field(Sigma), SigmaIncidence, -SigmaRays, SigmaLineality
)
end
2 changes: 1 addition & 1 deletion src/PolyhedralGeometry/helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ const scalar_type_to_oscar = Dict{String,Type}([
("Float", Float64),
])

const scalar_types_extended = Union{scalar_types,ZZRingElem}
const scalar_types_extended = Union{scalar_types,IntegerUnion}

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

Expand Down

0 comments on commit 8dbe909

Please sign in to comment.