@@ -208,8 +208,8 @@ Base.isapprox(α, p::APL; kwargs...) = isapprox(promote(p, α)...; kwargs...)
208208# through the MA API without modifying `p`. We should either copy the monomial
209209# here or implement a `MA.operate(-, p)` that copies it. We choose the first
210210# option.
211- Base.:- (m:: AbstractMonomialLike ) = (- 1 ) * MA. copy_if_mutable (m)
212- Base.:- (t:: AbstractTermLike ) = MA. operate (- , coefficient (t)) * MA . copy_if_mutable ( monomial (t))
211+ Base.:- (m:: AbstractMonomialLike ) = _term (- 1 , MA. copy_if_mutable (m) )
212+ Base.:- (t:: AbstractTermLike ) = _term ( MA. operate (- , coefficient (t)), monomial (t))
213213Base.:- (p:: APL ) = polynomial! ((- ). (terms (p)))
214214Base.:+ (p:: Union{APL, RationalPoly} ) = p
215215Base.:* (p:: Union{APL, RationalPoly} ) = p
@@ -271,9 +271,9 @@ MA.mutable_operate!(::typeof(*), m1::AbstractMonomial, m2::AbstractMonomialLike)
271271Base.:* (m1:: AbstractMonomialLike , m2:: AbstractMonomialLike ) = mapexponents (+ , m1, m2)
272272# Base.:*(m1::AbstractMonomialLike, m2::AbstractMonomialLike) = *(monomial(m1), monomial(m2))
273273
274- Base.:* (m:: AbstractMonomialLike , t:: AbstractTermLike ) = coefficient (t) * ( m * monomial (t))
275- Base.:* (t:: AbstractTermLike , m:: AbstractMonomialLike ) = coefficient (t) * ( monomial (t) * m)
276- Base.:* (t1:: AbstractTermLike , t2:: AbstractTermLike ) = (coefficient (t1) * coefficient (t2)) * ( monomial (t1) * monomial (t2))
274+ Base.:* (m:: AbstractMonomialLike , t:: AbstractTermLike ) = term ( coefficient (t), m * monomial (t))
275+ Base.:* (t:: AbstractTermLike , m:: AbstractMonomialLike ) = term ( coefficient (t), monomial (t) * m)
276+ Base.:* (t1:: AbstractTermLike , t2:: AbstractTermLike ) = term (coefficient (t1) * coefficient (t2), monomial (t1) * monomial (t2))
277277
278278Base.:* (t:: AbstractTermLike , p:: APL ) = polynomial! (map (te -> t * te, terms (p)))
279279Base.:* (p:: APL , t:: AbstractTermLike ) = polynomial! (map (te -> te * t, terms (p)))
@@ -290,6 +290,9 @@ function _polynomial_2terms(t1::TT, t2::TT, ::Type{T}) where {TT<:AbstractTerm,
290290 polynomial (termtype (TT, T)[t1, t2], SortedUniqState ())
291291 end
292292end
293+
294+ _term (α, mono) = term (α, MA. copy_if_mutable (mono))
295+
293296for op in [:+ , :- ]
294297 @eval begin
295298 Base.$ op (t1:: AbstractTermLike , t2:: AbstractTermLike ) = $ op (term (t1), term (t2))
@@ -299,7 +302,7 @@ for op in [:+, :-]
299302 # t1 > t2 would compare the coefficient in case the monomials are equal
300303 # and it will throw a MethodError in case the coefficients are not comparable
301304 if monomial (t1) == monomial (t2)
302- polynomial ($ op (coefficient (t1), coefficient (t2)) * monomial (t1), S)
305+ polynomial (_term ( $ op (coefficient (t1), coefficient (t2)), monomial (t1) ), S)
303306 elseif monomial (t1) > monomial (t2)
304307 ts = _polynomial_2terms (t1, $ op (t2), S)
305308 else
@@ -323,13 +326,13 @@ end
323326
324327LinearAlgebra. adjoint (v:: AbstractVariable ) = v
325328LinearAlgebra. adjoint (m:: AbstractMonomial ) = m
326- LinearAlgebra. adjoint (t:: AbstractTerm ) = LinearAlgebra. adjoint (coefficient (t)) * monomial (t)
329+ LinearAlgebra. adjoint (t:: AbstractTerm ) = _term ( LinearAlgebra. adjoint (coefficient (t)), monomial (t) )
327330LinearAlgebra. adjoint (p:: AbstractPolynomialLike ) = polynomial (map (LinearAlgebra. adjoint, terms (p)))
328331LinearAlgebra. adjoint (r:: RationalPoly ) = adjoint (numerator (r)) / adjoint (denominator (r))
329332
330333LinearAlgebra. transpose (v:: AbstractVariable ) = v
331334LinearAlgebra. transpose (m:: AbstractMonomial ) = m
332- LinearAlgebra. transpose (t:: AbstractTerm ) = LinearAlgebra. transpose (coefficient (t)) * monomial (t)
335+ LinearAlgebra. transpose (t:: AbstractTerm ) = _term ( LinearAlgebra. transpose (coefficient (t)), monomial (t) )
333336LinearAlgebra. transpose (p:: AbstractPolynomialLike ) = polynomial (map (LinearAlgebra. transpose, terms (p)))
334337LinearAlgebra. transpose (r:: RationalPoly ) = transpose (numerator (r)) / transpose (denominator (r))
335338
0 commit comments