Skip to content

Commit 22b468e

Browse files
authored
Remove allocations of isapproxzero (#224)
1 parent 1ce3191 commit 22b468e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/comparison.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ end
119119

120120
isapproxzero(m::AbstractMonomialLike; kwargs...) = false
121121
isapproxzero(t::AbstractTermLike; kwargs...) = isapproxzero(coefficient(t); kwargs...)
122-
isapproxzero(p::APL; kwargs...) = all(isapproxzero.(terms(p); kwargs...))
122+
isapproxzero(p::APL; kwargs...) = all(term -> isapproxzero(term; kwargs...), terms(p))
123123
isapproxzero(p::RationalPoly; kwargs...) = isapproxzero(p.num; kwargs...)
124124

125125
Base.isapprox(t1::AbstractTerm, t2::AbstractTerm; kwargs...) = isapprox(coefficient(t1), coefficient(t2); kwargs...) && monomial(t1) == monomial(t2)

test/allocations.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ function runtests()
1313
end
1414

1515
using MutableArithmetics
16+
import MultivariatePolynomials
17+
const MP = MultivariatePolynomials
1618
using TypedPolynomials
1719

1820
function test_polynomial_merge()
@@ -28,5 +30,34 @@ function test_polynomial_merge()
2830
end
2931
end
3032

33+
function test_isapproxzero()
34+
@polyvar x y
35+
p = x * y + x + y + 1
36+
alloc_test(0) do
37+
isapproxzero(p)
38+
end
39+
q = 1e-10 * x * y + 1e-12 * x
40+
alloc_test(0) do
41+
isapproxzero(q)
42+
end
43+
alloc_test(0) do
44+
isapproxzero(q; ztol = 1e-8)
45+
end
46+
end
47+
48+
function _test_gcd(T)
49+
o = one(T)
50+
@polyvar x y
51+
t1 = o * x * y
52+
t2 = o * x
53+
alloc_test(0) do
54+
gcd(t1, t2)
55+
end
56+
end
57+
58+
function test_gcd()
59+
_test_gcd(Int)
60+
end
61+
3162
end
3263
TestAllocations.runtests()

0 commit comments

Comments
 (0)