Java Code Examples for org.apache.commons.math.util.MathUtils#checkOrder()

The following examples show how to use org.apache.commons.math.util.MathUtils#checkOrder() . 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 org.apache.commons.math.exception.NonMonotonousSequenceException if
 * the {@code knots} array is not strictly increasing.
 *
 */
public PolynomialSplineFunction(double knots[], PolynomialFunction polynomials[]) {
    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);
    }
    MathUtils.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: 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 abscissae and the corresponding
 * ordinates.
 *
 * @param x Abscissae.
 * @param y Ordinates.
 * @throws org.apache.commons.math.exception.NonMonotonousSequenceException
 * 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.
 */
public StepFunction(double[] x,
                    double[] y) {
    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);
    }
    MathUtils.checkOrder(x);

    abscissa = MathUtils.copyOf(x);
    ordinate = MathUtils.copyOf(y);
}
 
Example 3
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 abscissae and the corresponding
 * ordinates.
 *
 * @param x Abscissae.
 * @param y Ordinates.
 * @throws org.apache.commons.math.exception.NonMonotonousSequenceException
 * 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.
 */
public StepFunction(double[] x,
                    double[] y) {
    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);
    }
    MathUtils.checkOrder(x);

    abscissa = MathUtils.copyOf(x);
    ordinate = MathUtils.copyOf(y);
}
 
Example 4
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 abscissae and the corresponding
 * ordinates.
 *
 * @param x Abscissae.
 * @param y Ordinates.
 * @throws org.apache.commons.math.exception.NonMonotonousSequenceException
 * 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.
 */
public StepFunction(double[] x,
                    double[] y) {
    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);
    }
    MathUtils.checkOrder(x);

    abscissa = MathUtils.copyOf(x);
    ordinate = MathUtils.copyOf(y);
}
 
Example 5
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 org.apache.commons.math.exception.NonMonotonousSequenceException
 * 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[]) {
    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;

    MathUtils.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]);
    }

    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 6
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 org.apache.commons.math.exception.NonMonotonousSequenceException
 * 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[]) {
    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;

    MathUtils.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]);
    }

    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 7
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 org.apache.commons.math.exception.NonMonotonousSequenceException
 * 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[]) {
    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;

    MathUtils.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]);
    }

    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 8
Source File: BicubicSplineInterpolatingFunction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param x Sample values of the x-coordinate, in increasing order.
 * @param y Sample values of the y-coordinate, in increasing order.
 * @param f Values of the function on every grid point.
 * @param dFdX Values of the partial derivative of function with respect
 * to x on every grid point.
 * @param dFdY Values of the partial derivative of function with respect
 * to y on every grid point.
 * @param d2FdXdY Values of the cross partial derivative of function on
 * every grid point.
 * @throws DimensionMismatchException if the various arrays do not contain
 * the expected number of elements.
 * @throws org.apache.commons.math.exception.NonMonotonousSequenceException
 * if {@code x} or {@code y} are not strictly increasing.
 * @throws NoDataException if any of the arrays has zero length.
 */
public BicubicSplineInterpolatingFunction(double[] x,
                                          double[] y,
                                          double[][] f,
                                          double[][] dFdX,
                                          double[][] dFdY,
                                          double[][] d2FdXdY)
    throws DimensionMismatchException {
    final int xLen = x.length;
    final int yLen = y.length;

    if (xLen == 0 || yLen == 0 || f.length == 0 || f[0].length == 0) {
        throw new NoDataException();
    }
    if (xLen != f.length) {
        throw new DimensionMismatchException(xLen, f.length);
    }
    if (xLen != dFdX.length) {
        throw new DimensionMismatchException(xLen, dFdX.length);
    }
    if (xLen != dFdY.length) {
        throw new DimensionMismatchException(xLen, dFdY.length);
    }
    if (xLen != d2FdXdY.length) {
        throw new DimensionMismatchException(xLen, d2FdXdY.length);
    }

    MathUtils.checkOrder(x);
    MathUtils.checkOrder(y);

    xval = x.clone();
    yval = y.clone();

    final int lastI = xLen - 1;
    final int lastJ = yLen - 1;
    splines = new BicubicSplineFunction[lastI][lastJ];

    for (int i = 0; i < lastI; i++) {
        if (f[i].length != yLen) {
            throw new DimensionMismatchException(f[i].length, yLen);
        }
        if (dFdX[i].length != yLen) {
            throw new DimensionMismatchException(dFdX[i].length, yLen);
        }
        if (dFdY[i].length != yLen) {
            throw new DimensionMismatchException(dFdY[i].length, yLen);
        }
        if (d2FdXdY[i].length != yLen) {
            throw new DimensionMismatchException(d2FdXdY[i].length, yLen);
        }
        final int ip1 = i + 1;
        for (int j = 0; j < lastJ; j++) {
            final int jp1 = j + 1;
            final double[] beta = new double[] {
                f[i][j], f[ip1][j], f[i][jp1], f[ip1][jp1],
                dFdX[i][j], dFdX[ip1][j], dFdX[i][jp1], dFdX[ip1][jp1],
                dFdY[i][j], dFdY[ip1][j], dFdY[i][jp1], dFdY[ip1][jp1],
                d2FdXdY[i][j], d2FdXdY[ip1][j], d2FdXdY[i][jp1], d2FdXdY[ip1][jp1]
            };

            splines[i][j] = new BicubicSplineFunction(computeSplineCoefficients(beta));
        }
    }
}
 
