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

Correct inverse plan logic #69

Merged
merged 2 commits into from
Jul 3, 2022
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 src/definitions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ plan_ifft(x::AbstractArray, region; kws...) =
plan_ifft!(x::AbstractArray, region; kws...) =
ScaledPlan(plan_bfft!(x, region; kws...), normalization(x, region))

plan_inv(p::ScaledPlan) = ScaledPlan(plan_inv(p.p), inv(p.scale))
plan_inv(p::ScaledPlan) = ScaledPlan(inv(p.p), inv(p.scale))
Copy link
Member

Choose a reason for hiding this comment

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

So to understand this correctly: that was a bug in the package and when fixing it tests would fail currently (without the changes in test/testplans.jl)?

Copy link
Contributor Author

@gaurav-arya gaurav-arya Jul 2, 2022

Choose a reason for hiding this comment

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

That's right: changing this without making changes to the test plans caused tests to fail.

I'm not sure to what extent I'd call the original version a bug in the package, but it did lead to ignoring the pinv field of any plan inside a scaled plan: so for example given a plan P a new inverse plan would be made for 2 * P, 3 * P, 4 * P, etc., which is why the caches made by the test plans were just ignored.

Copy link
Member

Choose a reason for hiding this comment

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

OK. I just wanted to make sure that this change is covered by the tests, ie that reverting it would cause errors.


LinearAlgebra.mul!(y::AbstractArray, p::ScaledPlan, x::AbstractArray) =
LinearAlgebra.lmul!(p.scale, LinearAlgebra.mul!(y, p.p, x))
Expand Down
48 changes: 24 additions & 24 deletions test/testplans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,20 @@ end
function AbstractFFTs.plan_bfft(x::AbstractArray{T}, region; kwargs...) where {T}
return InverseTestPlan{T}(region, size(x))
end

function AbstractFFTs.plan_inv(p::TestPlan{T}) where {T}
unscaled_pinv = InverseTestPlan{T}(p.region, p.sz)
unscaled_pinv.pinv = p
pinv = AbstractFFTs.ScaledPlan(
unscaled_pinv, AbstractFFTs.normalization(T, p.sz, p.region),
)
N = AbstractFFTs.normalization(T, p.sz, p.region)
unscaled_pinv.pinv = AbstractFFTs.ScaledPlan(p, N)
pinv = AbstractFFTs.ScaledPlan(unscaled_pinv, N)
return pinv
end
function AbstractFFTs.plan_inv(p::InverseTestPlan{T}) where {T}
unscaled_pinv = TestPlan{T}(p.region, p.sz)
unscaled_pinv.pinv = p
pinv = AbstractFFTs.ScaledPlan(
unscaled_pinv, AbstractFFTs.normalization(T, p.sz, p.region),
)
return pinv
function AbstractFFTs.plan_inv(pinv::InverseTestPlan{T}) where {T}
unscaled_p = TestPlan{T}(pinv.region, pinv.sz)
N = AbstractFFTs.normalization(T, pinv.sz, pinv.region)
unscaled_p.pinv = AbstractFFTs.ScaledPlan(pinv, N)
p = AbstractFFTs.ScaledPlan(unscaled_p, N)
return p
end

# Just a helper function since forward and backward are nearly identical
Expand Down Expand Up @@ -118,22 +117,23 @@ function AbstractFFTs.plan_inv(p::TestRPlan{T,N}) where {T,N}
firstdim = first(p.region)::Int
d = p.sz[firstdim]
sz = ntuple(i -> i == firstdim ? d ÷ 2 + 1 : p.sz[i], Val(N))
_N = AbstractFFTs.normalization(T, p.sz, p.region)

unscaled_pinv = InverseTestRPlan{T}(d, p.region, sz)
unscaled_pinv.pinv = p
pinv = AbstractFFTs.ScaledPlan(
unscaled_pinv, AbstractFFTs.normalization(T, p.sz, p.region),
)
unscaled_pinv.pinv = AbstractFFTs.ScaledPlan(p, _N)
pinv = AbstractFFTs.ScaledPlan(unscaled_pinv, _N)
return pinv
end
function AbstractFFTs.plan_inv(p::InverseTestRPlan{T,N}) where {T,N}
firstdim = first(p.region)::Int
sz = ntuple(i -> i == firstdim ? p.d : p.sz[i], Val(N))
unscaled_pinv = TestRPlan{T}(p.region, sz)
unscaled_pinv.pinv = p
pinv = AbstractFFTs.ScaledPlan(
unscaled_pinv, AbstractFFTs.normalization(T, sz, p.region),
)
return pinv

function AbstractFFTs.plan_inv(pinv::InverseTestRPlan{T,N}) where {T,N}
firstdim = first(pinv.region)::Int
sz = ntuple(i -> i == firstdim ? pinv.d : pinv.sz[i], Val(N))
_N = AbstractFFTs.normalization(T, sz, pinv.region)

unscaled_p = TestRPlan{T}(pinv.region, sz)
unscaled_p.pinv = AbstractFFTs.ScaledPlan(pinv, _N)
p = AbstractFFTs.ScaledPlan(unscaled_p, _N)
return p
end

Base.size(p::TestRPlan) = p.sz
Expand Down