org.apache.commons.math3.exception.NonMonotonicSequenceException Java Examples

The following examples show how to use org.apache.commons.math3.exception.NonMonotonicSequenceException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PolynomialSplineFunction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a polynomial spline function with the given segment delimiters
 * and interpolating polynomials.
 * The constructor copies both arrays and assigns the copies to the knots
 * and polynomials properties, respectively.
 *
 * @param knots Spline segment interval delimiters.
 * @param polynomials Polynomial functions that make up the spline.
 * @throws NullArgumentException if either of the input arrays is {@code null}.
 * @throws NumberIsTooSmallException if knots has length less than 2.
 * @throws DimensionMismatchException if {@code polynomials.length != knots.length - 1}.
 * @throws NonMonotonicSequenceException if the {@code knots} array is not strictly increasing.
 *
 */
public PolynomialSplineFunction(double knots[], PolynomialFunction polynomials[])
    throws NullArgumentException, NumberIsTooSmallException,
           DimensionMismatchException, NonMonotonicSequenceException{
    if (knots == null ||
        polynomials == null) {
        throw new NullArgumentException();
    }
    if (knots.length < 2) {
        throw new NumberIsTooSmallException(LocalizedFormats.NOT_ENOUGH_POINTS_IN_SPLINE_PARTITION,
                                            2, knots.length, false);
    }
    if (knots.length - 1 != polynomials.length) {
        throw new DimensionMismatchException(polynomials.length, knots.length);
    }
    MathArrays.checkOrder(knots);

    this.n = knots.length -1;
    this.knots = new double[n + 1];
    System.arraycopy(knots, 0, this.knots, 0, n + 1);
    this.polynomials = new PolynomialFunction[n];
    System.arraycopy(polynomials, 0, this.polynomials, 0, n);
}
 
Example #2
Source File: DividedDifferenceInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of parameters for the interpolator.
 */
@Test
public void testParameters() {
    UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();

    try {
        // bad abscissas array
        double x[] = { 1.0, 2.0, 2.0, 4.0 };
        double y[] = { 0.0, 4.0, 4.0, 2.5 };
        UnivariateFunction p = interpolator.interpolate(x, y);
        p.value(0.0);
        Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array");
    } catch (NonMonotonicSequenceException ex) {
        // expected
    }
}
 
Example #3
Source File: DividedDifferenceInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of parameters for the interpolator.
 */
@Test
public void testParameters() {
    UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();

    try {
        // bad abscissas array
        double x[] = { 1.0, 2.0, 2.0, 4.0 };
        double y[] = { 0.0, 4.0, 4.0, 2.5 };
        UnivariateFunction p = interpolator.interpolate(x, y);
        p.value(0.0);
        Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array");
    } catch (NonMonotonicSequenceException ex) {
        // expected
    }
}
 
Example #4
Source File: PiecewiseBicubicSplineInterpolator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public PiecewiseBicubicSplineInterpolatingFunction interpolate( final double[] xval,
                                                                final double[] yval,
                                                                final double[][] fval)
    throws DimensionMismatchException,
           NullArgumentException,
           NoDataException,
           NonMonotonicSequenceException {
    if ( xval == null ||
         yval == null ||
         fval == null ||
         fval[0] == null ) {
        throw new NullArgumentException();
    }

    if ( xval.length == 0 ||
         yval.length == 0 ||
         fval.length == 0 ) {
        throw new NoDataException();
    }

    MathArrays.checkOrder(xval);
    MathArrays.checkOrder(yval);

    return new PiecewiseBicubicSplineInterpolatingFunction( xval, yval, fval );
}
 
Example #5
Source File: DividedDifferenceInterpolator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute an interpolating function for the dataset.
 *
 * @param x Interpolating points array.
 * @param y Interpolating values array.
 * @return a function which interpolates the dataset.
 * @throws DimensionMismatchException if the array lengths are different.
 * @throws NumberIsTooSmallException if the number of points is less than 2.
 * @throws NonMonotonicSequenceException if {@code x} is not sorted in
 * strictly increasing order.
 */
