Skip to content

Commit 5a46d4a

Browse files
improve T∘H
for some reason precompiling the module kills my computer.
1 parent 453e9c6 commit 5a46d4a

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

src/FastTransforms.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__precompile__()
1+
#__precompile__()
22
module FastTransforms
33

44
using Base, ToeplitzMatrices

src/toeplitzhankel.jl

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
11
function partialchol(H::Hankel)
22
# Assumes positive definite
3-
σ=Array(eltype(H),0)
3+
σ=eltype(H)[]
44
n=size(H,1)
5-
C=Array(eltype(H),n,n)
5+
C=Vector{eltype(H)}[]
66
v=[H[:,1];vec(H[end,2:end])]
77
d=diag(H)
8-
98
@assert length(v) 2n-1
10-
11-
tol=1E-14
9+
tol=eps(eltype(H))*log(n)
1210
for k=1:n
1311
mx,idx=findmax(d)
14-
if mx tol
15-
break
12+
if mx tol break end
13+
push!(σ,inv(mx))
14+
push!(C,v[idx:n+idx-1])
15+
for j=1:k-1
16+
nCjidxσj = -C[j][idx]*σ[j]
17+
Base.axpy!(nCjidxσj, C[j], C[k])
1618
end
17-
push!(σ,1/mx)
1819
@simd for p=1:n
19-
@inbounds C[p,k]= v[p+idx-1] # H[p,idx]
20+
@inbounds d[p]-=C[k][p]^2/mx
2021
end
21-
for j=1:k-1,p=1:n
22-
@inbounds C[p,k]-=C[p,j]*C[idx,j]*σ[j]
23-
end
24-
@simd for p=1:n
25-
@inbounds d[p]-=C[p,k]^2/mx
26-
end
27-
end
28-
for k=1:length(σ),p=1:n
29-
@inbounds C[p,k]*=sqrt(σ[k])
3022
end
31-
C[:,1:length(σ)]
23+
for k=1:length(σ) scale!(C[k],sqrt(σ[k])) end
24+
C
3225
end
3326

3427
function toeplitzcholmult(T,C,v)
35-
ret=C[:,1].*(T*(C[:,1].*v))
36-
for j=2:size(C,2)
37-
ret+=C[:,j].*(T*(C[:,j].*v))
28+
n,K = length(v),length(C)
29+
ret,temp1,temp2 = zero(v),zero(v),zero(v)
30+
un,ze = one(eltype(v)),zero(eltype(v))
31+
broadcast!(*, temp1, C[K], v)
32+
A_mul_B!(un, T, temp1, ze, temp2)
33+
broadcast!(*, ret, C[K], temp2)
34+
for k=K-1:-1:1
35+
broadcast!(*, temp1, C[k], v)
36+
A_mul_B!(un, T, temp1, ze, temp2)
37+
broadcast!(*, temp1, C[k], temp2)
38+
broadcast!(+, ret, ret, temp1)
3839
end
3940
ret
4041
end
@@ -43,16 +44,16 @@ end
4344
# Plan a multiply by DL*(T.*H)*DR
4445
immutable ToeplitzHankelPlan{TT}
4546
T::TriangularToeplitz{TT}
46-
C::Matrix{TT} # A cholesky factorization of H: H=CC'
47+
C::Vector{Vector{TT}} # A cholesky factorization of H: H=CC'
4748
DL::Vector{TT}
4849
DR::Vector{TT}
4950

5051
ToeplitzHankelPlan(T,C,DL,DR)=new(T,C,DL,DR)
5152
end
5253

5354

54-
function ToeplitzHankelPlan(T::TriangularToeplitz,C::Matrix,DL::AbstractVector,DR::AbstractVector)
55-
TT=promote_type(eltype(T),eltype(C),eltype(DL),eltype(DR))
55+
function ToeplitzHankelPlan(T::TriangularToeplitz,C::Vector,DL::AbstractVector,DR::AbstractVector)
56+
TT=promote_type(eltype(T),eltype(C[1]),eltype(DL),eltype(DR))
5657
ToeplitzHankelPlan{TT}(T,C,collect(TT,DL),collect(TT,DR))
5758
end
5859
ToeplitzHankelPlan(T::TriangularToeplitz,C::Matrix) =
@@ -66,7 +67,9 @@ ToeplitzHankelPlan(T::TriangularToeplitz,H::Hankel,D...) =
6667

6768
# Legendre transforms
6869

70+
Λ = Cx
6971

72+
#=
7073
Λ{T}(::Type{T},z)=z<5?gamma(z+one(T)/2)/gamma(z+one(T)):exp(lgamma(z+one(T)/2)-lgamma(z+one(T)))
7174
7275
# use recurrence to construct Λ fast on a range of values
@@ -94,10 +97,10 @@ end
9497
9598
Λ(z::Number)=Λ(typeof(z),z)
9699
Λ(z::AbstractArray)=Λ(eltype(z),z)
97-
100+
=#
98101

99102
function leg2chebuTH{TT}(::Type{TT},n)
100-
λ=Λ(TT,0:0.5:n-1)
103+
λ=Λ(0:half(TT):n-1)
101104
t=zeros(TT,n)
102105
t[1:2:end]=λ[1:2:n]./(((1:2:n)-2))
103106
T=TriangularToeplitz(-2/π*t,:U)
@@ -108,7 +111,7 @@ end
108111
th_leg2chebuplan{TT}(::Type{TT},n)=ToeplitzHankelPlan(leg2chebuTH(TT,n)...,1:n,ones(TT,n))
109112

110113
function leg2chebTH{TT}(::Type{TT},n)
111-
λ=Λ(TT,0:0.5:n-1)
114+
λ=Λ(0:half(TT):n-1)
112115
t=zeros(TT,n)
113116
t[1:2:end]=λ[1:2:n]
114117
T=TriangularToeplitz(2/π*t,:U)

test/runtests.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ println("Testing BigFloat support of FFT and DCT methods")
176176

177177
include("fftBigFloattest.jl")
178178

179-
r=rand(1000)
180-
@test_approx_eq leg2cheb(r) cjt(r,0.,0.)
179+
println("Testing equivalence of CXN and ASY methods")
180+
181+
for k in round(Int,logspace(1,4,20))
182+
r = randn(k)./sqrt(1:k) # Proven O(1) error for ASY method.
183+
@test_approx_eq leg2cheb(r) cjt(r,0.,0.)
184+
end
181185

182186
@test_approx_eq leg2chebu([1.0,2,3,4,5]) [0.546875,0.5,0.5390625,1.25,1.3671875]

0 commit comments

Comments
 (0)