Skip to content

Commit 25fa684

Browse files
authored
Changes need for mutable pseudo_rem (#122)
* Changes need for mutable pseudo_rem * Fix * Back to master branch
1 parent 5dc9654 commit 25fa684

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

src/cmult.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ function _operate_exponents_to!(output::Vector{Int}, op::F, z1::Vector{Int}, z2:
2828
@. output = op(z1, z2)
2929
return
3030
end
31+
function _operate_exponents_to!(output::Vector{Vector{Int}}, op::F, z1::Vector{Vector{Int}}, z2::Vector{Int}) where {F<:Function}
32+
for i in eachindex(output)
33+
_operate_exponents_to!(output[i], op, z1[i], z2)
34+
end
35+
return
36+
end
3137
function _operate_exponents_to!(output::Vector{Int}, op::F, z1::Vector{Int}, z2::Vector{Int}, maps) where {F<:Function}
3238
I = maps[1]; i = 1; lI = length(I)
3339
J = maps[2]; j = 1; lJ = length(J)
@@ -133,5 +139,9 @@ function Base.:(*)(x::Monomial{true}, y::MonomialVector{true})
133139
w, Z = multdivmono(y.vars, x, +, y.Z)
134140
return MonomialVector{true}(w, Z)
135141
end
142+
function MA.operate!(::typeof(*), x::MonomialVector{true}, y::Monomial{true})
143+
_multdivmono!(x.Z, x.vars, copy(x.vars), y, +, copy(x.Z))
144+
return x
145+
end
136146
Base.:(*)(y::MonomialVector{true}, x::Monomial{true}) = x * y
137147
Base.:(*)(x::Monomial{true}, y::PolyVar{true}) = y * x

src/mult.jl

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,12 @@ end
3535
include("cmult.jl")
3636
include("ncmult.jl")
3737

38-
MP.multconstant(α, x::Monomial) = MP.term(α, MA.mutable_copy(x))
38+
MP.left_constant_mult(α, x::Monomial) = MP.term(α, MA.mutable_copy(x))
3939

4040
function zero_with_variables(::Type{Polynomial{C,T}}, vars::Vector{PolyVar{C}}) where{C, T}
4141
Polynomial(T[], emptymonovec(vars))
4242
end
4343

44-
function MP._multconstant::T, f, p::Polynomial{C,S} ) where {T, C, S}
45-
if iszero(α)
46-
zero_with_variables(polynomialtype(p, MA.promote_operation(*, T, S)), variables(p))
47-
else
48-
MP.mapcoefficientsnz(f, p)
49-
end
50-
end
51-
5244
# I do not want to cast x to TermContainer because that would force the promotion of eltype(q) with Int
5345
function Base.:(*)(x::DMonomialLike, p::Polynomial)
5446
Polynomial(MA.mutable_copy(p.a), x*p.x)
@@ -57,10 +49,17 @@ function Base.:(*)(x::DMonomialLike{false}, p::Polynomial)
5749
# Order may change, e.g. y * (x + y) = y^2 + yx
5850
Polynomial(monovec(MA.mutable_copy(p.a), [x*m for m in p.x])...)
5951
end
52+
6053
function Base.:(*)(p::Polynomial, x::DMonomialLike)
6154
Polynomial(MA.mutable_copy(p.a), p.x*x)
6255
end
6356

57+
function MA.operate!(::typeof(*), p::Polynomial, t::DMonomialLike)
58+
MA.operate!(*, p.x, monomial(t))
59+
return p
60+
end
61+
62+
6463
function _term_poly_mult(t::Term{C, S}, p::Polynomial{C, T}, op::Function) where {C, S, T}
6564
U = MA.promote_operation(op, S, T)
6665
if iszero(t)
@@ -143,18 +142,3 @@ end
143142
function MA.operate!(::typeof(*), p::Polynomial{C}, q::Polynomial{C}) where C
144143
return MA.operate_to!(p, *, p, q)
145144
end
146-
147-
# Overwrite this method for monomial-like terms because
148-
# otherwise it would check `iszero(α)` and in that case
149-
# dismiss of the variable of `p` by performing
150-
# `operate_to!(zero, output :: Polynomial )` which only
151-
# respects the variables that are stored already
152-
function MP._multconstant_to!(output::Polynomial, α, f, p :: DMonomialLike)
153-
if iszero(α)
154-
MA.operate!(zero, output)
155-
Future.copy!(output.x.vars, variables(p))
156-
return output
157-
else
158-
MP.mapcoefficientsnz_to!(output, f, p)
159-
end
160-
end

src/operators.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ function plusorminus(p::TermPoly{C, S}, q::TermPoly{C, T}, op::Function) where {
5353
Polynomial(a, MonomialVector{C}(allvars, Z))
5454
end
5555

56+
function MA.operate!(::typeof(*), p::Polynomial, t::Term)
57+
# In case `coefficient(t)` is a polynomial (e.g. in `gcd` algorithm)
58+
# We cannot do `MA.operate!(*, ...)`, we need `MP.right_constant_mult`
59+
MA.operate!(MP.right_constant_mult, p, coefficient(t))
60+
MA.operate!(*, p, monomial(t))
61+
return p
62+
end
63+
5664
function MA.operate_to!(output::Polynomial{C}, op::Union{typeof(+), typeof(-)},
5765
p::TermPoly{C}, q::TermPoly{C}) where C
5866
if output === p || output === q

src/poly.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ function MA.operate!(::typeof(one), p::Polynomial{C, T}) where {C, T}
270270
push!(p.x.Z, zeros(Int, length(p.x.vars)))
271271
else
272272
resize!(p.a, 1)
273-
MA.operate!(one, p.a[1])
273+
p.a[1] = MA.operate!!(one, p.a[1])
274274
resize!(p.x.Z, 1)
275275
MA.operate!(zero, p.x.Z[1])
276276
end

0 commit comments

Comments
 (0)