Skip to content

Commit 051d82a

Browse files
adding tests for Pade
1 parent 73047b2 commit 051d82a

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/Polynomials.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ function roots{T}(p::Poly{T})
342342
return r
343343
end
344344

345-
function gcd{T<:FloatingPoint, S<:FloatingPoint}(a::Poly{T}, b::Poly{S})
345+
function gcd{T, S}(a::Poly{T}, b::Poly{S})
346346
#Finds the Greatest Common Denominator of two polynomials recursively using
347347
#Euclid's algorithm: http://en.wikipedia.org/wiki/Polynomial_greatest_common_divisor#Euclid.27s_algorithm
348348
if all(abs(b.a).<=2*eps(S))

src/pade.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ function Pade{T}(c::Poly{T},m::Int,n::Int)
2323
rold,uold,vold = temp0,temp1,temp2
2424

2525
end
26+
if vnew[0] == 0
27+
d = gcd(rnew,vnew)
28+
rnew /= d
29+
vnew /= d
30+
end
2631
Pade(rnew/vnew[0],vnew/vnew[0])
2732
end
2833
padeval{T}(PQ::Pade{T},x) = polyval(PQ.p,x)./polyval(PQ.q,x)

test/tests.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,30 @@ pS3 = Poly([1, 2, 3, 4, 5], :s)
8686
@test_throws ErrorException pS1 * pX
8787
@test_throws ErrorException pS1 / pX
8888
@test_throws ErrorException pS1 % pX
89+
90+
#Tests for Pade approximants
91+
92+
println("Test for the exponential function.")
93+
a = Poly(1.//convert(Vector{Int},gamma(1:17)),"x")
94+
PQexp = Pade(a,8,8)
95+
@test padeval(PQexp,1.0) == exp(1.0)
96+
@test padeval(PQexp,-1.0) == exp(-1.0)
97+
98+
println("Test for the sine function.")
99+
b = Poly(convert(Vector{BigInt},sinpi((0:16)/2)).//convert(Vector{BigInt},gamma(BigFloat("1.0"):BigFloat("17.0"))),"x")
100+
PQsin = Pade(b,8,7)
101+
@test isapprox(padeval(PQsin,1.0),sin(1.0))
102+
@test isapprox(padeval(PQsin,-1.0),sin(-1.0))
103+
104+
println("Test for the cosine function.")
105+
c = Poly(convert(Vector{BigInt},sinpi((1:17)/2)).//convert(Vector{BigInt},gamma(BigFloat("1.0"):BigFloat("17.0"))),"x")
106+
PQcos = Pade(c,8,8)
107+
@test isapprox(padeval(PQcos,1.0),cos(1.0))
108+
@test isapprox(padeval(PQcos,-1.0),cos(-1.0))
109+
110+
println("Test for the summation of a factorially divergent series.")
111+
d = Poly(convert(Vector{BigInt},(-1).^(0:60).*gamma(BigFloat("1.0"):BigFloat("61.0"))).//1,"x")
112+
PQexpint = Pade(d,30,30)
113+
println("The approximate sum of the divergent series is: ",float64(padeval(PQexpint,1.0)))
114+
println("The approximate sum of the convergent series is: ",exp(1)*(-γ-sum([(-1).^k/k./gamma(k+1) for k=1:20])))
115+
@test isapprox(padeval(PQexpint,1.0) , exp(1)*(-γ-sum([(-1).^k/k./gamma(k+1) for k=1:20])))

0 commit comments

Comments
 (0)