@@ -125,19 +125,31 @@ are given in the order of the variables for the ring, as supplied when the
125125ring was created.
126126"""
127127function exponent_vector (a:: MPoly{T} , i:: Int ) where T <: RingElement
128+ e = Vector {Int} (undef, nvars (parent (a)))
129+ return exponent_vector! (e, a, i)
130+ end
131+
132+ function exponent_vector! (e:: Vector{Int} , a:: MPoly{T} , i:: Int ) where T <: RingElement
133+ @assert length (e) == nvars (parent (a))
128134 A = a. exps
129135 N = size (A, 1 )
130136
131137 ord = internal_ordering (parent (a))
132138 if ord == :lex
133- return [ Int (A[j, i]) for j in N: - 1 : 1 ]
139+ range = N: - 1 : 1
134140 elseif ord == :deglex
135- return [ Int (A[j, i]) for j in N - 1 : - 1 : 1 ]
141+ range = N - 1 : - 1 : 1
136142 elseif ord == :degrevlex
137- return [ Int (A[j, i]) for j in 1 : N - 1 ]
143+ range = 1 : N - 1
138144 else
139145 error (" invalid ordering" )
140146 end
147+ k = 1
148+ for j in range
149+ e[k] = Int (A[j, i])
150+ k += 1
151+ end
152+ return e
141153end
142154
143155@doc raw """
@@ -635,6 +647,11 @@ function coeff(x::MPoly, i::Int)
635647 return x. coeffs[i]
636648end
637649
650+ # Only for compatibility, we can't do anything in place here
651+ function coeff! (c:: T , x:: MPoly{T} , i:: Int ) where T <: RingElement
652+ return x. coeffs[i]
653+ end
654+
638655function trailing_coefficient (p:: MPoly{T} ) where T <: RingElement
639656 @req ! iszero (p) " Zero polynomial does not have a leading monomial"
640657 return coeff (p, length (p))
@@ -664,7 +681,9 @@ function monomial!(m::MPoly{T}, x::MPoly{T}, i::Int) where T <: RingElement
664681 N = size (x. exps, 1 )
665682 fit! (m, 1 )
666683 monomial_set! (m. exps, 1 , x. exps, i, N)
667- m. coeffs[1 ] = one (base_ring (x))
684+ if ! isassigned (m. coeffs, 1 ) || ! is_one (m. coeffs[1 ])
685+ m. coeffs[1 ] = one (base_ring (x))
686+ end
668687 m. length = 1
669688 return m
670689end
@@ -675,11 +694,17 @@ end
675694Return the $i$-th nonzero term of the polynomial $x$ (as a polynomial).
676695"""
677696function term (x:: MPoly , i:: Int )
678- R = base_ring (x)
697+ y = zero (parent (x))
698+ return term! (y, x, i)
699+ end
700+
701+ function term! (y:: T , x:: T , i:: Int ) where T <: MPoly
679702 N = size (x. exps, 1 )
680- exps = Matrix {UInt} (undef, N, 1 )
681- monomial_set! (exps, 1 , x. exps, i, N)
682- return parent (x)([deepcopy (x. coeffs[i])], exps)
703+ fit! (y, 1 )
704+ monomial_set! (y. exps, 1 , x. exps, i, N)
705+ y. coeffs[1 ] = deepcopy (x. coeffs[i])
706+ y. length = 1
707+ return y
683708end
684709
685710@doc raw """
@@ -804,69 +829,41 @@ Base.copy(f::Generic.MPoly) = deepcopy(f)
804829#
805830# ##############################################################################
806831
807- function Base. iterate (x:: MPolyCoeffs )
808- if length (x. poly) >= 1
809- return coeff (x. poly, 1 ), 1
810- else
811- return nothing
812- end
813- end
814-
815- function Base. iterate (x:: MPolyCoeffs , state)
816- state += 1
817- if length (x. poly) >= state
818- return coeff (x. poly, state), state
819- else
820- return nothing
821- end
822- end
823-
824- function Base. iterate (x:: MPolyExponentVectors )
825- if length (x. poly) >= 1
826- return exponent_vector (x. poly, 1 ), 1
827- else
828- return nothing
829- end
830- end
831-
832- function Base. iterate (x:: MPolyExponentVectors , state)
833- state += 1
834- if length (x. poly) >= state
835- return exponent_vector (x. poly, state), state
836- else
837- return nothing
838- end
839- end
840-
841- function Base. iterate (x:: MPolyTerms )
842- if length (x. poly) >= 1
843- return term (x. poly, 1 ), 1
832+ function Base. iterate (x:: MPolyCoeffs , state:: Union{Nothing, Int} = nothing )
833+ s = isnothing (state) ? 1 : state + 1
834+ if length (x. poly) >= s
835+ c = x. inplace ? coeff! (x. temp, x. poly, s) : coeff (x. poly, s)
836+ return c, s
844837 else
845838 return nothing
846839 end
847840end
848841
849- function Base. iterate (x:: MPolyTerms , state)
850- state += 1
851- if length (x. poly) >= state
852- return term (x. poly, state), state
842+ function Base. iterate (x:: MPolyExponentVectors , state:: Union{Nothing, Int} = nothing )
843+ s = isnothing (state) ? 1 : state + 1
844+ if length (x. poly) >= s
845+ v = x. inplace ? exponent_vector! (x. temp, x. poly, s) : exponent_vector (x. poly, s)
846+ return v, s
853847 else
854848 return nothing
855849 end
856850end
857851
858- function Base. iterate (x:: MPolyMonomials )
859- if length (x. poly) >= 1
860- return monomial (x. poly, 1 ), 1
852+ function Base. iterate (x:: MPolyTerms , state:: Union{Nothing, Int} = nothing )
853+ s = isnothing (state) ? 1 : state + 1
854+ if length (x. poly) >= s
855+ t = x. inplace ? term! (x. temp, x. poly, s) : term (x. poly, s)
856+ return t, s
861857 else
862858 return nothing
863859 end
864860end
865861
866- function Base. iterate (x:: MPolyMonomials , state)
867- state += 1
868- if length (x. poly) >= state
869- return monomial (x. poly, state), state
862+ function Base. iterate (x:: MPolyMonomials , state:: Union{Nothing, Int} = nothing )
863+ s = isnothing (state) ? 1 : state + 1
864+ if length (x. poly) >= s
865+ m = x. inplace ? monomial! (x. temp, x. poly, s) : monomial (x. poly, s)
866+ return m, s
870867 else
871868 return nothing
872869 end
0 commit comments