Skip to content

Commit 919fad8

Browse files
committed
work in itransform
1 parent 8bc78e5 commit 919fad8

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

src/chebyshevtransform.jl

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ struct IChebyshevTransformPlan{T,kind,K,inplace,N,R} <: ChebyshevPlan{T}
158158
IChebyshevTransformPlan{T,kind,K,inplace,N,R}() where {T,kind,K,inplace,N,R} = new{T,kind,K,inplace,N,R}()
159159
end
160160

161-
IChebyshevTransformPlan{T,kind,K}(F::FFTW.r2rFFTWPlan{T,K,inplace,N,R}) where {T,kind,K,inplace,N,R} =
161+
IChebyshevTransformPlan{T,kind,K}(F::FFTW.r2rFFTWPlan{T,K,inplace,N,R}) where {T,kind,K,inplace,N,R} =
162162
IChebyshevTransformPlan{T,kind,K,inplace,N,R}(F)
163163

164164
size(P::IChebyshevTransformPlan) = isdefined(P, :plan) ? size(P.plan) : (0,)
@@ -202,45 +202,69 @@ end
202202
plan_ichebyshevtransform!(x::AbstractArray, dims...; kws...) = plan_ichebyshevtransform!(x, Val(1), dims...; kws...)
203203
plan_ichebyshevtransform(x::AbstractArray, dims...; kws...) = plan_ichebyshevtransform(x, Val(1), dims...; kws...)
204204

205+
_icheb1_prerescale!(_, x::AbstractVector) = (x[1] *= 2)
206+
_icheb1_postrescale!(_, x::AbstractVector) = (x[1] /= 2)
207+
function _icheb1_prerescale!(d::Number, x::AbstractVector)
208+
lmul!(2, isone(d) ? view(x,:,1) : view(x,1,:))
209+
x
210+
end
211+
function _icheb1_postrescale!(_, x::AbstractVector)
212+
ldiv(2, isone(d) ? view(x,:,1) : view(x,1,:))
213+
x
214+
end
205215

206216
function *(P::IChebyshevTransformPlan{T,1,K,true}, x::AbstractVector{T}) where {T<:fftwNumber,K}
207217
n = length(x)
208218
n == 0 && return x
209219

210-
x[1] *= 2
211-
x = lmul!(one(T)/2, P.plan*x)
220+
_icheb1_prerescale!(P.plan.region, x)
221+
x = ldiv!(2^length(P.plan.region), P.plan*x)
212222
x
213-
end
223+
end
214224

215225
function mul!(y::AbstractVector{T}, P::IChebyshevTransformPlan{T,1,K,false}, x::AbstractVector{T}) where {T<:fftwNumber,K}
216226
n = length(x)
217227
length(y) == n || throw(DimensionMismatch("output must match dimension"))
218228
n == 0 && return y
219229

220-
x[1] *= 2 # Todo: don't mutate x
230+
_icheb1_prerescale!(P.plan.region, x) # Todo: don't mutate x
221231
_plan_mul!(y, P.plan, x)
222-
x[1] /= 2
223-
lmul!(one(T)/2, y)
224-
end
232+
_icheb1_postrescale!(P.plan.region, x)
233+
ldiv!(2^length(P.plan.region), y)
234+
end
235+
236+
_icheb2_prerescale!(_, x::AbstractVector) = (x[1] *= 2; x[end] *= 2)
237+
_icheb2_postrescale!(_, x::AbstractVector) = (x[1] /= 2; x[end] /= 2)
238+
function _icheb2_rescale!(d, y::AbstractVector)
239+
_icheb2_prerescale!(d, y)
240+
lmul!(convert(T, prod(size(y) .- 1))/2, y)
241+
y
242+
end
243+
function _icheb2_prerescale!(d::Number, x::AbstractVector)
244+
lmul!(2, isone(d) ? view(x,:,1) : view(x,1,:))
245+
x
246+
end
247+
function _icheb2_postrescale!(_, x::AbstractVector)
248+
ldiv(2, isone(d) ? view(x,:,1) : view(x,1,:))
249+
x
250+
end
225251

226252
function *(P::IChebyshevTransformPlan{T,2,K, true}, x::AbstractVector{T}) where {T<:fftwNumber,K}
227253
n = length(x)
228254