Example 9
Source File: SplineInterpolator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Computes an 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 org.apache.commons.math.exception.NonMonotonousSequenceException
 * if {@code x} is not sorted in strict increasing order.
 * @throws NumberIsTooSmallException if the size of {@code x} is smaller
 * than 3.
 */
public PolynomialSplineFunction interpolate(double x[], double y[]) {
    if (x.length != y.length) {
        throw new DimensionMismatchException(x.length, y.length);
    }

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

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

    MathUtils.checkOrder(x);

    // Differences between knot points
    double h[] = new double[n];
    for (int i = 0; i < n; i++) {
        h[i] = x[i + 1] - x[i];
    }

    double mu[] = new double[n];
    double z[] = new double[n + 1];
    mu[0] = 0d;
    z[0] = 0d;
    double g = 0;
    for (int i = 1; i < n; i++) {
        g = 2d * (x[i+1]  - x[i - 1]) - h[i - 1] * mu[i -1];
        mu[i] = h[i] / g;
        z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) /
                (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g;
    }

    // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
    double b[] = new double[n];
    double c[] = new double[n + 1];
    double d[] = new double[n];

    z[n] = 0d;
    c[n] = 0d;

    for (int j = n -1; j >=0; j--) {
        c[j] = z[j] - mu[j] * c[j + 1];
        b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d;
        d[j] = (c[j + 1] - c[j]) / (3d * h[j]);
    }

    PolynomialFunction polynomials[] = new PolynomialFunction[n];
    double coefficients[] = new double[4];
    for (int i = 0; i < n; i++) {
        coefficients[0] = y[i];
        coefficients[1] = b[i];
        coefficients[2] = c[i];
        coefficients[3] = d[i];
        polynomials[i] = new PolynomialFunction(coefficients);
    }

    return new PolynomialSplineFunction(x, polynomials);
}
 
Example 10
Source File: BicubicSplineInterpolatingFunction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param x Sample values of the x-coordinate, in increasing order.
 * @param y Sample values of the y-coordinate, in increasing order.
 * @param f Values of the function on every grid point.
 * @param dFdX Values of the partial derivative of function with respect
 * to x on every grid point.
 * @param dFdY Values of the partial derivative of function with respect
 * to y on every grid point.
 * @param d2FdXdY Values of the cross partial derivative of function on
 * every grid point.
 * @throws DimensionMismatchException if the various arrays do not contain
 * the expected number of elements.
 * @throws IllegalArgumentException if {@code x} or {@code y} are not strictly
 * increasing.
 */
