Java Code Examples for org.apache.commons.math3.util.MathArrays#checkOrder()

The following examples show how to use org.apache.commons.math3.util.MathArrays#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: 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 2
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.math3.exception.NonMonotonicSequenceException 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);
    }
    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 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 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 4
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 5
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 6
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 org.apache.commons.math3.exception.NonMonotonicSequenceException
 * if the {@code points} are not sorted in increasing order.
 */
public GaussIntegrator(double[] points,
                       double[] weights) {
    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 7
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 8
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.math3.exception.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[]) {
    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]);
    }

    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 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 NonMonotonicSequenceException 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[])
    throws DimensionMismatchException,
           NumberIsTooSmallException,
           NonMonotonicSequenceException {
    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.
    final int n = x.length - 1;

    MathArrays.checkOrder(x);

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

    final double mu[] = new double[n];
    final 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)
    final double b[] = new double[n];
    final double c[] = new double[n + 1];
    final 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]);
    }

    final PolynomialFunction polynomials[] = new PolynomialFunction[n];
    final 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: 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 NonMonotonicSequenceException 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[])
    throws DimensionMismatchException,
           NumberIsTooSmallException,
           NonMonotonicSequenceException {
    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.
    final int n = x.length - 1;

    MathArrays.checkOrder(x);

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

    final double mu[] = new double[n];
    final 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)
    final double b[] = new double[n];
    final double c[] = new double[n + 1];
    final 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]);
    }

    final PolynomialFunction polynomials[] = new PolynomialFunction[n];
    final 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 11
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 NonMonotonicSequenceException 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,
           NoDataException,
           NonMonotonicSequenceException {
    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);
    }

    MathArrays.checkOrder(x);
    MathArrays.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 12
Source File: PiecewiseBicubicSplineInterpolatingFunction.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. the expected number
 *        of elements.
 * @throws NonMonotonicSequenceException if {@code x} or {@code y} are not
 *         strictly increasing.
 * @throws NullArgumentException if any of the arguments are null
 * @throws NoDataException if any of the arrays has zero length.
 * @throws DimensionMismatchException if the length of x and y don't match the row, column
 *         height of f
 */
public PiecewiseBicubicSplineInterpolatingFunction(double[] x,
                                                   double[] y,
                                                   double[][] f)
    throws DimensionMismatchException,
           NullArgumentException,
           NoDataException,
           NonMonotonicSequenceException {
    if (x == null ||
        y == null ||
        f == null ||
        f[0] == null) {
        throw new NullArgumentException();
    }

    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 < MIN_NUM_POINTS ||
        yLen < MIN_NUM_POINTS ||
        f.length < MIN_NUM_POINTS ||
        f[0].length < MIN_NUM_POINTS) {
        throw new InsufficientDataException();
    }

    if (xLen != f.length) {
        throw new DimensionMismatchException(xLen, f.length);
    }

    if (yLen != f[0].length) {
        throw new DimensionMismatchException(yLen, f[0].length);
    }

    MathArrays.checkOrder(x);
    MathArrays.checkOrder(y);

    xval = x.clone();
    yval = y.clone();
    fval = f.clone();
}
 
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.math3.exception.NonMonotonicSequenceException
 * 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);
    }

    MathArrays.checkOrder(x);
    MathArrays.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 NonMonotonicSequenceException 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,
           NoDataException,
           NonMonotonicSequenceException {
    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);
    }

    MathArrays.checkOrder(x);
    MathArrays.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 15
Source File: BicubicInterpolatingFunction.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 NonMonotonicSequenceException if {@code x} or {@code y} are
 * not strictly increasing.
 * @throws NoDataException if any of the arrays has zero length.
 */
