Skip to content

Commit 2dcabeb

Browse files
authored
Add th_cheb2jac and th_jac2cheb (#229)
* Add Cheb2Jac and Jac2Cheb * seed random nums * Update toeplitzhankeltests.jl
1 parent 5028278 commit 2dcabeb

File tree

3 files changed

+99
-6
lines changed

3 files changed

+99
-6
lines changed

Project.toml

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
1313
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1414
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
1515
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
16-
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1716
ToeplitzMatrices = "c751599d-da0a-543b-9d20-d0a503d91d24"
1817

1918
[compat]
@@ -27,3 +26,11 @@ Reexport = "0.2, 1.0"
2726
SpecialFunctions = "0.10, 1, 2"
2827
ToeplitzMatrices = "0.7.1, 0.8"
2928
julia = "1.7"
29+
30+
31+
[extras]
32+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
33+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
34+
35+
[targets]
36+
test = ["Test", "Random"]

src/toeplitzhankel.jl

+70-2
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ Jac2JacPlanTH(plans, α, β, γ, δ, dims) = Jac2JacPlanTH(plans, promote(α, β
448448

449449
function *(P::Jac2JacPlanTH, A::AbstractArray)
450450
if P.α + P.β -1
451-
_jacobi_raise_a!(A, P.α, P.β)
451+
_jacobi_raise_a!(A, P.α, P.β, P.dims)
452452
c,d = _nearest_jacobi_par(P.α+1, P.γ), _nearest_jacobi_par(P.β, P.δ)
453453
else
454454
c,d = _nearest_jacobi_par(P.α, P.γ), _nearest_jacobi_par(P.β, P.δ)
@@ -690,4 +690,72 @@ plan_th_jac2jac!(::Type{S}, mn::NTuple{N,Int}, α, β, γ, δ, dims::UnitRange)
690690
plan_th_jac2jac!(::Type{S}, mn::Tuple{Int}, α, β, γ, δ, dims::Tuple{Int}=(1,)) where {S} = plan_th_jac2jac!(S, mn, α, β, γ, δ, dims...)
691691
plan_th_jac2jac!(::Type{S}, (m,n)::NTuple{2,Int}, α, β, γ, δ) where {S} = plan_th_jac2jac!(S, (m,n), α, β, γ, δ, (1,2))
692692
plan_th_jac2jac!(arr::AbstractArray{T}, α, β, γ, δ, dims...) where T = plan_th_jac2jac!(T, size(arr), α, β, γ, δ, dims...)
693-
th_jac2jac(v, α, β, γ, δ, dims...) = plan_th_jac2jac!(eltype(v), size(v), α, β, γ, δ, dims...)*copy(v)
693+
th_jac2jac(v, α, β, γ, δ, dims...) = plan_th_jac2jac!(eltype(v), size(v), α, β, γ, δ, dims...)*copy(v)
694+
695+
696+
####
697+
# cheb2jac
698+
####
699+
700+
struct Cheb2JacPlanTH{T, Pl<:Jac2JacPlanTH{T}} <: Plan{T}
701+
jac2jac::Pl
702+
end
703+
704+
705+
struct Jac2ChebPlanTH{T, Pl<:Jac2JacPlanTH{T}} <: Plan{T}
706+
jac2jac::Pl
707+
end
708+
709+
710+
function jac_cheb_recurrencecoefficients(T, N)
711+
n = 0:N
712+
h = one(T)/2
713+
A = (2n .+ one(T)) ./ (n .+ one(T))
714+
A[1] /= 2
715+
A, Zeros(n),
716+
((n .- h) .* (n .- h) .* (2n .+ one(T))) ./ ((n .+ one(T)) .* n .* (2n .- one(T)))
717+
end
718+
719+
720+
function *(P::Cheb2JacPlanTH{T}, X::AbstractArray) where T
721+
A,B,C = jac_cheb_recurrencecoefficients(T, max(size(X)...))
722+
723+
for d in P.jac2jac.dims
724+
if d == 1
725+
p = forwardrecurrence(size(X,1), A,B,C, one(T))
726+
X .= p .\ X
727+
else
728+
@assert d == 2
729+
n = size(X,2)
730+
p = forwardrecurrence(size(X,2), A,B,C, one(T))
731+
X .= X ./ transpose(p)
732+
end
733+
end
734+
P.jac2jac*X
735+
end
736+
737+
function *(P::Jac2ChebPlanTH{T}, X::AbstractArray) where T
738+
X = P.jac2jac*X
739+
A,B,C = jac_cheb_recurrencecoefficients(T, max(size(X)...))
740+
741+
for d in P.jac2jac.dims
742+
if d == 1
743+
p = forwardrecurrence(size(X,1), A,B,C, one(T))
744+
X .= p .* X
745+
else
746+
@assert d == 2
747+
n = size(X,2)
748+
p = forwardrecurrence(size(X,2), A,B,C, one(T))
749+
X .= X .* transpose(p)
750+
end
751+
end
752+
X
753+
end
754+
755+
plan_th_cheb2jac!(::Type{T}, mn, α, β, dims...) where T = Cheb2JacPlanTH(plan_th_jac2jac!(T, mn, -one(α)/2, -one(α)/2, α, β, dims...))
756+
plan_th_cheb2jac!(arr::AbstractArray{T}, α, β, dims...) where T = plan_th_cheb2jac!(T, size(arr), α, β, dims...)
757+
th_cheb2jac(v, α, β, dims...) = plan_th_cheb2jac!(eltype(v), size(v), α, β, dims...)*copy(v)
758+
759+
plan_th_jac2cheb!(::Type{T}, mn, α, β, dims...) where T = Jac2ChebPlanTH(plan_th_jac2jac!(T, mn, α, β, -one(α)/2, -one(α)/2, dims...))
760+
plan_th_jac2cheb!(arr::AbstractArray{T}, α, β, dims...) where T = plan_th_jac2cheb!(T, size(arr), α, β, dims...)
761+
th_jac2cheb(v, α, β, dims...) = plan_th_jac2cheb!(eltype(v), size(v), α, β, dims...)*copy(v)

test/toeplitzhankeltests.jl

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using FastTransforms, Test
1+
using FastTransforms, Test, Random
22
import FastTransforms: th_leg2cheb, th_cheb2leg, th_leg2chebu, th_ultra2ultra,th_jac2jac, th_leg2chebu,
33
lib_leg2cheb, lib_cheb2leg, lib_ultra2ultra, lib_jac2jac,
4-
plan_th_cheb2leg!, plan_th_leg2chebu!, plan_th_leg2cheb!, plan_th_ultra2ultra!, plan_th_jac2jac!
4+
plan_th_cheb2leg!, plan_th_leg2chebu!, plan_th_leg2cheb!, plan_th_ultra2ultra!, plan_th_jac2jac!,
5+
th_cheb2jac, th_jac2cheb
6+
7+
Random.seed!(0)
58

69
@testset "ToeplitzHankel" begin
710
for x in ([1.0], [1.0,2,3,4,5], [1.0+im,2-3im,3+4im,4-5im,5+10im], collect(1.0:1000))
@@ -26,11 +29,14 @@ import FastTransforms: th_leg2cheb, th_cheb2leg, th_leg2chebu, th_ultra2ultra,th
2629
@test th_jac2jac(x, -1/2,-1/2,1/2,0) lib_jac2jac(x, -1/2,-1/2,1/2,0)
2730
@test th_jac2jac(x, -1/2,-1/2,0,1/2) lib_jac2jac(x, -1/2,-1/2,0,1/2)
2831
@test th_jac2jac(x, -3/4,-3/4,0,3/4) lib_jac2jac(x, -3/4,-3/4,0,3/4)
29-
@test th_jac2jac(x,0, 0, 5, 5) lib_jac2jac(x, 0, 0, 5, 5)
3032
if length(x) < 10
33+
@test th_jac2jac(x,0, 0, 5, 5) lib_jac2jac(x, 0, 0, 5, 5)
3134
@test th_jac2jac(x, 5, 5, 0, 0) lib_jac2jac(x, 5, 5, 0, 0)
3235
end
3336

37+
@test th_cheb2jac(x, 0.2, 0.3) cheb2jac(x, 0.2, 0.3)
38+
@test th_jac2cheb(x, 0.2, 0.3) jac2cheb(x, 0.2, 0.3)
39+
3440
@test th_cheb2leg(th_leg2cheb(x)) x
3541
@test th_leg2cheb(th_cheb2leg(x)) x
3642
@test th_ultra2ultra(th_ultra2ultra(x, 0.1, 0.6), 0.6, 0.1) x
@@ -93,6 +99,18 @@ import FastTransforms: th_leg2cheb, th_cheb2leg, th_leg2chebu, th_ultra2ultra,th
9399
@test th_jac2jac(X, 0.1, 0.6, 3.1, 2.8, 1) hcat([jac2jac(X[:,j], 0.1, 0.6, 3.1, 2.8) for j=1:size(X,2)]...)
94100
@test th_jac2jac(X, 0.1, 0.6, 3.1, 2.8, 2) vcat([permutedims(jac2jac(X[k,:], 0.1, 0.6, 3.1, 2.8)) for k=1:size(X,1)]...)
95101
@test th_jac2jac(X, 0.1, 0.6, 3.1, 2.8) th_jac2jac(th_jac2jac(X, 0.1, 0.6, 3.1, 2.8, 1), 0.1, 0.6, 3.1, 2.8, 2)
102+
103+
@test th_jac2jac(X, -0.5, -0.5, 3.1, 2.8, 1) hcat([jac2jac(X[:,j], -0.5, -0.5, 3.1, 2.8) for j=1:size(X,2)]...)
104+
@test th_jac2jac(X, -0.5, -0.5, 3.1, 2.8, 2) vcat([permutedims(jac2jac(X[k,:], -0.5, -0.5, 3.1, 2.8)) for k=1:size(X,1)]...)
105+
@test th_jac2jac(X, -0.5, -0.5, 3.1, 2.8) th_jac2jac(th_jac2jac(X, -0.5, -0.5, 3.1, 2.8, 1), -0.5, -0.5, 3.1, 2.8, 2)
106+
107+
@test th_cheb2jac(X, 3.1, 2.8, 1) hcat([cheb2jac(X[:,j], 3.1, 2.8) for j=1:size(X,2)]...)
108+
@test th_cheb2jac(X, 3.1, 2.8, 2) vcat([permutedims(cheb2jac(X[k,:], 3.1, 2.8)) for k=1:size(X,1)]...)
109+
@test th_cheb2jac(X, 3.1, 2.8) th_cheb2jac(th_cheb2jac(X, 3.1, 2.8, 1), 3.1, 2.8, 2)
110+
111+
@test th_jac2cheb(X, 3.1, 2.8, 1) hcat([jac2cheb(X[:,j], 3.1, 2.8) for j=1:size(X,2)]...)
112+
@test th_jac2cheb(X, 3.1, 2.8, 2) vcat([permutedims(jac2cheb(X[k,:], 3.1, 2.8)) for k=1:size(X,1)]...)
113+
@test th_jac2cheb(X, 3.1, 2.8) th_jac2cheb(th_jac2cheb(X, 3.1, 2.8, 1), 3.1, 2.8, 2)
96114
end
97115

98116
@testset "BigFloat" begin

0 commit comments

Comments
 (0)