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

Implement mutable addition of term #67

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions src/mono.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,10 @@ MP.monomial(m::Monomial) = m
# end
# i > length(m1.z)
#end
function _add_variables!(mono::Monomial, allvars, map)
Future.copy!(mono.vars, allvars)
tmp = copy(mono.z)
resize!(mono.z, length(allvars))
fill!(mono.z, 0)
mono.z[map] = tmp
end
69 changes: 48 additions & 21 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,54 @@ function MA.mutable_operate_to!(output::Polynomial{C}, op::Union{typeof(+), type
return output
end

_copy_monos(m::Monomial) = copy(m)
_copy_monos(t::Term) = Term(t.α, copy(t.x))
_copy_monos(p::Polynomial) = Polynomial(p.a, copy(p.x))
function _merge_vars!(p, q)
if _vars(p) == _vars(q)
return q
end
varsvec = [_vars(p), _vars(q)]
allvars, maps = mergevars(varsvec)
if length(allvars) != length(_vars(p))
_add_variables!(p.x, allvars, maps[1])
end
if length(allvars) != length(_vars(q))
# We could avoid promoting `q` to the same variables
# like in `plusorminus` to avoid extra allocation but it then
# gives slower comparison. There is a tradeoff and the approach used here
# should be better of `q` has less terms and then the same term is compared
# many times.
q = _copy_monos(q)
_add_variables!(q, allvars, maps[2])
end
return q
end
function MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::Polynomial,
q::Union{PolyVar, Monomial, Term})
return MA.mutable_operate!(op, p, polynomial(q))
v::PolyVar)
return MA.mutable_operate!(op, p, monomial(v))
end
function MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::Polynomial,
t::Union{Monomial, Term})
t = _merge_vars!(p, t)
z = MP.exponents(t)
i = findfirst(eachindex(p.a)) do i
return samevars_grlex(p.x.Z[i], z) <= 0
end
if i === nothing
push!(p.a, MA.operate(op, MP.coefficient(t)))
push!(p.x.Z, copy(z))
else
comp = samevars_grlex(p.x.Z[i], z)
if iszero(comp)
p.a[i] = MA.operate!(op, p.a[i], coefficient(t))
else
@assert comp < 0
insert!(p.a, i, MA.operate(op, MP.coefficient(t)))
insert!(p.x.Z, i, copy(z))
end
end
return p
end
const _NoVarTerm{T} = Tuple{T, Vector{Int}}
function MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::Polynomial{false},
Expand All @@ -80,25 +125,7 @@ end
# TODO need to check that this also works for non-commutative
function MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::Polynomial{true},
q::Polynomial{true})
if _vars(p) != _vars(q)
varsvec = [_vars(p), _vars(q)]
allvars, maps = mergevars(varsvec)
if length(allvars) != length(_vars(p))
_add_variables!(p.x, allvars, maps[1])
end
if length(allvars) == length(_vars(q))
rhs = q
else
# We could avoid promoting `q` to the same variables
# like in `plusorminus` to avoid extra allocation but it then
# gives slower comparison. There is a tradeoff and the approach used here
# should be better of `q` has less terms and then the same term is compared
# many times.
rhs = Polynomial(q.a, copy(q.x))
_add_variables!(rhs.x, allvars, maps[2])
end
return MA.mutable_operate!(op, p, rhs)
end
q = _merge_vars!(p, q)
get1(i) = (p.a[i], p.x.Z[i])
get2(i) = (MA.scaling_convert(eltype(p.a), MA.operate(op, q.a[i])), copy(q.x.Z[i]))
function set(i, t::_NoVarTerm)
Expand Down
1 change: 1 addition & 0 deletions src/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,4 @@ function MA.mutable_operate!(::typeof(one), p::Polynomial{C, T}) where {C, T}
end
return p
end
_add_variables!(p::Polynomial, allvars, map) = _add_variables!(p.x, allvars, map)
1 change: 1 addition & 0 deletions src/term.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ function MA.mutable_operate!(::typeof(one), t::Term)
MA.mutable_operate!(zero, t.x.z)
return t
end
_add_variables!(t::Term, allvars, map) = _add_variables!(t.x, allvars, map)