Skip to content

Commit

Permalink
Merge pull request #493 from oscar-system/bl/decoration
Browse files Browse the repository at this point in the history
pairs, decorations and more
  • Loading branch information
benlorenz authored Jul 19, 2024
2 parents 21d1804 + 4d89d6c commit 0ca3e72
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Polymake"
uuid = "d720cf60-89b5-51f5-aff5-213f193123e7"
repo = "https://github.com/oscar-system/Polymake.jl.git"
version = "0.11.18"
version = "0.11.19"

[deps]
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"
Expand Down
1 change: 1 addition & 0 deletions src/Polymake.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ include("incidencematrix.jl")
include("tropicalnumber.jl")
include("polynomial.jl")
include("map.jl")
include("misc.jl")

include("polymake_direct_calls.jl")

Expand Down
4 changes: 4 additions & 0 deletions src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function Array{T}(vec::AbstractVector) where T
return arr
end

function Array{T}(t::Tuple) where T <: Array_suppT
return Array{T}(collect(t))
end

Array(n::Base.Integer, elt::T) where T =
Array{convert_to_pm_type(T)}(n, elt)

Expand Down
46 changes: 31 additions & 15 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ for (pm_T, jl_T) in [
(Array, AbstractVector),
(Set, AbstractSet),
(SparseMatrix, AbstractMatrix),
(SparseVector, AbstractVector)
(SparseVector, AbstractVector),
]
@eval begin
convert(::Type{$pm_T}, itr::$jl_T) = $pm_T(itr)
Expand All @@ -23,6 +23,10 @@ convert(::Type{<:Polynomial{C,E}}, itr::Polynomial{C,E}) where {C,E} = itr
convert(::Type{<:Polynomial{C1,E1}}, itr::Polynomial{C2,E2}) where {C1,C2,E1,E2} = Polynomial{C1,E1}(itr)

convert(::Type{BasicDecoration}, p::StdPair) = BasicDecoration(first(p),last(p))
Polymake.BasicDecoration(p::Pair{<:AbstractSet{<:Base.Integer},<:Base.Integer}) = BasicDecoration(convert(PolymakeType, first(p)), convert(PolymakeType,last(p)))
Polymake.BasicDecoration(p::Tuple{<:AbstractSet{<:Base.Integer},<:Base.Integer}) = BasicDecoration(convert(PolymakeType, first(p)), convert(PolymakeType,last(p)))
Polymake.BasicDecoration(s::Base.Set{<:Base.Integer}, i::Base.Integer) = BasicDecoration(Set(s), i)


########### Converting to objects polymake understands ###############

Expand All @@ -33,6 +37,9 @@ convert(::Type{PolymakeType}, v::Visual) = v.obj
convert(::Type{PolymakeType}, ::Nothing) = call_function(PropertyValue, :common, :get_undef)
convert(::Type{OptionSet}, dict) = OptionSet(dict)

# long (>=3) uniform tuples need some extra treatment
convert(::Type{PolymakeType}, x::Tuple{T,T,T,Vararg{T}}) where T = Polymake.Array(map(convert_to_pm_type(T), collect(x)))

as_perl_array(t::SmallObject) = Polymake.call_function(PropertyValue, :common, :as_perl_array, t)
as_perl_array_of_array(t::SmallObject) = Polymake.call_function(PropertyValue, :common, :as_perl_array_of_array, t)

Expand All @@ -53,6 +60,12 @@ to_cxx_type(::Type{<:Array{T}}) where T =
Array{to_cxx_type(T)}
to_cxx_type(::Type{<:Polynomial{S,T}}) where {S,T} =
Polynomial{to_cxx_type(S), to_cxx_type(T)}
to_cxx_type(::Type{<:Tuple{A,B}}) where {A,B} =
StdPair{to_cxx_type(A), to_cxx_type(B)}
to_cxx_type(::Type{<:Pair{A,B}}) where {A,B} =
StdPair{to_cxx_type(A), to_cxx_type(B)}
to_cxx_type(::Type{<:StdPair{A,B}}) where {A,B} =
StdPair{to_cxx_type(A), to_cxx_type(B)}

to_jl_type(::Type{T}) where T = T
to_jl_type(::Type{CxxWrap.CxxBool}) = Bool
Expand All @@ -66,6 +79,7 @@ if Int64 != CxxWrap.CxxLong
end
Int64(r::Rational) = Int64(new_int_from_rational(r))

const PmInt64 = to_cxx_type(Int64)

#################### Guessing the polymake type ######################

