@@ -330,24 +330,62 @@ _not_first_indices(n, ::Type{Graded{M}}) where {M} = _not_first_indices(n, M)
330330"""
331331 struct ExponentsIterator{M}(
332332 object;
333- min_deg ::Int = 0,
334- max_deg ::Union{Nothing,Int} = nothing,
333+ mindegree ::Int = 0,
334+ maxdegree ::Union{Nothing,Int} = nothing,
335335 inline::Bool = false,
336336 )
337337
338338An iterator for generating monomial exponents for monomial
339339ordering `M`. The type of the vector of exponents is the type of
340340`object` and is length (i.e., the number of variables) is `length(object)`.
341+
342+ See also [`monomials`](@ref).
343+
344+ ### Examples
345+
346+ The following example shows how to generate all exponents of
347+ monomials of 2 variables up to degree 2.
348+ ```jldoctest
349+ julia> collect(ExponentsIterator((0, 0), maxdegree = 2))
350+ 6-element Vector{Tuple{Int64, Int64}}:
351+ (0, 0)
352+ (0, 1)
353+ (1, 0)
354+ (0, 2)
355+ (1, 1)
356+ (2, 0)
357+ ```
358+ Note that you can easily generate the tuple of exponents
359+ of arbitrary length using `ntuple` as follows:
360+ ```jldoctest
361+ julia> collect(ExponentsIterator(ntuple(zero, 3), mindegree = 2, maxdegree = 2))
362+ 6-element Vector{Tuple{Int64, Int64, Int64}}:
363+ (0, 0, 2)
364+ (0, 1, 1)
365+ (0, 2, 0)
366+ (1, 0, 1)
367+ (1, 1, 0)
368+ (2, 0, 0)
369+ ```
370+ You can also change the monomial ordering as follows:
371+ ```jldoctest
372+ julia> collect(Iterators.take(ExponentsIterator{LexOrder}(zeros(Int, 2), mindegree = 2, maxdegree = 4), 4))
373+ 4-element Vector{Vector{Int64}}:
374+ [0, 2]
375+ [0, 3]
376+ [0, 4]
377+ [1, 0]
378+ ```
341379"""
342380struct ExponentsIterator{M,D<: Union{Nothing,Int} ,O}
343381 object:: O # Used to get number of variables and get new zero elements
344- min_deg :: Int
345- max_deg :: D
382+ mindegree :: Int
383+ maxdegree :: D
346384 inline:: Bool
347385end
348386
349- function ExponentsIterator {M} (object; min_deg :: Int = 0 , max_deg :: Union{Nothing,Int} = nothing , inline:: Bool = false ) where {M}
350- ExponentsIterator {M,typeof(max_deg ),typeof(object)} (object, min_deg, max_deg , inline)
387+ function ExponentsIterator {M} (object; mindegree :: Int = 0 , maxdegree :: Union{Nothing,Int} = nothing , inline:: Bool = false ) where {M}
388+ ExponentsIterator {M,typeof(maxdegree ),typeof(object)} (object, mindegree, maxdegree , inline)
351389end
352390
353391function ExponentsIterator (args... ; kws... )
@@ -359,9 +397,9 @@ Base.IteratorSize(::Type{<:ExponentsIterator{M,Nothing}}) where {M} = Base.IsInf
359397Base. IteratorSize (:: Type{<:ExponentsIterator{M,Int}} ) where {M} = Base. HasLength ()
360398
361399function Base. length (it:: ExponentsIterator{M,Int} ) where {M}
362- len = binomial (nvariables (it) + it. max_deg , nvariables (it))
363- if it. min_deg > 0
364- len -= binomial (nvariables (it) + it. min_deg , nvariables (it))
400+ len = binomial (nvariables (it) + it. maxdegree , nvariables (it))
401+ if it. mindegree > 0
402+ len -= binomial (nvariables (it) + it. mindegree - 1 , nvariables (it))
365403 end
366404 return len
367405end
@@ -371,14 +409,14 @@ nvariables(it::ExponentsIterator) = length(it.object)
371409_increase_degree (it:: ExponentsIterator{<:Graded,Nothing} , _) = false
372410_increase_degree (it:: ExponentsIterator{<:Graded,Int} , _) = false
373411_increase_degree (it:: ExponentsIterator{M,Nothing} , _) where {M} = true
374- _increase_degree (it:: ExponentsIterator{M,Int} , deg) where {M} = deg < it. max_deg
412+ _increase_degree (it:: ExponentsIterator{M,Int} , deg) where {M} = deg < it. maxdegree
375413
376414# We just changed the degree by removing `Δ`,
377415# In graded ordering, we just add `Δ` to maintain the same degree
378416_adjust_degree (:: ExponentsIterator{<:Graded} , _, Δ) = Δ
379- # Otherwise, we just need the degree to stay above `it.min_deg `,
380- # so we need to add `it.min_deg - deg`
381- _adjust_degree (it:: ExponentsIterator , deg, _) = min (0 , it. min_deg - deg)
417+ # Otherwise, we just need the degree to stay above `it.mindegree `,
418+ # so we need to add `it.mindegree - deg`
419+ _adjust_degree (it:: ExponentsIterator , deg, _) = min (0 , it. mindegree - deg)
382420
383421_setindex! (x, v, i) = Base. setindex! (x, v, i)
384422_setindex! (x:: Tuple , v, i) = Base. setindex (x, v, i)
@@ -401,7 +439,7 @@ function _iterate!(it::ExponentsIterator{M}, z, deg) where {M}
401439 I = _not_first_indices (nvariables (it), M)
402440 i = findfirst (i -> ! iszero (z[i]), I)
403441 if isnothing (i)
404- if ! isnothing (it. max_deg ) && deg == it. max_deg
442+ if ! isnothing (it. maxdegree ) && deg == it. maxdegree
405443 return
406444 end
407445 z = _zero! (z)
@@ -410,7 +448,7 @@ function _iterate!(it::ExponentsIterator{M}, z, deg) where {M}
410448 end
411449 j = I[i]
412450 Δ = z[j] - 1
413- _setindex! (z, 0 , j)
451+ z = _setindex! (z, 0 , j)
414452 deg -= Δ
415453 Δ = _adjust_degree (it, deg, Δ)
416454 deg += Δ
@@ -424,8 +462,8 @@ function Base.iterate(it::ExponentsIterator{M}) where {M}
424462 return
425463 end
426464 z = _zero (it. object)
427- z = _setindex! (z, it. min_deg , _last_lex_index (nvariables (it), M))
428- return z, (z, it. min_deg )
465+ z = _setindex! (z, it. mindegree , _last_lex_index (nvariables (it), M))
466+ return z, (z, it. mindegree )
429467end
430468
431469function Base. iterate (it:: ExponentsIterator , state)
0 commit comments