public PolynomialFunctionNewtonForm interpolate(double x[], double y[])
    throws DimensionMismatchException,
           NumberIsTooSmallException,
           NonMonotonicSequenceException {
    /**
     * a[] and c[] are defined in the general formula of Newton form:
     * p(x) = a[0] + a[1](x-c[0]) + a[2](x-c[0])(x-c[1]) + ... +
     *        a[n](x-c[0])(x-c[1])...(x-c[n-1])
     */
    PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true);

    /**
     * When used for interpolation, the Newton form formula becomes
     * p(x) = f[x0] + f[x0,x1](x-x0) + f[x0,x1,x2](x-x0)(x-x1) + ... +
     *        f[x0,x1,...,x[n-1]](x-x0)(x-x1)...(x-x[n-2])
     * Therefore, a[k] = f[x0,x1,...,xk], c[k] = x[k].
     * <p>
     * Note x[], y[], a[] have the same length but c[]'s size is one less.</p>
     */
    final double[] c = new double[x.length-1];
    System.arraycopy(x, 0, c, 0, c.length);

    final double[] a = computeDividedDifference(x, y);
    return new PolynomialFunctionNewtonForm(a, c);
}
 
Example #6
Source File: ModifiedLoess.java    From thunderstorm with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Compute an interpolating function by performing a loess fit
 * on the data at the original abscissae and then building a cubic spline
 * with a
 * {@link org.apache.commons.math3.analysis.interpolation.SplineInterpolator}
 * on the resulting fit.
 *
 * @param xval the arguments for the interpolation points
 * @param yval the values for the interpolation points
 * @return A cubic spline built upon a loess fit to the data at the original abscissae
 * @throws NonMonotonicSequenceException if {@code xval} not sorted in
 * strictly increasing order.
 * @throws DimensionMismatchException if {@code xval} and {@code yval} have
 * different sizes.
 * @throws NoDataException if {@code xval} or {@code yval} has zero size.
 * @throws NotFiniteNumberException if any of the arguments and values are
 * not finite real numbers.
 * @throws NumberIsTooSmallException if the bandwidth is too small to
 * accomodate the size of the input data (i.e. the bandwidth must be
 * larger than 2/n).
 */
public final PolynomialSplineFunction interpolate(double[] xval,
                                                  double[] yval)
    throws NonMonotonicSequenceException,
           DimensionMismatchException,
           NoDataException,
           NotFiniteNumberException,
           NumberIsTooSmallException {
    double[] smoothed = smooth(xval, yval);
    DoubleList newX = new ArrayDoubleList();
    DoubleList newSmoothed = new ArrayDoubleList();
    newX.add(xval[0]);
    newSmoothed.add(smoothed[0]);
    for(int i = 1; i < xval.length; i++){
        if(xval[i] != xval[i-1]){
            newX.add(xval[i]);
            newSmoothed.add(smoothed[i]);
        }
    }
    xval = newX.toArray();
    smoothed = newSmoothed.toArray();
    
    return new SplineInterpolator().interpolate(xval, smoothed);
}
 
Example #7
Source File: DividedDifferenceInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of parameters for the interpolator.
 */
@Test
public void testParameters() {
    UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();

    try {
        // bad abscissas array
        double x[] = { 1.0, 2.0, 2.0, 4.0 };
        double y[] = { 0.0, 4.0, 4.0, 2.5 };
        UnivariateFunction p = interpolator.interpolate(x, y);
        p.value(0.0);
        Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array");
    } catch (NonMonotonicSequenceException ex) {
        // expected
    }
}
 
Example #8
Source File: PolynomialFunctionLagrangeForm.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Evaluate the Lagrange polynomial using
 * <a href="http://mathworld.wolfram.com/NevillesAlgorithm.html">
 * Neville's Algorithm</a>. It takes O(n^2) time.
 *
 * @param x Interpolating points array.
 * @param y Interpolating values array.
 * @param z Point at which the function value is to be computed.
 * @return the function value.
 * @throws DimensionMismatchException if {@code x} and {@code y} have
 * different lengths.
 * @throws NonMonotonicSequenceException
 * if {@code x} is not sorted in strictly increasing order.
 * @throws NumberIsTooSmallException if the size of {@code x} is less
 * than 2.
 */