Expand All @@ -78,6 +92,7 @@ convert_to_pm_type(::Type{T}) where T <: TropicalNumber = T

convert_to_pm_type(::Nothing) = Nothing
convert_to_pm_type(::Type{Int32}) = Int64
convert_to_pm_type(::Type{CxxWrap.CxxLong}) = CxxWrap.CxxLong
convert_to_pm_type(::Type{<:AbstractFloat}) = Float64
convert_to_pm_type(::Type{<:AbstractString}) = String
convert_to_pm_type(::Type{<:Union{Base.Integer, Integer}}) = Integer
Expand All @@ -90,44 +105,45 @@ convert_to_pm_type(::Type{<:Union{AbstractSparseMatrix, SparseMatrix}}) = Sparse
convert_to_pm_type(::Type{<:AbstractSparseMatrix{<:Union{Bool, CxxWrap.CxxBool}}}) = IncidenceMatrix
convert_to_pm_type(::Type{<:Union{AbstractSparseVector, SparseVector}}) = SparseVector
convert_to_pm_type(::Type{<:Array}) = Array
convert_to_pm_type(::Type{<:Union{Pair, <:StdPair}}) = StdPair
convert_to_pm_type(::Type{<:Union{Pair, StdPair}}) = StdPair
convert_to_pm_type(::Type{<:Pair{A,B}}) where {A,B} = StdPair{convert_to_pm_type(A),convert_to_pm_type(B)}
convert_to_pm_type(::Type{<:StdPair{A,B}}) where {A,B} = StdPair{convert_to_pm_type(A),convert_to_pm_type(B)}
convert_to_pm_type(::Type{<:Tuple{A,B}}) where {A,B} = StdPair{convert_to_pm_type(A),convert_to_pm_type(B)}
convert_to_pm_type(::Type{<:Polynomial{<:Rational, <:Union{Int64, CxxWrap.CxxLong}}}) = Polynomial{Rational, CxxWrap.CxxLong}
convert_to_pm_type(::Type{<:AbstractVector{T}}) where T<:Tuple = Polymake.Array{convert_to_pm_type(T)}
convert_to_pm_type(::Type{<:BasicDecoration}) = BasicDecoration
# only for 3 or more elements:
convert_to_pm_type(::Type{<:Tuple{A,A,A,Vararg{A}}}) where A = Polymake.Array{convert_to_pm_type(A)}

# Graph, EdgeMap, NodeMap
const DirType = Union{Directed, Undirected}
convert_to_pm_type(::Type{<:Graph{T}}) where T<:DirType = Graph{T}

convert_to_pm_type(::Type{<:EdgeMap{T,Int64}}) where T<:DirType = EdgeMap{T, Int64}
EdgeMap{Dir, T}(g::Graph{Dir}) where Dir<:DirType where T<:Union{Int64, CxxWrap.CxxLong} = EdgeMap{Dir,to_cxx_type(T)}(g)
convert_to_pm_type(::Type{<:EdgeMap{S,T}}) where S<:DirType where T = EdgeMap{S, convert_to_pm_type(T)}
EdgeMap{Dir, T}(g::Graph{Dir}) where Dir<:DirType where T = EdgeMap{Dir,to_cxx_type(T)}(g)

convert_to_pm_type(::Type{<:NodeMap{T,Int64}}) where T<:DirType = NodeMap{T, Int64}
NodeMap{Dir, T}(g::Graph{Dir}) where Dir<:DirType where T<:Union{Int64, CxxWrap.CxxLong} = NodeMap{Dir,to_cxx_type(T)}(g)
convert_to_pm_type(::Type{<:NodeMap{T,<:Set{Int64}}}) where T<:DirType = NodeMap{T, Set{Int64}}
NodeMap{Dir, T}(g::Graph{Dir}) where Dir<:DirType where T<:Set{<:Union{Int64, CxxWrap.CxxLong}} = NodeMap{Dir,to_cxx_type(T)}(g)
convert_to_pm_type(::Type{<:NodeMap{S,T}}) where S <: DirType where T = NodeMap{S, convert_to_pm_type(T)}
NodeMap{Dir, T}(g::Graph{Dir}) where Dir<:DirType where T = NodeMap{Dir,to_cxx_type(T)}(g)



convert_to_pm_type(::Type{HomologyGroup{T}}) where T<:Integer = HomologyGroup{T}
convert_to_pm_type(::Type{<:QuadraticExtension{T}}) where T<:Rational = QuadraticExtension{Rational}
convert_to_pm_type(::Type{<:TropicalNumber{S,T}}) where S<:Union{Max,Min} where T<:Rational = TropicalNumber{S,Rational}
# convert_to_pm_type(::Type{<:Union{AbstractSet, Set}}) = Set

