# Why I can't use mul! and plan\_fft together?

**URL:** <https://discourse.julialang.org/t/why-i-cant-use-mul-and-plan-fft-together/75186>\
**Category:** New to Julia\
**Tags:** question, fftw, bug, linearalgebra\
**Created:** [January 25, 2022, 5:12pm UTC](https://discourse.julialang.org/t/why-i-cant-use-mul-and-plan-fft-together/75186 "2022-01-25T17:12:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![liangqimin1998](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liangqimin1998/32/33172_2.png) [@liangqimin1998](https://discourse.julialang.org/u/liangqimin1998)\
**Post date:** [January 25, 2022, 5:12pm UTC](https://discourse.julialang.org/t/why-i-cant-use-mul-and-plan-fft-together/75186/1 "2022-01-25T17:12:22Z")

</div>

Hi, I just started to use Julia recently, and when I try to use mul! and plan\_fft to get better performance, I always get a bug that I can’t deal with  
My input is

```julia
u0=zeros(nz,nr)
for i in 1:nz
    for j in 1:nr
        R=sqrt((6*((i*h)-1))^2+((j-0.5)*h)^2)
        u0[i,j]=tanh((1.2-R)/(sqrt(2)*e))
    end
end
FFT=plan_fft(u0)
up=similar(u0);
mul!(up,FFT,u0);

```

and Julia tells me that

```julia
MethodError: no method matching mul!(::Matrix{Float64}, ::FFTW.cFFTWPlan{ComplexF64, -1, false, 2, UnitRange{Int64}}, ::Matrix{Float64}, ::Bool, ::Bool)

```

I’ve tired to search everywhere, but it seems no one has experienced similar situation like me.

---

<div class="post-metadata">

**Author:** ![RGerzaguet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgerzaguet/32/45492_2.png) [@RGerzaguet](https://discourse.julialang.org/u/RGerzaguet)\
**Post date:** [January 25, 2022, 5:30pm UTC](https://discourse.julialang.org/t/why-i-cant-use-mul-and-plan-fft-together/75186/2 "2022-01-25T17:30:22Z")

</div>

It lacks some parameters to be sure but can you try with setting `up` as Complex ? The output of the FFT is indeed a complex vector and you initialise a Float64.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [January 25, 2022, 10:33pm UTC](https://discourse.julialang.org/t/why-i-cant-use-mul-and-plan-fft-together/75186/3 "2022-01-25T22:33:00Z")

</div>

See the `plan_fft` docs:

> To apply P to an array A, use P \* A; in general, the syntax for applying plans is much like that of matrices. (A plan can only be applied to arrays of the same size as the A for which the plan was created.) You can also apply a plan with a preallocated output array Â by calling mul!(Â, plan, A). **(For mul!, however, the input array A must be a complex floating-point array like the output Â.)** You can compute the inverse-transform plan by inv(P) and apply the inverse plan with P \ Â (the inverse plan is cached and reused for subsequent calls to inv or ), and apply the inverse plan to a pre-allocated output array A with ldiv!(A, P, Â).

Note that for a real-valued input array, you may be able to achieve better performance with `rfft`/`plan_rfft`, since a FFT on real input produces Hermitian-symmetric output with redundant negative frequency terms.

edit: for 1024 x 1024 real input,

```julia
julia> @btime mul!($û_rfft, $p_rfft, $u)
  3.237 ms (0 allocations: 0 bytes)

julia> @btime mul!($û_fft, $p_fft, $u_complex)
  22.086 ms (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![liangqimin1998](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liangqimin1998/32/33172_2.png) [@liangqimin1998](https://discourse.julialang.org/u/liangqimin1998)\
**Post date:** [January 26, 2022, 12:36pm UTC](https://discourse.julialang.org/t/why-i-cant-use-mul-and-plan-fft-together/75186/4 "2022-01-26T12:36:59Z")

</div>

Thank you for your advice, after turning the input array into a complex array, everything seems to work fine. I also realized that rift is much faster than fft, but since I need to get full wave numbers for my simulation, I have to stick on fft this time.🙂

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 26, 2022, 12:59pm UTC](https://discourse.julialang.org/t/why-i-cant-use-mul-and-plan-fft-together/75186/5 "2022-01-26T12:59:02Z")

</div>

Instead of trying to anticipate the eltype of the output array, can’t you just do

```julia
up = FFT * u0

```

?  
You’re not saving on allocating the output anyway, at least unless you reuse `up` multiple times.
