Skip to content

Commit 3ba4ee3

Browse files
authored
BigFloat chebyshev transform (#118)
* BigFloat Cheb transform * v0.10.1 * add tests, comment out broken 2nd kind transform code
1 parent 9d3012c commit 3ba4ee3

File tree

4 files changed

+100
-9
lines changed

4 files changed

+100
-9
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FastTransforms"
22
uuid = "057dd010-8810-581a-b7be-e3fc3b93f78c"
3-
version = "0.10.0"
3+
version = "0.10.1"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"

src/chebyshevtransform.jl

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ end
7575
*(P::ChebyshevTransformPlan{T,k,false}, x::AbstractVector{T}) where {T,k} =
7676
ChebyshevTransformPlan{T,k,true}(P)*copy(x)
7777

78-
chebyshevtransform!(x::AbstractVector{T}, kind=Val(1)) where T<:fftwNumber =
78+
chebyshevtransform!(x::AbstractVector{T}, kind=Val(1)) where T =
7979
plan_chebyshevtransform!(x, kind)*x
8080

8181

@@ -167,7 +167,7 @@ end
167167
*(P::IChebyshevTransformPlan{T,k,false},x::AbstractVector{T}) where {T,k} =
168168
IChebyshevTransformPlan{T,k,true}(P)*copy(x)
169169

170-
ichebyshevtransform!(x::AbstractVector{T}, kind=Val(1)) where {T<:fftwNumber} =
170+
ichebyshevtransform!(x::AbstractVector{T}, kind=Val(1)) where T =
171171
plan_ichebyshevtransform!(x, kind)*x
172172

173173
ichebyshevtransform(x, kind=Val(1)) = ichebyshevtransform!(copy(x), kind)
@@ -433,3 +433,85 @@ chebyshevpoints(n::Integer, kind=Val(1)) = chebyshevpoints(Float64, n, kind)
433433
# x = P.plan*x
434434
# rmul!(x,half(T))
435435
# end
436+
437+
438+
###
439+
# BigFloat
440+
# Use `Nothing` and fall back too FFT
441+
###
442+
443+
plan_chebyshevtransform(x::AbstractVector{T}, ::Val{kind}) where {T,kind} =
444+
ChebyshevTransformPlan{T,kind,false,Nothing}()
445+
plan_ichebyshevtransform(x::AbstractVector{T}, ::Val{kind}) where {T,kind} =
446+
IChebyshevTransformPlan{T,kind,false,Nothing}()
447+
448+
plan_chebyshevtransform!(x::AbstractVector{T}, ::Val{kind}) where {T,kind} =
449+
ChebyshevTransformPlan{T,kind,true,Nothing}()
450+
plan_ichebyshevtransform!(x::AbstractVector{T}, ::Val{kind}) where {T,kind} =
451+
IChebyshevTransformPlan{T,kind,true,Nothing}()
452+
453+
#following Chebfun's @Chebtech1/vals2coeffs.m and @Chebtech2/vals2coeffs.m
454+
function *(P::ChebyshevTransformPlan{T,1,false,Nothing}, x::AbstractVector{T}) where T
455+
n = length(x)
456+
if n == 1
457+
x
458+
else
459+
w = [2exp(im*convert(T,π)*k/2n) for k=0:n-1]
460+
ret = w.*ifft([x;reverse(x)])[1:n]
461+
ret = T<:Real ? real(ret) : ret
462+
ret[1] /= 2
463+
ret
464+
end
465+
end
466+
467+
468+
# function *(P::ChebyshevTransformPlan{T,2,false,Nothing}, x::AbstractVector{T}) where T
469+
# n = length(x)
470+
# if n == 1
471+
# x
472+
# else
473+
# ret = ifft([x;x[end:-1:2]])[1:n]
474+
# ret = T<:Real ? real(ret) : ret
475+
# ret[2:n-1] *= 2
476+
# ret
477+
# end
478+
# end
479+
480+
481+
*(P::ChebyshevTransformPlan{T,1,true,Nothing}, x::AbstractVector{T}) where T =
482+
copyto!(x, ChebyshevTransformPlan{T,1,false,Nothing}() * x)
483+
# *(P::ChebyshevTransformPlan{T,2,true,Nothing}, x::AbstractVector{T}) where T =
484+
# copyto!(x, ChebyshevTransformPlan{T,2,false,Nothing}() * x)
485+
486+
487+
#following Chebfun's @Chebtech1/vals2coeffs.m and @Chebtech2/vals2coeffs.m
488+
function *(P::IChebyshevTransformPlan{T,1,false,Nothing}, x::AbstractVector{T}) where T
489+
n = length(x)
490+
if n == 1
491+
x
492+
else
493+
w = [exp(-im*convert(T,π)*k/2n)/2 for k=0:2n-1]
494+
w[1] *= 2;w[n+1] *= 0;w[n+2:end] *= -1
495+
ret = fft(w.*[x;one(T);x[end:-1:2]])
496+
ret = T<:Real ? real(ret) : ret
497+
ret[1:n]
498+
end
499+
end
500+
501+
# function *(P::IChebyshevTransformPlan{T,2,true,Nothing}, x::AbstractVector{T}) where T
502+
# n = length(x)
503+
# if n == 1
504+
# x
505+
# else
506+
# x[1] *= 2; x[end] *= 2
507+
# chebyshevtransform!(x, Val(2))
508+
# x[1] *= 2; x[end] *= 2
509+
# lmul!(convert(T,n-1)/2, x)
510+
# x
511+
# end
512+
# end
513+
514+
*(P::IChebyshevTransformPlan{T,1,true,Nothing}, x::AbstractVector{T}) where T =
515+
copyto!(x, IChebyshevTransformPlan{T,1,false,Nothing}() * x)
516+
# *(P::IChebyshevTransformPlan{T,2,false,Nothing}, x::AbstractVector{T}) where T =
517+
# IChebyshevTransformPlan{T,2,true,Nothing}() * copy(x)

src/libfasttransforms.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,12 @@ function lmul!(p::FTPlan{Complex{Float64}, 2, SPINSPHEREANALYSIS}, x::Matrix{Com
595595
return x
596596
end
597597

598-
*(p::FTPlan{T}, x::Array{T}) where T = lmul!(p, deepcopy(x))
599-
*(p::AdjointFTPlan{T}, x::Array{T}) where T = lmul!(p, deepcopy(x))
600-
*(p::TransposeFTPlan{T}, x::Array{T}) where T = lmul!(p, deepcopy(x))
601-
\(p::FTPlan{T}, x::Array{T}) where T = ldiv!(p, deepcopy(x))
602-
\(p::AdjointFTPlan{T}, x::Array{T}) where T = ldiv!(p, deepcopy(x))
603-
\(p::TransposeFTPlan{T}, x::Array{T}) where T = ldiv!(p, deepcopy(x))
598+
*(p::FTPlan{T}, x::AbstractArray{T}) where T = lmul!(p, Array(x))
599+
*(p::AdjointFTPlan{T}, x::AbstractArray{T}) where T = lmul!(p, Array(x))
600+
*(p::TransposeFTPlan{T}, x::AbstractArray{T}) where T = lmul!(p, Array(x))
601+
\(p::FTPlan{T}, x::AbstractArray{T}) where T = ldiv!(p, Array(x))
602+
\(p::AdjointFTPlan{T}, x::AbstractArray{T}) where T = ldiv!(p, Array(x))
603+
\(p::TransposeFTPlan{T}, x::AbstractArray{T}) where T = ldiv!(p, Array(x))
604604

605605
*(p::FTPlan{T, 1}, x::UniformScaling{S}) where {T, S} = lmul!(p, Matrix{promote_type(T, S)}(x, p.n, p.n))
606606
*(p::AdjointFTPlan{T, FTPlan{T, 1, K}}, x::UniformScaling{S}) where {T, S, K} = lmul!(p, Matrix{promote_type(T, S)}(x, p.parent.n, p.parent.n))

test/chebyshevtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,13 @@ using FastTransforms, Test
215215
@test ichebyshevutransform([1,2,3]) == ichebyshevutransform([1.,2,3])
216216
@test ichebyshevutransform([1,2,3], Val(2)) == ichebyshevutransform([1.,2,3], Val(2))
217217
end
218+
219+
@testset "BigFloat" begin
220+
x = BigFloat[1,2,3]
221+
@test ichebyshevtransform(chebyshevtransform(x)) x
222+
@test plan_chebyshevtransform(x)x chebyshevtransform(x)
223+
@test plan_ichebyshevtransform(x)x ichebyshevtransform(x)
224+
@test plan_chebyshevtransform!(x)copy(x) chebyshevtransform(x)
225+
@test plan_ichebyshevtransform!(x)copy(x) ichebyshevtransform(x)
226+
end
218227
end

0 commit comments

Comments
 (0)