|
| 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 |
0 commit comments