Skip to content

Commit 721a926

Browse files
blegatYingboMa
andauthored
Avoid closure for operate! on +/- (#209)
* Avoid closure for operate! on +/- Co-authored-by: Yingbo Ma <[email protected]> * Fixes * Add allocation tests Co-authored-by: Yingbo Ma <[email protected]>
1 parent c75277d commit 721a926

File tree

3 files changed

+75
-14
lines changed

3 files changed

+75
-14
lines changed

src/default_polynomial.jl

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,53 @@ Base.copy(p::VectorPolynomial) = MA.mutable_copy(p)
151151

152152
function grlex end
153153
function MA.operate!(op::Union{typeof(+), typeof(-)}, p::Polynomial{T,TT}, q::Polynomial) where {T,TT}
154-
get1(i) = p.terms[i]
155-
function get2(i)
156-
t = q.terms[i]
157-
TT(MA.scaling_convert(T, MA.operate(op, coefficient(t))), monomial(t))
158-
end
159-
set(i, t::TT) = p.terms[i] = t
160-
push(t::TT) = push!(p.terms, t)
161-
compare_monomials(t::TT, j::Int) = grlex(monomial(q.terms[j]), monomial(t))
162-
compare_monomials(i::Int, j::Int) = compare_monomials(get1(i), j)
163-
combine(i::Int, j::Int) = p.terms[i] = Term(MA.operate!!(op, coefficient(p.terms[i]), coefficient(q.terms[j])), monomial(p.terms[i]))
164-
combine(t::TT, j::Int) = TT(MA.operate!!(op, coefficient(t), coefficient(q.terms[j])), monomial(t))
165-
resize(n) = resize!(p.terms, n)
154+
get1 = let p=p
155+
i -> p.terms[i]
156+
end
157+
get2 = let p=p
158+
i -> begin
159+
t = q.terms[i]
160+
TT(MA.scaling_convert(T, MA.operate(op, coefficient(t))), monomial(t))
161+
end
162+
end
163+
set = let p=p
164+
(i, t) -> begin
165+
p.terms[i] = t
166+
end
167+
end
168+
push = let p=p
169+
t -> push!(p.terms, t)
170+
end
171+
compare_monomials = let q=q
172+
(t, j) -> begin
173+
if t isa Int && j isa Int
174+
t = get1(t)
175+
end
176+
grlex(monomial(q.terms[j]), monomial(t))
177+
end
178+
end
179+
combine = let p=p, q=q
180+
(i, j) -> begin
181+
if i isa Int
182+
p.terms[i] = Term(MA.operate!!(op, coefficient(p.terms[i]), coefficient(q.terms[j])), monomial(p.terms[i]))
183+
else
184+
typeof(i)(MA.operate!!(op, coefficient(i), coefficient(q.terms[j])), monomial(i))
185+
end
186+
end
187+
end
188+
resize = let p=p
189+
(n) -> resize!(p.terms, n)
190+
end
166191
# We can modify the coefficient since it's the result of `combine`.
167-
keep(t::Term) = !MA.iszero!!(coefficient(t))
168-
keep(i::Int) = !MA.iszero!!(coefficient(p.terms[i]))
192+
keep = let p=p
193+
i -> begin
194+
if i isa Int
195+
!MA.iszero!!(coefficient(p.terms[i]))
196+
else
197+
!MA.iszero!!(coefficient(i))
198+
end
199+
end
200+
end
169201
polynomial_merge!(
170202
nterms(p), nterms(q), get1, get2, set, push,
171203
compare_monomials, combine, keep, resize

test/allocations.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module TestAllocations
2+
3+
include("utils.jl")
4+
5+
function runtests()
6+
for name in names(@__MODULE__; all = true)
7+
if startswith("$name", "test_")
8+
@testset "$(name)" begin
9+
getfield(@__MODULE__, name)()
10+
end
11+
end
12+
end
13+
end
14+
15+
using MutableArithmetics
16+
using TypedPolynomials
17+
18+
function test_polynomial_merge()
19+
@polyvar x
20+
p = x^2 + x + 1
21+
q = 2x + 3
22+
alloc_test(0) do
23+
add!!(p, q)
24+
end
25+
end
26+
27+
end
28+
TestAllocations.runtests()

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ end
2525
if try_import(:TypedPolynomials)
2626
Mod = TypedPolynomials
2727
include("commutativetests.jl")
28+
include("allocations.jl")
2829
end

0 commit comments

Comments
 (0)