-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend FTPlans to Arrays and higher dimensions #252
Merged
ioannisPApapadopoulos
merged 8 commits into
JuliaApproximation:master
from
ioannisPApapadopoulos:jp/arrays
Sep 23, 2024
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
04556c5
* and \ for FTPlan applied to an Array
16b6f43
N-dimensional plan
87b1924
type-inference
e0dd9d6
fix N-dimensional transform when dims[1] != 1
5bdb5e2
perm Vector -> perm Tuple
b17e76d
use Base.invperm
1fd17ca
size of plans
9aca321
bump version number
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
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 | ||
size(P::ArrayPlan, k::Int) = P.szs[k] | ||
size(P::ArrayPlan, k...) = P.szs[[k...]] | ||
|
||
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 | ||
size(P::NDimsPlan, k::Int) = P.szs[k] | ||
size(P::NDimsPlan, k...) = P.szs[[k...]] | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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) | ||
@test size(FT,1) == size(c,1) | ||
@test size(FT,1,2) == (size(c,1), size(c,2)) | ||
|
||
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) | ||
@test size(P,1) == size(c,1) | ||
@test size(P,1,2) == (size(c,1), size(c,2)) | ||
|
||
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the array construction in the indexing? Shouldn't this just be
P.szs[k...]
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've probably misunderstood what
k
is. I copied thesize(P, k...)
structure from size(P::JacobiTransformPlan, k...) = size(P.chebtransform, k...) and assumed it is a tuple. In which caseP.szs[k...]
does not work.e.g.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps I've misunderstood this. Are you trying to obtain multiple indices in one go?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the intention! Although I don't need that functionality in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps best to remove it then, as it's not the conventional usage of
size
. One may broadcastsize
over indices to obtain the sizes along multiple dimensions at once.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note sizes of plans and arrays behave differently
compare w FFT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
judging by this line here https://github.com/JuliaMath/FFTW.jl/blob/f888022d7a1ff78491abf8f33f1055cc52a68f0a/src/fft.jl#L254 I think I should only really keep
size(P::NDimsPlan) = P.szs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...or maybe I should keep
size(P::NDimsPlan, k::Int) = P.szs[k]
?