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

ScaledPlan backwards rule #1173

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 15 additions & 1 deletion src/lib/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,21 @@ end
@adjoint function \(P::AbstractFFTs.Plan, xs)
return P \ xs, function(Δ)
N = prod(size(Δ)[[P.region...]])
return (nothing, (P * Δ)/N)
return (nothing, (P * Δ) / N)
end
end

@adjoint function *(P::AbstractFFTs.ScaledPlan, xs)
return P * xs, function(Δ)
N = prod(size(xs)[[P.p.region...]])
return (nothing, N * P.scale^2 * (P \ Δ))
end
end

@adjoint function \(P::AbstractFFTs.ScaledPlan, xs)
return P \ xs, function(Δ)
N = prod(size(xs)[[P.p.region...]])
return (nothing, (P * Δ) / (N * P.scale^2))
end
end

Expand Down
42 changes: 20 additions & 22 deletions test/gradcheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1530,35 +1530,33 @@ end
mirrorI = mirrorIndex(i,sizeX[1])
FreqIndMat = findicateMat(mirrorI, j, size(X̂r,1), sizeX[2])
listOfSols = [(fft, bfft(indicateMat), bfft(indicateMat*im),
plan_fft(X), i, X),
(ifft, 1/N*fft(indicateMat), 1/N*fft(indicateMat*im),
plan_fft(X), i, X),
(bfft, fft(indicateMat), fft(indicateMat*im), nothing, i,
X),
(rfft, real.(brfft(FreqIndMat, sizeX[1])),
real.(brfft(FreqIndMat*im, sizeX[1])), plan_rfft(X),
mirrorI, X),
((K)->(irfft(K,sizeX[1])), 1/N * rfft(indicateMat),
zeros(size(X̂r)), plan_rfft(X), i, X̂r)]
for (trans, solRe, solIm, P, mI, evalX) in listOfSols
plan_fft(X), plan_ifft(X), i, X),
(ifft, 1/N*fft(indicateMat), 1/N*fft(indicateMat*im),
plan_ifft(X), plan_fft(X), i, X),
(bfft, fft(indicateMat), fft(indicateMat*im),
plan_bfft(X), nothing, i, X),
(rfft, real.(brfft(FreqIndMat, sizeX[1])),
real.(brfft(FreqIndMat*im, sizeX[1])), plan_rfft(X),
plan_irfft(X̂r, sizeX[1]), mirrorI, X),
((K)->(irfft(K, sizeX[1])), 1/N * rfft(indicateMat),
zeros(size(X̂r)), plan_irfft(X̂r, sizeX[1]),
plan_rfft(X), i, X̂r)] # 3 of the FFT plan tests fail for this case, hence the checks eltype(evalX) <: Real
for (trans, solRe, solIm, P, Pinv, mI, evalX) in listOfSols
@test oldgradient((X)->real.(trans(X))[mI, j], evalX)[1] ≈
solRe
@test oldgradient((X)->imag.(trans(X))[mI, j], evalX)[1] ≈
solIm
if typeof(P) <:AbstractFFTs.Plan && maximum(trans .== [fft,rfft])
@test oldgradient((X)->real.(P * X)[mI, j], evalX)[1] ≈
if typeof(P) <:AbstractFFTs.Plan
(eltype(evalX) <: Real) && @test oldgradient((X)->real.(P * X)[mI, j], evalX)[1] ≈
solRe
@test oldgradient((X)->imag.(P * X)[mI, j], evalX)[1] ≈
(eltype(evalX) <: Real) && @test oldgradient((X)->imag.(P * X)[mI, j], evalX)[1] ≈
solIm
elseif typeof(P) <: AbstractFFTs.Plan
@test oldgradient((X)->real.(P \ X)[mI, j], evalX)[1] ≈
end
if typeof(Pinv) <: AbstractFFTs.Plan
@test oldgradient((X)->real.(Pinv \ X)[mI, j], evalX)[1] ≈
solRe
# for whatever reason the rfft_plan doesn't handle this case well,
# even though irfft does
if eltype(evalX) <: Real
@test oldgradient((X)->imag.(P \ X)[mI, j], evalX)[1] ≈
solIm
end
(eltype(evalX) <: Real) && @test oldgradient((X)->imag.(Pinv \ X)[mI, j], evalX)[1] ≈
solIm
end
end
end
Expand Down