Skip to content

Commit

Permalink
wrapped vectors and matrices over polynomials (#421)
Browse files Browse the repository at this point in the history
* WIP: bump libpolymake compat for polymake 4.9

keep 0.8.3 to simplify testing in the libpolymake repo

use version 0.8.4 for Polymake.jl to allow testing with Oscar
if it breaks we can bump that to 0.9 as well

versions will be adjusted later

* test: names and descriptions

* wrapped vectors and matrices over polynomials

* vectors: try fixing macos convert

* vectors: try fixing macos convert, #2

* vectors: try fixing macos convert, #3

* project: only support libpolymake_julia 0.9

* bump version to 0.9

---------

Co-authored-by: Benjamin Lorenz <[email protected]>
Co-authored-by: Benjamin Lorenz <[email protected]>
  • Loading branch information
3 people authored Feb 3, 2023
1 parent 8305862 commit dde4dbe
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 4 deletions.
4 changes: 2 additions & 2 deletions 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.8.4"
version = "0.9.0"

[deps]
BinaryWrappers = "f01c122e-0ea1-4f85-ad8f-907073ad7a9f"
Expand Down Expand Up @@ -30,7 +30,7 @@ JSON = "^0.20, ^0.21"
Mongoc = "~0.6.0, ~0.7.0, ~0.8.0"
Scratch = "1.1"
julia = "^1.6"
libpolymake_julia_jll = "~0.8.3"
libpolymake_julia_jll = "~0.9.0"

[extras]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Expand Down
2 changes: 1 addition & 1 deletion src/Polymake.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import TOPCOM_jll
using libpolymake_julia_jll


const jlpolymake_version_range = (v"0.8.3", v"0.9")
const jlpolymake_version_range = (v"0.9.0", v"0.10")

struct PolymakeError <: Exception
msg
Expand Down
3 changes: 3 additions & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ to_cxx_type(::Type{<:AbstractSet{T}}) where T =
Set{to_cxx_type(T)}
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_jl_type(::Type{T}) where T = T
to_jl_type(::Type{CxxWrap.CxxBool}) = Bool
Expand Down Expand Up @@ -77,6 +79,7 @@ convert_to_pm_type(::Type{<:AbstractSparseMatrix{<:Union{Bool, CxxWrap.CxxBool}}
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{<:Polynomial{<:Rational, <:Union{Int64, CxxWrap.CxxLong}}}) = Polynomial{Rational, CxxWrap.CxxLong}

# Graph, EdgeMap, NodeMap
const DirType = Union{Directed, Undirected}
Expand Down
4 changes: 4 additions & 0 deletions src/matrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ function Matrix{T}(::UndefInitializer, m::Base.Integer, n::Base.Integer) where
return Matrix{to_cxx_type(T)}(convert(Int64, m), convert(Int64,n))
end

function Matrix{Polynomial{Rational, CxxWrap.CxxLong}}(::UndefInitializer, m::Base.Integer, n::Base.Integer)
return _same_element_matrix_polynomial(Polynomial{Rational, CxxWrap.CxxLong}([0], permutedims([0])), convert(Int64, m), convert(Int64, n))
end

function Matrix{T}(mat::AbstractMatrix) where T
res = Matrix{T}(undef, size(mat)...)
@inbounds res .= mat
Expand Down
2 changes: 1 addition & 1 deletion src/setup_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const SmallObject = Union{
EdgeMap,
NodeMap,
}
const VecOrMat_eltypes = Union{Int64, Integer, Rational, Float64, QuadraticExtension{Rational}, CxxWrap.CxxLong}
const VecOrMat_eltypes = Union{Int64, Integer, Rational, Float64, QuadraticExtension{Rational}, CxxWrap.CxxLong, Polynomial{Rational, CxxWrap.CxxLong}}

const TypeConversionFunctions = Dict(
Symbol("Int") => to_int,
Expand Down
4 changes: 4 additions & 0 deletions src/vectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ function Vector{T}(::UndefInitializer, n::Base.Integer) where
return Vector{to_cxx_type(T)}(convert(Int64, n))
end

function Vector{Polynomial{Rational, Int64}}(::UndefInitializer, n::Base.Integer)
return Vector{Polynomial{Rational, CxxWrap.CxxLong}}(convert(Int64, n), Polynomial{Rational, CxxWrap.CxxLong}([0], permutedims([0])))
end

function Vector{T}(vec::AbstractVector) where T
res = Vector{T}(undef, size(vec)...)
@inbounds res .= vec
Expand Down
37 changes: 37 additions & 0 deletions test/matrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,41 @@
@test Y + T.(2 * jl_m) == T.(2 * jl_m) + Y == Y .+ T.(2 * jl_m) == T.(2 * jl_m) .+ Y == (1 + sr2) * jl_y
end
end

for S in [Polymake.Rational]
T = Polymake.Polynomial{S, CxxWrap.CxxLong}
@test Polymake.Matrix{T} <: AbstractMatrix
@test Polymake.Matrix{T}(undef, 3,4) isa AbstractMatrix
@test Polymake.Matrix{T}(undef, 3,4) isa Polymake.Matrix
@test Polymake.Matrix{T}(undef, 3,4) isa Polymake.Matrix{<:supertype(T)}
@test Polymake.Matrix{T}(undef, 3,4) isa Polymake.Matrix{Polymake.to_cxx_type(T)}
M = Polymake.Matrix{T}(undef, 3,4)
M[1,1] = T([-1, 2], [0 1 1; 1 0 1])
M[end] = T([1, 1, 1], [1 0 0; 0 1 0; 0 0 1])
@test M[1,1] isa T
@test M[1,1] == T([-1, 2], [0 1 1; 1 0 1])
@test M[end] isa T
@test M[end] == M[end, end] == T([1, 1, 1], [1 0 0; 0 1 0; 0 0 1])

@test eltype(M) == Polymake.Polynomial{Polymake.Rational, CxxWrap.CxxLong}

@test_throws BoundsError M[0, 1]
@test_throws BoundsError M[2, 5]

@test length(M) == 12
@test size(M) == (3,4)

for U in [IntTypes; Polymake.Integer]
V = Polymake.Matrix{T}(M) # local copy
@test setindex!(V, T([2, 3], [2 3 0; 0 2 3]), 1, 1) isa Polymake.Matrix{T}
@test V[U(1), 1] isa T
@test V[1, U(1)] == T([2, 3], [2 3 0; 0 2 3])
# testing the return value of brackets operator
@test (V[2, 1] = T([4, 5], [1 0 0; 0 0 1])) isa T
V[2, 1] = T([4, 5], [1 0 0; 0 0 1])
@test V[2, 1] == T([4, 5], [1 0 0; 0 0 1])
@test string(V) == "pm::Matrix<pm::Polynomial<pm::Rational, long> >\n2*x_0^2*x_1^3 + 3*x_1^2*x_2^3 0 0 0\n4*x_0 + 5*x_2 0 0 0\n0 0 0 x_0 + x_1 + x_2\n"
end
end

end
12 changes: 12 additions & 0 deletions test/perlobj.jl
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,16 @@
@testset "toplevel visual" begin
@test visual(Polymake.Visual, polytope.cube(3)) isa Polymake.Visual
end

@testset "names and description" begin
p = polytope.cube(3)
Polymake.setname!(p,"somename")
@test occursin("somename", String(Polymake.getname(p)))
Polymake.setname!(p,"other")
@test occursin("other", String(Polymake.getname(p)))
@test occursin("cube of dimension", String(Polymake.getdescription(p)))
Polymake.setdescription!(p, "dual of the cross polytope")
@test occursin("cross polytope", String(Polymake.getdescription(p)))
end

end
36 changes: 36 additions & 0 deletions test/vectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,41 @@
@test Y + T.(2 * jl_v) == T.(2 * jl_v) + Y == Y .+ T.(2 * jl_v) == T.(2 * jl_v) .+ Y == (1 + sr2) * jl_y
end
end

for S in [Polymake.Rational]
T = Polymake.Polynomial{S, Int64}
@test Polymake.Vector{T} <: AbstractVector
@test Polymake.Vector{T}(undef, 3) isa AbstractVector
@test Polymake.Vector{T}(undef, 3) isa Polymake.Vector
@test Polymake.Vector{T}(undef, 3) isa AbstractVector{<:supertype(T)}
@test Polymake.Vector{T}(undef, 3) isa Polymake.Vector{Polymake.to_cxx_type(T)}
V = Polymake.Vector{T}(undef, 3)
V[1] = T([-1, 2], [0 1 1; 1 0 1])
V[end] = T([1, 1, 1], [1 0 0; 0 1 0; 0 0 1])
@test V[1] isa Polymake.to_cxx_type(T)
@test V[1] == T([-1, 2], [0 1 1; 1 0 1])
@test V[end] isa Polymake.to_cxx_type(T)
@test V[end] == T([1, 1, 1], [1 0 0; 0 1 0; 0 0 1])

@test eltype(V) == Polymake.Polynomial{Polymake.Rational, CxxWrap.CxxLong}

@test_throws BoundsError V[0]
@test_throws BoundsError V[5]

@test length(V) == 3
@test size(V) == (3,)

for U in [IntTypes; Polymake.Integer]
W = Polymake.Vector{T}(V) # local copy
@test setindex!(W, T([2, 3], [2 3 0; 0 2 3]), 1) isa Polymake.Vector{Polymake.to_cxx_type(T)}
@test W[U(1)] isa Polymake.to_cxx_type(T)
@test W[U(1)] == T([2, 3], [2 3 0; 0 2 3])
# testing the return value of brackets operator
@test (W[3] = T([4, 5], [1 0 0; 0 0 1])) isa Polymake.to_cxx_type(T)
W[3] = T([4, 5], [1 0 0; 0 0 1])
@test W[3] == T([4, 5], [1 0 0; 0 0 1])
@test string(W) == "pm::Vector<pm::Polynomial<pm::Rational, long> >\n2*x_0^2*x_1^3 + 3*x_1^2*x_2^3 0 4*x_0 + 5*x_2"
end
end

end

2 comments on commit dde4dbe

@benlorenz
Copy link
Member

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/76957

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.9.0 -m "<description of version>" dde4dbe6019772448aa1f147ca05cc084ff88db8
git push origin v0.9.0

Please sign in to comment.