Search in sources :

Example 1 with AkimaSplineInterpolator

use of org.apache.commons.math3.analysis.interpolation.AkimaSplineInterpolator in project mafscaling by vimsh.

the class BilinearInterpolator method interpolate.

/**
 * Method returns an interpolated/extrapolated value, based on
 * @param x, array of x values
 * @param y, array of y values
 * @param xi, is x value you want to interpolate at
 * @param type, interpolation method type
 * @return interpolated value
 * @throws Exception
 */
public static double interpolate(double[] x, double[] y, double xi, InterpolatorType type) throws Exception {
    UnivariateInterpolator interpolator = null;
    switch(type) {
        case AkimaCubicSpline:
            interpolator = new AkimaSplineInterpolator();
            break;
        case Linear:
            interpolator = new LinearInterpolator();
            break;
        case Regression:
            interpolator = new LoessInterpolator();
            break;
        case CubicSpline:
            interpolator = new SplineInterpolator();
            break;
        default:
            throw new Exception("Invalid interpolator type for this function");
    }
    UnivariateFunction function = interpolator.interpolate(x, y);
    PolynomialFunction[] polynomials = ((PolynomialSplineFunction) function).getPolynomials();
    if (xi > x[x.length - 1])
        return polynomials[polynomials.length - 1].value(xi - x[x.length - 2]);
    if (xi < x[0])
        return polynomials[0].value(xi - x[0]);
    return function.value(xi);
}
Also used : LoessInterpolator(org.apache.commons.math3.analysis.interpolation.LoessInterpolator) UnivariateFunction(org.apache.commons.math3.analysis.UnivariateFunction) LinearInterpolator(org.apache.commons.math3.analysis.interpolation.LinearInterpolator) AkimaSplineInterpolator(org.apache.commons.math3.analysis.interpolation.AkimaSplineInterpolator) SplineInterpolator(org.apache.commons.math3.analysis.interpolation.SplineInterpolator) PiecewiseBicubicSplineInterpolator(org.apache.commons.math3.analysis.interpolation.PiecewiseBicubicSplineInterpolator) AkimaSplineInterpolator(org.apache.commons.math3.analysis.interpolation.AkimaSplineInterpolator) PolynomialFunction(org.apache.commons.math3.analysis.polynomials.PolynomialFunction) UnivariateInterpolator(org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator) PolynomialSplineFunction(org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction) NumberIsTooSmallException(org.apache.commons.math3.exception.NumberIsTooSmallException) OutOfRangeException(org.apache.commons.math3.exception.OutOfRangeException) NonMonotonicSequenceException(org.apache.commons.math3.exception.NonMonotonicSequenceException) DimensionMismatchException(org.apache.commons.math3.exception.DimensionMismatchException) NoDataException(org.apache.commons.math3.exception.NoDataException)

Aggregations

UnivariateFunction (org.apache.commons.math3.analysis.UnivariateFunction)1 AkimaSplineInterpolator (org.apache.commons.math3.analysis.interpolation.AkimaSplineInterpolator)1 LinearInterpolator (org.apache.commons.math3.analysis.interpolation.LinearInterpolator)1 LoessInterpolator (org.apache.commons.math3.analysis.interpolation.LoessInterpolator)1 PiecewiseBicubicSplineInterpolator (org.apache.commons.math3.analysis.interpolation.PiecewiseBicubicSplineInterpolator)1 SplineInterpolator (org.apache.commons.math3.analysis.interpolation.SplineInterpolator)1 UnivariateInterpolator (org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator)1 PolynomialFunction (org.apache.commons.math3.analysis.polynomials.PolynomialFunction)1 PolynomialSplineFunction (org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction)1 DimensionMismatchException (org.apache.commons.math3.exception.DimensionMismatchException)1 NoDataException (org.apache.commons.math3.exception.NoDataException)1 NonMonotonicSequenceException (org.apache.commons.math3.exception.NonMonotonicSequenceException)1 NumberIsTooSmallException (org.apache.commons.math3.exception.NumberIsTooSmallException)1 OutOfRangeException (org.apache.commons.math3.exception.OutOfRangeException)1