Skip to content
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

pass init to prod in normalization #81

Merged
merged 10 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 9 additions & 1 deletion src/definitions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,15 @@ summary(p::ScaledPlan) = string(p.scale, " * ", summary(p.p))
*(p::Plan, I::UniformScaling) = ScaledPlan(p, I.λ)

# Normalization for ifft, given unscaled bfft, is 1/prod(dimensions)
normalization(::Type{T}, sz, region) where T = one(T) / Int(prod(sz[r] for r in region))::Int
jishnub marked this conversation as resolved.
Show resolved Hide resolved
# ensure that region is a subset of eachindex(sz).
_checkindex(szinds, region::AbstractVector) = checkindex(Bool, szinds, region)
# this method handles the case where region is not an array, e.g. it is a Tuple
_checkindex(szinds, region) = all(r -> checkindex(Bool, szinds, r), region)
@inline function normalization(::Type{T}, sz, region) where T
@boundscheck (!isempty(region) && _checkindex(eachindex(sz), region)) ||
jishnub marked this conversation as resolved.
Show resolved Hide resolved
throw(BoundsError(sz, region))
one(T) / mapreduce(r -> Int(@inbounds sz[r])::Int, *, region; init=oneunit(eltype(sz)))::Int
jishnub marked this conversation as resolved.
Show resolved Hide resolved
end
normalization(X, region) = normalization(real(eltype(X)), size(X), region)

plan_ifft(x::AbstractArray, region; kws...) =
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ end
# p::TestPlan)
f9(p::Plan{T}, sz) where {T} = AbstractFFTs.normalization(real(T), sz, fftdims(p))
@test @inferred(f9(plan_fft(zeros(10), 1), 10)) == 1/10

@test_throws BoundsError AbstractFFTs.normalization(Float64, (2,), 1:3)
@test_throws BoundsError AbstractFFTs.normalization(Float64, (2,), Int[])
@test_throws BoundsError AbstractFFTs.normalization(Float64, (2,), (1,3,))
end

@testset "ChainRules" begin
Expand Down