# Interpolations with Unitful Vectors

**URL:** <https://discourse.julialang.org/t/interpolations-with-unitful-vectors/80919>\
**Category:** General Usage\
**Tags:** interpolations, unitful\
**Created:** [May 11, 2022, 10:18pm UTC](https://discourse.julialang.org/t/interpolations-with-unitful-vectors/80919 "2022-05-11T22:18:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmoored4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmoored4/32/18311_2.png) [@dmoored4](https://discourse.julialang.org/u/dmoored4)\
**Post date:** [May 11, 2022, 10:18pm UTC](https://discourse.julialang.org/t/interpolations-with-unitful-vectors/80919/1 "2022-05-11T22:18:39Z")

</div>

Often I need to perform interpolations of unit-aware vectors and I would prefer to have the flexibility to use the interpolation functions offered by [Dierckx](https://github.com/kbarbary/Dierckx.jl%5D), [Interpolations](https://github.com/JuliaMath/Interpolations.jl) etc.

I haven’t found this in docs or discourse so I wanted to provide it here in case it’s useful to anybody else and see if maybe there’s already a better way that I overlooked.

```julia
using Unitful, Interpolations

# Creating an x and y vector with units
Xs = (0:0.1:5) .* u"m"
Ys = Xs.^2

# Defining a function which will return an interpolator which strips units according to input xs and applies units to output interpolated values based on the units of ys
function UnitfulLinterp(xs, ys; interpolator_method=LinearInterpolation)
	x_unit = unit(xs[1])
	y_unit = unit(ys[1])

	xs = ustrip.(x_unit, xs)
	ys = ustrip.(y_unit, ys)

	itp = interpolator_method(xs, ys)

	interpolator(x) = itp(ustrip(x_unit, x)) * y_unit
	
	return interpolator
end

# creating my unit-aware interpolator, itp
itp = UnitfulLinterp(Xs, Ys)

# passing a single value to be interpolated
x_to_eval = 5u"mm"
interpolated_y = itp(x_to_eval)
> 0.0005008241058144431 m^2

# passing a vector of values and broadcasting the interpolator
xs_to_eval = [5.0u"mm", 12.0u"inch", 15u"ft"]
interpolated_ys = itp.(xs_to_eval)
> 0.000500824 m^2
> 0.0933604 m^2
> 20.9052 m^2

```

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [May 11, 2022, 11:50pm UTC](https://discourse.julialang.org/t/interpolations-with-unitful-vectors/80919/2 "2022-05-11T23:50:40Z")

</div>

I think this is an oversight in Interpolations. Unitful interpolation works fine for Gridded interpolation as shown in the [tests](https://github.com/JuliaMath/Interpolations.jl/blob/master/test/extrapolation/type-stability.jl#L73), the trick there is that both positions and values are arrays, not ranges. For your example here:

```julia
Xs = collect((0:0.1:5)u"m")
Ys = Xs .^ 2
itp = LinearInterpolation(Xs, Ys)

itp(5.0u"inch")

```

The problem is that currently scaled interpolation does not work, lets see if this [PR](https://github.com/JuliaMath/Interpolations.jl/pull/490) to fix it is accepted, then this will work:

```julia
Xs = (0:0.1:5)u"m"
Ys = Xs .^ 2
sitp = scale(interpolate(Ys, BSpline(Linear())), Xs)

sitp(5.0u"inch")
```