public BicubicInterpolatingFunction(double[] x,
                                    double[] y,
                                    double[][] f,
                                    double[][] dFdX,
                                    double[][] dFdY,
                                    double[][] d2FdXdY)
    throws DimensionMismatchException,
           NoDataException,
           NonMonotonicSequenceException {
    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);
    }

    MathArrays.checkOrder(x);
    MathArrays.checkOrder(y);

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

    final int lastI = xLen - 1;
    final int lastJ = yLen - 1;
    splines = new BicubicFunction[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;
        final double xR = xval[ip1] - xval[i];
        for (int j = 0; j < lastJ; j++) {
            final int jp1 = j + 1;
            final double yR = yval[jp1] - yval[j];
            final double xRyR = xR * yR;
            final double[] beta = new double[] {
                f[i][j], f[ip1][j], f[i][jp1], f[ip1][jp1],
                dFdX[i][j] * xR, dFdX[ip1][j] * xR, dFdX[i][jp1] * xR, dFdX[ip1][jp1] * xR,
                dFdY[i][j] * yR, dFdY[ip1][j] * yR, dFdY[i][jp1] * yR, dFdY[ip1][jp1] * yR,
                d2FdXdY[i][j] * xRyR, d2FdXdY[ip1][j] * xRyR, d2FdXdY[i][jp1] * xRyR, d2FdXdY[ip1][jp1] * xRyR
            };

            splines[i][j] = new BicubicFunction(computeSplineCoefficients(beta));
        }
    }
}
 
Example 16
Source File: AkimaSplineInterpolator.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 xvals the arguments for the interpolation points
 * @param yvals the values for the interpolation points
 * @return a function which interpolates the data set
 * @throws DimensionMismatchException if {@code xvals} and {@code yvals} have
 *         different sizes.
 * @throws NonMonotonicSequenceException if {@code xvals} is not sorted in
 *         strict increasing order.
 * @throws NumberIsTooSmallException if the size of {@code xvals} is smaller
 *         than 5.
 */
public PolynomialSplineFunction interpolate(double[] xvals,
                                            double[] yvals)
    throws DimensionMismatchException,
           NumberIsTooSmallException,
           NonMonotonicSequenceException {
    if (xvals == null ||
        yvals == null) {
        throw new NullArgumentException();
    }

    if (xvals.length != yvals.length) {
        throw new DimensionMismatchException(xvals.length, yvals.length);
    }

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

    MathArrays.checkOrder(xvals);

    final int numberOfDiffAndWeightElements = xvals.length - 1;

    final double[] differences = new double[numberOfDiffAndWeightElements];
    final double[] weights = new double[numberOfDiffAndWeightElements];

    for (int i = 0; i < differences.length; i++) {
        differences[i] = (yvals[i + 1] - yvals[i]) / (xvals[i + 1] - xvals[i]);
    }

    for (int i = 1; i < weights.length; i++) {
        weights[i] = FastMath.abs(differences[i] - differences[i - 1]);
    }

    // Prepare Hermite interpolation scheme.
    final double[] firstDerivatives = new double[xvals.length];

    for (int i = 2; i < firstDerivatives.length - 2; i++) {
        final double wP = weights[i + 1];
        final double wM = weights[i - 1];
        if (Precision.equals(wP, 0.0) &&
            Precision.equals(wM, 0.0)) {
            final double xv = xvals[i];
            final double xvP = xvals[i + 1];
            final double xvM = xvals[i - 1];
            firstDerivatives[i] = (((xvP - xv) * differences[i - 1]) + ((xv - xvM) * differences[i])) / (xvP - xvM);
        } else {
            firstDerivatives[i] = ((wP * differences[i - 1]) + (wM * differences[i])) / (wP + wM);
        }
    }

    firstDerivatives[0] = differentiateThreePoint(xvals, yvals, 0, 0, 1, 2);
    firstDerivatives[1] = differentiateThreePoint(xvals, yvals, 1, 0, 1, 2);
    firstDerivatives[xvals.length - 2] = differentiateThreePoint(xvals, yvals, xvals.length - 2,
                                                                 xvals.length - 3, xvals.length - 2,
                                                                 xvals.length - 1);
    firstDerivatives[xvals.length - 1] = differentiateThreePoint(xvals, yvals, xvals.length - 1,
                                                                 xvals.length - 3, xvals.length - 2,
                                                                 xvals.length - 1);

    return interpolateHermiteSorted(xvals, yvals, firstDerivatives);
}
 
Example 17
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.math3.exception.NonMonotonicSequenceException
 * 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 MathArrays.checkOrder(x, MathArrays.OrderDirection.INCREASING, true, abort);
}
 
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.math3.exception.NonMonotonicSequenceException
 * 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 MathArrays.checkOrder(x, MathArrays.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.math3.exception.NonMonotonicSequenceException
 * 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 MathArrays.checkOrder(x, MathArrays.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.math3.exception.NonMonotonicSequenceException
 * 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)
    throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException {
    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 MathArrays.checkOrder(x, MathArrays.OrderDirection.INCREASING, true, abort);
}