Skip to content

Commit

Permalink
Speed up is_homogeneous (#4119)
Browse files Browse the repository at this point in the history
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 <lars.goettgens@rwth-aachen.de>
  • Loading branch information
fingolfin and lgoettgens authored Sep 23, 2024
1 parent 55ea957 commit 38f37bc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 8 additions & 5 deletions src/Rings/mpoly-graded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 38f37bc

Please sign in to comment.