public BicubicSplineInterpolatingFunction(double[] x,
                                          double[] y,
                                          double[][] f,
                                          double[][] dFdX,
                                          double[][] dFdY,
                                          double[][] d2FdXdY)
    throws DimensionMismatchException {
    final int xLen = x.length;
    final int yLen = y.length;

    if (xLen == 0 || yLen == 0 || f.length == 0 || f[0].length == 0) {
        throw MathRuntimeException.createIllegalArgumentException("no data");
    }
    if (xLen != f.length) {
        throw new DimensionMismatchException(xLen, f.length);
    }
    if (xLen != dFdX.length) {
        throw new DimensionMismatchException(xLen, dFdX.length);
    }
    if (xLen != dFdY.length) {
        throw new DimensionMismatchException(xLen, dFdY.length);
    }
    if (xLen != d2FdXdY.length) {
        throw new DimensionMismatchException(xLen, d2FdXdY.length);
    }

    MathUtils.checkOrder(x, 1, true);
    MathUtils.checkOrder(y, 1, true);

    xval = x.clone();
    yval = y.clone();

    final int lastI = xLen - 1;
    final int lastJ = yLen - 1;
    splines = new BicubicSplineFunction[lastI][lastJ];

    for (int i = 0; i < lastI; i++) {
        if (f[i].length != yLen) {
            throw new DimensionMismatchException(f[i].length, yLen);
        }
        if (dFdX[i].length != yLen) {
            throw new DimensionMismatchException(dFdX[i].length, yLen);
        }
        if (dFdY[i].length != yLen) {
            throw new DimensionMismatchException(dFdY[i].length, yLen);
        }
        if (d2FdXdY[i].length != yLen) {
            throw new DimensionMismatchException(d2FdXdY[i].length, yLen);
        }
        final int ip1 = i + 1;
        for (int j = 0; j < lastJ; j++) {
            final int jp1 = j + 1;
            final double[] beta = new double[] {
                f[i][j], f[ip1][j], f[i][jp1], f[ip1][jp1],
                dFdX[i][j], dFdX[ip1][j], dFdX[i][jp1], dFdX[ip1][jp1],
                dFdY[i][j], dFdY[ip1][j], dFdY[i][jp1], dFdY[ip1][jp1],
                d2FdXdY[i][j], d2FdXdY[ip1][j], d2FdXdY[i][jp1], d2FdXdY[ip1][jp1]
            };

            splines[i][j] = new BicubicSplineFunction(computeSplineCoefficients(beta));
        }
    }
}
 
Example 11
Source File: SplineInterpolator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Computes an 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 org.apache.commons.math.exception.NonMonotonousSequenceException
 * if {@code x} is not sorted in strict increasing order.
 * @throws NumberIsTooSmallException if the size of {@code x} is smaller
 * than 3.
 */
public PolynomialSplineFunction interpolate(double x[], double y[]) {
    if (x.length != y.length) {
        throw new DimensionMismatchException(x.length, y.length);
    }

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

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

    MathUtils.checkOrder(x);

    // Differences between knot points
    double h[] = new double[n];
    for (int i = 0; i < n; i++) {
        h[i] = x[i + 1] - x[i];
    }

    double mu[] = new double[n];
    double z[] = new double[n + 1];
    mu[0] = 0d;
    z[0] = 0d;
    double g = 0;
    for (int i = 1; i < n; i++) {
        g = 2d * (x[i+1]  - x[i - 1]) - h[i - 1] * mu[i -1];
        mu[i] = h[i] / g;
        z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) /
                (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g;
    }

    // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
    double b[] = new double[n];
    double c[] = new double[n + 1];
    double d[] = new double[n];

    z[n] = 0d;
    c[n] = 0d;

    for (int j = n -1; j >=0; j--) {
        c[j] = z[j] - mu[j] * c[j + 1];
        b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d;
        d[j] = (c[j + 1] - c[j]) / (3d * h[j]);
    }

    PolynomialFunction polynomials[] = new PolynomialFunction[n];
    double coefficients[] = new double[4];
    for (int i = 0; i < n; i++) {
        coefficients[0] = y[i];
        coefficients[1] = b[i];
        coefficients[2] = c[i];
        coefficients[3] = d[i];
        polynomials[i] = new PolynomialFunction(coefficients);
    }

    return new PolynomialSplineFunction(x, polynomials);
}
 
Example 12
Source File: BicubicSplineInterpolatingFunction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param x Sample values of the x-coordinate, in increasing order.
 * @param y Sample values of the y-coordinate, in increasing order.
 * @param f Values of the function on every grid point.
 * @param dFdX Values of the partial derivative of function with respect
 * to x on every grid point.
 * @param dFdY Values of the partial derivative of function with respect
 * to y on every grid point.
 * @param d2FdXdY Values of the cross partial derivative of function on
 * every grid point.
 * @throws DimensionMismatchException if the various arrays do not contain
 * the expected number of elements.
 * @throws org.apache.commons.math.exception.NonMonotonousSequenceException
 * if {@code x} or {@code y} are not strictly increasing.
 * @throws NoDataException if any of the arrays has zero length.
 */