229-
x[1] *= 2; x[end] *= 2
255+
_icheb2_prerescale!(P.plan.region, x)
230256
x = inv(P)*x
231-
x[1] *= 2; x[end] *= 2
232-
lmul!(convert(T,n-1)/2,x)
257+
_icheb2_rescale!(P.plan.region, x)
233258
end
234259

235260
function mul!(y::AbstractVector{T}, P::IChebyshevTransformPlan{T,2,K,false}, x::AbstractVector{T}) where {T<:fftwNumber,K}
236261
n = length(x)
237262
length(y) == n || throw(DimensionMismatch("output must match dimension"))
238263

239-
x[1] *= 2; x[end] *= 2
264+
_icheb2_prerescale!(P.plan.region, x)
240265
_plan_mul!(y, inv(P), x)
241-
x[1] /= 2; x[end] /= 2
242-
y[1] *= 2; y[end] *= 2
243-
lmul!(convert(T,(n-1))/2,y)
266+
_icheb2_postrescale!(P.plan.region, x)
267+
_icheb2_rescale!(P.plan.region, y)
244268
end
245269

246270
*(P::IChebyshevTransformPlan{T,kind,K,false},x::AbstractVector{T}) where {T,kind,K} = mul!(similar(x), P, convert(Array,x))
@@ -256,10 +280,10 @@ struct ChebyshevUTransformPlan{T,kind,inplace,P} <: ChebyshevPlan{T}
256280
ChebyshevUTransformPlan{T,kind,inplace,P}() where {T,kind,inplace,P} = new{T,kind,inplace,P}()
257281
end
258282

259-
ChebyshevUTransformPlan{T,kind,inplace}(plan::FFTW.r2rFFTWPlan{T,P}) where {T,kind,inplace,P} =
283+
ChebyshevUTransformPlan{T,kind,inplace}(plan::FFTW.r2rFFTWPlan{T,P}) where {T,kind,inplace,P} =
260284
ChebyshevUTransformPlan{T,kind,inplace,P}(plan)
261285

262-
ChebyshevUTransformPlan{T,kind,inplace}(plan::ChebyshevUTransformPlan{T,kind,inp,P}) where {T,kind,inplace,inp,P} =
286+
ChebyshevUTransformPlan{T,kind,inplace}(plan::ChebyshevUTransformPlan{T,kind,inp,P}) where {T,kind,inplace,inp,P} =
263287
ChebyshevUTransformPlan{T,kind,inplace,P}(plan.plan)
264288

265289

@@ -336,10 +360,10 @@ struct IChebyshevUTransformPlan{T,kind,inplace,P} <: ChebyshevPlan{T}
336360
IChebyshevUTransformPlan{T,kind,inplace,P}() where {T,kind,inplace,P} = new{T,kind,inplace,P}()
337361
end
338362

339-
IChebyshevUTransformPlan{T,kind,inplace}(F::FFTW.r2rFFTWPlan{T,P}) where {T,kind,inplace,P} =
363+
IChebyshevUTransformPlan{T,kind,inplace}(F::FFTW.r2rFFTWPlan{T,P}) where {T,kind,inplace,P} =
340364
IChebyshevUTransformPlan{T,kind,inplace,P}(F)
341365

342-
IChebyshevUTransformPlan{T,kind,true}(F::IChebyshevUTransformPlan{T,kind,false,P}) where {T,kind,P} =
366+
IChebyshevUTransformPlan{T,kind,true}(F::IChebyshevUTransformPlan{T,kind,false,P}) where {T,kind,P} =
343367
IChebyshevUTransformPlan{T,kind,true,P}(F.plan)
344368

345369
function plan_ichebyshevutransform!(x::AbstractVector{T}, ::Val{1}) where T<:fftwNumber
@@ -401,7 +425,7 @@ ichebyshevutransform!(x::AbstractVector{T}, kind=Val(1)) where {T<:fftwNumber} =
401425

402426
ichebyshevutransform(x, kind=Val(1)) = ichebyshevutransform!(Array(x), kind)
403427

404-
*(P::IChebyshevUTransformPlan{T,k,false},x::AbstractVector{T}) where {T,k} =
428+
*(P::IChebyshevUTransformPlan{T,k,false},x::AbstractVector{T}) where {T,k} =
405429
IChebyshevUTransformPlan{T,k,true}(P)*Array(x)
406430

407431

0 commit comments

Comments
 (0)