Skip to content

Conversation

@joschmitt
Copy link
Collaborator

@joschmitt joschmitt commented Oct 24, 2025

Add an inplace option to the iterators of exponents, coefficients, terms and monomials of a multivariate polynomial.
This is still a draft. Is this the "interface" we want?
Regarding the new functions coeff!, term! and exponent_vector!: Should they be exported (I did it for now because monomial! is exported as well)? Do I need to implement an "abstract fall-back" in src/MPoly.jl?

Nemocas/Nemo.jl#2161 implements the (few) necessary functions for QQMPolyRingElem. Using those, I get the following timings in oscar-system/Oscar.jl#5483 :

# Previously
julia> @btime inv_c[n-k+1:n];
  3.628 s (28973402 allocations: 1.73 GiB)

# With inplace iterators
julia> @btime inv_c[n-k+1:n];
  1.356 s (2811 allocations: 279.24 KiB)

So that moves in the right direction.

Comment on lines +682 to +686
if !isassigned(m.coeffs, 1) || !is_one(m.coeffs[1])
m.coeffs[1] = one(base_ring(x))
end
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit ugly, but it gets the monomials iterator to constant allocations. Otherwise it would allocate a new 1 all the time. Is this too much of a corner case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me

@codecov
Copy link

codecov bot commented Oct 29, 2025

Codecov Report

❌ Patch coverage is 90.90909% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.00%. Comparing base (82f6998) to head (a597880).

Files with missing lines Patch % Lines
src/MPoly.jl 55.55% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2196      +/-   ##
==========================================
- Coverage   88.01%   88.00%   -0.01%     
==========================================
  Files         127      127              
  Lines       31765    31798      +33     
==========================================
+ Hits        27957    27984      +27     
- Misses       3808     3814       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines 398 to 404
# S may be the type of anything that can store an exponent vector, for example
# Vector{Int}, ZZMatrix, ...
struct MPolyExponentVectors{T <: AbstractAlgebra.RingElem, S}
poly::T
inplace::Bool
temp::S # only used if inplace == true
end
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exponent_vectors iterator can now in principle take anything as type for the exponents, as long as there is a exponent_vector!(::T, ...) method for that type. Now one can do exponent_vectors(Vector{ZZRingElem}, ::QQMPolyRingElem) and it would for example allow to add a variant exponent_vectors(ZZMatrix, ...) in Nemo like mentioned in oscar-system/Oscar.jl#5483 (comment) .

@joschmitt joschmitt marked this pull request as ready for review October 29, 2025 16:04
@joschmitt joschmitt closed this Oct 30, 2025
@joschmitt joschmitt reopened this Oct 30, 2025
@fingolfin
Copy link
Member

Thank you very much for working on this. It seems fine to me. But I'd like triage to have a look at it before merging.

@joschmitt joschmitt changed the title Towards inplace iterators for exponents of multivariate polynomials etc. "Inplace" iterators over the coefficients, monomials, terms and exponent vectors of a multivariate polynomial Nov 4, 2025
@joschmitt joschmitt added the release notes: use title For PRs: the title of this PR is suitable for direct use in the release notes label Nov 4, 2025
@lgoettgens
Copy link
Member

IMO this should also have the abstract fallbacks, see Nemocas/Nemo.jl#2161 (comment)

@joschmitt
Copy link
Collaborator Author

IMO this should also have the abstract fallbacks, see Nemocas/Nemo.jl#2161 (comment)

Also done.

Copy link
Member

@lgoettgens lgoettgens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some more minor comments. I didn't really think about exponent vectors yet, mostly about the iterators that return some ring elem of some kind

function coefficients(a::MPolyRingElem{T}) where T <: RingElement
return Generic.MPolyCoeffs(a)
function coefficients(a::MPolyRingElem{T}; inplace::Bool = false) where T <: RingElement
return Generic.MPolyCoeffs(a, inplace=inplace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Generic.MPolyCoeffs(a, inplace=inplace)
return Generic.MPolyCoeffs(a; inplace)

IMO a bit neater, but I don't really care

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never really got the hang of this syntax, so if you don't care, I'll prefer my version.

Copy link
Member

@lgoettgens lgoettgens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for working on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes: use title For PRs: the title of this PR is suitable for direct use in the release notes triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants