Skip to content

Commit 19019d6

Browse files
authored
mapexponents for polynomial (#124)
* mapexponents for polynomial * Simplify * Fixes
1 parent 25fa684 commit 19019d6

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

src/cmult.jl

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,36 @@ function _operate_exponents_to!(output::Vector{Int}, op::F, z1::Vector{Int}, z2:
5353
end
5454
return
5555
end
56+
function _operate_exponents_to!(output::Vector{Vector{Int}}, op::F, z1::Vector{Vector{Int}}, z2::Vector{Int}, maps) where {F<:Function}
57+
for i in eachindex(output)
58+
_operate_exponents_to!(output[i], op, z1[i], z2, maps)
59+
end
60+
return
61+
end
5662
# Not used yet
5763
#function _operate_exponents!(op::F, Z::Vector{Vector{Int}}, z2::Vector{Int}, args::Vararg{Any,N}) where {F<:Function,N}
5864
# return Vector{Int}[_operate_exponents!(op, z, z2, args...) for z in Z]
5965
#end
66+
function _resize!(output::Vector, n)
67+
resize!(output, n)
68+
end
69+
function _resize!(output::Vector{<:Vector}, n)
70+
for out in output
71+
_resize!(out, n)
72+
end
73+
end
6074
function _multdivmono!(output, output_variables::Vector{PolyVar{true}},
6175
v::Vector{PolyVar{true}}, x::Monomial{true}, op, z)
6276
if v == x.vars
6377
if output_variables != v
6478
resize!(output_variables, length(v))
6579
copyto!(output_variables, v)
66-
resize!(output, length(output_variables))
80+
_resize!(output, length(output_variables))
6781
end
6882
_operate_exponents_to!(output, op, z, x.z)
6983
else
7084
maps = mergevars_to!(output_variables, [v, x.vars])
71-
resize!(output, length(output_variables))
85+
_resize!(output, length(output_variables))
7286
_operate_exponents_to!(output, op, z, x.z, maps)
7387
end
7488
return
@@ -135,13 +149,23 @@ function MP.mapexponents(f::Function, x::Monomial{true}, y::Monomial{true})
135149
w, z = multdivmono(x.vars, y, f, x.z)
136150
return Monomial{true}(w, z)
137151
end
138-
function Base.:(*)(x::Monomial{true}, y::MonomialVector{true})
139-
w, Z = multdivmono(y.vars, x, +, y.Z)
152+
function MP.mapexponents(f::Function, x::MonomialVector{true}, y::Monomial{true})
153+
w, Z = multdivmono(x.vars, y, f, x.Z)
140154
return MonomialVector{true}(w, Z)
141155
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))
156+
function MP.mapexponents!(f::Function, x::MonomialVector{true}, y::Monomial{true})
157+
_multdivmono!(x.Z, x.vars, copy(x.vars), y, f, copy.(x.Z))
144158
return x
145159
end
146-
Base.:(*)(y::MonomialVector{true}, x::Monomial{true}) = x * y
160+
function MP.mapexponents(f::Function, x::MonomialVector{true}, y::PolyVar{true})
161+
return MP.mapexponents(f, x, MP.monomial(y))
162+
end
163+
function MP.mapexponents!(f::Function, x::MonomialVector{true}, y::PolyVar{true})
164+
return MP.mapexponents!(f, x, MP.monomial(y))
165+
end
166+
function MA.operate!(::typeof(*), x::MonomialVector{true}, y::DMonomialLike{true})
167+
return MP.mapexponents!(+, x, y)
168+
end
169+
Base.:(*)(y::MonomialVector{true}, x::DMonomialLike{true}) = MP.mapexponents(+, y, x)
170+
Base.:(*)(x::DMonomialLike{true}, y::MonomialVector{true}) = y * x
147171
Base.:(*)(x::Monomial{true}, y::PolyVar{true}) = y * x

src/mult.jl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,6 @@ function Base.:(*)(x::DMonomialLike{false}, p::Polynomial)
5050
Polynomial(monovec(MA.mutable_copy(p.a), [x*m for m in p.x])...)
5151
end
5252

53-
function Base.:(*)(p::Polynomial, x::DMonomialLike)
54-
Polynomial(MA.mutable_copy(p.a), p.x*x)
55-
end
56-
57-
function MA.operate!(::typeof(*), p::Polynomial, t::DMonomialLike)
58-
MA.operate!(*, p.x, monomial(t))
59-
return p
60-
end
61-
62-
6353
function _term_poly_mult(t::Term{C, S}, p::Polynomial{C, T}, op::Function) where {C, S, T}
6454
U = MA.promote_operation(op, S, T)
6555
if iszero(t)
@@ -140,5 +130,11 @@ function MA.operate_to!(p::Polynomial{true, T}, ::typeof(*), q1::MP.AbstractPoly
140130
end
141131
end
142132
function MA.operate!(::typeof(*), p::Polynomial{C}, q::Polynomial{C}) where C
143-
return MA.operate_to!(p, *, p, q)
133+
if iszero(q)
134+
return MA.operate!(zero, p)
135+
elseif nterms(q) == 1
136+
return MA.operate!(*, p, MP.leadingterm(q))
137+
else
138+
return MA.operate_to!(p, *, p, q)
139+
end
144140
end

src/poly.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,12 @@ function MP.mapcoefficients_to!(output::Polynomial, f::Function, p::Polynomial;
306306
end
307307
return output
308308
end
309+
310+
function MP.mapexponents(f::Function, p::Polynomial, m::DMonomialLike)
311+
return Polynomial(MA.mutable_copy(p.a), MP.mapexponents(f, p.x, m))
312+
end
313+
314+
function MP.mapexponents!(f::Function, p::Polynomial, m::DMonomialLike)
315+
MP.mapexponents!(f, p.x, m)
316+
return p
317+
end

0 commit comments

Comments
 (0)