From 90c2c88361da0bcfa888ca491834893a1dd4e683 Mon Sep 17 00:00:00 2001 From: ederc Date: Thu, 19 Sep 2024 15:39:43 +0200 Subject: [PATCH] polynomial_ring cached=false for combinatorics (#3856) * 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 --- src/Combinatorics/Matroids/ChowRings.jl | 6 +-- .../Matroids/matroid_strata_grassmannian.jl | 4 +- src/Combinatorics/Matroids/properties.jl | 49 ++++++++++++------- test/Combinatorics/Matroids/Matroids.jl | 4 +- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/Combinatorics/Matroids/ChowRings.jl b/src/Combinatorics/Matroids/ChowRings.jl index 8db7dba35522..9eda21a3f7e8 100644 --- a/src/Combinatorics/Matroids/ChowRings.jl +++ b/src/Combinatorics/Matroids/ChowRings.jl @@ -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 @@ -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] diff --git a/src/Combinatorics/Matroids/matroid_strata_grassmannian.jl b/src/Combinatorics/Matroids/matroid_strata_grassmannian.jl index 166a145d18b8..acfb32f4dd8b 100644 --- a/src/Combinatorics/Matroids/matroid_strata_grassmannian.jl +++ b/src/Combinatorics/Matroids/matroid_strata_grassmannian.jl @@ -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 @@ -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 diff --git a/src/Combinatorics/Matroids/properties.jl b/src/Combinatorics/Matroids/properties.jl index 7124ca7efa4f..1f8f22afc81a 100644 --- a/src/Combinatorics/Matroids/properties.jl +++ b/src/Combinatorics/Matroids/properties.jl @@ -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). @@ -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. @@ -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). @@ -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 diff --git a/test/Combinatorics/Matroids/Matroids.jl b/test/Combinatorics/Matroids/Matroids.jl index 0458d77f9f73..94474e845106 100644 --- a/test/Combinatorics/Matroids/Matroids.jl +++ b/test/Combinatorics/Matroids/Matroids.jl @@ -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] @@ -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