# specific converts for container types we wrap:
convert_to_pm_type(::Type{<:Set{<:Base.Integer}}) = Set{Int64}
convert_to_pm_type(::Type{<:Base.AbstractSet{<:Base.Integer}}) = Set{Int64}

for (pmT, jlT) in [(Integer, Base.Integer),
(Int64, Union{Int32,Int64,CxxWrap.CxxLong}),
(Int64, Union{Int32,Int64}),
(CxxWrap.CxxLong, CxxWrap.CxxLong),
(Rational, Union{Base.Rational, Rational}),
(TropicalNumber{Max, Rational}, TropicalNumber{Max, Rational}),
(TropicalNumber{Min, Rational}, TropicalNumber{Min, Rational}),
(OscarNumber, OscarNumber),
(QuadraticExtension{Rational}, QuadraticExtension{Rational})]
@eval begin
convert_to_pm_type(::Type{<:AbstractMatrix{T}}) where T<:$jlT = Matrix{to_cxx_type($pmT)}
convert_to_pm_type(::Type{<:AbstractVector{T}}) where T<:$jlT = Vector{to_cxx_type($pmT)}
convert_to_pm_type(::Type{<:AbstractMatrix{T}}) where T<:$jlT = Matrix{convert_to_pm_type($pmT)}
convert_to_pm_type(::Type{<:AbstractVector{T}}) where T<:$jlT = Vector{convert_to_pm_type($pmT)}
convert_to_pm_type(::Type{<:AbstractSet{T}}) where T<:$jlT = Set{convert_to_pm_type($pmT)}
end
end

Expand Down
4 changes: 3 additions & 1 deletion src/meta.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Meta
import JSON
import Polymake: appname_module_dict, module_appname_dict, shell_context_help
import Polymake: Rational, PolymakeType, PropertyValue, OptionSet, QuadraticExtension, OscarNumber, DirType
import Polymake: Rational, PolymakeType, PropertyValue, OptionSet, QuadraticExtension, OscarNumber, DirType, Symmetric, NonSymmetric, BasicDecoration

struct UnparsablePolymakeFunction <: Exception
msg::String
Expand Down Expand Up @@ -35,7 +35,9 @@ translate_type_to_pm_string(::Type{<:OscarNumber}) = "OscarNumber"
translate_type_to_pm_string(::typeof(min)) = "Min"
translate_type_to_pm_string(::typeof(max)) = "Max"
translate_type_to_pm_string(::Type{T}) where T <: DirType = string(nameof(T))
translate_type_to_pm_string(::Type{T}) where T <: Union{Symmetric,NonSymmetric} = string(nameof(T))
translate_type_to_pm_string(::Type{<:QuadraticExtension{T}}) where T = string("QuadraticExtension<", translate_type_to_pm_string(T), ">")
translate_type_to_pm_string(::Type{<:BasicDecoration}) = "graph::BasicDecoration"

