Skip to content

Commit 5221953

Browse files
authored
Merge pull request #241 from jverzani/issue_240
add tolerance to gcd; adjust chop; close issue #240
2 parents 4d5649e + f68d93a commit 5221953

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "Polynomials"
33
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
44
license = "MIT"
55
author = "JuliaMath"
6-
version = "1.1.2"
6+
version = "1.1.3"
77

88
[deps]
99
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"

src/common.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,10 @@ function chop!(p::AbstractPolynomial{T};
209209
rtol::Real = Base.rtoldefault(real(T)),
210210
atol::Real = 0,) where {T}
211211
isempty(coeffs(p)) && return p
212+
tol = norm(coeffs(p)) * rtol + atol
212213
for i = lastindex(p):-1:0
213214
val = p[i]
214-
if !isapprox(val, zero(T); rtol = rtol, atol = atol)
215+
if abs(val) > tol #!isapprox(val, zero(T); rtol = rtol, atol = atol)
215216
resize!(p.coeffs, i + 1);
216217
return p
217218
end
@@ -520,7 +521,7 @@ function Base.divrem(num::P, den::O) where {P <: AbstractPolynomial,O <: Abstrac
520521
end
521522

522523
"""
523-
gcd(a::AbstractPolynomial, b::AbstractPolynomial)
524+
gcd(a::AbstractPolynomial, b::AbstractPolynomial; atol::Real=0, rtol::Real=Base.rtoldefault)
524525
525526
Find the greatest common denominator of two polynomials recursively using
526527
[Euclid's algorithm](http://en.wikipedia.org/wiki/Polynomial_greatest_common_divisor#Euclid.27s_algorithm).
@@ -535,15 +536,18 @@ Polynomial(4.0 - 6.0*x + 2.0*x^2)
535536
536537
```
537538
"""
538-
function Base.gcd(p1::AbstractPolynomial{T}, p2::AbstractPolynomial{S}) where {T,S}
539+
function Base.gcd(p1::AbstractPolynomial{T}, p2::AbstractPolynomial{S};
540+
atol::Real=zero(real(promote_type(T,S))),
541+
rtol::Real=Base.rtoldefault(real(promote_type(T,S)))
542+
) where {T,S}
539543
r₀, r₁ = promote(p1, p2)
540544
iter = 1
541545
itermax = length(r₁)
542546

543547
while !iszero(r₁) && iter itermax
544548
_, rtemp = divrem(r₀, r₁)
545549
r₀ = r₁
546-
r₁ = truncate(rtemp)
550+
r₁ = truncate(rtemp; atol=atol, rtol=rtol)
547551
iter += 1
548552
end
549553
return r₀

test/StandardBasis.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,13 @@ end
673673
@test degree(gcd(p1, P(eps(0.)))) == 0 # ditto
674674
@test degree(gcd(p1, P(0))) == degree(p1) # P(0) has the roots of p1
675675
@test degree(gcd(p1 + p2 * 170.10734737144486, p2)) == 0 # see, c.f., #122
676+
677+
## Issue #240; add tolerance to gcd
678+
a = P([0.8457170323029561, 0.47175077674705257, 0.9775441940117577]);
679+
b = P([0.5410010714904849, 0.533604905984294]);
680+
d = P([0.5490673726445683, 0.15991109487875477]);
681+
@test Polynomials.isconstant(gcd(a*d,b*d))
682+
@test !Polynomials.isconstant(gcd(a*d, b*d, atol=sqrt(eps())))
676683

677684
p1 = fromroots(P, [1.,2.,3.])
678685
p2 = fromroots(P, [1.,2.,6.])

0 commit comments

Comments
 (0)