Skip to content

Commit

Permalink
polynomial_ring cached=false for combinatorics (#3856)
Browse files Browse the repository at this point in the history
* sets poly ring caching to false for combinatorics

* Provide `parent` keywords and fix tests

* `Int` -> `ZZRingElem`

* Document optional polynomial ring

---------

Co-authored-by: Johannes Schmitt <jschmitt@posteo.eu>
  • Loading branch information
ederc and joschmitt authored Sep 19, 2024
1 parent 6a7407f commit 90c2c88
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/Combinatorics/Matroids/ChowRings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ function chow_ring(M::Matroid; ring::Union{MPolyRing,Nothing}=nothing, extended:

@req length(var_names) > 0 "Chow ring is empty"
if graded
ring, vars = graded_polynomial_ring(QQ, var_names, cached=false)
ring, vars = graded_polynomial_ring(QQ, var_names; cached=false)
else
ring, vars = polynomial_ring(QQ, var_names, cached=false)
ring, vars = polynomial_ring(QQ, var_names; cached=false)
end
else
if extended
Expand Down Expand Up @@ -166,7 +166,7 @@ function augmented_chow_ring(M::Matroid)
var_names = vcat(element_var_names, flat_var_names)
s = length(var_names)

ring, vars = polynomial_ring(QQ, var_names)
ring, vars = polynomial_ring(QQ, var_names; cached=false)
element_vars = vars[1:n]
flat_vars = vars[n+1:s]

Expand Down
4 changes: 2 additions & 2 deletions src/Combinatorics/Matroids/matroid_strata_grassmannian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function make_polynomial_ring(Bs::Vector{Vector{Int}}, B::Vector{Int},
F::AbstractAlgebra.Ring)

MC = bases_matrix_coordinates(Bs, B)
R, x = polynomial_ring(F, :"x"=>MC)
R, x = polynomial_ring(F, :"x"=>MC; cached=false)
xdict = Dict{Vector{Int}, MPolyRingElem}([MC[i] => x[i] for i in 1:length(MC)])
return R, x, xdict
end
Expand Down Expand Up @@ -330,7 +330,7 @@ function realization_polynomial_ring(Bs::Vector{Vector{Int}}, A::Vector{Int},
MC = realization_bases_coordinates(Bs, A)
D = partial_matrix_max_rows(MC)
MR = [x for x in MC if x[1] != D[x[2]]]
R, x = polynomial_ring(F, :"x"=>MR)
R, x = polynomial_ring(F, :"x"=>MR; cached=false)
xdict = Dict{Vector{Int}, MPolyRingElem}(MR[i] => x[i] for i in 1:length(MR))
return R, x, xdict
end
Expand Down
49 changes: 31 additions & 18 deletions src/Combinatorics/Matroids/properties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,8 @@ end

@doc raw"""
tutte_polynomial(M::Matroid)
tutte_polynomial(M::Matroid; parent::ZZMPolyRing)
tutte_polynomial(parent::ZZMPolyRing, M::Matroid)
Return the Tutte polynomial of `M`. This is polynomial in the variables x and y with integral coefficients.
See Section 15.3 in [Oxl11](@cite).
Expand All @@ -896,15 +898,20 @@ x^3 + 4*x^2 + 7*x*y + 3*x + y^4 + 3*y^3 + 6*y^2 + 3*y
```
"""
function tutte_polynomial(M::Matroid)
R, (x, y) = polynomial_ring(ZZ, ["x", "y"])
poly = pm_object(M).TUTTE_POLYNOMIAL
exp = Polymake.monomials_as_matrix(poly)
return R(Vector{Int}(Polymake.coefficients_as_vector(poly)),[[exp[i,1],exp[i,2]] for i in 1:size(exp)[1]])
function tutte_polynomial(M::Matroid;
parent::ZZMPolyRing = polynomial_ring(ZZ, [:x, :y]; cached = false)[1])
@assert ngens(parent) >= 2
poly = pm_object(M).TUTTE_POLYNOMIAL
exp = Polymake.monomials_as_matrix(poly)
return parent(Vector{ZZRingElem}(Polymake.coefficients_as_vector(poly)), [[exp[i, 1],exp[i, 2]] for i in 1:size(exp)[1]])
end

tutte_polynomial(R::ZZMPolyRing, M::Matroid) = tutte_polynomial(M, parent = R)

@doc raw"""
characteristic_polynomial(M::Matroid)
characteristic_polynomial(M::Matroid; parent::ZZPolyRing)
characteristic_polynomial(parent::ZZPolyRing, M::Matroid)
Return the characteristic polynomial of `M`. This is polynomial in the variable q with integral coefficients.
It is computed as an evaluation of the Tutte polynmomial.
Expand All @@ -917,13 +924,17 @@ q^3 - 7*q^2 + 14*q - 8
```
"""
function characteristic_polynomial(M::Matroid)
R, q = polynomial_ring(ZZ, 'q')
return (-1)^rank(M)*tutte_polynomial(M)(1-q,0)
function characteristic_polynomial(M::Matroid;
parent::ZZPolyRing = polynomial_ring(ZZ, :q; cached = false)[1])
return (-1)^rank(M) * tutte_polynomial(M)(1 - gen(parent), 0)
end

characteristic_polynomial(R::ZZPolyRing, M::Matroid) = characteristic_polynomial(M, parent = R)

@doc raw"""
reduced_characteristic_polynomial(M::Matroid)
reduced_characteristic_polynomial(M::Matroid; parent::ZZPolyRing)
reduced_characteristic_polynomial(parent::ZZPolyRing, M::Matroid)
Return the reduced characteristic polynomial of `M`. This is the quotient of the characteristic polynomial by (q-1).
See Section 15.2 in [Oxl11](@cite).
Expand All @@ -935,18 +946,20 @@ q^2 - 6*q + 8
```
"""
function reduced_characteristic_polynomial(M::Matroid)
R, q = polynomial_ring(ZZ, 'q')
p = characteristic_polynomial(M)
c = Vector{Int}(undef,degree(p))
s = 0
for i in 1:degree(p)
s-= coeff(p,i-1)
c[i] = s
end
return R(c)
function reduced_characteristic_polynomial(M::Matroid;
parent::ZZPolyRing = polynomial_ring(ZZ, :q; cached = false)[1])
p = characteristic_polynomial(M, parent = parent)
c = Vector{ZZRingElem}(undef, degree(p))
s = ZZ(0)
for i in 1:degree(p)
s -= coeff(p, i - 1)
c[i] = s
end
return parent(c)
end

reduced_characteristic_polynomial(R::ZZPolyRing, M::Matroid) = reduced_characteristic_polynomial(M, parent = R)

# This function compares two sets A and B in reverse lexicographic order.
# It assumes that both sets are of the same length and ordered.
# It returns true if A is less than B in this order
Expand Down
4 changes: 2 additions & 2 deletions test/Combinatorics/Matroids/Matroids.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
@test vertical_connectivity(M) == values[4]
@test girth(M) == values[5]
@test tutte_connectivity(M) == values[6]
@test characteristic_polynomial(M) == values[7]
@test characteristic_polynomial(R, M) == values[7]
@test length(cobases(M)) == length(bases(M))
@test cohyperplanes(M) == [setdiff(matroid_groundset(M),set) for set in circuits(M)]
@test is_regular(M) == values[8]
Expand Down Expand Up @@ -288,7 +288,7 @@

@test cobases(N) == [['i','j'], [2,'j'], [2,'i'], [1,'j'], [1,'i']]

@test charpoly(N) == R(q^2-3q+2)
@test charpoly(R, N) == R(q^2-3q+2)

@test is_clutter(bases(N)) == true
@test is_clutter(circuits(N)) == true
Expand Down

0 comments on commit 90c2c88

Please sign in to comment.