public static double evaluate(double x[], double y[], double z)
    throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException {
    if (verifyInterpolationArray(x, y, false)) {
        return evaluateInternal(x, y, z);
    }

    // Array is not sorted.
    final double[] xNew = new double[x.length];
    final double[] yNew = new double[y.length];
    System.arraycopy(x, 0, xNew, 0, x.length);
    System.arraycopy(y, 0, yNew, 0, y.length);

    MathArrays.sortInPlace(xNew, yNew);
    // Second check in case some abscissa is duplicated.
    verifyInterpolationArray(xNew, yNew, true);
    return evaluateInternal(xNew, yNew, z);
}
 
Example #9
Source File: DividedDifferenceInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of parameters for the interpolator.
 */
@Test
public void testParameters() {
    UnivariateInterpolator interpolator = new DividedDifferenceInterpolator();

    try {
        // bad abscissas array
        double x[] = { 1.0, 2.0, 2.0, 4.0 };
        double y[] = { 0.0, 4.0, 4.0, 2.5 };
        UnivariateFunction p = interpolator.interpolate(x, y);
        p.value(0.0);
        Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array");
    } catch (NonMonotonicSequenceException ex) {
        // expected
    }
}
 
Example #10
Source File: PolynomialSplineFunction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a polynomial spline function with the given segment delimiters
 * and interpolating polynomials.
 * The constructor copies both arrays and assigns the copies to the knots
 * and polynomials properties, respectively.
 *
 * @param knots Spline segment interval delimiters.
 * @param polynomials Polynomial functions that make up the spline.
 * @throws NullArgumentException if either of the input arrays is {@code null}.
 * @throws NumberIsTooSmallException if knots has length less than 2.
 * @throws DimensionMismatchException if {@code polynomials.length != knots.length - 1}.
 * @throws NonMonotonicSequenceException if the {@code knots} array is not strictly increasing.
 *
 */
public PolynomialSplineFunction(double knots[], PolynomialFunction polynomials[])
    throws NullArgumentException, NumberIsTooSmallException,
           DimensionMismatchException, NonMonotonicSequenceException{
    if (knots == null ||
        polynomials == null) {
        throw new NullArgumentException();
    }
    if (knots.length < 2) {
        throw new NumberIsTooSmallException(LocalizedFormats.NOT_ENOUGH_POINTS_IN_SPLINE_PARTITION,
                                            2, knots.length, false);
    }
    if (knots.length - 1 != polynomials.length) {
        throw new DimensionMismatchException(polynomials.length, knots.length);
    }
    MathArrays.checkOrder(knots);

    this.n = knots.length -1;
    this.knots = new double[n + 1];
    System.arraycopy(knots, 0, this.knots, 0, n + 1);
    this.polynomials = new PolynomialFunction[n];
    System.arraycopy(polynomials, 0, this.polynomials, 0, n);
}
 
Example #11
Source File: DividedDifferenceInterpolator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute an interpolating function for the dataset.
 *
 * @param x Interpolating points array.
 * @param y Interpolating values array.
 * @return a function which interpolates the dataset.
 * @throws DimensionMismatchException if the array lengths are different.
 * @throws NumberIsTooSmallException if the number of points is less than 2.
 * @throws NonMonotonicSequenceException if {@code x} is not sorted in
 * strictly increasing order.
 */
public PolynomialFunctionNewtonForm interpolate(double x[], double y[])
    throws DimensionMismatchException,
           NumberIsTooSmallException,
           NonMonotonicSequenceException {
    /**
     * a[] and c[] are defined in the general formula of Newton form:
     * p(x) = a[0] + a[1](x-c[0]) + a[2](x-c[0])(x-c[1]) + ... +
     *        a[n](x-c[0])(x-c[1])...(x-c[n-1])
     */
    PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true);

    /**
     * When used for interpolation, the Newton form formula becomes
     * p(x) = f[x0] + f[x0,x1](x-x0) + f[x0,x1,x2](x-x0)(x-x1) + ... +
     *        f[x0,x1,...,x[n-1]](x-x0)(x-x1)...(x-x[n-2])
     * Therefore, a[k] = f[x0,x1,...,xk], c[k] = x[k].
     * <p>
     * Note x[], y[], a[] have the same length but c[]'s size is one less.</p>
     */
    final double[] c = new double[x.length-1];
    System.arraycopy(x, 0, c, 0, c.length);

    final double[] a = computeDividedDifference(x, y);
    return new PolynomialFunctionNewtonForm(a, c);
}
 
