Example usage for org.apache.commons.math3.analysis.polynomials PolynomialFunctionLagrangeForm PolynomialFunctionLagrangeForm

List of usage examples for org.apache.commons.math3.analysis.polynomials PolynomialFunctionLagrangeForm PolynomialFunctionLagrangeForm

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis.polynomials PolynomialFunctionLagrangeForm PolynomialFunctionLagrangeForm.

Prototype

public PolynomialFunctionLagrangeForm(double x[], double y[]) 

Source Link

Document

Construct a Lagrange polynomial with the given abscissas and function values.

Usage

From source file:com.opengamma.strata.math.impl.util.CommonsMathWrapperTest.java

@Test
public void testLagrange() {
    int n = OG_POLYNOMIAL.getCoefficients().length;
    double[] x = new double[n];
    double[] y = new double[n];
    for (int i = 0; i < n; i++) {
        x[i] = i;// ww  w  .  j  av a  2 s.  c  om
        y[i] = OG_POLYNOMIAL.applyAsDouble(x[i]);
    }
    Function<Double, Double> unwrapped = CommonsMathWrapper.unwrap(new PolynomialFunctionLagrangeForm(x, y));
    for (int i = 0; i < 100; i++) {
        assertEquals(unwrapped.apply(i + 0.5), OG_POLYNOMIAL.applyAsDouble(i + 0.5), 1e-9);
    }
}

From source file:uk.ac.diamond.scisoft.analysis.processing.operations.oned.SplineBaselineOperation.java

/**
 * Performs the subtraction of the spline baseline.
 * <p>//from  w ww .  j a v a 2 s . c  om
 * Given the x position of the knot points, return a copy of the data which
 * has had a spline subtracted such that the knot points become zero. 
 * @param input
 *          input data
 * @param knots
 *          position of the zeroes in the new data
 * @return
 *       data with the baseline subtracted.
 */
private Dataset subtractSplineBaseline(Dataset input, Dataset knots) {
    if (knots == null || knots.getSize() <= 0)// || knots.getShape().length < 2 )
        // Without sufficient data points for a fit, return the original data
        return input;

    Dataset xaxis = null;
    // Get the axis, or create one.
    if (AbstractOperation.getFirstAxes(input) != null && AbstractOperation.getFirstAxes(input).length != 0
            && AbstractOperation.getFirstAxes(input)[0] != null)
        xaxis = DatasetUtils.sliceAndConvertLazyDataset(AbstractOperation.getFirstAxes(input)[0]);
    else
        xaxis = DoubleDataset.createRange(input.getSize());

    // y values at the xs
    Dataset ys = Maths.interpolate(xaxis, input, knots, null, null);

    Dataset ybase = null;

    switch (knots.getSize()) {
    case 0:
        ybase = DoubleDataset.zeros(xaxis);
        break;
    case 1:
        // Subtract a constant
        ybase = Maths.multiply(ys.getDouble(0), DoubleDataset.ones(xaxis));
        break;
    case 2:
        // Subtract a linear fit
        ybase = Interpolation1D.linearInterpolation(knots, ys, xaxis);

        // Extrapolating function for the linear fit
        PolynomialFunctionLagrangeForm linearLagrange = new PolynomialFunctionLagrangeForm(
                new double[] { knots.getDouble(0), knots.getDouble(1) },
                new double[] { ys.getDouble(0), ys.getDouble(1) });

        // Calculate the values at the extrapolated points
        for (int i = 0; i < xaxis.getSize(); i++)
            if ((xaxis.getDouble(i) < knots.min().doubleValue())
                    || (xaxis.getDouble(i) >= knots.max().doubleValue()))
                ybase.set(linearLagrange.value(xaxis.getDouble(i)), i);

        break;
    default:
        // Spline interpolation between the outer most knots
        ybase = Interpolation1D.splineInterpolation(knots, ys, xaxis);

        // Extrapolation
        final int degree = 3;
        double[] lowerInterval = new double[degree + 1];
        double[] lowerValues = new double[degree + 1];
        double[] upperInterval = new double[degree + 1];
        double[] upperValues = new double[degree + 1];

        // Get 4 x and y values for the end intervals. the end points, the
        // next-to-end points and two equispaced points in between.
        for (int i = 0; i <= degree; i++) {
            lowerInterval[i] = ((degree - i) * knots.getDouble(0) + i * knots.getDouble(1)) / degree;
            lowerValues[i] = Interpolation1D.splineInterpolation(knots, ys,
                    new DoubleDataset(Arrays.copyOfRange(lowerInterval, i, i + 1), 1)).getDouble(0);
            upperInterval[i] = ((degree - i) * knots.getDouble(knots.getSize() - 2)
                    + i * knots.getDouble(knots.getSize() - 1)) / degree;
            upperValues[i] = Interpolation1D.splineInterpolation(knots, ys,
                    new DoubleDataset(Arrays.copyOfRange(upperInterval, i, i + 1), 1)).getDouble(0);
        }

        // Lower (smaller value) and upper (larger value) extrapolating
        // functions. 4 points define a cubic, to match the cubic spline
        // used in the interpolation
        PolynomialFunctionLagrangeForm lowerLagrange = new PolynomialFunctionLagrangeForm(lowerInterval,
                lowerValues);
        PolynomialFunctionLagrangeForm upperLagrange = new PolynomialFunctionLagrangeForm(upperInterval,
                upperValues);

        // Loop through all points. If the x value falls outside the knots,
        // replace the y-value of the baseline with the interpolated value.
        for (int i = 0; i < xaxis.getSize(); i++)
            if (xaxis.getDouble(i) < knots.min().doubleValue())
                ybase.set(lowerLagrange.value(xaxis.getDouble(i)), i);
            else if (xaxis.getDouble(i) >= knots.max().doubleValue())
                ybase.set(upperLagrange.value(xaxis.getDouble(i)), i);

        break;
    }
    // Finally, perform the subtraction.
    return Maths.subtract(input, ybase);
}