diff --git a/src/chebyshevtransform.jl b/src/chebyshevtransform.jl index fca2b336..bd24a67e 100644 --- a/src/chebyshevtransform.jl +++ b/src/chebyshevtransform.jl @@ -375,7 +375,7 @@ function plan_chebyshevutransform(x::AbstractArray{T,N}, ::Val{2}, dims...; kws. ChebyshevUTransformPlan{T,2}(FFTW.plan_r2r(x, USECONDKIND, dims...; kws...)) end -for f in [:_chebu1_prescale!, :_chebu1_postscale!, :_chebu2_prescale!, :_chebu2_postscale!, +for f in [:_chebu1_prescale!, :_chebu2_prescale!, :_chebu2_postscale!, :_ichebu1_postscale!] _f = Symbol(:_, f) @eval begin @@ -399,12 +399,6 @@ function __chebu1_prescale!(d::Number, X::AbstractArray{T}) where {T} applydim!(v -> v .*= sinpi.(r) ./ m, X, d, :) end -@inline function __chebu1_postscale!(d::Number, X::AbstractArray{T}) where {T} - m = size(X,d) - r = one(T)/(2m) .+ ((1:m) .- one(T))./m - applydim!(v -> v ./= sinpi.(r) ./ m, X, d, :) -end - function *(P::ChebyshevUTransformPlan{T,1,K,true,N}, x::AbstractArray{T,N}) where {T,K,N} length(x) ≤ 1 && return x _chebu1_prescale!(P.plan.region, x) @@ -418,9 +412,8 @@ end function mul!(y::AbstractArray{T}, P::ChebyshevUTransformPlan{T,1,K,false}, x::AbstractArray{T}) where {T,K} size(y) == size(x) || throw(DimensionMismatch("output must match dimension")) isempty(x) && return y - _chebu1_prescale!(P.plan.region, x) # Todo don't mutate x - _plan_mul!(y, P.plan, x) - _chebu1_postscale!(P.plan.region, x) + # rescale a copy: rescaling x and then undoing it would not restore x exactly + _plan_mul!(y, P.plan, _chebu1_prescale!(P.plan.region, Array{T}(x))) for d in P.plan.region ldiv_dim_end!(2, d, y) end @@ -456,9 +449,8 @@ function mul!(y::AbstractArray{T}, P::ChebyshevUTransformPlan{T,2,K,false}, x::A for d in P.plan.region sc *= one(T)/(size(x,d)+1) end - _chebu2_prescale!(P.plan.region, x) # TODO don't mutate x - _plan_mul!(y, P.plan, x) - _chebu2_postscale!(P.plan.region, x) + # rescale a copy: rescaling x and then undoing it would not restore x exactly + _plan_mul!(y, P.plan, _chebu2_prescale!(P.plan.region, Array{T}(x))) lmul!(sc, y) end diff --git a/src/toeplitzhankel.jl b/src/toeplitzhankel.jl index 82851112..0eec4353 100644 --- a/src/toeplitzhankel.jl +++ b/src/toeplitzhankel.jl @@ -261,6 +261,20 @@ _nearest_jacobi_par(α::T, ::T) where T<:Integer = α _nearest_jacobi_par(α, γ) = _nearest_jacobi_par(promote(α,γ)...) +# The Toeplitz-Hankel conversion requires a positive parameter +# so a negative parameter is first raised by one. +_ultra_th_start(λ) = λ < 0 ? λ + 1 : λ + +""" + _nearest_ultra_par(λ, μ) + +returns a positive number that is an integer different than μ but less than 1 away from λ > 0. +""" +function _nearest_ultra_par(λ, μ) + c = _nearest_jacobi_par(λ, μ) + c < 1 && isapproxinteger(c) ? c + 1 : c # avoid 0, for which the conversion is singular +end + struct Ultra2UltraPlanTH{T, Plans, Dims} <: Plan{T} plans::Plans λ₁::T @@ -268,15 +282,19 @@ struct Ultra2UltraPlanTH{T, Plans, Dims} <: Plan{T} dims::Dims end +Ultra2UltraPlanTH(plans, λ₁, λ₂, dims) = Ultra2UltraPlanTH(plans, promote(λ₁, λ₂)..., dims) + function *(P::Ultra2UltraPlanTH, A::AbstractArray) ret = A if isapproxinteger(P.λ₂ - P.λ₁) _ultra2ultra_integerinc!(ret, P.λ₁, P.λ₂, P.dims) else + λ = _ultra_th_start(P.λ₁) + _ultra2ultra_integerinc!(ret, P.λ₁, λ, P.dims) for p in P.plans ret = p*ret end - c = _nearest_jacobi_par(P.λ₁, P.λ₂) + c = _nearest_ultra_par(λ, P.λ₂) _ultra2ultra_integerinc!(ret, c, P.λ₂, P.dims) end @@ -308,13 +326,12 @@ end function plan_th_ultra2ultra!(::Type{S}, mn, λ₁, λ₂, dims) where {S} - c = _nearest_jacobi_par(λ₁, λ₂) - if isapproxinteger(λ₂ - λ₁) # TODO: don't make extra plan plans = typeof(_good_plan_th_ultra2ultra!(S, mn, λ₂+0.1, λ₂, dims))[] else - plans = [_good_plan_th_ultra2ultra!(S, mn, λ₁, c, dims)] + λ = _ultra_th_start(λ₁) + plans = [_good_plan_th_ultra2ultra!(S, mn, λ, _nearest_ultra_par(λ, λ₂), dims)] end Ultra2UltraPlanTH(plans, λ₁, λ₂, dims) diff --git a/test/chebyshevtests.jl b/test/chebyshevtests.jl index b5ea712e..63d98ce3 100644 --- a/test/chebyshevtests.jl +++ b/test/chebyshevtests.jl @@ -164,11 +164,11 @@ using FastTransforms, Test gcopy = copy(g) P = @inferred(plan_chebyshevutransform(f)) @test P*f ≈ g - @test f ≈ fcopy + @test f == fcopy # the input is not modified @test_throws ArgumentError P * T[1,2] P = @inferred(plan_chebyshevutransform(f, 1:1)) @test P*f ≈ g - @test f ≈ fcopy + @test f == fcopy @test_throws ArgumentError P * T[1,2] P = @inferred(plan_chebyshevutransform!(f)) @@ -238,11 +238,11 @@ using FastTransforms, Test gcopy = copy(g) P = @inferred(plan_chebyshevutransform(f, Val(2))) @test @inferred(P*f) ≈ g - @test f ≈ fcopy + @test f == fcopy # the input is not modified @test_throws ArgumentError P * T[1,2] P = @inferred(plan_chebyshevutransform(f, Val(2), 1:1)) @test @inferred(P*f) ≈ g - @test f ≈ fcopy + @test f == fcopy @test_throws ArgumentError P * T[1,2] P = @inferred(plan_chebyshevutransform!(f, Val(2))) @@ -310,6 +310,10 @@ using FastTransforms, Test @test @inferred(chebyshevutransform(X)) ≈ @inferred(chebyshevutransform!(copy(X))) ≈ chebyshevutransform(chebyshevutransform(X,1),2) @test @inferred(chebyshevutransform(X,Val(2))) ≈ @inferred(chebyshevutransform!(copy(X),Val(2))) ≈ chebyshevutransform(chebyshevutransform(X,Val(2),1),Val(2),2) + + X₀ = copy(X) + chebyshevutransform(X); chebyshevutransform(X,2); chebyshevutransform(X,Val(2)); chebyshevutransform(X,Val(2),1) + @test X == X₀ # the input is not modified end @testset "ichebyshevutransform" begin @@ -393,6 +397,10 @@ using FastTransforms, Test @test @inferred(chebyshevutransform(X)) ≈ @inferred(chebyshevutransform!(copy(X))) ≈ chebyshevutransform(chebyshevutransform(chebyshevutransform(X,1),2),3) @test @inferred(chebyshevutransform(X,Val(2))) ≈ @inferred(chebyshevutransform!(copy(X),Val(2))) ≈ chebyshevutransform(chebyshevutransform(chebyshevutransform(X,Val(2),1),Val(2),2),Val(2),3) + + X₀ = copy(X) + chebyshevutransform(X); chebyshevutransform(X,3); chebyshevutransform(X,Val(2)); chebyshevutransform(X,Val(2),2) + @test X == X₀ # the input is not modified end @testset "ichebyshevutransform" begin diff --git a/test/toeplitzhankeltests.jl b/test/toeplitzhankeltests.jl index 5e4589af..5ceae338 100644 --- a/test/toeplitzhankeltests.jl +++ b/test/toeplitzhankeltests.jl @@ -16,6 +16,13 @@ Random.seed!(0) @test th_ultra2ultra(x,0.1, 2.2) ≈ lib_ultra2ultra(x, 0.1, 2.2) @test th_ultra2ultra(x, 2.2, 0.1) ≈ lib_ultra2ultra(x, 2.2, 0.1) @test th_ultra2ultra(x, 1, 3) ≈ lib_ultra2ultra(x, 1, 3) + # converting via λ == 0 gave NaN, and negative starting parameters were inaccurate + @test th_ultra2ultra(x, 0.3, 1.0) ≈ lib_ultra2ultra(x, 0.3, 1.0) + @test th_ultra2ultra(x, 0.5, 2.0) ≈ lib_ultra2ultra(x, 0.5, 2.0) + @test th_ultra2ultra(x, -0.3, 1.0) ≈ lib_ultra2ultra(x, -0.3, 1.0) + @test th_ultra2ultra(x, -0.3, 0.5) ≈ lib_ultra2ultra(x, -0.3, 0.5) + @test th_ultra2ultra(x, -0.4, -0.3) ≈ lib_ultra2ultra(x, -0.4, -0.3) + @test th_ultra2ultra(x, 0.3, 1) ≈ th_ultra2ultra(x, 0.3, 1.0) # mixed parameter types @test @inferred(th_jac2jac(x,0.1, 0.2,0.1,0.4)) ≈ lib_jac2jac(x, 0.1, 0.2,0.1,0.4) @test th_jac2jac(x,0.1, 0.2,0.3,0.2) ≈ lib_jac2jac(x, 0.1, 0.2,0.3,0.2) @test th_jac2jac(x,0.1, 0.2,0.3,0.4) ≈ lib_jac2jac(x, 0.1, 0.2,0.3,0.4) @@ -47,6 +54,7 @@ Random.seed!(0) @test th_cheb2leg(th_leg2cheb(x)) ≈ x @test th_leg2cheb(th_cheb2leg(x)) ≈ x @test th_ultra2ultra(th_ultra2ultra(x, 0.1, 0.6), 0.6, 0.1) ≈ x + @test th_ultra2ultra(th_ultra2ultra(x, 1.0, 0.3), 0.3, 1.0) ≈ x @test th_jac2jac(th_jac2jac(x, 0.1, 0.6, 0.1, 0.8), 0.1, 0.8, 0.1, 0.6) ≈ x @test th_jac2jac(th_jac2jac(x, 0.1, 0.6, 0.2, 0.8), 0.2, 0.8, 0.1, 0.6) ≈ x end @@ -91,6 +99,12 @@ Random.seed!(0) @test th_ultra2ultra(th_ultra2ultra(X, 0.1, 0.6), 0.6, 0.1) ≈ X + for (λ, μ) in ((0.3, 1.0), (-0.3, 0.5)) + @test th_ultra2ultra(X, λ, μ, 1) ≈ hcat([ultra2ultra(X[:,j], λ, μ) for j=1:size(X,2)]...) + @test th_ultra2ultra(X, λ, μ, 2) ≈ vcat([permutedims(ultra2ultra(X[k,:], λ, μ)) for k=1:size(X,1)]...) + @test th_ultra2ultra(X, λ, μ) ≈ th_ultra2ultra(th_ultra2ultra(X, λ, μ, 1), λ, μ, 2) + end + @test th_jac2jac(X, 0.1, 0.6, 0.1, 0.8, 1) ≈ hcat([jac2jac(X[:,j], 0.1, 0.6, 0.1, 0.8) for j=1:size(X,2)]...) @test th_jac2jac(X, 0.1, 0.6, 0.1, 0.8, 2) ≈ vcat([permutedims(jac2jac(X[k,:], 0.1, 0.6, 0.1, 0.8)) for k=1:size(X,1)]...) @test th_jac2jac(X, 0.1, 0.6, 0.1, 0.8) ≈ th_jac2jac(th_jac2jac(X, 0.1, 0.6, 0.1, 0.8, 1), 0.1, 0.6, 0.1, 0.8, 2)