Example #12
Source File: PolynomialFunctionLagrangeForm.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Evaluate the Lagrange polynomial using
 * <a href="http://mathworld.wolfram.com/NevillesAlgorithm.html">
 * Neville's Algorithm</a>. It takes O(n^2) time.
 *
 * @param x Interpolating points array.
 * @param y Interpolating values array.
 * @param z Point at which the function value is to be computed.
 * @return the function value.
 * @throws DimensionMismatchException if {@code x} and {@code y} have
 * different lengths.
 * @throws NonMonotonicSequenceException
 * if {@code x} is not sorted in strictly increasing order.
 * @throws NumberIsTooSmallException if the size of {@code x} is less
 * than 2.
 */
public static double evaluate(double x[], double y[], double z)
    throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException {
    if (verifyInterpolationArray(x, y, false)) {
        return evaluateInternal(x, y, z);
    }

    // Array is not sorted.
    final double[] xNew = new double[x.length];
    final double[] yNew = new double[y.length];
    System.arraycopy(x, 0, xNew, 0, x.length);
    System.arraycopy(y, 0, yNew, 0, y.length);

    MathArrays.sortInPlace(xNew, yNew);
    // Second check in case some abscissa is duplicated.
    verifyInterpolationArray(xNew, yNew, true);
    return evaluateInternal(xNew, yNew, z);
}
 
Example #13
Source File: PolynomialSplineFunction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a polynomial spline function with the given segment delimiters
 * and interpolating polynomials.
 * The constructor copies both arrays and assigns the copies to the knots
 * and polynomials properties, respectively.
 *
 * @param knots Spline segment interval delimiters.
 * @param polynomials Polynomial functions that make up the spline.
 * @throws NullArgumentException if either of the input arrays is {@code null}.
 * @throws NumberIsTooSmallException if knots has length less than 2.
 * @throws DimensionMismatchException if {@code polynomials.length != knots.length - 1}.
 * @throws NonMonotonicSequenceException if the {@code knots} array is not strictly increasing.
 *
 */
public PolynomialSplineFunction(double knots[], PolynomialFunction polynomials[])
    throws NullArgumentException, NumberIsTooSmallException,
           DimensionMismatchException, NonMonotonicSequenceException{
    if (knots == null ||
        polynomials == null) {
        throw new NullArgumentException();
    }
    if (knots.length < 2) {
        throw new NumberIsTooSmallException(LocalizedFormats.NOT_ENOUGH_POINTS_IN_SPLINE_PARTITION,
                                            2, knots.length, false);
    }
    if (knots.length - 1 != polynomials.length) {
        throw new DimensionMismatchException(polynomials.length, knots.length);
    }
    MathArrays.checkOrder(knots);

    this.n = knots.length -1;
    this.knots = new double[n + 1];
    System.arraycopy(knots, 0, this.knots, 0, n + 1);
    this.polynomials = new PolynomialFunction[n];
    System.arraycopy(polynomials, 0, this.polynomials, 0, n);
}
 
Example #14
Source File: StepFunction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds a step function from a list of arguments and the corresponding
 * values. Specifically, returns the function h(x) defined by <pre><code>
 * h(x) = y[0] for all x < x[1]
 *        y[1] for x[1] <= x < x[2]
 *        ...
 *        y[y.length - 1] for x >= x[x.length - 1]
 * </code></pre>
 * The value of {@code x[0]} is ignored, but it must be strictly less than
 * {@code x[1]}.
 *
 * @param x Domain values where the function changes value.
 * @param y Values of the function.
 * @throws NonMonotonicSequenceException
 * if the {@code x} array is not sorted in strictly increasing order.
 * @throws NullArgumentException if {@code x} or {@code y} are {@code null}.
 * @throws NoDataException if {@code x} or {@code y} are zero-length.
 * @throws DimensionMismatchException if {@code x} and {@code y} do not
 * have the same length.
 */
