Skip to content

Commit 15bb9ad

Browse files
add Leg2Cheb and Cheb2Leg
1 parent c8899f5 commit 15bb9ad

File tree

4 files changed

+160
-10
lines changed

4 files changed

+160
-10
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
julia 0.5
22
ToeplitzMatrices 0.2
3-
Compat 0.17
3+
Compat 0.19

src/FastTransforms.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
__precompile__()
22
module FastTransforms
33

4-
using Base, ToeplitzMatrices, Compat
4+
using Base, ToeplitzMatrices, HierarchicalMatrices, Compat
55

6-
import Base: *
7-
import Base: view
6+
import Base: *, view
7+
8+
import HierarchicalMatrices: HierarchicalMatrix
89

910
export cjt, icjt, jjt, plan_cjt, plan_icjt
1011
export leg2cheb, cheb2leg, leg2chebu, ultra2ultra, jac2jac
12+
export plan_leg2cheb, plan_cheb2leg
1113
export gaunt
1214
export paduatransform, ipaduatransform, paduatransform!, ipaduatransform!, paduapoints
1315
export plan_paduatransform!, plan_ipaduatransform!
@@ -42,12 +44,14 @@ include("cjt.jl")
4244

4345
include("toeplitzhankel.jl")
4446

45-
leg2cheb(x...)=th_leg2cheb(x...)
46-
cheb2leg(x...)=th_cheb2leg(x...)
47+
#leg2cheb(x...)=th_leg2cheb(x...)
48+
#cheb2leg(x...)=th_cheb2leg(x...)
4749
leg2chebu(x...)=th_leg2chebu(x...)
4850
ultra2ultra(x...)=th_ultra2ultra(x...)
4951
jac2jac(x...)=th_jac2jac(x...)
5052

53+
include("hierarchical.jl")
54+
5155
include("gaunt.jl")
5256

5357

