Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.17.6"
version = "0.17.7"


[deps]
Expand Down
26 changes: 19 additions & 7 deletions src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,16 @@ transpose(P::SliceNDimsPlan) = NDimsPlan(transpose(P.F), P.szs, reverse(P.dims))


# dims for a transform acting on M-dimensional slices is a non-empty tuple of M-tuples,
# e.g. ((1,3),) or ((1,3),(2,4)) for M = 2. Returns the size of the slices.
# e.g. ((1,3),) or ((1,3),(2,4)) for M = 2, or a single M-tuple such as (1,3), which is
# shorthand for ((1,3),). The plans do not use NDimsPlan(F, szs, (1,3)) directly, since
# there a tuple of integers means applying a vector transform along each dimension.
_slicedims(dims::NTuple{M,Int}, ::Val{M}) where M = (dims,)
_slicedims(dims::Tuple, ::Val) = dims

# Returns the size of the slices for dims normalized by _slicedims.
function _slicesize(A::AbstractArray, dims::Tuple, M::Int)
if !(dims isa Tuple{NTuple{M,Int},Vararg{NTuple{M,Int}}})
throw(ArgumentError("dims must be a non-empty tuple of $M-tuples of dimensions such as $((ntuple(k -> 2k-1, M),)), got $dims"))
throw(ArgumentError("dims must be a $M-tuple of dimensions such as $(ntuple(k -> 2k-1, M)), or a non-empty tuple of them such as $((ntuple(k -> 2k-1, M),)), got $dims"))
end
_checkslicedims(size(A), dims)
return map(i -> size(A, i), first(dims))
Expand All @@ -201,11 +207,13 @@ end
function _dimsdoc(signature, description, M)
example = M == 2 ? "`dims = ((1,3),)` acts on `A[:,k,:]` for every `k`, and `dims = ((1,3), (2,4))` acts on `A[:,k,:,l]` for every `k` and `l`, then on `A[k,:,l,:]`" :
"`dims = ((1,2,4),)` acts on `A[:,:,k,:]` for every `k`"
single = M == 2 ? "`dims = (1,3)` is the same as `dims = ((1,3),)`" : "`dims = (1,2,4)` is the same as `dims = ((1,2,4),)`"
"""
$signature

Plan the $description on the slices of `A` spanned by each entry of `dims`, applied in
order. For example, $example. All slices must have the same size. See [`NDimsPlan`](@ref).
order. For example, $example. When the transform is applied once, the entry can be given
on its own, so $single. All slices must have the same size. See [`NDimsPlan`](@ref).
"""
end

