@@ -92,6 +92,47 @@ function MA.operate_to!(
9292 return output
9393end
9494
95+ """
96+ _constant_term_idx(p::Polynomial)
97+
98+ Return the index of the constant term in `p` according to its monomial ordering.
99+ """
100+ _constant_term_idx (p:: Polynomial{V, M, T} ) where {V, M <: Reverse , T} = lastindex (p. x)
101+ _constant_term_idx (p:: Polynomial ) = firstindex (p. x)
102+
103+ """
104+ _insert_constant_term!(p::Polynomial)
105+
106+ Insert a constant (degree 0) term into polynomial `p` at the appropriate position for the
107+ monomial ordering of `p`. Does not check if a constant term already exists.
108+ """
109+ function _insert_constant_term! (p:: Polynomial{V, M, T} ) where {V, M <: Reverse , T}
110+ push! (MP. coefficients (p), zero (T))
111+ push! (MP. monomials (p). Z, zeros (Int, length (MP. variables (p))))
112+ return p
113+ end
114+
115+ function _insert_constant_term! (p:: Polynomial{V, M, T} ) where {V, M, T}
116+ insert! (MP. coefficients (p), 1 , zero (T))
117+ insert! (MP. monomials (p). Z, 1 , zeros (Int, length (MP. variables (p))))
118+ return p
119+ end
120+
121+ function MA. operate! (op:: Union{typeof(+), typeof(-)} , p:: Polynomial{V, M, T} , x:: T ) where {V, M, T}
122+ c_idx = _constant_term_idx (p)
123+ if MP. nterms (p) == 0 || ! MP. isconstant (MP. terms (p)[c_idx])
124+ _insert_constant_term! (p)
125+ c_idx = _constant_term_idx (p)
126+ end
127+ coeffs = MP. coefficients (p)
128+ coeffs[c_idx] = op (coeffs[c_idx], x)
129+ if iszero (coeffs[c_idx])
130+ deleteat! (coeffs, c_idx)
131+ deleteat! (MP. monomials (p), c_idx)
132+ end
133+ return p
134+ end
135+
95136function MA. operate! (
96137 op:: Union{typeof(+),typeof(-)} ,
97138 p:: Polynomial ,
0 commit comments