public StepFunction(double[] x,
                    double[] y)
    throws NullArgumentException, NoDataException,
           DimensionMismatchException, NonMonotonicSequenceException {
    if (x == null ||
        y == null) {
        throw new NullArgumentException();
    }
    if (x.length == 0 ||
        y.length == 0) {
        throw new NoDataException();
    }
    if (y.length != x.length) {
        throw new DimensionMismatchException(y.length, x.length);
    }
    MathArrays.checkOrder(x);

    abscissa = MathArrays.copyOf(x);
    ordinate = MathArrays.copyOf(y);
}
 
Example #15
Source File: StepFunction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds a step function from a list of arguments and the corresponding
 * values. Specifically, returns the function h(x) defined by <pre><code>
 * h(x) = y[0] for all x < x[1]
 *        y[1] for x[1] <= x < x[2]
 *        ...
 *        y[y.length - 1] for x >= x[x.length - 1]
 * </code></pre>
 * The value of {@code x[0]} is ignored, but it must be strictly less than
 * {@code x[1]}.
 *
 * @param x Domain values where the function changes value.
 * @param y Values of the function.
 * @throws NonMonotonicSequenceException
 * if the {@code x} array is not sorted in strictly increasing order.
 * @throws NullArgumentException if {@code x} or {@code y} are {@code null}.
 * @throws NoDataException if {@code x} or {@code y} are zero-length.
 * @throws DimensionMismatchException if {@code x} and {@code y} do not
 * have the same length.
 */
public StepFunction(double[] x,
                    double[] y)
    throws NullArgumentException, NoDataException,
           DimensionMismatchException, NonMonotonicSequenceException {
    if (x == null ||
        y == null) {
        throw new NullArgumentException();
    }
    if (x.length == 0 ||
        y.length == 0) {
        throw new NoDataException();
    }
    if (y.length != x.length) {
        throw new DimensionMismatchException(y.length, x.length);
    }
    MathArrays.checkOrder(x);

    abscissa = MathArrays.copyOf(x);
    ordinate = MathArrays.copyOf(y);
}
 
Example #16
Source File: NevilleInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of parameters for the interpolator.
 */
@Test
public void testParameters() {
    UnivariateInterpolator interpolator = new NevilleInterpolator();

    try {
        // bad abscissas array
        double x[] = { 1.0, 2.0, 2.0, 4.0 };
        double y[] = { 0.0, 4.0, 4.0, 2.5 };
        UnivariateFunction p = interpolator.interpolate(x, y);
        p.value(0.0);
        Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array");
    } catch (NonMonotonicSequenceException ex) {
        // expected
    }
}
 
Example #17
Source File: NevilleInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of parameters for the interpolator.
 */
@Test
public void testParameters() {
    UnivariateInterpolator interpolator = new NevilleInterpolator();

    try {
        // bad abscissas array
        double x[] = { 1.0, 2.0, 2.0, 4.0 };
        double y[] = { 0.0, 4.0, 4.0, 2.5 };
        UnivariateFunction p = interpolator.interpolate(x, y);
        p.value(0.0);
        Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array");
    } catch (NonMonotonicSequenceException ex) {
        // expected
    }
}
 
Example #18
Source File: PolynomialFunctionLagrangeForm.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Evaluate the Lagrange polynomial using
 * <a href="http://mathworld.wolfram.com/NevillesAlgorithm.html">
 * Neville's Algorithm</a>. It takes O(n^2) time.
 *
 * @param x Interpolating points array.
 * @param y Interpolating values array.
 * @param z Point at which the function value is to be computed.
 * @return the function value.
 * @throws DimensionMismatchException if {@code x} and {@code y} have
 * different lengths.
 * @throws NonMonotonicSequenceException
 * if {@code x} is not sorted in strictly increasing order.
 * @throws NumberIsTooSmallException if the size of {@code x} is less
 * than 2.
 */