public BicubicSplineInterpolatingFunction(double[] x,
                                          double[] y,
                                          double[][] f,
                                          double[][] dFdX,
                                          double[][] dFdY,
                                          double[][] d2FdXdY)
    throws DimensionMismatchException {
    final int xLen = x.length;
    final int yLen = y.length;

    if (xLen == 0 || yLen == 0 || f.length == 0 || f[0].length == 0) {
        throw new NoDataException();
    }
    if (xLen != f.length) {
        throw new DimensionMismatchException(xLen, f.length);
    }
    if (xLen != dFdX.length) {
        throw new DimensionMismatchException(xLen, dFdX.length);
    }
    if (xLen != dFdY.length) {
        throw new DimensionMismatchException(xLen, dFdY.length);
    }
    if (xLen != d2FdXdY.length) {
        throw new DimensionMismatchException(xLen, d2FdXdY.length);
    }

    MathUtils.checkOrder(x);
    MathUtils.checkOrder(y);

    xval = x.clone();
    yval = y.clone();

    final int lastI = xLen - 1;
    final int lastJ = yLen - 1;
    splines = new BicubicSplineFunction[lastI][lastJ];

    for (int i = 0; i < lastI; i++) {
        if (f[i].length != yLen) {
            throw new DimensionMismatchException(f[i].length, yLen);
        }
        if (dFdX[i].length != yLen) {
            throw new DimensionMismatchException(dFdX[i].length, yLen);
        }
        if (dFdY[i].length != yLen) {
            throw new DimensionMismatchException(dFdY[i].length, yLen);
        }
        if (d2FdXdY[i].length != yLen) {
            throw new DimensionMismatchException(d2FdXdY[i].length, yLen);
        }
        final int ip1 = i + 1;
        for (int j = 0; j < lastJ; j++) {
            final int jp1 = j + 1;
            final double[] beta = new double[] {
                f[i][j], f[ip1][j], f[i][jp1], f[ip1][jp1],
                dFdX[i][j], dFdX[ip1][j], dFdX[i][jp1], dFdX[ip1][jp1],
                dFdY[i][j], dFdY[ip1][j], dFdY[i][jp1], dFdY[ip1][jp1],
                d2FdXdY[i][j], d2FdXdY[ip1][j], d2FdXdY[i][jp1], d2FdXdY[ip1][jp1]
            };

            splines[i][j] = new BicubicSplineFunction(computeSplineCoefficients(beta));
        }
    }
}
 
Example 13
Source File: BicubicSplineInterpolatingFunction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param x Sample values of the x-coordinate, in increasing order.
 * @param y Sample values of the y-coordinate, in increasing order.
 * @param f Values of the function on every grid point.
 * @param dFdX Values of the partial derivative of function with respect
 * to x on every grid point.
 * @param dFdY Values of the partial derivative of function with respect
 * to y on every grid point.
 * @param d2FdXdY Values of the cross partial derivative of function on
 * every grid point.
 * @throws DimensionMismatchException if the various arrays do not contain
 * the expected number of elements.
 * @throws org.apache.commons.math.exception.NonMonotonousSequenceException
 * if {@code x} or {@code y} are not strictly increasing.
 * @throws NoDataException if any of the arrays has zero length.
 */