translate_type_to_pm_string(T) = throw(DomainError(T, "$T has been passed as a type parameter but no translation to a C++ template was defined. You may define such translation by appropriately extending
`Polymake.Meta.translate_type_to_pm_string`."))
Expand Down
3 changes: 3 additions & 0 deletions src/misc.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Base.:(==)(a::BasicDecoration, b::BasicDecoration) =
Polymake.decoration_face(a) == Polymake.decoration_face(b) &&
Polymake.decoration_rank(a) == Polymake.decoration_rank(b)
22 changes: 15 additions & 7 deletions test/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@

@testset verbose=true "Polymake.Array{Polymake.Set{Int64}}" begin
elt = Base.Set([1,2,3,4])
T = Polymake.Set{Polymake.to_cxx_type(Int64)}
T = Polymake.Set{Polymake.PmInt64}

@test Polymake.Array{T}([elt, Base.Set(elt .% 3)]) isa Polymake.Array
@test Polymake.Array{T}([elt, Base.Set(elt .% 3)]) isa Polymake.Array{T}
Expand Down Expand Up @@ -123,23 +123,31 @@
c = Polymake.polytope.cube(2, 1, 0)
PC = Polymake.polytope.PointConfiguration(POINTS=c.VERTICES)
all = Polymake.polytope.topcom_all_triangulations(PC)
@test all isa Polymake.Array{Polymake.Set{Polymake.Set{Polymake.to_cxx_type(Int64)}}}
@test all isa Polymake.Array{Polymake.Set{Polymake.Set{Polymake.PmInt64}}}
@test length(all) == 2
@test size(all) == (2,)
for triang in all
@test triang isa Polymake.Set{Polymake.Set{Polymake.to_cxx_type(Int64)}}
@test triang isa Polymake.Set{Polymake.Set{Polymake.PmInt64}}
end
end

@testset verbose=true "Polymake.Array{Polymake.StdPair{Polymake.Array{Int64}, Polymake.Array{Int64}}}" begin
c = Polymake.polytope.cube(2, 1, 0)
aut = Polymake.graph.automorphisms(c.VERTICES_IN_FACETS)
@test length(aut) == 2
@test aut isa Polymake.Array{Polymake.StdPair{Polymake.Array{Polymake.to_cxx_type(Int64)}, Polymake.Array{Polymake.to_cxx_type(Int64)}}}
@test aut isa Polymake.Array{Polymake.StdPair{Polymake.Array{Polymake.PmInt64}, Polymake.Array{Polymake.PmInt64}}}
for p in aut
@test p isa Polymake.StdPair{Polymake.Array{Polymake.to_cxx_type(Int64)}, Polymake.Array{Polymake.to_cxx_type(Int64)}}
@test first(p) isa Polymake.Array{Polymake.to_cxx_type(Int64)}
@test last(p) isa Polymake.Array{Polymake.to_cxx_type(Int64)}
@test p isa Polymake.StdPair{Polymake.Array{Polymake.PmInt64}, Polymake.Array{Polymake.PmInt64}}
@test first(p) isa Polymake.Array{Polymake.PmInt64}
@test last(p) isa Polymake.Array{Polymake.PmInt64}
end
end
@testset verbose=true "Polymake.Array{Polymake.StdPair{Polymake.Set{Int64}, Int64}}" begin
a = Base.Array([(Set([1,2,3]),4),(Set([6,7,8]),9)])
pma = convert(Polymake.PolymakeType, a)
@test pma isa Polymake.Array
@test eltype(pma) <: Polymake.StdPair
@test first(first(pma)) isa Polymake.Set{Polymake.PmInt64}
@test last(first(pma)) isa Union{Int64,Polymake.PmInt64}
end
end
15 changes: 9 additions & 6 deletions test/convert.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@testset verbose=true "converting" begin
@test Polymake.convert_to_pm_type(Base.Vector{Base.Int}) == Polymake.Vector{Polymake.to_cxx_type(Int64)}
@test Polymake.convert_to_pm_type(Base.Matrix{Base.Int}) == Polymake.Matrix{Polymake.to_cxx_type(Int64)}
@test Polymake.convert_to_pm_type(Base.Vector{Base.Int}) == Polymake.Vector{Int64}
@test Polymake.convert_to_pm_type(Base.Matrix{Base.Int}) == Polymake.Matrix{Int64}
@test Polymake.convert_to_pm_type(Base.Vector{BigInt}) == Polymake.Vector{Polymake.Integer}
@test Polymake.convert_to_pm_type(Base.Matrix{BigInt}) == Polymake.Matrix{Polymake.Integer}
@test Polymake.convert_to_pm_type(Base.Matrix{Base.Rational{BigInt}}) == Polymake.Matrix{Polymake.Rational}
Expand All @@ -11,15 +11,18 @@
@test Polymake.convert_to_pm_type(Base.Vector{String}) == Polymake.Array{String}

@test Polymake.convert_to_pm_type(Base.Vector{Base.Set{Int64}}) == Polymake.Array{Polymake.Set{Int64}}
@test Polymake.convert_to_pm_type(Base.Vector{Base.Set{Polymake.Integer}}) == Polymake.Array{Polymake.Set{Int64}}
@test Polymake.convert_to_pm_type(Base.Vector{Base.Set{Polymake.Integer}}) == Polymake.Array{Polymake.Set{Polymake.Integer}}
@test Polymake.convert_to_pm_type(Base.Vector{Base.Set{Int64}}) == Polymake.Array{Polymake.Set{Int64}}

@test Polymake.convert_to_pm_type(Base.Vector{Base.Vector{Int64}}) == Polymake.Array{Polymake.Array{Int64}}

@test Polymake.convert_to_pm_type(Pair{Base.Set{Int64},Int64}) == Polymake.StdPair{Polymake.Set{Int64},Int64}
@test Polymake.convert_to_pm_type(Tuple{Base.Set{Int64},Int64}) == Polymake.StdPair{Polymake.Set{Int64},Int64}

y = Base.Vector{Base.Set{Int64}}([Base.Set([3,3]), Base.Set([3]), Base.Set([1,2])])
@test convert(Polymake.PolymakeType, y) isa Polymake.Array{Polymake.Set{Polymake.to_cxx_type(Int)}}
@test convert(Polymake.PolymakeType, y) isa Polymake.Array{Polymake.Set{Polymake.PmInt64}}
y = Base.Vector{Base.Set{Int64}}([Base.Set([3,3]), Base.Set([3]), Base.Set([1,2])])
@test convert(Polymake.PolymakeType, y) isa Polymake.Array{Polymake.Set{Polymake.to_cxx_type(Int)}}
@test convert(Polymake.PolymakeType, y) isa Polymake.Array{Polymake.Set{Polymake.PmInt64}}

@testset verbose=true "convert_to_pm_type(PolymakeType)" begin
for T in [Polymake.Integer, Polymake.Rational, Polymake.Array, Polymake.IncidenceMatrix,
Expand All @@ -37,7 +40,7 @@

@testset verbose=true "@convert_to" begin
@test (@convert_to Integer 64) isa Polymake.Integer
@test (@convert_to Array{Set{Int}} [Set([1, 2, 4, 5, 7, 8]), Set([1]), Set([6, 9])]) isa Polymake.Array{Polymake.Set{Polymake.to_cxx_type(Int64)}}
@test (@convert_to Array{Set{Int}} [Set([1, 2, 4, 5, 7, 8]), Set([1]), Set([6, 9])]) isa Polymake.Array{Polymake.Set{Polymake.PmInt64}}
@test (@convert_to Vector{Float} [10, 11, 12]) isa Polymake.Vector{Float64}
@test (@convert_to Matrix{Rational} [10/1 11/1 12/1]) isa Polymake.Matrix{Polymake.Rational}
@test (@convert_to Polymake.Matrix{Polymake.Rational} [10/1 11/1 12/1]) isa Polymake.Matrix{Polymake.Rational}
Expand Down
9 changes: 8 additions & 1 deletion test/graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,18 @@
c = Polymake.polytope.cube(3)
faces = c.HASSE_DIAGRAM.FACES
@test faces isa Polymake.NodeMap
@test faces isa Polymake.NodeMap{Polymake.Directed, Polymake.Set{Polymake.to_cxx_type(Int64)}}
@test faces isa Polymake.NodeMap{Polymake.Directed, Polymake.Set{Polymake.PmInt64}}
@test Polymake._get_entry(faces, 0) == Set([0,1,2,3,4,5,6,7])
nm = Polymake.NodeMap{Polymake.Directed, Int64}(c.HASSE_DIAGRAM.ADJACENCY)
Polymake._set_entry(nm, 0, 10)
@test Polymake._get_entry(nm, 0) == 10

dec = c.HASSE_DIAGRAM.DECORATION
@test dec isa Polymake.NodeMap{Polymake.Directed, Polymake.BasicDecoration}
decc = copy(dec)
@test Polymake._get_entry(dec, 0) == Polymake.BasicDecoration(Set(0:7), 4)
Polymake._set_entry(decc, 0, Polymake.BasicDecoration(Polymake.Set(0:2), 2))
@test Polymake._get_entry(decc, 0) == Polymake.BasicDecoration((Set(0:2), 2))
end

@testset verbose=true "shortest_path_dijkstra" begin
Expand Down
4 changes: 3 additions & 1 deletion test/perlobj.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
@test G isa Polymake.GraphAllocated{Polymake.Undirected}
@test Polymake.bigobject("graph::Graph", ADJACENCY=G) isa Polymake.BigObject

@test Polymake.graph.PartiallyOrderedSet{Polymake.BasicDecoration}() isa Polymake.BigObject

@test polytope.cube(3, 1//4, -1//4) isa Polymake.BigObject

function test_pm_macro()
Expand Down Expand Up @@ -234,7 +236,7 @@
@testset verbose=true "polymake MILP" begin
p = @pm polytope.Polytope( INEQUALITIES=[1 1 -1; -1 0 1; 7 -1 -1] )
intvar = Polymake.Set([0,1,2])
@test Polymake.convert(Polymake.PolymakeType, intvar) isa Polymake.Set{Polymake.to_cxx_type(Int64)}
@test Polymake.convert(Polymake.PolymakeType, intvar) isa Polymake.Set{Polymake.PmInt64}

obj = [0,-1,-1]

Expand Down

2 comments on commit 0ca3e72

@benlorenz
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/111366

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.11.19 -m "<description of version>" 0ca3e720db5b7ef2fc2dde84c6396a41918e703f
git push origin v0.11.19

Please sign in to comment.