Skip to content

Commit

Permalink
Merge pull request #252 from ioannisPApapadopoulos/jp/arrays
Browse files Browse the repository at this point in the history
Extend FTPlans to Arrays and higher dimensions
  • Loading branch information
ioannisPApapadopoulos authored Sep 23, 2024
2 parents d8ccedd + 9aca321 commit b493372
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FastTransforms"
uuid = "057dd010-8810-581a-b7be-e3fc3b93f78c"
version = "0.16.4"
version = "0.16.5"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
1 change: 1 addition & 0 deletions src/FastTransforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ for f in (:jac2jac,
@eval $f(x::AbstractArray, y...; z...) = $lib_f(x, y...; z...)
end

include("arrays.jl")
# following use Toeplitz-Hankel to avoid expensive plans
# for f in (:leg2cheb, :cheb2leg, :ultra2ultra)
# th_f = Symbol("th_", f)
Expand Down
86 changes: 86 additions & 0 deletions src/arrays.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
struct ArrayPlan{T, FF<:FTPlan{<:T}, Szs<:Tuple, Dims<:Tuple{<:Int}} <: Plan{T}
F::FF
szs::Szs
dims::Dims
end
size(P::ArrayPlan) = P.szs

function ArrayPlan(F::FTPlan{<:T}, c::AbstractArray{T}, dims::Tuple{<:Int}=(1,)) where T
szs = size(c)
@assert F.n == szs[dims[1]]
ArrayPlan(F, size(c), dims)
end

function *(P::ArrayPlan, f::AbstractArray)
F, dims, szs = P.F, P.dims, P.szs
@assert length(dims) == 1
@assert szs == size(f)
d = first(dims)

perm = (d, ntuple(i-> i + (i >= d), ndims(f) -1)...)
fp = permutedims(f, perm)

fr = reshape(fp, size(fp,1), :)

permutedims(reshape(F*fr, size(fp)...), invperm(perm))
end

function \(P::ArrayPlan, f::AbstractArray)
F, dims, szs = P.F, P.dims, P.szs
@assert length(dims) == 1
@assert szs == size(f)
d = first(dims)

perm = (d, ntuple(i-> i + (i >= d), ndims(f) -1)...)
fp = permutedims(f, perm)

fr = reshape(fp, size(fp,1), :)

permutedims(reshape(F\fr, size(fp)...), invperm(perm))
end

struct NDimsPlan{T, FF<:ArrayPlan{<:T}, Szs<:Tuple, Dims<:Tuple} <: Plan{T}
F::FF
szs::Szs
dims::Dims
function NDimsPlan(F, szs, dims)
if length(Set(szs[[dims...]])) > 1
error("Different size in dims axes not yet implemented in N-dimensional transform.")
end
new{eltype(F), typeof(F), typeof(szs), typeof(dims)}(F, szs, dims)
end
end

size(P::NDimsPlan) = P.szs

function NDimsPlan(F::FTPlan, szs::Tuple, dims::Tuple)
NDimsPlan(ArrayPlan(F, szs, (first(dims),)), szs, dims)
end

function *(P::NDimsPlan, f::AbstractArray)
F, dims = P.F, P.dims
@assert size(P) == size(f)
g = copy(f)
t = 1:ndims(g)
d1 = dims[1]
for d in dims
perm = ntuple(k -> k == d1 ? t[d] : k == d ? t[d1] : t[k], ndims(g))
gp = permutedims(g, perm)
g = permutedims(F*gp, invperm(perm))
end
return g
end

function \(P::NDimsPlan, f::AbstractArray)
F, dims = P.F, P.dims
@assert size(P) == size(f)
g = copy(f)
t = 1:ndims(g)
d1 = dims[1]
for d in dims
perm = ntuple(k -> k == d1 ? t[d] : k == d ? t[d1] : t[k], ndims(g))
gp = permutedims(g, perm)
g = permutedims(F\gp, invperm(perm))
end
return g
end
64 changes: 64 additions & 0 deletions test/arraystests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using FastTransforms, Test
import FastTransforms: ArrayPlan, NDimsPlan

@testset "Array transform" begin
@testset "ArrayPlan" begin
c = randn(5,20,10)
F = plan_cheb2leg(c)
FT = ArrayPlan(F, c)

@test size(FT) == size(c)

f = similar(c);
for k in axes(c,3)
f[:,:,k] = (F*c[:,:,k])
end
@test f FT*c
@test c FT\f

F = plan_cheb2leg(Vector{Float64}(axes(c,2)))
FT = ArrayPlan(F, c, (2,))
for k in axes(c,3)
f[:,:,k] = (F*c[:,:,k]')'
end
@test f FT*c
@test c FT\f
end

@testset "NDimsPlan" begin
c = randn(20,10,20)
@test_throws ErrorException("Different size in dims axes not yet implemented in N-dimensional transform.") NDimsPlan(ArrayPlan(plan_cheb2leg(c), c), size(c), (1,2))

c = randn(5,20)
F = plan_cheb2leg(c)
FT = ArrayPlan(F, c)
P = NDimsPlan(F, size(c), (1,))
@test F*c FT*c P*c

c = randn(20,20,5);
F = plan_cheb2leg(c)
FT = ArrayPlan(F, c)
P = NDimsPlan(FT, size(c), (1,2))

@test size(P) == size(c)

f = similar(c);
for k in axes(f,3)
f[:,:,k] = (F*(F*c[:,:,k])')'
end
@test f P*c
@test c P\f

c = randn(5,10,10,60)
F = plan_cheb2leg(randn(10))
P = NDimsPlan(F, size(c), (2,3))
f = similar(c)
for i in axes(f,1), j in axes(f,4)
f[i,:,:,j] = (F*(F*c[i,:,:,j])')'
end
@test f P*c
@test c P\f
end
end


1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ include("clenshawtests.jl")
include("toeplitzplanstests.jl")
include("toeplitzhankeltests.jl")
include("symmetrictoeplitzplushankeltests.jl")
include("arraystests.jl")

2 comments on commit b493372

@ioannisPApapadopoulos
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/115814

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.16.5 -m "<description of version>" b493372bbc26704e002e9611939382e515d73c76
git push origin v0.16.5

Please sign in to comment.