I'm trying to linearly interpolate an Array[Option[Long]]
. For example given:
val example1 = Array(Some(20l), None, Some(60l))
val example2 = Array(Some(20l), None, None, Some(80l))
val example3 = Array(Some(20l), None, None, Some(80l), Some(90l), Some(100l))
val example4 = Array(Some(20l), None, None, Some(80l), None, Some(82l))
I'm expecting:
val example1Interpolated = Array(20l, 40l, 60l)
val example2Interpolated = Array(20l, 40l, 60l, 80l)
val example3Interpolated = Array(20l, 40l, 60l, 80l, 90l, 100l)
val example4Interpolated = Array(20l, 40l, 60l, 80l, 81l, 82l)
There's no relationship between the elements in the collection (e.g. example4
). However the values are monotonically increasing.
For those familiar with Python I'm looking for the Scala equivalent of the following:
def interpolate(input_):
nans = np.isnan(input_)
get_index = lambda z: z.nonzero()[0]
input_[nans] = np.interp(get_index(nans), get_index(~nans), input_[~nans])
return input_
Which for:
interpolate(np.array([20, np.nan, 60]))
interpolate(np.array([20, np.nan, np.nan, 80]))
interpolate(np.array([20, np.nan, np.nan, 80, np.nan, 82]))
yields:
array([ 20., 40., 60.])
array([ 20., 40., 60., 80.])
array([ 20., 40., 60., 80., 81., 82.])