@@ -32,33 +32,37 @@ Base.rem(f::APL, g::Union{APL, AbstractVector{<:APL}}; kwargs...) = divrem(f, g;
32
32
33
33
proddiff (x, y) = x/ y - x/ y
34
34
function 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)
37
39
lt = leadingterm (g)
38
40
rg = removeleadingterm (g)
39
41
lm = monomial (lt)
40
42
while ! iszero (rf)
41
43
ltf = leadingterm (rf)
42
44
if isapproxzero (ltf; kwargs... )
43
- rf = removeleadingterm ( rf)
45
+ rf = MA . operate! (removeleadingterm, rf)
44
46
elseif divides (lm, ltf)
45
47
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)
48
51
elseif lm > monomial (ltf)
49
52
# Since the monomials are sorted in decreasing order,
50
53
# lm is larger than all of them hence it cannot divide any of them
51
- r += rf
54
+ r = MA . add! (r, rf)
52
55
break
53
56
else
54
- r += ltf
55
- rf = removeleadingterm ( rf)
57
+ r = MA . add! (r, ltf)
58
+ rf = MA . operate! (removeleadingterm, rf)
56
59
end
57
60
end
58
61
q, r
59
62
end
60
63
function 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))
62
66
r = zero (rf)
63
67
q = similar (g, typeof (rf))
64
68
for i in eachindex (q)
@@ -71,15 +75,16 @@ function Base.divrem(f::APL{T}, g::AbstractVector{<:APL{S}}; kwargs...) where {T
71
75
while ! iszero (rf)
72
76
ltf = leadingterm (rf)
73
77
if isapproxzero (ltf; kwargs... )
74
- rf = removeleadingterm ( rf)
78
+ rf = MA . operate! (removeleadingterm, rf)
75
79
continue
76
80
end
77
81
divisionoccured = false
78
82
for i in useful
79
83
if divides (lm[i], ltf)
80
84
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])
83
88
divisionoccured = true
84
89
break
85
90
elseif lm[i] > monomial (ltf)
@@ -90,11 +95,11 @@ function Base.divrem(f::APL{T}, g::AbstractVector{<:APL{S}}; kwargs...) where {T
90
95
end
91
96
if ! divisionoccured
92
97
if isempty (useful)
93
- r += rf
98
+ r = MA . add! (r, rf)
94
99
break
95
100
else
96
- r += ltf
97
- rf = removeleadingterm ( rf)
101
+ r = MA . add! (r, ltf)
102
+ rf = MA . operate! (removeleadingterm, rf)
98
103
end
99
104
end
100
105
end
0 commit comments