src/hierarchical.jl

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
UpperTriangularHierarchicalMatrix{T}(::Type{T}, f::Function, bd::Int64) = UpperTriangularHierarchicalMatrix(T, f, bd, bd)
2+
UpperTriangularHierarchicalMatrix{T}(::Type{T}, f::Function, b::Int64, d::Int64) = UpperTriangularHierarchicalMatrix(T, f, 1, b, 1, d)
3+
4+
function UpperTriangularHierarchicalMatrix{T}(::Type{T}, f::Function, a::Int64, b::Int64, c::Int64, d::Int64)
5+
if (b-a+1) < BLOCKSIZE(T) && (d-c+1) < BLOCKSIZE(T)
6+
i = (b-a)÷2
7+
j = (d-c)÷2
8+
H = HierarchicalMatrix(T, 2, 2)
9+
H[Block(1), Block(1)] = T[j i ? f(T,i,j) : zero(T) for i=a:a+i, j=c:c+j]
10+
H[Block(1), Block(2)] = T[f(T,i,j) for i=a:a+i, j=c+j+1:d]
11+
H[Block(2), Block(2)] = T[j i ? f(T,i,j) : zero(T) for i=a+i+1:b, j=c+j+1:d]
12+
13+
H
14+
else
15+
i = (b-a)÷2
16+
j = (d-c)÷2
17+
H = HierarchicalMatrix(T, 2, 2)
18+
H[Block(1), Block(1)] = UpperTriangularHierarchicalMatrix(T, f, a, a+i, c, c+j)
19+
H[Block(1), Block(2)] = HierarchicalMatrix(T, f, a, a+i, c+j+1, d)
20+
H[Block(2), Block(2)] = UpperTriangularHierarchicalMatrix(T, f, a+i+1, b, c+j+1, d)
21+
22+
H
23+
end
24+
end
25+
26+
function HierarchicalMatrix{T}(::Type{T}, f::Function, a::Int64, b::Int64, c::Int64, d::Int64)
27+
if (b-a+1) < BLOCKSIZE(T) && (d-c+1) < BLOCKSIZE(T)
28+
i = (b-a)÷2
29+
j = (d-c)÷2
30+
H = HierarchicalMatrix(T, 2, 2)
31+
H[Block(1), Block(1)] = barycentricmatrix(T, f, a, a+i, c, c+j)
32+
H[Block(1), Block(2)] = barycentricmatrix(T, f, a, a+i, c+j+1, d)
33+
H[Block(2), Block(1)] = T[f(T,i,j) for i=a+i+1:b, j=c:c+j]
34+
H[Block(2), Block(2)] = barycentricmatrix(T, f, a+i+1, b, c+j+1, d)
35+
36+
H
37+
else
38+
i = (b-a)÷2
39+
j = (d-c)÷2
40+
H = HierarchicalMatrix(T, 2, 2)
41+
H[Block(1), Block(1)] = barycentricmatrix(T, f, a, a+i, c, c+j)
42+
H[Block(1), Block(2)] = barycentricmatrix(T, f, a, a+i, c+j+1, d)
43+
H[Block(2), Block(1)] = HierarchicalMatrix(T, f, a+i+1, b, c, c+j)
44+
H[Block(2), Block(2)] = barycentricmatrix(T, f, a+i+1, b, c+j+1, d)
45+
46+
H
47+
end
48+
end
49+
50+
function Meven{T}(::Type{T}, x, y)
51+
T(Λ(1.0*(y-x)).*Λ(1.0*(y+x-2)))
52+
end
53+
54+
function Modd{T}(::Type{T}, x, y)
55+
T(Λ(1.0*(y-x)).*Λ(1.0*(y+x-1)))
56+
end
57+
58+
function Leven{T}(::Type{T}, x, y)
59+
if x == y
60+
if x == 1.0
61+
T(1.0)
62+
else
63+
T(sqrt(π)/2/Λ(2.0*(x-1.0)))
64+
end
65+
else
66+
T(-(y-1.0)*(2.0*x-1.5)/(2.0*x+2.0*y-3.0)/(y-x)*Λ(1.0*(y-x-1.0)).*Λ(1.0*(y+x-2.5)))
67+
end
68+
end
69+
70+
function Lodd{T}(::Type{T}, x, y)
71+
if x == y
72+
if x == 1.0
73+
T(1.0)
74+
else
75+
T(sqrt(π)/2/Λ(2.0*x-1.0))
76+
end
77+
else
78+
T(-(2.0*y-1.0)*(2.0*x-0.5)/(2.0*x+2.0*y-1.0)/(2.0*y-2.0*x)*Λ(1.0*(y-x-1)).*Λ(0.5*(2.0*y+2.0*x-3)))
79+
end
80+
end
81+
82+
immutable LegendreToChebyshevPlan{T} <: AbstractMatrix{T}
83+
even::HierarchicalMatrix{T}
84+
odd::HierarchicalMatrix{T}
85+
end
86+
87+
Base.size(P::LegendreToChebyshevPlan) = (size(P.even, 1)+size(P.odd, 1), size(P.even, 2)+size(P.odd, 2))
88+
function Base.getindex(P::LegendreToChebyshevPlan, i::Int, j::Int)
89+
if isodd(i) && isodd(j)
90+
P.even[(i+1)÷2,(j+1)÷2]
91+
elseif iseven(i) && iseven(j)
92+
P.odd[i÷2,j÷2]
93+
else
94+
zero(eltype(P))
95+
end
96+
end
97+
98+
immutable ChebyshevToLegendrePlan{T} <: AbstractMatrix{T}
99+
even::HierarchicalMatrix{T}
100+
odd::HierarchicalMatrix{T}
101+
end
102+
103+
Base.size(P::ChebyshevToLegendrePlan) = (size(P.even, 1)+size(P.odd, 1), size(P.even, 2)+size(P.odd, 2))
104+
function Base.getindex(P::ChebyshevToLegendrePlan, i::Int, j::Int)
105+
if isodd(i) && isodd(j)
106+
P.even[(i+1)÷2,(j+1)÷2]
107+
elseif iseven(i) && iseven(j)
108+
P.odd[i÷2,j÷2]
109+
else
110+
zero(eltype(P))
111+
end
112+
end
113+
114+
LegendreToChebyshevPlan(v::Vector) = LegendreToChebyshevPlan(plan_even_leg2cheb(v), plan_odd_leg2cheb(v))
115+
ChebyshevToLegendrePlan(v::Vector) = ChebyshevToLegendrePlan(plan_even_cheb2leg(v), plan_odd_cheb2leg(v))
116+
117+
plan_leg2cheb(v::Vector) = LegendreToChebyshevPlan(v)
118+
plan_cheb2leg(v::Vector) = ChebyshevToLegendrePlan(v)
119+
120+
evenlength(v::Vector) = (L = length(v); iseven(L) ? L÷2 : (L+1)÷2)
121+
oddlength(v::Vector) = (L = length(v); iseven(L) ? L÷2 : (L-1)÷2)
122+
123+
plan_even_leg2cheb(v::Vector) = UpperTriangularHierarchicalMatrix(eltype(v), Meven, evenlength(v))
124+
plan_odd_leg2cheb(v::Vector) = UpperTriangularHierarchicalMatrix(eltype(v), Modd, oddlength(v))
125+
126+
plan_even_cheb2leg(v::Vector) = UpperTriangularHierarchicalMatrix(eltype(v), Leven, evenlength(v))
127+
plan_odd_cheb2leg(v::Vector) = UpperTriangularHierarchicalMatrix(eltype(v), Lodd, oddlength(v))
128+
129+
function *(P::LegendreToChebyshevPlan,v::AbstractVector)
130+
u = zero(v)
131+
u[1:2:end] = P.even*view(v,1:2:length(v))
132+
u[2:2:end] = P.odd*view(v,2:2:length(v))
133+
scale!(2/π, u)
134+
u[1] *= 0.5
135+
u
136+
end
137+
138+
function *(P::ChebyshevToLegendrePlan,v::AbstractVector)
139+
u = zero(v)
140+
u[1:2:end] = P.even*view(v,1:2:length(v))
141+
u[2:2:end] = P.odd*view(v,2:2:length(v))
142+
u
143+
end
144+
145+
leg2cheb(v::Vector) = plan_leg2cheb(v)*v
146+
cheb2leg(v::Vector) = plan_cheb2leg(v)*v

