@@ -208,8 +208,8 @@ Base.isapprox(α, p::APL; kwargs...) = isapprox(promote(p, α)...; kwargs...)
208
208
# through the MA API without modifying `p`. We should either copy the monomial
209
209
# here or implement a `MA.operate(-, p)` that copies it. We choose the first
210
210
# 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))
213
213
Base.:- (p:: APL ) = polynomial! ((- ). (terms (p)))
214
214
Base.:+ (p:: Union{APL, RationalPoly} ) = p
215
215
Base.:* (p:: Union{APL, RationalPoly} ) = p
@@ -271,9 +271,9 @@ MA.mutable_operate!(::typeof(*), m1::AbstractMonomial, m2::AbstractMonomialLike)
271
271
Base.:* (m1:: AbstractMonomialLike , m2:: AbstractMonomialLike ) = mapexponents (+ , m1, m2)
272
272
# Base.:*(m1::AbstractMonomialLike, m2::AbstractMonomialLike) = *(monomial(m1), monomial(m2))
273
273
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))
277
277
278
278
Base.:* (t:: AbstractTermLike , p:: APL ) = polynomial! (map (te -> t * te, terms (p)))
279
279
Base.:* (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,
290
290
polynomial (termtype (TT, T)[t1, t2], SortedUniqState ())
291
291
end
292
292
end
293
+
294
+ _term (α, mono) = term (α, MA. copy_if_mutable (mono))
295
+
293
296
for op in [:+ , :- ]
294
297
@eval begin
295
298
Base.$ op (t1:: AbstractTermLike , t2:: AbstractTermLike ) = $ op (term (t1), term (t2))
@@ -299,7 +302,7 @@ for op in [:+, :-]
299
302
# t1 > t2 would compare the coefficient in case the monomials are equal
300
303
# and it will throw a MethodError in case the coefficients are not comparable
301
304
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)
303
306
elseif monomial (t1) > monomial (t2)
304
307
ts = _polynomial_2terms (t1, $ op (t2), S)
305
308
else
@@ -323,13 +326,13 @@ end
323
326
324
327
LinearAlgebra. adjoint (v:: AbstractVariable ) = v
325
328
LinearAlgebra. 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) )
327
330
LinearAlgebra. adjoint (p:: AbstractPolynomialLike ) = polynomial (map (LinearAlgebra. adjoint, terms (p)))
328
331
LinearAlgebra. adjoint (r:: RationalPoly ) = adjoint (numerator (r)) / adjoint (denominator (r))
329
332
330
333
LinearAlgebra. transpose (v:: AbstractVariable ) = v
331
334
LinearAlgebra. 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) )
333
336
LinearAlgebra. transpose (p:: AbstractPolynomialLike ) = polynomial (map (LinearAlgebra. transpose, terms (p)))
334
337
LinearAlgebra. transpose (r:: RationalPoly ) = transpose (numerator (r)) / transpose (denominator (r))
335
338
0 commit comments