# FFT with @view

**URL:** <https://discourse.julialang.org/t/fft-with-view/123137>\
**Category:** General Usage\
**Tags:** question, fftw, linearalgebra\
**Created:** [November 27, 2024, 1:52am UTC](https://discourse.julialang.org/t/fft-with-view/123137 "2024-11-27T01:52:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AshtonSBradley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashtonsbradley/32/11140_2.png) [@AshtonSBradley](https://discourse.julialang.org/u/AshtonSBradley)\
**Post date:** [November 27, 2024, 1:52am UTC](https://discourse.julialang.org/t/fft-with-view/123137/1 "2024-11-27T01:52:22Z")

</div>

I want to do fft and want to write the result on a predefined array. However, there is a problem with this when the source and target arrays are both views:

```julia
using FFTW, LinearAlgebra
M = randn(100,100,100)
C = @view M[:,:,1]
B = plan_fft(C)
A = @view M[:,:,2]

mul!(A,B,C)

ERROR: MethodError: no method matching mul!(::SubArray{…}, ::FFTW.cFFTWPlan{…}, ::SubArray{…}, ::Bool, ::Bool)
The function `mul!` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  mul!(::AbstractVecOrMat, ::UniformScaling, ::AbstractVecOrMat, ::Number, ::Number)
   @ LinearAlgebra ~/.julia/juliaup/julia-1.11.1+0.x64.apple.darwin14/share/julia/stdlib/v1.11/LinearAlgebra/src/uniformscaling.jl:285
  mul!(::AbstractMatrix, ::AbstractVecOrMat, ::AbstractVecOrMat, ::Number, ::Number)
   @ LinearAlgebra ~/.julia/juliaup/julia-1.11.1+0.x64.apple.darwin14/share/julia/stdlib/v1.11/LinearAlgebra/src/matmul.jl:285
  mul!(::AbstractArray, ::Number, ::AbstractArray, ::Number, ::Number)
   @ LinearAlgebra ~/.julia/juliaup/julia-1.11.1+0.x64.apple.darwin14/share/julia/stdlib/v1.11/LinearAlgebra/src/generic.jl:132
  ...

Stacktrace:
 [1] mul!(C::SubArray{…}, A::FFTW.cFFTWPlan{…}, B::SubArray{…})
   @ LinearAlgebra ~/.julia/juliaup/julia-1.11.1+0.x64.apple.darwin14/share/julia/stdlib/v1.11/LinearAlgebra/src/matmul.jl:253
 [2] top-level scope
   @ REPL[6]:1
Some type information was truncated. Use `show(err)` to see complete types.

```

However, I can do mul!(A,B,C) if A and C are regular arrays.

Is there a way to do this using mul! ? I know there is an in place fft plan one can make, but that doesn’t suit my use case. I need this mul! approach to work otherwise I will redesign the code to avoid @view.

---

<div class="post-metadata">

**Author:** ![jmert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmert/32/3161_2.png) [@jmert](https://discourse.julialang.org/u/jmert)\
**Post date:** [November 27, 2024, 2:37am UTC](https://discourse.julialang.org/t/fft-with-view/123137/2 "2024-11-27T02:37:09Z")

</div>

Fundamentally, you need a complex destination for your FFT, and then the docs for the AbstractFFT interface make it clear that the [`mul!` interface requires both input and output to be complex arrays](https://juliamath.github.io/AbstractFFTs.jl/stable/api/#AbstractFFTs.plan_fft):

```julia
julia> using FFTW, LinearAlgebra

julia> M = randn(100, 100, 100);

julia> Mc = complex(M);

julia> C = @view Mc[:, :, 1];

julia> A = @view Mc[:, :, 2];

julia> B = plan_fft(C);

julia> mul!(A, B, C)
100×100 view(::Array{ComplexF64, 3}, :, :, 2) with eltype ComplexF64:
...

```

---

<div class="post-metadata">

**Author:** ![AshtonSBradley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashtonsbradley/32/11140_2.png) [@AshtonSBradley](https://discourse.julialang.org/u/AshtonSBradley)\
**Post date:** [November 27, 2024, 4:34am UTC](https://discourse.julialang.org/t/fft-with-view/123137/3 "2024-11-27T04:34:15Z")

</div>

True, a hastily distilled example. Thank you. But in the problem code the target is complex. I will have to revisit … I must have misdiagnosed the issue

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [November 27, 2024, 8:38am UTC](https://discourse.julialang.org/t/fft-with-view/123137/4 "2024-11-27T08:38:08Z")

</div>

maybe you are using `plan_rfft` and you see something similar to

> [@\`plan\_rfft\` together used with \`mul!\` and \`@view\` complains about wrong-strides-output](https://discourse.julialang.org/t/plan-rfft-together-used-with-mul-and-view-complains-about-wrong-strides-output/58215):
>
> Hey, initially I wanted to have rfft! but since it doesn’t exist, I tried the workaround mentioned [in this discourse discussion](https://discourse.julialang.org/t/in-place-rfft-with-padded-arrays/3340). However, mul! complains when used with a plan\_rfft generated matrix: using LinearAlgebra, FFTW function main() # array we want to process x = randn((60,40)) y = rfft(randn((80,60))) # just create a view storage y2 = view(y, 1:31, 1:40) p = plan\_rfft(x) @time y2 .= p \* x; # this still allocates memory because of p \* x @t…

---

<div class="post-metadata">

**Author:** ![AshtonSBradley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashtonsbradley/32/11140_2.png) [@AshtonSBradley](https://discourse.julialang.org/u/AshtonSBradley)\
**Post date:** [November 29, 2024, 2:09am UTC](https://discourse.julialang.org/t/fft-with-view/123137/5 "2024-11-29T02:09:37Z")

</div>

thank you!