public BicubicSplineInterpolatingFunction(double[] x,
                                          double[] y,
                                          double[][] f,
                                          double[][] dFdX,
                                          double[][] dFdY,
                                          double[][] d2FdXdY)
    throws DimensionMismatchException {
    final int xLen = x.length;
    final int yLen = y.length;

    if (xLen == 0 || yLen == 0 || f.length == 0 || f[0].length == 0) {
        throw new NoDataException();
    }
    if (xLen != f.length) {
        throw new DimensionMismatchException(xLen, f.length);
    }
    if (xLen != dFdX.length) {
        throw new DimensionMismatchException(xLen, dFdX.length);
    }
    if (xLen != dFdY.length) {
        throw new DimensionMismatchException(xLen, dFdY.length);
    }
    if (xLen != d2FdXdY.length) {
        throw new DimensionMismatchException(xLen, d2FdXdY.length);
    }

    MathUtils.checkOrder(x);
    MathUtils.checkOrder(y);

    xval = x.clone();
    yval = y.clone();

    final int lastI = xLen - 1;
    final int lastJ = yLen - 1;
    splines = new BicubicSplineFunction[lastI][lastJ];

    for (int i = 0; i < lastI; i++) {
        if (f[i].length != yLen) {
            throw new DimensionMismatchException(f[i].length, yLen);
        }
        if (dFdX[i].length != yLen) {
            throw new DimensionMismatchException(dFdX[i].length, yLen);
        }
        if (dFdY[i].length != yLen) {
            throw new DimensionMismatchException(dFdY[i].length, yLen);
        }
        if (d2FdXdY[i].length != yLen) {
            throw new DimensionMismatchException(d2FdXdY[i].length, yLen);
        }
        final int ip1 = i + 1;
        for (int j = 0; j < lastJ; j++) {
            final int jp1 = j + 1;
            final double[] beta = new double[] {
                f[i][j], f[ip1][j], f[i][jp1], f[ip1][jp1],
                dFdX[i][j], dFdX[ip1][j], dFdX[i][jp1], dFdX[ip1][jp1],
                dFdY[i][j], dFdY[ip1][j], dFdY[i][jp1], dFdY[ip1][jp1],
                d2FdXdY[i][j], d2FdXdY[ip1][j], d2FdXdY[i][jp1], d2FdXdY[ip1][jp1]
            };

            splines[i][j] = new BicubicSplineFunction(computeSplineCoefficients(beta));
        }
    }
}
 
Example 14
Source File: BicubicSplineInterpolatingFunction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param x Sample values of the x-coordinate, in increasing order.
 * @param y Sample values of the y-coordinate, in increasing order.
 * @param f Values of the function on every grid point.
 * @param dFdX Values of the partial derivative of function with respect
 * to x on every grid point.
 * @param dFdY Values of the partial derivative of function with respect
 * to y on every grid point.
 * @param d2FdXdY Values of the cross partial derivative of function on
 * every grid point.
 * @throws DimensionMismatchException if the various arrays do not contain
 * the expected number of elements.
 * @throws IllegalArgumentException if {@code x} or {@code y} are not strictly
 * increasing.
 */
public BicubicSplineInterpolatingFunction(double[] x,
                                          double[] y,
                                          double[][] f,
                                          double[][] dFdX,
                                          double[][] dFdY,
                                          double[][] d2FdXdY)
    throws DimensionMismatchException {
    final int xLen = x.length;
    final int yLen = y.length;

    if (xLen == 0 || yLen == 0 || f.length == 0 || f[0].length == 0) {
        throw MathRuntimeException.createIllegalArgumentException("no data");
    }
    if (xLen != f.length) {
        throw new DimensionMismatchException(xLen, f.length);
    }
    if (xLen != dFdX.length) {
        throw new DimensionMismatchException(xLen, dFdX.length);
    }
    if (xLen != dFdY.length) {
        throw new DimensionMismatchException(xLen, dFdY.length);
    }
    if (xLen != d2FdXdY.length) {
        throw new DimensionMismatchException(xLen, d2FdXdY.length);
    }

    MathUtils.checkOrder(x, 1, true);
    MathUtils.checkOrder(y, 1, true);

    xval = x.clone();
    yval = y.clone();

    final int lastI = xLen - 1;
    final int lastJ = yLen - 1;
    splines = new BicubicSplineFunction[lastI][lastJ];

    for (int i = 0; i < lastI; i++) {
        if (f[i].length != yLen) {
            throw new DimensionMismatchException(f[i].length, yLen);
        }
        if (dFdX[i].length != yLen) {
            throw new DimensionMismatchException(dFdX[i].length, yLen);
        }
        if (dFdY[i].length != yLen) {
            throw new DimensionMismatchException(dFdY[i].length, yLen);
        }
        if (d2FdXdY[i].length != yLen) {
            throw new DimensionMismatchException(d2FdXdY[i].length, yLen);
        }
        final int ip1 = i + 1;
        for (int j = 0; j < lastJ; j++) {
            final int jp1 = j + 1;
            final double[] beta = new double[] {
                f[i][j], f[ip1][j], f[i][jp1], f[ip1][jp1],
                dFdX[i][j], dFdX[ip1][j], dFdX[i][jp1], dFdX[ip1][jp1],
                dFdY[i][j], dFdY[ip1][j], dFdY[i][jp1], dFdY[ip1][jp1],
                d2FdXdY[i][j], d2FdXdY[ip1][j], d2FdXdY[i][jp1], d2FdXdY[ip1][jp1]
            };

            splines[i][j] = new BicubicSplineFunction(computeSplineCoefficients(beta));
        }
    }
}
 
