Skip to content

Commit 73047b2

Browse files
Basic support for Pade approximants
Computed via the extended euclidean algorithm.
1 parent 3a01738 commit 73047b2

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Polynomials.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Polynomials
44
#todo: sparse polynomials?
55

66
export Poly, polyval, polyint, polyder, poly, roots
7+
export Pade, padeval
78

89
import Base: length, endof, getindex, setindex!, copy, zero, one, convert
910
import Base: show, print, *, /, //, -, +, ==, divrem, rem, eltype
@@ -351,4 +352,7 @@ function gcd{T<:FloatingPoint, S<:FloatingPoint}(a::Poly{T}, b::Poly{S})
351352
return gcd(b, r)
352353
end
353354
end
355+
356+
include("pade.jl")
357+
354358
end # module Poly

src/pade.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
immutable Pade{T<:Number,S<:Number}
2+
p::Poly{T}
3+
q::Poly{S}
4+
var::Symbol
5+
function Pade(p::Poly{T},q::Poly{S})
6+
if p.var != q.var
7+
error("Polynomials must have same variable")
8+
end
9+
new(p, q, p.var)
10+
end
11+
end
12+
Pade{T<:Number,S<:Number}(p::Poly{T}, q::Poly{S}) = Pade{T,S}(p,q)
13+
14+
function Pade{T}(c::Poly{T},m::Int,n::Int)
15+
@assert m+n < length(c)
16+
rold,rnew = Poly([zeros(T,m+n+1),one(T)],c.var),Poly(c.a[1:m+n+1],c.var)
17+
uold,vold = Poly([one(T)],c.var),Poly([zero(T)],c.var)
18+
unew,vnew = vold,uold
19+
for i=1:n
20+
temp0,temp1,temp2 = rnew,unew,vnew
21+
q,rnew = divrem(rold,rnew)
22+
unew,vnew = uold-q*unew,vold-q*vnew
23+
rold,uold,vold = temp0,temp1,temp2
24+
25+
end
26+
Pade(rnew/vnew[0],vnew/vnew[0])
27+
end
28+
padeval{T}(PQ::Pade{T},x) = polyval(PQ.p,x)./polyval(PQ.q,x)

0 commit comments

Comments
 (0)