# I didn't ask for plan\_bfft, but got MethodError: no method matching plan\_bfft(::Vector{Number}, ::UnitRange{Int64})

**URL:** <https://discourse.julialang.org/t/i-didnt-ask-for-plan-bfft-but-got-methoderror-no-method-matching-plan-bfft-vector-number-unitrange-int64/82791>\
**Category:** Signal and Image Processing\
**Tags:** fftw\
**Created:** [June 15, 2022, 4:49am UTC](https://discourse.julialang.org/t/i-didnt-ask-for-plan-bfft-but-got-methoderror-no-method-matching-plan-bfft-vector-number-unitrange-int64/82791 "2022-06-15T04:49:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yly](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yly](https://discourse.julialang.org/u/Yly)\
**Post date:** [June 15, 2022, 4:49am UTC](https://discourse.julialang.org/t/i-didnt-ask-for-plan-bfft-but-got-methoderror-no-method-matching-plan-bfft-vector-number-unitrange-int64/82791/1 "2022-06-15T04:49:04Z")

</div>

While coding an iterative FFT function, I came upon a strange bug that depends on the arguments to the function in a complicated way. I’ve managed to reduce the problematic code to the following:

```julia
using FFTW
unitize(x::Complex) = x==0 ? 0 : x/abs(x)
function problematic(ys::Tuple,phis::Tuple,nit)
    n = length(ys)
    x = sum(ifft(ys[i]) for i=1:n)/n
    for j=1:nit
        thetas = ((unitize.(fft(x.*phi)) for phi in phis)...,)
        x = sum(ifft(ys[k].*thetas[k]) for k=1:n)/n
    end
    return x
end
x = [exp(-j^2) for j=-10:.1:10]
quad = exp.(im*[-10:.1:10;].^2)
y0 = abs.(fft(x))
y1 = abs.(fft(x.*quad))
x1 = problematic((y0,y1),(ones(length(x)),quad),4); # This works
x1 = problematic((y0,y1),(ones(length(x)),quad),5); # But this does not work

```

The various parameters and the auxiliary function `unitize` are, unfortunately, necessary for this demonstration, as slight tweaks to the function eliminates the error. The final parameter to `problematic` determines the number of iterations, and changing it slightly affects whether or not an error is thrown. With slightly different inputs `y0`, `y1`, `quad`, etc. the exact number of iterations at which the error shows up will change also. Under some conditions, I also find that the number of iterations required for an error differs between the REPL and a Jupyter notebook.

At any rate, sometimes this code throws a `MethodError: no method matching plan_bfft(::Vector{Number}, ::UnitRange{Int64})`. I find this strange, because I didn’t ask for `plan_bfft` at all. Is the compiler automatically planning an FFT when it sees many FFTs in a loop? Did I do something silly, or is this a bug?

I’m using Julia v1.7.2 and FFTW v1.4.6.

---

<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:** [June 15, 2022, 5:30am UTC](https://discourse.julialang.org/t/i-didnt-ask-for-plan-bfft-but-got-methoderror-no-method-matching-plan-bfft-vector-number-unitrange-int64/82791/2 "2022-06-15T05:30:06Z")

</div>

I’m not certain about why this only fails for `nit = 5` and not `4`, but it might be something about inference giving up beyond a certain tuple length (just a guess.)

However, I think the problem originates from this type instability:

> [@Yly](#):
>
> `unitize(x::Complex) = x==0 ? 0 : x/abs(x)`

Try for example:

```julia
unitize(x::Complex) = x==0 ? x/one(x) : x/abs(x)

```

and see if that helps.

---

<div class="post-metadata">

**Author:** ![Yly](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yly](https://discourse.julialang.org/u/Yly)\
**Post date:** [June 16, 2022, 2:16am UTC](https://discourse.julialang.org/t/i-didnt-ask-for-plan-bfft-but-got-methoderror-no-method-matching-plan-bfft-vector-number-unitrange-int64/82791/3 "2022-06-16T02:16:31Z")

</div>

This does indeed seem to fix the problem (thanks!), as does this modification:  
`unitize(x::Complex) = x==0 ? zero(x) : x/abs(x)`  
which I believe also resolves the type instability–correct? I don’t understand yet why this is important. Are you able to give me some intuition as to what significance this change has? Is there a reason you used the expression `x/one(x)` instead of `zero(x)`?

---

<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:** [June 16, 2022, 6:57am UTC](https://discourse.julialang.org/t/i-didnt-ask-for-plan-bfft-but-got-methoderror-no-method-matching-plan-bfft-vector-number-unitrange-int64/82791/4 "2022-06-16T06:57:24Z")

</div>

It’s ok for `Complex{<:AbstractFloat}`:

```julia
julia> @code_warntype unitize(1.0 + 2.0im)
...
Body::ComplexF64

```

but not for `Complex{<:Integer}` (or `Complex{<:Rational}` etc.)

```julia
julia> @code_warntype unitize(1 + 2im)
...
Body::Union{Complex{Int64}, ComplexF64}

```

Because:

```julia
julia> unitize(1 + 2im)
0.4472135954999579 + 0.8944271909999159im

julia> unitize(0 + 0im)
0 + 0im

```

Just in case, I would also use `iszero(x)` instead of `x == 0`, though it’s not a problem for any cases I can think of off the top of my head.
