From 38f37bc2aea3b23146c1f351a89f2604655ba08c Mon Sep 17 00:00:00 2001 From: Max Horn Date: Mon, 23 Sep 2024 15:32:14 +0200 Subject: [PATCH] Speed up `is_homogeneous` (#4119) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some measurements taking the example from the `is_homogeneous` docstring: R, (x, y, z) = graded_polynomial_ring(QQ, ["x", "y", "z"], [1, 2, 3]) f = x^2+y*z W = [1 2 1 0; 3 4 0 1] S, (w, x, y, z) = graded_polynomial_ring(QQ, ["w", "x", "y", "z"], W) F = w^3*y^3*z^3 + w^2*x*y^2*z^2 + w*x^2*y*z + x^3 Before: julia> @btime is_homogeneous(f); 1.533 μs (62 allocations: 2.28 KiB) julia> @btime is_homogeneous(F); 3.932 μs (152 allocations: 5.53 KiB) After: julia> @btime is_homogeneous(f); 334.071 ns (10 allocations: 416 bytes) julia> @btime is_homogeneous(F); 556.150 ns (12 allocations: 656 bytes) --------- Co-authored-by: Lars Göttgens --- Project.toml | 2 +- src/Rings/mpoly-graded.jl | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Project.toml b/Project.toml index 9f9318fb906e..f58c37819f69 100644 --- a/Project.toml +++ b/Project.toml @@ -29,7 +29,7 @@ AbstractAlgebra = "0.43.1" AlgebraicSolving = "0.7.0" Distributed = "1.6" GAP = "0.11.3" -Hecke = "0.34.1" +Hecke = "0.34.3" JSON = "^0.20, ^0.21" JSON3 = "1.13.2" LazyArtifacts = "1.6" diff --git a/src/Rings/mpoly-graded.jl b/src/Rings/mpoly-graded.jl index 7b0f02398022..4ac97f7009a6 100644 --- a/src/Rings/mpoly-graded.jl +++ b/src/Rings/mpoly-graded.jl @@ -970,14 +970,17 @@ true function is_homogeneous(F::MPolyDecRingElem) D = parent(F).D d = parent(F).d - S = Set{elem_type(D)}() + S = nothing + u = zero(D) for c = MPolyExponentVectors(forget_decoration(F)) - u = parent(F).D[0] + u = zero!(u) for i=1:length(c) - u += c[i]*d[i] + u = addmul_delayed_reduction!(u, d[i], c[i]) end - push!(S, u) - if length(S) > 1 + u = reduce!(u) + if S === nothing + S = deepcopy(u) + elseif S != u return false end end