Skip to content

Commit

Permalink
Speed up is_homogeneous
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);
      314.970 ns (6 allocations: 288 bytes)

    julia> @Btime is_homogeneous(F);
      642.638 ns (8 allocations: 520 bytes)
  • Loading branch information
fingolfin committed Sep 19, 2024
1 parent b5d0b43 commit 0e78024
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Rings/mpoly-graded.jl
Original file line number Diff line number Diff line change
Expand Up @@ -978,14 +978,20 @@ 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]
zero!(u.coeff)
# TODO: once Hecke supports zero! on FinGenAbGroupElem, switch to this:
#zero!(u)
for i=1:length(c)
u += c[i]*d[i]
addmul!(u.coeff, d[i].coeff, c[i])
# TODO: once Hecke supports addmul! on FinGenAbGroupElem, switch to this:
#addmul!(u, d[i], c[i])
end
push!(S, u)
if length(S) > 1
if S === nothing
S = u
elseif S != u
return false
end
end
Expand Down

0 comments on commit 0e78024

Please sign in to comment.