Skip to content

Commit f811ba2

Browse files
committed
Add docstrings
1 parent 64d24c0 commit f811ba2

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

src/comparison.jl

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
338338
An iterator for generating monomial exponents for monomial
339339
ordering `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
"""
342380
struct 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
347385
end
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)
351389
end
352390

353391
function ExponentsIterator(args...; kws...)
@@ -359,9 +397,9 @@ Base.IteratorSize(::Type{<:ExponentsIterator{M,Nothing}}) where {M} = Base.IsInf
359397
Base.IteratorSize(::Type{<:ExponentsIterator{M,Int}}) where {M} = Base.HasLength()
360398

361399
function 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
367405
end
@@ -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)
429467
end
430468

431469
function Base.iterate(it::ExponentsIterator, state)

src/polynomial.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,12 @@ end
252252
253253
Returns an iterator over the monomials of `p` of the nonzero terms of the polynomial sorted in the decreasing order.
254254
255-
monomials(vars::Tuple, degs::AbstractVector{Int}, filter::Function = m -> true)
255+
monomials(vars::Union{Vector{<:AbstractVariable},Tuple}, degs::AbstractVector{Int}, filter::Function = m -> true)
256256
257257
Builds the vector of all the monomial_vector `m` with variables `vars` such that the degree `degree(m)` is in `degs` and `filter(m)` is `true`.
258258
259+
See also [`ExponentsIterator`](@ref).
260+
259261
### Examples
260262
261263
Calling `monomials` on ``4x^2y + xy + 2x`` should return an iterator of ``[x^2y, xy, x]``.

0 commit comments

Comments
 (0)