public static double evaluate(double x[], double y[], double z)
    throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException {
    if (verifyInterpolationArray(x, y, false)) {
        return evaluateInternal(x, y, z);
    }

    // Array is not sorted.
    final double[] xNew = new double[x.length];
    final double[] yNew = new double[y.length];
    System.arraycopy(x, 0, xNew, 0, x.length);
    System.arraycopy(y, 0, yNew, 0, y.length);

    MathArrays.sortInPlace(xNew, yNew);
    // Second check in case some abscissa is duplicated.
    verifyInterpolationArray(xNew, yNew, true);
    return evaluateInternal(xNew, yNew, z);
}
 
Example #19
Source File: NevilleInterpolatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test of parameters for the interpolator.
 */
@Test
public void testParameters() throws Exception {
    UnivariateInterpolator interpolator = new NevilleInterpolator();

    try {
        // bad abscissas array
        double x[] = { 1.0, 2.0, 2.0, 4.0 };
        double y[] = { 0.0, 4.0, 4.0, 2.5 };
        UnivariateFunction p = interpolator.interpolate(x, y);
        p.value(0.0);
        Assert.fail("Expecting NonMonotonicSequenceException - bad abscissas array");
    } catch (NonMonotonicSequenceException ex) {
        // expected
    }
}
 
Example #20
Source File: DividedDifferenceInterpolator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute an interpolating function for the dataset.
 *
 * @param x Interpolating points array.
 * @param y Interpolating values array.
 * @return a function which interpolates the dataset.
 * @throws DimensionMismatchException if the array lengths are different.
 * @throws NumberIsTooSmallException if the number of points is less than 2.
 * @throws NonMonotonicSequenceException if {@code x} is not sorted in
 * strictly increasing order.
 */
public PolynomialFunctionNewtonForm interpolate(double x[], double y[])
    throws DimensionMismatchException,
           NumberIsTooSmallException,
           NonMonotonicSequenceException {
    /**
     * a[] and c[] are defined in the general formula of Newton form:
     * p(x) = a[0] + a[1](x-c[0]) + a[2](x-c[0])(x-c[1]) + ... +
     *        a[n](x-c[0])(x-c[1])...(x-c[n-1])
     */
    PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true);

    /**
     * When used for interpolation, the Newton form formula becomes
     * p(x) = f[x0] + f[x0,x1](x-x0) + f[x0,x1,x2](x-x0)(x-x1) + ... +
     *        f[x0,x1,...,x[n-1]](x-x0)(x-x1)...(x-x[n-2])
     * Therefore, a[k] = f[x0,x1,...,xk], c[k] = x[k].
     * <p>
     * Note x[], y[], a[] have the same length but c[]'s size is one less.</p>
     */
    final double[] c = new double[x.length-1];
    System.arraycopy(x, 0, c, 0, c.length);

    final double[] a = computeDividedDifference(x, y);
    return new PolynomialFunctionNewtonForm(a, c);
}
 
Example #21
Source File: DividedDifferenceInterpolator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return a copy of the divided difference array.
 * <p>
 * The divided difference array is defined recursively by <pre>
 * f[x0] = f(x0)
 * f[x0,x1,...,xk] = (f[x1,...,xk] - f[x0,...,x[k-1]]) / (xk - x0)
 * </pre></p>
 * <p>
 * The computational complexity is O(N^2).</p>
 *
 * @param x Interpolating points array.
 * @param y Interpolating values array.
 * @return a fresh copy of the divided difference array.
 * @throws DimensionMismatchException if the array lengths are different.
 * @throws NumberIsTooSmallException if the number of points is less than 2.
 * @throws NonMonotonicSequenceException
 * if {@code x} is not sorted in strictly increasing order.
 */
protected static double[] computeDividedDifference(final double x[], final double y[])
    throws DimensionMismatchException,
           NumberIsTooSmallException,
           NonMonotonicSequenceException {
    PolynomialFunctionLagrangeForm.verifyInterpolationArray(x, y, true);

    final double[] divdiff = y.clone(); // initialization

    final int n = x.length;
    final double[] a = new double [n];
    a[0] = divdiff[0];
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < n-i; j++) {
            final double denominator = x[j+i] - x[j];
            divdiff[j] = (divdiff[j+1] - divdiff[j]) / denominator;
        }
        a[i] = divdiff[0];
    }

    return a;
}
 
