# FFT for Unitful vector

**URL:** <https://discourse.julialang.org/t/fft-for-unitful-vector/59044>\
**Category:** General Usage\
**Tags:** package, unitful\
**Created:** [April 11, 2021, 6:29pm UTC](https://discourse.julialang.org/t/fft-for-unitful-vector/59044 "2021-04-11T18:29:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [April 11, 2021, 6:29pm UTC](https://discourse.julialang.org/t/fft-for-unitful-vector/59044/1 "2021-04-11T18:29:29Z")

</div>

I am trying to use Unitful package to better track units of my physical quantities, and I am a bit confused:

Here is example:

```Julia
using Unitful

trajectory = trajectory .* u"m"

fft(trajectory)

```

I got the following error:

> ERROR: MethodError: no method matching plan\_fft(::Matrix{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}, ::UnitRange{Int64})

It is a bug or a feature?

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [April 11, 2021, 7:21pm UTC](https://discourse.julialang.org/t/fft-for-unitful-vector/59044/2 "2021-04-11T19:21:37Z")

</div>

To use quantities with units, packages need to be written in a generic way. You didn’t say where you’re getting the function `fft` from, but if it’s from say FFTW, then that’s calling out to a library written in C, and that code can only handle a few hardware-supported numeric types like Float64. So you need to unwrap and rewrap your quantities to interface with such code, e.g. `fft(ustrip.(u"m", trajectory))`.

You can actually still get benefits from using units in this way though, e.g.

```julia
julia> x = rand(4)
4-element Vector{Float64}:
 0.16106228971764325
 0.7034731999527042
 0.6401812191825158
 0.43054032170468615

julia> x_m = x * u"m"
4-element Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}:
 0.16106228971764325 m
  0.7034731999527042 m
  0.6401812191825158 m
 0.43054032170468615 m

julia> x_mm = x * u"mm"
4-element Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(mm,), 𝐋, nothing}}}:
 0.16106228971764325 mm
  0.7034731999527042 mm
  0.6401812191825158 mm
 0.43054032170468615 mm

julia> ustrip.(u"m", x_m)
4-element Vector{Float64}:
 0.16106228971764325
 0.7034731999527042
 0.6401812191825158
 0.43054032170468615

julia> ustrip.(u"m", x_mm)
4-element Vector{Float64}:
 0.00016106228971764325
 0.0007034731999527042
 0.0006401812191825159
 0.00043054032170468615

```

That is, `ustrip` will convert quantities to the type you specify before stripping off the unit, so that you can be sure what goes into `fft` has the units you expect.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [April 11, 2021, 8:23pm UTC](https://discourse.julialang.org/t/fft-for-unitful-vector/59044/3 "2021-04-11T20:23:33Z")

</div>

Notice BTW that you can strip (and replace) units without copying – they are purely in the type:

```julia
julia> x_re = reinterpret(Float64, x_m)
4-element reinterpret(Float64, ::Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}):
 0.16106228971764325
 0.7034731999527042
 0.6401812191825158
 0.43054032170468615

julia> x_re[1] # no units, but a view of x_m
0.16106228971764325

julia> fft(x_re) # accepts ReinterpretArray
4-element Vector{ComplexF64}:
   1.9352570305575494 + 0.0im
 -0.47911892946487256 - 0.27293287824801804im
  -0.3327700127572313 + 0.0im
 -0.47911892946487256 + 0.27293287824801804im

julia> @which fft
AbstractFFTs

```

Also, the basic definitions of `fft` etc. are in a small package to make it easy to extend them, without the package which does so being tied to FFTW.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 11, 2021, 8:31pm UTC](https://discourse.julialang.org/t/fft-for-unitful-vector/59044/4 "2021-04-11T20:31:30Z")

</div>

> [@mcabbott](#):
>
> Notice BTW that you can strip (and replace) units without copying – they are purely in the type:

Probably works only for arrays of homogeneous quantities (all with exactly the same units)?

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [April 11, 2021, 8:35pm UTC](https://discourse.julialang.org/t/fft-for-unitful-vector/59044/5 "2021-04-11T20:35:29Z")

</div>

Yes, absolutely. I guess it could still make sense to take the FFT of something like `Any[1u"m", 1u"cm"]`, for which this trick would not work.

(And, I think `fft` copies real vectors to make complex ones before getting to work, in any case. So ideally one would do both steps at once, and reinterpret only dense arrays of unit-homogeneous complex numbers.)

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [April 12, 2021, 7:08am UTC](https://discourse.julialang.org/t/fft-for-unitful-vector/59044/6 "2021-04-12T07:08:44Z")

</div>

Thank you all for answers!

How should I deal with matrix where are NaNs?

```julia
a = zeros(N,1) .*u"m*s^-2"
if somecondition
  a[ii] = value_in_ms^-2
else
  a[ii] = NaN
end

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 12, 2021, 7:29am UTC](https://discourse.julialang.org/t/fft-for-unitful-vector/59044/7 "2021-04-12T07:29:15Z")

</div>

You would probably have to impute it. For instance, have its value entirely depend on its neighbors (e.g. with Interpolations.jl).

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [April 12, 2021, 12:16pm UTC](https://discourse.julialang.org/t/fft-for-unitful-vector/59044/8 "2021-04-12T12:16:48Z")

</div>

That’s super useful! I was considering whether or not use to use units in a package where I wasn’t sure if the cost of allocating a new array was worth it, but this makes it basically free.

Would it make sense to put something like this in Unitful itself?

```julia
"""
    with_unit(v::AbstractArray, u::Unitful.Units) -> Base.ReinterpretArray{…}

Creates a view of `v` in which each element has units given by `u`.

!!! warning
    This does not perform any unit conversion or check the units of `v` first.
"""
with_unit(v::AbstractArray, u::Unitful.Units) = reinterpret(typeof(one(eltype(v))*u), v)

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [April 12, 2021, 12:54pm UTC](https://discourse.julialang.org/t/fft-for-unitful-vector/59044/9 "2021-04-12T12:54:01Z")

</div>

You could even spell it `x_m = reinterpret(u"m", x)` so as not to need to remember a new name; perhaps a slight abuse but not piracy.

Should any existing functions default to this (when the array is suitable)? Unitful doesn’t seem to have much support for arrays, things like `uconvert.(u"cm", x_m)` and `ustrip.(u"cm", x_m)` are at present errors without the broadcasting.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 12, 2021, 1:02pm UTC](https://discourse.julialang.org/t/fft-for-unitful-vector/59044/10 "2021-04-12T13:02:16Z")

</div>

> [@mcabbott](#):
>
> Unitful doesn’t seem to have much support for arrays

There are a few linear algebra operations that don’t work with unitful arrays:

> <https://github.com/PainterQubits/Unitful.jl/issues/46>
>
> Some linear algebra operations like \`\\\` have a hard time with unitful numbers. T…ake the following case:
> 
> \`\`\`julia
> a = \[1.0u"N" 2.0u"N"
> 3.0u"N" 1.0u"N"\]
> b = \[1.0u"N";3.0u"N"\]
> \`\`\`
> 
> If you try \`\\\` it gives a dimension error. If you try \`\*\` then it uses the generic \`\*\` fallback and not BLAS. The same is true if you use:
> 
> \`\`\`julia
> A = rand(100,100)u"N"
> b = rand(100)u"N"
> \`\`\`
> 
> I suggest trying something like, performing a dimensional analysis, strip the units, use the general \`\*\` or \`\\\`, and then re-apply units. 
> 
> To do this properly, you might need a \`UnitfulArray\` instead of an array of Unitful values. I am not sure if stripping the units on an array of Unitful numbers can be done without making a temporary.

And stripping the units isn’t always a good idea, especially for inhomogenous arrays (another example in the issue)

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [April 12, 2021, 5:25pm UTC](https://discourse.julialang.org/t/fft-for-unitful-vector/59044/11 "2021-04-12T17:25:48Z")

</div>

I am struggling with the type of argument in the function

I would like to construct multiple dispatch function

for example:

```julia
>> a = rand(N) .*u"m*s^-2"

>> typeof(a)
Vector{Quantity{Float64, 𝐋 𝐓^-2, Unitful.FreeUnits{(m, s^-2), 𝐋 𝐓^-2, nothing}}}

function do_st_with_a(a)
 do_st_with_a # units are not needed
end

function do_st_with_a(a:: Vector{Quantity{Float64, 𝐋 𝐓^-2, Unitful.FreeUnits{(m, s^-2), 𝐋 𝐓^-2, nothing}}})
 do_st_with_a # units are needed
end

```

This give me an error: ERROR: syntax: missing comma or } in argument list

What type should I put to the second definition of the function to get multiple dispatch depending on presence of units in input array?

The second thing which confused me is this

```julia
>> typeof(a[1]) == typeof(Unitful.Acceleration)

false

```
