Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep variables for constant zero Polynomials #81

Merged
merged 5 commits into from
Apr 9, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/mult.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ include("cmult.jl")
include("ncmult.jl")

MP.multconstant(α, x::Monomial) = MP.term(α, MA.mutable_copy(x))

function zero_with_variables( ::Type{Polynomial{C,T}}, vars :: Vector{PolyVar{C}} ) where{C, T}
Polynomial( T[], emptymonovec(vars) )
blegat marked this conversation as resolved.
Show resolved Hide resolved
end

function MP._multconstant(α::T, f, p::Polynomial{C,S} ) where {T, C, S}
if iszero(α)
zero_with_variables(polynomialtype(p, MA.promote_operation(*, T, S)), variables(p))
else
MP.mapcoefficientsnz(f, p)
end
end

MP.mapcoefficientsnz(f::Function, p::Polynomial) = Polynomial(map(f, p.a), MA.mutable_copy(p.x))
function MP.mapcoefficientsnz_to!(output::Polynomial, f::Function, t::MP.AbstractTermLike)
MP.mapcoefficientsnz_to!(output, f, polynomial(t))
Expand Down Expand Up @@ -60,7 +73,7 @@ end
function _term_poly_mult(t::Term{C, S}, p::Polynomial{C, T}, op::Function) where {C, S, T}
U = MA.promote_operation(op, S, T)
if iszero(t)
zero(Polynomial{C, U})
zero( Polynomial{C,U} )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove spaces before and after parenthesis to stay consistent with the rest of the code base

else
n = nterms(p)
allvars, maps = mergevars([t.x.vars, p.x.vars])
Expand Down Expand Up @@ -138,3 +151,20 @@ end
function MA.mutable_operate!(::typeof(*), p::Polynomial{C}, q::Polynomial{C}) where C
return MA.mutable_operate_to!(p, *, p, q)
end

# Overwrite this method for monomial-like terms because
# otherwise it would check `iszero(α)` and in that case
# dismiss of the variable of `p` by performing
# `operate_to!(zero, output :: Polynomial )` which only
# respects the variables that are stored already
function MP._multconstant_to!(output::Polynomial, α, f, p :: DMonomialLike)
if iszero(α)
empty!(output.a)
blegat marked this conversation as resolved.
Show resolved Hide resolved
empty!(output.x.vars)
push!(output.x.vars, variables(p)...)
blegat marked this conversation as resolved.
Show resolved Hide resolved
empty!(output.x.Z)
return output
else
MP.mapcoefficientsnz_to!(output, f, p)
end
end