-
Notifications
You must be signed in to change notification settings - Fork 36
Chain rules for FFT plans via AdjointPlans #67
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
Changes from 22 commits
ad71816
c91ad50
061eef9
497ff4d
5d5c06c
ef84edf
d7ff394
aa8e575
9d99886
ac7c78c
8474141
769c090
3ed83df
552d49f
1e9ece2
8ddfa97
87758c8
09b8b38
d967aa2
2a423e2
eedba14
25bb86b
2a2d685
fe3b06a
266c88f
403ce47
e137ae3
e601347
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ eltype(::Type{<:Plan{T}}) where {T} = T | |
|
||
# size(p) should return the size of the input array for p | ||
size(p::Plan, d) = size(p)[d] | ||
output_size(p::Plan, d) = output_size(p)[d] | ||
ndims(p::Plan) = length(size(p)) | ||
length(p::Plan) = prod(size(p))::Int | ||
|
||
|
@@ -255,6 +256,7 @@ ScaledPlan(p::Plan{T}, scale::Number) where {T} = ScaledPlan{T}(p, scale) | |
ScaledPlan(p::ScaledPlan, α::Number) = ScaledPlan(p.p, p.scale * α) | ||
|
||
size(p::ScaledPlan) = size(p.p) | ||
output_size(p::ScaledPlan) = output_size(p.p) | ||
|
||
fftdims(p::ScaledPlan) = fftdims(p.p) | ||
|
||
|
@@ -576,3 +578,78 @@ Pre-plan an optimized real-input unnormalized transform, similar to | |
the same as for [`brfft`](@ref). | ||
""" | ||
plan_brfft | ||
|
||
############################################################################## | ||
|
||
struct NoProjectionStyle end | ||
struct RealProjectionStyle end | ||
struct RealInverseProjectionStyle | ||
dim::Int | ||
end | ||
const ProjectionStyle = Union{NoProjectionStyle, RealProjectionStyle, RealInverseProjectionStyle} | ||
|
||
output_size(p::Plan) = _output_size(p, ProjectionStyle(p)) | ||
_output_size(p::Plan, ::NoProjectionStyle) = size(p) | ||
_output_size(p::Plan, ::RealProjectionStyle) = rfft_output_size(size(p), fftdims(p)) | ||
_output_size(p::Plan, s::RealInverseProjectionStyle) = brfft_output_size(size(p), s.dim, fftdims(p)) | ||
|
||
struct AdjointPlan{T,P<:Plan} <: Plan{T} | ||
p::P | ||
AdjointPlan{T,P}(p) where {T,P} = new(p) | ||
end | ||
|
||
""" | ||
p' | ||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
adjoint(p::Plan) | ||
|
||
Form the adjoint operator of an FFT plan. Returns a plan which performs the adjoint operation | ||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
the original plan. Note that this differs from the corresponding backwards plan in the case of real | ||
FFTs due to the halving of one of the dimensions of the FFT output, as described in [`rfft`](@ref). | ||
|
||
!!! note | ||
Adjoint plans do not currently support `LinearAlgebra.mul!`. Further, as a new addition to `AbstractFFTs`, | ||
coverage of `Base.adjoint` in downstream implementations may be limited. | ||
""" | ||
Base.adjoint(p::Plan{T}) where {T} = AdjointPlan{T, typeof(p)}(p) | ||
Base.adjoint(p::AdjointPlan) = p.p | ||
# always have AdjointPlan inside ScaledPlan. | ||
Base.adjoint(p::ScaledPlan) = ScaledPlan(p.p', p.scale) | ||
|
||
size(p::AdjointPlan) = output_size(p.p) | ||
output_size(p::AdjointPlan) = size(p.p) | ||
|
||
Base.:*(p::AdjointPlan, x::AbstractArray) = _mul(p, x, ProjectionStyle(p.p)) | ||
|
||
function _mul(p::AdjointPlan{T}, x::AbstractArray, ::NoProjectionStyle) where {T} | ||
dims = fftdims(p.p) | ||
N = normalization(T, size(p.p), dims) | ||
return (p.p \ x) / N | ||
end | ||
|
||
function _mul(p::AdjointPlan{T}, x::AbstractArray, ::RealProjectionStyle) where {T<:Real} | ||
dims = fftdims(p.p) | ||
N = normalization(T, size(p.p), dims) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's right, since we should only expect an Also, regarding the use of |
||
halfdim = first(dims) | ||
d = size(p.p, halfdim) | ||
n = output_size(p.p, halfdim) | ||
scale = reshape( | ||
[(i == 1 || (i == n && 2 * (i - 1)) == d) ? N : 2 * N for i in 1:n], | ||
ntuple(i -> i == halfdim ? n : 1, Val(ndims(x))) | ||
) | ||
return p.p \ (x ./ scale) | ||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
function _mul(p::AdjointPlan{T}, x::AbstractArray, ::RealInverseProjectionStyle) where {T} | ||
dims = fftdims(p.p) | ||
N = normalization(real(T), output_size(p.p), dims) | ||
halfdim = first(dims) | ||
n = size(p.p, halfdim) | ||
d = output_size(p.p, halfdim) | ||
scale = reshape( | ||
[(i == 1 || (i == n && 2 * (i - 1)) == d) ? 1 : 2 for i in 1:n], | ||
ntuple(i -> i == halfdim ? n : 1, Val(ndims(x))) | ||
) | ||
return scale ./ N .* (p.p \ x) | ||
devmotion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
inv(p::AdjointPlan) = adjoint(inv(p.p)) |
Uh oh!
There was an error while loading. Please reload this page.