src/toeplitzhankel.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,21 @@ function jac2jacTH{S}(::Type{S},n,α,β,γ,δ)
156156
T,H,DL,DR
157157
end
158158

159-
immutable ChebyshevToLegendrePlan{TH}
159+
immutable ChebyshevToLegendrePlanTH{TH}
160160
toeplitzhankel::TH
161161
end
162162

163-
ChebyshevToLegendrePlan{S}(::Type{S},n) = ChebyshevToLegendrePlan(th_cheb2legplan(S,n))
163+
ChebyshevToLegendrePlanTH{S}(::Type{S},n) = ChebyshevToLegendrePlanTH(th_cheb2legplan(S,n))
164164

165-
function *(P::ChebyshevToLegendrePlan,v::AbstractVector)
165+
function *(P::ChebyshevToLegendrePlanTH,v::AbstractVector)
166166
w = zero(v)
167167
S,n = eltype(v),length(v)
168168
w[1:2:end] = -one(S)./(one(S):two(S):n)./(-one(S):two(S):n-two(S))
169169
[dot(w,v);P.toeplitzhankel*view(v,2:n)]
170170
end
171171

172172
th_leg2chebplan{S}(::Type{S},n)=ToeplitzHankelPlan(leg2chebTH(S,n)...,ones(S,n))
173-
th_cheb2legplan{S}(::Type{S},n)=ChebyshevToLegendrePlan(ToeplitzHankelPlan(cheb2legTH(S,n)...))
173+
th_cheb2legplan{S}(::Type{S},n)=ChebyshevToLegendrePlanTH(ToeplitzHankelPlan(cheb2legTH(S,n)...))
174174
th_leg2chebuplan{S}(::Type{S},n)=ToeplitzHankelPlan(leg2chebuTH(S,n)...,1:n,ones(S,n))
175175
th_ultra2ultraplan{S}(::Type{S},n,λ₁,λ₂)=ToeplitzHankelPlan(ultra2ultraTH(S,n,λ₁,λ₂)...)
176176
th_jac2jacplan{S}(::Type{S},n,α,β,γ,δ)=ToeplitzHankelPlan(jac2jacTH(S,n,α,β,γ,δ)...)

0 commit comments

Comments
 (0)