Expand All @@ -221,8 +229,10 @@ for (plan_f, K, M, req, opt) in ((:plan_sph2fourier, SPHERE, 2, (), ()),
(:plan_spinsph2fourier, SPINSPHERE, 2, (:s,), ()))
for k in 0:length(opt)
params = (req..., opt[1:k]...)
ex = :($plan_f(A::AbstractArray{T}, $(params...), dims::Tuple) where T =
NDimsPlan($plan_f(T, first(_slicesize(A, dims, $M)), $(params...)), size(A), dims))
ex = :(function $plan_f(A::AbstractArray{T}, $(params...), dims::Tuple) where T
slicedims = _slicedims(dims, Val($M))
NDimsPlan($plan_f(T, first(_slicesize(A, slicedims, $M)), $(params...)), size(A), slicedims)
end)
if k == length(opt)
args = "A, " * join(map(p -> "$p, ", req)) * (isempty(opt) ? "" : "[" * join(opt, ", ") * ",] ") * "dims"
doc = _dimsdoc("$plan_f($args)", kind2string(K) * " transform", M)
Expand Down Expand Up @@ -253,6 +263,8 @@ for (plan_f, K, M, params) in ((:plan_sph_synthesis, SPHERESYNTHESIS, 2, ()),
(:plan_spinsph_analysis, SPINSPHEREANALYSIS, 2, (:s,)))
args = "A, " * join(map(p -> "$p, ", params)) * "dims; flags=FFTW.ESTIMATE"
doc = _dimsdoc("$plan_f($args)", kind2string(K), M)
@eval @doc $doc $plan_f(A::AbstractArray{T}, $(params...), dims::Tuple; kws...) where T =
NDimsPlan($plan_f(T, _slicesize(A, dims, $M)..., $(params...); kws...), size(A), dims)
@eval @doc $doc function $plan_f(A::AbstractArray{T}, $(params...), dims::Tuple; kws...) where T
slicedims = _slicedims(dims, Val($M))
NDimsPlan($plan_f(T, _slicesize(A, slicedims, $M)..., $(params...); kws...), size(A), slicedims)
end
end
23 changes: 22 additions & 1 deletion test/arraystests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,22 @@ import FastTransforms: ArrayPlan, NDimsPlan
@test P\B ≈ A
@test dot(P'*C, A) ≈ dot(C, P*A)

# a single entry is shorthand for a tuple containing it
A = randn(n, 3, m)
@test_throws ArgumentError plan_disk2cxf(A, (1,3))
P = plan_disk2cxf(A, α, β, (1,3))
@test P isa NDimsPlan
@test P.dims == ((1,3),)
@test P*A ≈ plan_disk2cxf(A, α, β, ((1,3),))*A
@test P\(P*A) ≈ A
@test plan_disk2cxf(A, (1,3))*A ≈ plan_disk2cxf(A, ((1,3),))*A
@test disk2cxf(A, α, β, (1,3)) ≈ P*A
@test cxf2disk(P*A, α, β, (1,3)) ≈ A
@test plan_disk2cxf(A[:,1,:], α, β, (1,2))*A[:,1,:] ≈ F*A[:,1,:]

@test_throws ArgumentError plan_disk2cxf(A, (1,))
@test_throws ArgumentError plan_disk2cxf(A, (1,2,3))
@test_throws ArgumentError plan_disk2cxf(A, (1,4))
@test_throws ArgumentError plan_disk2cxf(A, (1,1))
@test_throws ArgumentError plan_disk2cxf(A, ((1,4),))
@test_throws ArgumentError plan_disk2cxf(A, ((1,1),))
@test_throws ErrorException plan_disk2cxf(randn(n, n+1, m, m+4), ((1,3), (2,4)))
Expand All @@ -159,6 +173,9 @@ import FastTransforms: ArrayPlan, NDimsPlan
@test inv(pa)*A ≈ ps*A
@test inv(ps)*A ≈ pa*A
@test pa*complex.(A, A) ≈ complex.(Ba, Ba)
@test plan_disk_analysis(A, (1,3))*A ≈ Ba
@test plan_disk_synthesis(A, (1,3))*A ≈ Bs
@test plan_disk_analysis(A, (1,3); flags=FastTransforms.FFTW.MEASURE)*A ≈ Ba
for k in axes(A,2)
Ba[:,k,:] = Fa'*A[:,k,:]
end
Expand Down Expand Up @@ -188,6 +205,8 @@ import FastTransforms: ArrayPlan, NDimsPlan
P = plan_f(A, params..., dims)
F = plan_f(A[slice(1)...], params...)
@test P isa NDimsPlan
# a single entry is shorthand for a tuple containing it
@test plan_f(A, params..., only(dims))*A ≈ P*A
for (Q, G) in ((P, F), (P', F'), (transpose(P), transpose(F)))
B = similar(A)
for k in axes(A, findfirst(i -> i ∉ only(dims), 1:ndims(A)))
Expand Down Expand Up @@ -229,6 +248,7 @@ import FastTransforms: ArrayPlan, NDimsPlan
@testset "$plan_f" begin
test_slices(plan_f, params, randn(S, n, K, m), ((1,3),), k -> (:, k, :); inverse)
@test_throws ArgumentError plan_f(randn(S, n, K, m), params..., ((1,2,3),))
@test_throws ArgumentError plan_f(randn(S, n, K, m), params..., (1,2,3))
end
end

Expand All @@ -238,6 +258,7 @@ import FastTransforms: ArrayPlan, NDimsPlan
@testset "$plan_f" begin
test_slices(plan_f, params, randn(n, n, K, n), ((1,2,4),), k -> (:, :, k, :); inverse)
@test_throws ArgumentError plan_f(randn(n, n, K, n), params..., ((1,2),))
@test_throws ArgumentError plan_f(randn(n, n, K, n), params..., (1,2))
end
end

Expand Down
Loading