Example 15
Source File: BicubicSplineInterpolatingFunction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param x Sample values of the x-coordinate, in increasing order.
 * @param y Sample values of the y-coordinate, in increasing order.
 * @param f Values of the function on every grid point.
 * @param dFdX Values of the partial derivative of function with respect
 * to x on every grid point.
 * @param dFdY Values of the partial derivative of function with respect
 * to y on every grid point.
 * @param d2FdXdY Values of the cross partial derivative of function on
 * every grid point.
 * @throws DimensionMismatchException if the various arrays do not contain
 * the expected number of elements.
 * @throws IllegalArgumentException if {@code x} or {@code y} are not strictly
 * increasing.
 */
public BicubicSplineInterpolatingFunction(double[] x,
                                          double[] y,
                                          double[][] f,
                                          double[][] dFdX,
                                          double[][] dFdY,
                                          double[][] d2FdXdY)
    throws DimensionMismatchException {
    final int xLen = x.length;
    final int yLen = y.length;

    if (xLen == 0 || yLen == 0 || f.length == 0 || f[0].length == 0) {
        throw MathRuntimeException.createIllegalArgumentException("no data");
    }
    if (xLen != f.length) {
        throw new DimensionMismatchException(xLen, f.length);
    }
    if (xLen != dFdX.length) {
        throw new DimensionMismatchException(xLen, dFdX.length);
    }
    if (xLen != dFdY.length) {
        throw new DimensionMismatchException(xLen, dFdY.length);
    }
    if (xLen != d2FdXdY.length) {
        throw new DimensionMismatchException(xLen, d2FdXdY.length);
    }

    MathUtils.checkOrder(x, 1, true);
    MathUtils.checkOrder(y, 1, true);

    xval = x.clone();
    yval = y.clone();

    final int lastI = xLen - 1;
    final int lastJ = yLen - 1;
    splines = new BicubicSplineFunction[lastI][lastJ];

    for (int i = 0; i < lastI; i++) {
        if (f[i].length != yLen) {
            throw new DimensionMismatchException(f[i].length, yLen);
        }
        if (dFdX[i].length != yLen) {
            throw new DimensionMismatchException(dFdX[i].length, yLen);
        }
        if (dFdY[i].length != yLen) {
            throw new DimensionMismatchException(dFdY[i].length, yLen);
        }
        if (d2FdXdY[i].length != yLen) {
            throw new DimensionMismatchException(d2FdXdY[i].length, yLen);
        }
        final int ip1 = i + 1;
        for (int j = 0; j < lastJ; j++) {
            final int jp1 = j + 1;
            final double[] beta = new double[] {
                f[i][j], f[ip1][j], f[i][jp1], f[ip1][jp1],
                dFdX[i][j], dFdX[ip1][j], dFdX[i][jp1], dFdX[ip1][jp1],
                dFdY[i][j], dFdY[ip1][j], dFdY[i][jp1], dFdY[ip1][jp1],
                d2FdXdY[i][j], d2FdXdY[ip1][j], d2FdXdY[i][jp1], d2FdXdY[ip1][jp1]
            };

            splines[i][j] = new BicubicSplineFunction(computeSplineCoefficients(beta));
        }
    }
}
 
Example 16
Source File: BicubicSplineInterpolatingFunction.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param x Sample values of the x-coordinate, in increasing order.
 * @param y Sample values of the y-coordinate, in increasing order.
 * @param f Values of the function on every grid point.
 * @param dFdX Values of the partial derivative of function with respect
 * to x on every grid point.
 * @param dFdY Values of the partial derivative of function with respect
 * to y on every grid point.
 * @param d2FdXdY Values of the cross partial derivative of function on
 * every grid point.
 * @throws DimensionMismatchException if the various arrays do not contain
 * the expected number of elements.
 * @throws IllegalArgumentException if {@code x} or {@code y} are not strictly
 * increasing.
 */
