Skip to content

Commit

Permalink
Merge pull request #2492 from oscar-system/lk/vector_tropical_number
Browse files Browse the repository at this point in the history
  • Loading branch information
benlorenz authored Jun 26, 2023
2 parents 53ea281 + ba231f5 commit 8d75bb2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ GAP = "0.9.4"
Hecke = "0.18.13"
JSON = "^0.20, ^0.21"
Nemo = "0.34.3"
Polymake = "0.9.0"
Polymake = "0.10.0"
Preferences = "1"
RandomExtensions = "0.4.3"
RecipesBase = "1.2.1"
Expand Down
2 changes: 1 addition & 1 deletion src/PolyhedralGeometry/LP_file_format.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ julia> save_mps(stdout, milp)
* Columns: 2
* Format: MPS
*
Name unnamed#0
NAME unnamed#0
ROWS
N C0000000
G R0000000
Expand Down
65 changes: 27 additions & 38 deletions src/TropicalGeometry/TropicalGeometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,35 @@ include("groebner_basis.jl")
include("groebner_polyhedron.jl")
include("points.jl")

# Temporarily we will turn tropical polynomials into strings. This will be
# removed once Polymake.jl wraps its tropical polynomials and tropical numbers
#
# Warning: This function ignores all boundary cases!
function tropical_polynomial_to_polymake(f)
fstr = ""
if convention(base_ring(f)) == min
fstr *= "min("
else
fstr *= "max("
end
td = total_degree(f)
for i in 1:length(f)
fstr *= repr(coeff(f,i).data)
e = exponent_vector(f,i)
if td - sum(e) != 0
fstr *= "+"
fstr *= repr(td-sum(e))
fstr *= "x_0"
end
if !iszero(e)
for j in 1:length(e)
if !iszero(e[j])
fstr *= "+"
fstr *= repr(e[j])
fstr *= "*x_"
fstr *= repr(j)
end
end
end
if i != length(f)
fstr *= ","
end
end
fstr *= ")"
result = ["x_"*repr(i) for i in 0:nvars(parent(f))]
prepend!(result, [fstr])
return result
# Decompose a tropical polynomial into parts that Polymake can eat.
# First function deals with the coefficients,
# Second function then deals with the entire polynomial.
function homogenize_and_convert_to_pm(t::Oscar.TropicalSemiringElem{S}) where S<:Union{typeof(max), typeof(min)}
Add = S == typeof(max) ? Polymake.Max : Polymake.Min
if isinf(t)
return Polymake.TropicalNumber{Add}()
else
return Polymake.TropicalNumber{Add}(Polymake.new_rational_from_fmpq(data(t)))
end
end

function homogenize_and_convert_to_pm(f::Oscar.MPolyRingElem{Oscar.TropicalSemiringElem{S}}) where S<:Union{typeof(max), typeof(min)}
Add = S == typeof(max) ? Polymake.Max : Polymake.Min
coeffs = (Polymake.TropicalNumber{Add})[]
td = total_degree(f)
exps = (Vector{Int})[]
for term in terms(f)
push!(coeffs, homogenize_and_convert_to_pm(leading_coefficient(term)))
exp = leading_exponent_vector(term)
prepend!(exp, td-sum(exp))
push!(exps, exp)
end
exps = matrix(ZZ, exps)
coeffs = Polymake.Vector{Polymake.TropicalNumber{Add, Polymake.Rational}}(coeffs)
return coeffs, exps
end



###
# Allow gcd of vectors of univariate rational polynomials
Expand Down
5 changes: 2 additions & 3 deletions src/TropicalGeometry/hypersurface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ function TropicalHypersurface(f::AbstractAlgebra.Generic.MPoly{Oscar.TropicalSem
error("Tropical hypersurfaces of constant polynomials not supported.")
end
M = convention(base_ring(f))
fstr = Tuple(tropical_polynomial_to_polymake(f))
pmpoly = Polymake.common.totropicalpolynomial(fstr...)
pmhypproj = Polymake.tropical.Hypersurface{M}(POLYNOMIAL=pmpoly)
coeffs, exps = homogenize_and_convert_to_pm(f)
pmhypproj = Polymake.tropical.Hypersurface{M}(MONOMIALS=exps, COEFFICIENTS=coeffs)
pmhyp = Polymake.tropical.affine_chart(pmhypproj)
Vf = TropicalHypersurface{M, true}(polyhedral_complex(pmhyp))
w = pmhypproj.WEIGHTS
Expand Down
2 changes: 1 addition & 1 deletion test/PolyhedralGeometry/linear_program.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
* Columns: 2
* Format: MPS
*
Name unnamed#2
NAME unnamed#2
ROWS
N C0000000
G R0000000
Expand Down
14 changes: 14 additions & 0 deletions test/TropicalGeometry/hypersurface.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@testset "hypersurface" begin
for addition in (max, min)
T = TropicalSemiring(addition)
R,(x,y,z) = PolynomialRing(T,3)
b = [T(10),T(3//2095), T(-5//2)]
m = [[1,0,1],[0,3,4],[1,0,7]]
f = R(b,m)
hyp = TropicalHypersurface(f)
coeffs, exps = Oscar.homogenize_and_convert_to_pm(f)
@test hyp isa TropicalHypersurface{addition}
@test coeffs == get_attribute(hyp, :polymake_bigobject).COEFFICIENTS
@test exps == matrix(ZZ, get_attribute(hyp, :polymake_bigobject).MONOMIALS)
end
end
1 change: 1 addition & 0 deletions test/TropicalGeometry/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ using Oscar
include("groebner_basis.jl")
include("groebner_fan.jl")
include("initial.jl")
include("hypersurface.jl")

0 comments on commit 8d75bb2

Please sign in to comment.