Skip to content
chrismedrela edited this page Jun 1, 2014 · 6 revisions

This feature is not implemented yet!

Interpolation

breeze.interpolation module supports interpolation facilities for data in one dimension (univariate interpolation). At this moment, there is only linear interpolation available, but you can easily write your own 1d interpolation facilities.

All examples in this document assumes that you've imported necessary modules:

scala> import breeze.interpolation._

1D interpolation

1D (univariate) interpolators gets the coordinates of nodes. One vector for each coordinate is required:

scala> val x = DenseVector(0.0, 1.0, 2.0, 3.0)
scala> val y = DenseVector(2.0, 4.0, 8.0, 5.0)
scala> val f = LinearInterpolator(x, y)

The interpolator returns an interpolating function. You can ask for the value at given point:

scala> f(2.5)
6.5

The function is an universal function, so you can also pass an vector or matrix or any other iterable object:

scala> f(DenseVector(1.0, 1.25, 1.5))
DenseVector(4.0, 5.0, 6.0)

Writing your own 1D interpolator

There are two ways to write your own univariate interpolator. The simpler but more rigid one is to extend the HandyUnivariateInterpolator and pass it vector of x and y coordinates. You need to implement only valueAt(x) method where you can assume that the argument x is in the domain (so it's between the minimum and maximum of x coordinates):

class MyInterpolator (val X: Vector[Double],
                      val Y: Vector[Double])
                     extends UnivariateInterpolator(X, Y) {
  def valueAt(x: Double): Double = ...
}

The second way is more flexible. You only need to extend UnivariateInterpolator trait and implement apply(x) method. You are on your own to validate the x argument.

class DummyInterpolator() extends UnivariateInterpolator {
  def apply(x: Double): Double = 0
}

No matter which way you implement your interpolator, it's always an universal function and, therefore, it automatically plays well when you pass an vector or matrix instead of a plain double.

Clone this wiki locally