public BicubicSplineInterpolatingFunction(double[] x,
                                          double[] y,
                                          double[][] f,
                                          double[][] dFdX,
                                          double[][] dFdY,
                                          double[][] d2FdXdY)
    throws DimensionMismatchException {
    final int xLen = x.length;
    final int yLen = y.length;

    if (xLen == 0 || yLen == 0 || f.length == 0 || f[0].length == 0) {
        throw MathRuntimeException.createIllegalArgumentException("no data");
    }
    if (xLen != f.length) {
        throw new DimensionMismatchException(xLen, f.length);
    }
    if (xLen != dFdX.length) {
        throw new DimensionMismatchException(xLen, dFdX.length);
    }
    if (xLen != dFdY.length) {
        throw new DimensionMismatchException(xLen, dFdY.length);
    }
    if (xLen != d2FdXdY.length) {
        throw new DimensionMismatchException(xLen, d2FdXdY.length);
    }

    MathUtils.checkOrder(x, 1, true);
    MathUtils.checkOrder(y, 1, true);

    xval = x.clone();
    yval = y.clone();

    final int lastI = xLen - 1;
    final int lastJ = yLen - 1;
    splines = new BicubicSplineFunction[lastI][lastJ];

    for (int i = 0; i < lastI; i++) {
        if (f[i].length != yLen) {
            throw new DimensionMismatchException(f[i].length, yLen);
        }
        if (dFdX[i].length != yLen) {
            throw new DimensionMismatchException(dFdX[i].length, yLen);
        }
        if (dFdY[i].length != yLen) {
            throw new DimensionMismatchException(dFdY[i].length, yLen);
        }
        if (d2FdXdY[i].length != yLen) {
            throw new DimensionMismatchException(d2FdXdY[i].length, yLen);
        }
        final int ip1 = i + 1;
        for (int j = 0; j < lastJ; j++) {
            final int jp1 = j + 1;
            final double[] beta = new double[] {
                f[i][j], f[ip1][j], f[i][jp1], f[ip1][jp1],
                dFdX[i][j], dFdX[ip1][j], dFdX[i][jp1], dFdX[ip1][jp1],
                dFdY[i][j], dFdY[ip1][j], dFdY[i][jp1], dFdY[ip1][jp1],
                d2FdXdY[i][j], d2FdXdY[ip1][j], d2FdXdY[i][jp1], d2FdXdY[ip1][jp1]
            };

            splines[i][j] = new BicubicSplineFunction(computeSplineCoefficients(beta));
        }
    }
}
 
Example 17
Source File: SplineInterpolator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Computes an 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 org.apache.commons.math.exception.NonMonotonousSequenceException
 * if {@code x} is not sorted in strict increasing order.
 * @throws NumberIsTooSmallException if the size of {@code x} is smaller
 * than 3.
 */
public PolynomialSplineFunction interpolate(double x[], double y[]) {
    if (x.length != y.length) {
        throw new DimensionMismatchException(x.length, y.length);
    }

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

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

    MathUtils.checkOrder(x);

    // Differences between knot points
    double h[] = new double[n];
    for (int i = 0; i < n; i++) {
        h[i] = x[i + 1] - x[i];
    }

    double mu[] = new double[n];
    double z[] = new double[n + 1];
    mu[0] = 0d;
    z[0] = 0d;
    double g = 0;
    for (int i = 1; i < n; i++) {
        g = 2d * (x[i+1]  - x[i - 1]) - h[i - 1] * mu[i -1];
        mu[i] = h[i] / g;
        z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1])+ y[i - 1] * h[i]) /
                (h[i - 1] * h[i]) - h[i - 1] * z[i - 1]) / g;
    }

    // cubic spline coefficients --  b is linear, c quadratic, d is cubic (original y's are constants)
    double b[] = new double[n];
    double c[] = new double[n + 1];
    double d[] = new double[n];

    z[n] = 0d;
    c[n] = 0d;

    for (int j = n -1; j >=0; j--) {
        c[j] = z[j] - mu[j] * c[j + 1];
        b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d;
        d[j] = (c[j + 1] - c[j]) / (3d * h[j]);
    }

    PolynomialFunction polynomials[] = new PolynomialFunction[n];
    double coefficients[] = new double[4];
    for (int i = 0; i < n; i++) {
        coefficients[0] = y[i];
        coefficients[1] = b[i];
        coefficients[2] = c[i];
        coefficients[3] = d[i];
        polynomials[i] = new PolynomialFunction(coefficients);
    }

    return new PolynomialSplineFunction(x, polynomials);
}
 
