Skip to content

Commit 8bc78e5

Browse files
committed
2nd kind matrix transforms
1 parent cdaaf23 commit 8bc78e5

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/chebyshevtransform.jl

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,41 @@ function mul!(y::AbstractArray{T}, P::ChebyshevTransformPlan{T,1,K,false}, x::Ab
8989
_cheb1_rescale!(P.plan.region, y)
9090
end
9191

92-
function *(P::ChebyshevTransformPlan{T,2,K,true}, x::AbstractVector{T}) where {T,K}
92+
93+
_cheb2_rescale!(_, y::AbstractVector) = (y[1] /= 2; y[end] /= 2; ldiv!(length(y)-1, y))
94+
95+
function _cheb2_rescale!(d::Number, y::AbstractMatrix{T}) where T
96+
if isone(d)
97+
ldiv!(2, @view(y[1,:]))
98+
ldiv!(2, @view(y[end,:]))
99+
else
100+
ldiv!(2, @view(y[:,1]))
101+
ldiv!(2, @view(y[:,end]))
102+
end
103+
ldiv!(size(y,d)-1, y)
104+
end
105+
106+
# TODO: higher dimensional arrays
107+
function _cheb2_rescale!(d::UnitRange, y::AbstractMatrix{T}) where T
108+
@assert d == 1:2
109+
ldiv!(2, @view(y[1,:]))
110+
ldiv!(2, @view(y[end,:]))
111+
ldiv!(2, @view(y[:,1]))
112+
ldiv!(2, @view(y[:,end]))
113+
ldiv!(prod(size(y) .- 1), y)
114+
end
115+
116+
function *(P::ChebyshevTransformPlan{T,2,K,true}, x::AbstractArray{T}) where {T,K}
93117
n = length(x)
94118
y = P.plan*x # will be === x if in-place
95-
y[1] /= 2; y[end] /= 2
96-
lmul!(inv(convert(T,n-1)),y)
119+
_cheb2_rescale!(P.plan.region, y)
97120
end
98121

99-
function mul!(y::AbstractVector{T}, P::ChebyshevTransformPlan{T,2,K,false}, x::AbstractVector) where {T,K}
122+
function mul!(y::AbstractArray{T}, P::ChebyshevTransformPlan{T,2,K,false}, x::AbstractArray) where {T,K}
100123
n = length(x)
101124
length(y) == n || throw(DimensionMismatch("output must match dimension"))
102125
_plan_mul!(y, P.plan, x)
103-
y[1] /= 2; y[end] /= 2
104-
lmul!(inv(convert(T,n-1)),y)
126+
_cheb2_rescale!(P.plan.region, y)
105127
end
106128

107129
*(P::ChebyshevTransformPlan{T,kind,K,false}, x::AbstractArray{T}) where {T,kind,K} =

test/chebyshevtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,11 @@ using FastTransforms, Test
176176
X = randn(4,5)
177177
@test @inferred(chebyshevtransform(X,1)) @inferred(chebyshevtransform!(copy(X),1)) hcat(chebyshevtransform.([X[:,k] for k=axes(X,2)])...)
178178
@test chebyshevtransform(X,2) chebyshevtransform!(copy(X),2) hcat(chebyshevtransform.([X[k,:] for k=axes(X,1)])...)'
179+
@test @inferred(chebyshevtransform(X,Val(2),1)) @inferred(chebyshevtransform!(copy(X),Val(2),1)) hcat(chebyshevtransform.([X[:,k] for k=axes(X,2)],Val(2))...)
180+
@test chebyshevtransform(X,Val(2),2) chebyshevtransform!(copy(X),Val(2),2) hcat(chebyshevtransform.([X[k,:] for k=axes(X,1)],Val(2))...)'
179181

180-
@test @inferred(chebyshevtransform(X)) == @inferred(chebyshevtransform!(copy(X))) == chebyshevtransform(chebyshevtransform(X,1),2)
182+
@test @inferred(chebyshevtransform(X)) @inferred(chebyshevtransform!(copy(X))) chebyshevtransform(chebyshevtransform(X,1),2)
183+
@test @inferred(chebyshevtransform(X,Val(2))) @inferred(chebyshevtransform!(copy(X),Val(2))) chebyshevtransform(chebyshevtransform(X,Val(2),1),Val(2),2)
181184

182185

183186
X = randn(1,1)

0 commit comments

Comments
 (0)