diff --git a/src/definitions.jl b/src/definitions.jl index 5fd7e1e..41df3e5 100644 --- a/src/definitions.jl +++ b/src/definitions.jl @@ -345,12 +345,11 @@ plan_irfft ############################################################################## """ - fftshift!(dest, src, [dim]) + fftshift!(dest, src, [dim]) Nonallocating version of [`fftshift`](@ref). Stores the result of the shift of the `src` array into the `dest` array. """ function fftshift!(dest, src, dim = 1:ndims(src)) - @assert size(dest)==size(src) s = ntuple(d -> d in dim ? div(size(dest,d),2) : 0, Val(ndims(dest))) circshift!(dest, src, s) end @@ -372,18 +371,14 @@ The output of `fftshift` is allocated. If one desires to store the output in a p """ fftshift -function fftshift(x, dim = 1:ndims(x)) - dest = similar(x) - fftshift!(dest, x, dim) -end +fftshift(x, dim = 1:ndims(x)) = fftshift!(similar(x), x, dim) """ - ifftshift!(dest, src, [dim]) + ifftshift!(dest, src, [dim]) Nonallocating version of [`ifftshift`](@ref). Stores the result of the shift of the `src` array into the `dest` array. """ function ifftshift!(dest, src, dim = 1:ndims(src)) - @assert size(dest)==size(src) s = ntuple(d -> d in dim ? -div(size(src,d),2) : 0, Val(ndims(src))) circshift!(dest, src, s) end @@ -405,10 +400,7 @@ The output of `ifftshift` is allocated. If one desires to store the output in a """ ifftshift -function ifftshift(x, dim = 1:ndims(x)) - dest = similar(x) - ifftshift!(dest, x, dim) -end +ifftshift(x, dim = 1:ndims(x)) = ifftshift!(similar(x), x, dim) ############################################################################## diff --git a/test/runtests.jl b/test/runtests.jl index 869c745..95c7c5d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -103,26 +103,26 @@ end @test @inferred(AbstractFFTs.fftshift([1 2 3])) == [3 1 2] @test @inferred(AbstractFFTs.fftshift([1, 2, 3])) == [3, 1, 2] @test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6])) == [6 4 5; 3 1 2] - a = [0 0 0] - b = [0, 0, 0] - c = [0 0 0; 0 0 0] - @test (AbstractFFTs.fftshift!(a, [1 2 3]); a == [3 1 2]) - @test (AbstractFFTs.fftshift!(b, [1, 2, 3]); b == [3, 1, 2]) + a = [0 0 0] + b = [0, 0, 0] + c = [0 0 0; 0 0 0] + @test (AbstractFFTs.fftshift!(a, [1 2 3]); a == [3 1 2]) + @test (AbstractFFTs.fftshift!(b, [1, 2, 3]); b == [3, 1, 2]) @test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6]); c == [6 4 5; 3 1 2]) @test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6], 1)) == [4 5 6; 1 2 3] @test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6], ())) == [1 2 3; 4 5 6] @test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6], (1,2))) == [6 4 5; 3 1 2] @test @inferred(AbstractFFTs.fftshift([1 2 3; 4 5 6], 1:2)) == [6 4 5; 3 1 2] - @test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], 1); c == [4 5 6; 1 2 3]) - @test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], ()); c == [1 2 3; 4 5 6]) - @test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], (1,2)); c == [6 4 5; 3 1 2]) - @test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], 1:2); c == [6 4 5; 3 1 2]) + @test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], 1); c == [4 5 6; 1 2 3]) + @test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], ()); c == [1 2 3; 4 5 6]) + @test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], (1,2)); c == [6 4 5; 3 1 2]) + @test (AbstractFFTs.fftshift!(c, [1 2 3; 4 5 6], 1:2); c == [6 4 5; 3 1 2]) @test @inferred(AbstractFFTs.ifftshift([1 2 3])) == [2 3 1] @test @inferred(AbstractFFTs.ifftshift([1, 2, 3])) == [2, 3, 1] @test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6])) == [5 6 4; 2 3 1] - @test (AbstractFFTs.ifftshift!(a, [1 2 3]); a == [2 3 1]) + @test (AbstractFFTs.ifftshift!(a, [1 2 3]); a == [2 3 1]) @test (AbstractFFTs.ifftshift!(b, [1, 2, 3]); b == [2, 3, 1]) @test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6]); c == [5 6 4; 2 3 1]) @@ -130,10 +130,10 @@ end @test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6], ())) == [1 2 3; 4 5 6] @test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6], (1,2))) == [5 6 4; 2 3 1] @test @inferred(AbstractFFTs.ifftshift([1 2 3; 4 5 6], 1:2)) == [5 6 4; 2 3 1] - @test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], 1); c == [4 5 6; 1 2 3]) - @test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], ()); c == [1 2 3; 4 5 6]) - @test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], (1,2)); c == [5 6 4; 2 3 1]) - @test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], 1:2); c == [5 6 4; 2 3 1]) + @test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], 1); c == [4 5 6; 1 2 3]) + @test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], ()); c == [1 2 3; 4 5 6]) + @test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], (1,2)); c == [5 6 4; 2 3 1]) + @test (AbstractFFTs.ifftshift!(c, [1 2 3; 4 5 6], 1:2); c == [5 6 4; 2 3 1]) end @testset "FFT Frequencies" begin