Example 18
Source File: PolynomialFunctionLagrangeForm.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Check that the interpolation arrays are valid.
 * The arrays features checked by this method are that both arrays have the
 * same length and this length is at least 2.
 *
 * @param x Interpolating points array.
 * @param y Interpolating values array.
 * @param abort Whether to throw an exception if {@code x} is not sorted.
 * @throws DimensionMismatchException if the array lengths are different.
 * @throws NumberIsTooSmallException if the number of points is less than 2.
 * @throws org.apache.commons.math.exception.NonMonotonousSequenceException
 * if {@code x} is not sorted in strictly increasing order and {@code abort}
 * is {@code true}.
 * @return {@code false} if the {@code x} is not sorted in increasing order,
 * {@code true} otherwise.
 * @see #evaluate(double[], double[], double)
 * @see #computeCoefficients()
 */
public static boolean verifyInterpolationArray(double x[], double y[], boolean abort) {
    if (x.length != y.length) {
        throw new DimensionMismatchException(x.length, y.length);
    }
    if (x.length < 2) {
        throw new NumberIsTooSmallException(LocalizedFormats.WRONG_NUMBER_OF_POINTS, 2, x.length, true);
    }

    return MathUtils.checkOrder(x, MathUtils.OrderDirection.INCREASING, true, abort);
}
 
Example 19
Source File: PolynomialFunctionLagrangeForm.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Check that the interpolation arrays are valid.
 * The arrays features checked by this method are that both arrays have the
 * same length and this length is at least 2.
 *
 * @param x Interpolating points array.
 * @param y Interpolating values array.
 * @param abort Whether to throw an exception if {@code x} is not sorted.
 * @throws DimensionMismatchException if the array lengths are different.
 * @throws NumberIsTooSmallException if the number of points is less than 2.
 * @throws org.apache.commons.math.exception.NonMonotonousSequenceException
 * if {@code x} is not sorted in strictly increasing order and {@code abort}
 * is {@code true}.
 * @return {@code false} if the {@code x} is not sorted in increasing order,
 * {@code true} otherwise.
 * @see #evaluate(double[], double[], double)
 * @see #computeCoefficients()
 */
public static boolean verifyInterpolationArray(double x[], double y[], boolean abort) {
    if (x.length != y.length) {
        throw new DimensionMismatchException(x.length, y.length);
    }
    if (x.length < 2) {
        throw new NumberIsTooSmallException(LocalizedFormats.WRONG_NUMBER_OF_POINTS, 2, x.length, true);
    }

    return MathUtils.checkOrder(x, MathUtils.OrderDirection.INCREASING, true, abort);
}
 
Example 20
Source File: PolynomialFunctionLagrangeForm.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Check that the interpolation arrays are valid.
 * The arrays features checked by this method are that both arrays have the
 * same length and this length is at least 2.
 *
 * @param x Interpolating points array.
 * @param y Interpolating values array.
 * @param abort Whether to throw an exception if {@code x} is not sorted.
 * @throws DimensionMismatchException if the array lengths are different.
 * @throws NumberIsTooSmallException if the number of points is less than 2.
 * @throws org.apache.commons.math.exception.NonMonotonousSequenceException
 * if {@code x} is not sorted in strictly increasing order and {@code abort}
 * is {@code true}.
 * @return {@code false} if the {@code x} is not sorted in increasing order,
 * {@code true} otherwise.
 * @see #evaluate(double[], double[], double)
 * @see #computeCoefficients()
 */
public static boolean verifyInterpolationArray(double x[], double y[], boolean abort) {
    if (x.length != y.length) {
        throw new DimensionMismatchException(x.length, y.length);
    }
    if (x.length < 2) {
        throw new NumberIsTooSmallException(LocalizedFormats.WRONG_NUMBER_OF_POINTS, 2, x.length, true);
    }

    return MathUtils.checkOrder(x, MathUtils.OrderDirection.INCREASING, true, abort);
}