Skip to content

Commit 04556c5

Browse files
author
ioannisPApapadopoulos
committed
* and \ for FTPlan applied to an Array
1 parent d8ccedd commit 04556c5

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/FastTransforms.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ for f in (:jac2jac,
128128
@eval $f(x::AbstractArray, y...; z...) = $lib_f(x, y...; z...)
129129
end
130130

131+
include("arrays.jl")
131132
# following use Toeplitz-Hankel to avoid expensive plans
132133
# for f in (:leg2cheb, :cheb2leg, :ultra2ultra)
133134
# th_f = Symbol("th_", f)

src/arrays.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
struct ArrayPlan{T, F<:FTPlan{<:T}, Szs<:Tuple, Dims<:Tuple{<:Int}} <: Plan{T}
2+
F::F
3+
szs::Szs
4+
dims::Dims
5+
end
6+
size(P::ArrayPlan, k...) = P.szs[[k...]]
7+
8+
function ArrayPlan(F::FTPlan{<:T}, c::AbstractArray{T}, dims::Tuple{<:Int}=(1,)) where T
9+
szs = size(c)
10+
@assert F.n == szs[dims[1]]
11+
ArrayPlan(F, size(c), dims)
12+
end
13+
14+
function inv_perm(d::Vector{<:Int})
15+
inv_d = Vector{Int}(undef, length(d))
16+
for (i, val) in enumerate(d)
17+
inv_d[val] = i
18+
end
19+
return inv_d
20+
end
21+
22+
function *(P::ArrayPlan, f::AbstractArray)
23+
F, dims, szs = P.F, P.dims, P.szs
24+
@assert length(dims) == 1
25+
@assert szs == size(f)
26+
d = first(dims)
27+
28+
perm = [d; setdiff(1:ndims(f), d)]
29+
fp = permutedims(f, perm)
30+
31+
fr = reshape(fp, size(fp,1), prod(size(fp)[2:end]))
32+
33+
permutedims(reshape(F*fr, size(fp)...), inv_perm(perm))
34+
end
35+
36+
function \(P::ArrayPlan, f::AbstractArray)
37+
F, dims, szs = P.F, P.dims, P.szs
38+
@assert length(dims) == 1
39+
@assert szs == size(f)
40+
d = first(dims)
41+
42+
perm = [d; setdiff(1:ndims(f), d)]
43+
fp = permutedims(f, perm)
44+
45+
fr = reshape(fp, size(fp,1), prod(size(fp)[2:end]))
46+
47+
permutedims(reshape(F\fr, size(fp)...), inv_perm(perm))
48+
end

test/arraystests.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using FastTransforms, Test
2+
import FastTransforms: ArrayPlan
3+
4+
@testset "Array transform" begin
5+
c = randn(5,20,10)
6+
F = plan_cheb2leg(c)
7+
FT = ArrayPlan(F, c)
8+
9+
f = similar(c);
10+
for k in axes(c,3)
11+
f[:,:,k] = (F*c[:,:,k])
12+
end
13+
@test f FT*c
14+
@test c FT\f
15+
16+
F = plan_cheb2leg(Vector{Float64}(axes(c,2)))
17+
FT = ArrayPlan(F, c, (2,))
18+
for k in axes(c,3)
19+
f[:,:,k] = (F*c[:,:,k]')'
20+
end
21+
@test f FT*c
22+
@test c FT\f
23+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ include("clenshawtests.jl")
1212
include("toeplitzplanstests.jl")
1313
include("toeplitzhankeltests.jl")
1414
include("symmetrictoeplitzplushankeltests.jl")
15+
include("arraystests.jl")

0 commit comments

Comments
 (0)