@@ -32,33 +32,37 @@ Base.rem(f::APL, g::Union{APL, AbstractVector{<:APL}}; kwargs...) = divrem(f, g;
3232
3333proddiff (x, y) = x/ y - x/ y
3434function Base. divrem (f:: APL{T} , g:: APL{S} ; kwargs... ) where {T, S}
35- rf = convert (polynomialtype (f, Base. promote_op (proddiff, T, S)), f)
36- q = r = zero (rf)
35+ # `promote_type(typeof(f), typeof(g))` is needed for TypedPolynomials in case they use different variables
36+ rf = convert (polynomialtype (promote_type (typeof (f), typeof (g)), Base. promote_op (proddiff, T, S)), MA. copy_if_mutable (f))
37+ q = zero (rf)
38+ r = zero (rf)
3739 lt = leadingterm (g)
3840 rg = removeleadingterm (g)
3941 lm = monomial (lt)
4042 while ! iszero (rf)
4143 ltf = leadingterm (rf)
4244 if isapproxzero (ltf; kwargs... )
43- rf = removeleadingterm ( rf)
45+ rf = MA . operate! (removeleadingterm, rf)
4446 elseif divides (lm, ltf)
4547 qt = _div (ltf, lt)
46- q += qt
47- rf = removeleadingterm (rf) - qt * rg
48+ q = MA. add! (q, qt)
49+ rf = MA. operate! (removeleadingterm, rf)
50+ rf = MA. operate! (MA. sub_mul, rf, qt, rg)
4851 elseif lm > monomial (ltf)
4952 # Since the monomials are sorted in decreasing order,
5053 # lm is larger than all of them hence it cannot divide any of them
51- r += rf
54+ r = MA . add! (r, rf)
5255 break
5356 else
54- r += ltf
55- rf = removeleadingterm ( rf)
57+ r = MA . add! (r, ltf)
58+ rf = MA . operate! (removeleadingterm, rf)
5659 end
5760 end
5861 q, r
5962end
6063function Base. divrem (f:: APL{T} , g:: AbstractVector{<:APL{S}} ; kwargs... ) where {T, S}
61- rf = convert (polynomialtype (f, Base. promote_op (proddiff, T, S)), f)
64+ # `promote_type(typeof(f), eltype(g))` is needed for TypedPolynomials in case they use different variables
65+ rf = convert (polynomialtype (promote_type (typeof (f), eltype (g)), Base. promote_op (proddiff, T, S)), MA. copy_if_mutable (f))
6266 r = zero (rf)
6367 q = similar (g, typeof (rf))
6468 for i in eachindex (q)
@@ -71,15 +75,16 @@ function Base.divrem(f::APL{T}, g::AbstractVector{<:APL{S}}; kwargs...) where {T
7175 while ! iszero (rf)
7276 ltf = leadingterm (rf)
7377 if isapproxzero (ltf; kwargs... )
74- rf = removeleadingterm ( rf)
78+ rf = MA . operate! (removeleadingterm, rf)
7579 continue
7680 end
7781 divisionoccured = false
7882 for i in useful
7983 if divides (lm[i], ltf)
8084 qt = _div (ltf, lt[i])
81- q[i] += qt
82- rf = removeleadingterm (rf) - qt * rg[i]
85+ q[i] = MA. add! (q[i], qt)
86+ rf = MA. operate! (removeleadingterm, rf)
87+ rf = MA. operate! (MA. sub_mul, rf, qt, rg[i])
8388 divisionoccured = true
8489 break
8590 elseif lm[i] > monomial (ltf)
@@ -90,11 +95,11 @@ function Base.divrem(f::APL{T}, g::AbstractVector{<:APL{S}}; kwargs...) where {T
9095 end
9196 if ! divisionoccured
9297 if isempty (useful)
93- r += rf
98+ r = MA . add! (r, rf)
9499 break
95100 else
96- r += ltf
97- rf = removeleadingterm ( rf)
101+ r = MA . add! (r, ltf)
102+ rf = MA . operate! (removeleadingterm, rf)
98103 end
99104 end
100105 end
0 commit comments