"Inplace" iterators over the coefficients, monomials, terms and exponent vectors of a multivariate polynomial#2196
Conversation
| if !isassigned(m.coeffs, 1) || !is_one(m.coeffs[1]) | ||
| m.coeffs[1] = one(base_ring(x)) | ||
| end |
There was a problem hiding this comment.
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?
4f33ce7 to
7275f84
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2196 +/- ##
==========================================
- Coverage 87.99% 87.99% -0.01%
==========================================
Files 127 127
Lines 31769 31802 +33
==========================================
+ Hits 27956 27983 +27
- Misses 3813 3819 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| # 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 |
There was a problem hiding this comment.
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) .
|
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. |
|
IMO this should also have the abstract fallbacks, see Nemocas/Nemo.jl#2161 (comment) |
Also done. |
lgoettgens
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
| return Generic.MPolyCoeffs(a, inplace=inplace) | |
| return Generic.MPolyCoeffs(a; inplace) |
IMO a bit neater, but I don't really care
There was a problem hiding this comment.
I never really got the hang of this syntax, so if you don't care, I'll prefer my version.
lgoettgens
left a comment
There was a problem hiding this comment.
Looks great! Thanks for working on this!
a597880 to
d6e24a0
Compare
|
|
||
| If `inplace` is `true`, the elements of the iterator may share their memory. This | ||
| means that an element returned by the iterator may be overwritten 'in place' in | ||
| the next iteration step. This may result in significantly fewer memory allocations. |
There was a problem hiding this comment.
I added these sentences to the doc strings. I'm happy for any suggestions. If I understand the suggestion in oscar-system/Oscar.jl#5530 (comment) correctly, we want to have something to add to all iterator doc strings that allow inplace. So maybe we should agree on a nice wording here and then we can copy and paste it everywhere :)
EDIT: I kept this intentionally vague ("may") because with this generic code it might be that the inplace version actually doesn't do anything different (because something is not implemented for a particular type).
There was a problem hiding this comment.
I like this, if others agree I will add this same wording to the other methods in Oscar.
There was a problem hiding this comment.
Maybe mention that it might to lead to unexpected behavior when calling collect on such an iterator.
There was a problem hiding this comment.
I added a bit more, so this is ready for the next round of feedback :)
I feel like in an ideal world, we would put this paragraph somewhere "central" in the documentation and just link there from all iterators that support inplace = true. But I don't know where.
There was a problem hiding this comment.
Looks good to me, thanks. (And I agree, one "central place" would be great, but for the time being this here is fine.)
|
I'll merge this beginning of next week, if there are no further comments. |
Use `@inferred` correctly and fix return type
Use `@inferred` correctly and fix return type
Add an
inplaceoption 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!andexponent_vector!: Should they be exported (I did it for now becausemonomial!is exported as well)? Do I need to implement an "abstract fall-back" insrc/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 :So that moves in the right direction.