Example #22
Source File: PiecewiseBicubicSplineInterpolator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public PiecewiseBicubicSplineInterpolatingFunction interpolate( final double[] xval,
                                                                final double[] yval,
                                                                final double[][] fval)
    throws DimensionMismatchException,
           NullArgumentException,
           NoDataException,
           NonMonotonicSequenceException {
    if ( xval == null ||
         yval == null ||
         fval == null ||
         fval[0] == null ) {
        throw new NullArgumentException();
    }

    if ( xval.length == 0 ||
         yval.length == 0 ||
         fval.length == 0 ) {
        throw new NoDataException();
    }

    MathArrays.checkOrder(xval);
    MathArrays.checkOrder(yval);

    return new PiecewiseBicubicSplineInterpolatingFunction( xval, yval, fval );
}
 
Example #23
Source File: GaussIntegrator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an integrator from the given {@code points} and {@code weights}.
 * The integration interval is defined by the first and last value of
 * {@code points} which must be sorted in increasing order.
 *
 * @param points Integration points.
 * @param weights Weights of the corresponding integration nodes.
 * @throws NonMonotonicSequenceException if the {@code points} are not
 * sorted in increasing order.
 * @throws DimensionMismatchException if points and weights don't have the same length
 */
public GaussIntegrator(double[] points,
                       double[] weights)
    throws NonMonotonicSequenceException, DimensionMismatchException {
    if (points.length != weights.length) {
        throw new DimensionMismatchException(points.length,
                                             weights.length);
    }

    MathArrays.checkOrder(points, MathArrays.OrderDirection.INCREASING, true, true);

    this.points = points.clone();
    this.weights = weights.clone();
}
 
Example #24
Source File: LinearInterpolator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes a linear interpolating function for the data set.
 *
 * @param x the arguments for the interpolation points
 * @param y the values for the interpolation points
 * @return a function which interpolates the data set
 * @throws DimensionMismatchException if {@code x} and {@code y}
 * have different sizes.
 * @throws NonMonotonicSequenceException if {@code x} is not sorted in
 * strict increasing order.
 * @throws NumberIsTooSmallException if the size of {@code x} is smaller
 * than 2.
 */
public PolynomialSplineFunction interpolate(double x[], double y[])
    throws DimensionMismatchException,
           NumberIsTooSmallException,
           NonMonotonicSequenceException {
    if (x.length != y.length) {
        throw new DimensionMismatchException(x.length, y.length);
    }

    if (x.length < 2) {
        throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
                                            x.length, 2, true);
    }

    // Number of intervals.  The number of data points is n + 1.
    int n = x.length - 1;

    MathArrays.checkOrder(x);

    // Slope of the lines between the datapoints.
    final double m[] = new double[n];
    for (int i = 0; i < n; i++) {
        m[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]);
    }

    final PolynomialFunction polynomials[] = new PolynomialFunction[n];
    final double coefficients[] = new double[2];
    for (int i = 0; i < n; i++) {
        coefficients[0] = y[i];
        coefficients[1] = m[i];
        polynomials[i] = new PolynomialFunction(coefficients);
    }

    return new PolynomialSplineFunction(x, polynomials);
}
 
Example #25
Source File: LinearInterpolator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes a linear interpolating function for the data set.
 *
 * @param x the arguments for the interpolation points
 * @param y the values for the interpolation points
 * @return a function which interpolates the data set
 * @throws DimensionMismatchException if {@code x} and {@code y}
 * have different sizes.
 * @throws NonMonotonicSequenceException if {@code x} is not sorted in
 * strict increasing order.
 * @throws NumberIsTooSmallException if the size of {@code x} is smaller
 * than 2.
 */
public PolynomialSplineFunction interpolate(double x[], double y[])
    throws DimensionMismatchException,
           NumberIsTooSmallException,
           NonMonotonicSequenceException {
    if (x.length != y.length) {
        throw new DimensionMismatchException(x.length, y.length);
    }

    if (x.length < 2) {
        throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
                                            x.length, 2, true);
    }

    // Number of intervals.  The number of data points is n + 1.
    int n = x.length - 1;

    MathArrays.checkOrder(x);

    // Slope of the lines between the datapoints.
    final double m[] = new double[n];
    for (int i = 0; i < n; i++) {
        m[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]);
    }

    final PolynomialFunction polynomials[] = new PolynomialFunction[n];
    final double coefficients[] = new double[2];
    for (int i = 0; i < n; i++) {
        coefficients[0] = y[i];
        coefficients[1] = m[i];
        polynomials[i] = new PolynomialFunction(coefficients);
    }

    return new PolynomialSplineFunction(x, polynomials);
}
 
