Skip to content

Commit 6315b50

Browse files
authored
Add a term method with coefficient and monomial (#149)
* Add a term method with coefficient and monomial * Fixes * Update DynamicPolynomials and TypedPolynomials lower bounds
1 parent a58fa07 commit 6315b50

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
1111

1212
[compat]
1313
DataStructures = "0.17.7, 0.18"
14-
DynamicPolynomials = "0.3.12"
14+
DynamicPolynomials = "0.3.16"
1515
MutableArithmetics = "0.2"
16-
TypedPolynomials = "0.2.8"
16+
TypedPolynomials = "0.2.9"
1717
julia = "1"
1818

1919
[extras]

src/operators.jl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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))
213213
Base.:-(p::APL) = polynomial!((-).(terms(p)))
214214
Base.:+(p::Union{APL, RationalPoly}) = p
215215
Base.:*(p::Union{APL, RationalPoly}) = p
@@ -271,9 +271,9 @@ MA.mutable_operate!(::typeof(*), m1::AbstractMonomial, m2::AbstractMonomialLike)
271271
Base.:*(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

278278
Base.:*(t::AbstractTermLike, p::APL) = polynomial!(map(te -> t * te, terms(p)))
279279
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,
290290
polynomial(termtype(TT, T)[t1, t2], SortedUniqState())
291291
end
292292
end
293+
294+
_term(α, mono) = term(α, MA.copy_if_mutable(mono))
295+
293296
for 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

324327
LinearAlgebra.adjoint(v::AbstractVariable) = v
325328
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))
327330
LinearAlgebra.adjoint(p::AbstractPolynomialLike) = polynomial(map(LinearAlgebra.adjoint, terms(p)))
328331
LinearAlgebra.adjoint(r::RationalPoly) = adjoint(numerator(r)) / adjoint(denominator(r))
329332

330333
LinearAlgebra.transpose(v::AbstractVariable) = v
331334
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))
333336
LinearAlgebra.transpose(p::AbstractPolynomialLike) = polynomial(map(LinearAlgebra.transpose, terms(p)))
334337
LinearAlgebra.transpose(r::RationalPoly) = transpose(numerator(r)) / transpose(denominator(r))
335338

src/term.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
export constantterm, term, termtype, zeroterm, coefficient, monomial
22

33
"""
4+
term(coef, mono::AbstractMonomialLike)
5+
6+
Returns a term with coefficient `coef` and monomial `mono`. There are two key
7+
difference between this and `coef * mono`:
8+
9+
* `term(coef, mono)` does not copy `coef` and `mono` so modifying this term
10+
with MutableArithmetics may modifying the input of this function.
11+
To avoid this, call `term(MA.copy_if_mutable(coef), MA.copy_if_mutable(mono))`
12+
where `MA = MutableArithmetics`.
13+
* Suppose that `coef = (x + 1)` and `mono = x^2`, `coef * mono` gives the polynomial
14+
with integer coefficients `x^3 + x^2` which `term(x + 1, x^2)` gives a term
15+
with polynomial coefficient `x + 1`.
16+
417
term(p::AbstractPolynomialLike)
518
619
Converts the polynomial `p` to a term.
720
When applied on a polynomial, it throws an `InexactError` if it has more than one term.
821
When applied to a term, it is the identity and does not copy it.
922
When applied to a monomial, it create a term of type `AbstractTerm{Int}`.
1023
"""
24+
function term end
25+
term(coef, var::AbstractVariable) = term(coef, monomial(var))
1126
term(p::APL) = convert(termtype(p), p)
1227

1328
"""

0 commit comments

Comments
 (0)