@@ -209,9 +209,10 @@ function chop!(p::AbstractPolynomial{T};
209
209
rtol:: Real = Base. rtoldefault (real (T)),
210
210
atol:: Real = 0 ,) where {T}
211
211
isempty (coeffs (p)) && return p
212
+ tol = norm (coeffs (p)) * rtol + atol
212
213
for i = lastindex (p): - 1 : 0
213
214
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)
215
216
resize! (p. coeffs, i + 1 );
216
217
return p
217
218
end
@@ -520,7 +521,7 @@ function Base.divrem(num::P, den::O) where {P <: AbstractPolynomial,O <: Abstrac
520
521
end
521
522
522
523
"""
523
- gcd(a::AbstractPolynomial, b::AbstractPolynomial)
524
+ gcd(a::AbstractPolynomial, b::AbstractPolynomial; atol::Real=0, rtol::Real=Base.rtoldefault )
524
525
525
526
Find the greatest common denominator of two polynomials recursively using
526
527
[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)
535
536
536
537
```
537
538
"""
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}
539
543
r₀, r₁ = promote (p1, p2)
540
544
iter = 1
541
545
itermax = length (r₁)
542
546
543
547
while ! iszero (r₁) && iter ≤ itermax
544
548
_, rtemp = divrem (r₀, r₁)
545
549
r₀ = r₁
546
- r₁ = truncate (rtemp)
550
+ r₁ = truncate (rtemp; atol = atol, rtol = rtol )
547
551
iter += 1
548
552
end
549
553
return r₀
0 commit comments