Example #26
Source File: UnivariatePeriodicInterpolatorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=NonMonotonicSequenceException.class)
public void testUnsortedSamples() {
    final double[] xval = { 2, 3, 7, 4, 6 };
    final double[] yval = { 1, 6, 5, -1, -2 };
    final double period = 10;

    final UnivariateInterpolator interpolator
        = new UnivariatePeriodicInterpolator(new LinearInterpolator(), period);
    interpolator.interpolate(xval, yval);
}
 
Example #27
Source File: GaussIntegrator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an integrator from the given {@code points} and {@code weights}.
 * The integration interval is defined by the first and last value of
 * {@code points} which must be sorted in increasing order.
 *
 * @param points Integration points.
 * @param weights Weights of the corresponding integration nodes.
 * @throws NonMonotonicSequenceException if the {@code points} are not
 * sorted in increasing order.
 */
public GaussIntegrator(double[] points,
                       double[] weights)
    throws NonMonotonicSequenceException {
    if (points.length != weights.length) {
        throw new DimensionMismatchException(points.length,
                                             weights.length);
    }

    MathArrays.checkOrder(points, MathArrays.OrderDirection.INCREASING, true, true);

    this.points = points.clone();
    this.weights = weights.clone();
}
 
Example #28
Source File: GaussIntegrator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an integrator from the given {@code points} and {@code weights}.
 * The integration interval is defined by the first and last value of
 * {@code points} which must be sorted in increasing order.
 *
 * @param points Integration points.
 * @param weights Weights of the corresponding integration nodes.
 * @throws NonMonotonicSequenceException if the {@code points} are not
 * sorted in increasing order.
 * @throws DimensionMismatchException if points and weights don't have the same length
 */
public GaussIntegrator(double[] points,
                       double[] weights)
    throws NonMonotonicSequenceException, DimensionMismatchException {
    if (points.length != weights.length) {
        throw new DimensionMismatchException(points.length,
                                             weights.length);
    }

    MathArrays.checkOrder(points, MathArrays.OrderDirection.INCREASING, true, true);

    this.points = points.clone();
    this.weights = weights.clone();
}
 
Example #29
Source File: PolynomialFunctionLagrangeForm.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a Lagrange polynomial with the given abscissas and function
 * values. The order of interpolating points are not important.
 * <p>
 * The constructor makes copy of the input arrays and assigns them.</p>
 *
 * @param x interpolating points
 * @param y function values at interpolating points
 * @throws DimensionMismatchException if the array lengths are different.
 * @throws NumberIsTooSmallException if the number of points is less than 2.
 * @throws NonMonotonicSequenceException
 * if two abscissae have the same value.
 */
public PolynomialFunctionLagrangeForm(double x[], double y[])
    throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException {
    this.x = new double[x.length];
    this.y = new double[y.length];
    System.arraycopy(x, 0, this.x, 0, x.length);
    System.arraycopy(y, 0, this.y, 0, y.length);
    coefficientsComputed = false;

    if (!verifyInterpolationArray(x, y, false)) {
        MathArrays.sortInPlace(this.x, this.y);
        // Second check in case some abscissa is duplicated.
        verifyInterpolationArray(this.x, this.y, true);
    }
}
 
Example #30
Source File: UnivariatePeriodicInterpolatorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test(expected=NonMonotonicSequenceException.class)
public void testUnsortedSamples() {
    final double[] xval = { 2, 3, 7, 4, 6 };
    final double[] yval = { 1, 6, 5, -1, -2 };
    final double period = 10;

    final UnivariateInterpolator interpolator
        = new UnivariatePeriodicInterpolator(new LinearInterpolator(), period);
    interpolator.interpolate(xval, yval);
}