Java Code Examples for org.apache.commons.math3.exception.util.LocalizedFormats#NUMBER_OF_POINTS

The following examples show how to use org.apache.commons.math3.exception.util.LocalizedFormats#NUMBER_OF_POINTS . 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: BaseRuleFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets a copy of the quadrature rule with the given number of integration
 * points.
 *
 * @param numberOfPoints Number of integration points.
 * @return a copy of the integration rule.
 * @throws NotStrictlyPositiveException if {@code numberOfPoints < 1}.
 * @throws DimensionMismatchException if the elements of the rule pair do not
 * have the same length.
 */
public Pair<double[], double[]> getRule(int numberOfPoints)
    throws NotStrictlyPositiveException, DimensionMismatchException {

    if (numberOfPoints <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS,
                                               numberOfPoints);
    }

    // Try to obtain the rule from the cache.
    Pair<double[], double[]> cached = pointsAndWeightsDouble.get(numberOfPoints);

    if (cached == null) {
        // Rule not computed yet.

        // Compute the rule.
        final Pair<T[], T[]> rule = getRuleInternal(numberOfPoints);
        cached = convertToDouble(rule);

        // Cache it.
        pointsAndWeightsDouble.put(numberOfPoints, cached);
    }

    // Return a copy.
    return new Pair<double[], double[]>(cached.getFirst().clone(),
                                        cached.getSecond().clone());
}
 
Example 2
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 3
Source File: BaseRuleFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets a copy of the quadrature rule with the given number of integration
 * points.
 *
 * @param numberOfPoints Number of integration points.
 * @return a copy of the integration rule.
 * @throws NotStrictlyPositiveException if {@code numberOfPoints < 1}.
 * @throws DimensionMismatchException if the elements of the rule pair do not
 * have the same length.
 */
public Pair<double[], double[]> getRule(int numberOfPoints)
    throws NotStrictlyPositiveException, DimensionMismatchException {

    if (numberOfPoints <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS,
                                               numberOfPoints);
    }

    // Try to obtain the rule from the cache.
    Pair<double[], double[]> cached = pointsAndWeightsDouble.get(numberOfPoints);

    if (cached == null) {
        // Rule not computed yet.

        // Compute the rule.
        final Pair<T[], T[]> rule = getRuleInternal(numberOfPoints);
        cached = convertToDouble(rule);

        // Cache it.
        pointsAndWeightsDouble.put(numberOfPoints, cached);
    }

    // Return a copy.
    return new Pair<double[], double[]>(cached.getFirst().clone(),
                                        cached.getSecond().clone());
}
 
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 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 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 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 6
Source File: BaseRuleFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets a copy of the quadrature rule with the given number of integration
 * points.
 *
 * @param numberOfPoints Number of integration points.
 * @return a copy of the integration rule.
 * @throws NotStrictlyPositiveException if {@code numberOfPoints < 1}.
 * @throws DimensionMismatchException if the elements of the rule pair do not
 * have the same length.
 */
public Pair<double[], double[]> getRule(int numberOfPoints)
    throws NotStrictlyPositiveException, DimensionMismatchException {

    if (numberOfPoints <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS,
                                               numberOfPoints);
    }

    // Try to obtain the rule from the cache.
    Pair<double[], double[]> cached = pointsAndWeightsDouble.get(numberOfPoints);

    if (cached == null) {
        // Rule not computed yet.

        // Compute the rule.
        final Pair<T[], T[]> rule = getRuleInternal(numberOfPoints);
        cached = convertToDouble(rule);

        // Cache it.
        pointsAndWeightsDouble.put(numberOfPoints, cached);
    }

    // Return a copy.
    return new Pair<double[], double[]>(cached.getFirst().clone(),
                                        cached.getSecond().clone());
}
 
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.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 8
Source File: BaseRuleFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets a copy of the quadrature rule with the given number of integration
 * points.
 *
 * @param numberOfPoints Number of integration points.
 * @return a copy of the integration rule.
 * @throws NotStrictlyPositiveException if {@code numberOfPoints < 1}.
 * @throws DimensionMismatchException if the elements of the rule pair do not
 * have the same length.
 */
public Pair<double[], double[]> getRule(int numberOfPoints)
    throws NotStrictlyPositiveException, DimensionMismatchException {

    if (numberOfPoints <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS,
                                               numberOfPoints);
    }

    // Try to obtain the rule from the cache.
    Pair<double[], double[]> cached = pointsAndWeightsDouble.get(numberOfPoints);

    if (cached == null) {
        // Rule not computed yet.

        // Compute the rule.
        final Pair<T[], T[]> rule = getRuleInternal(numberOfPoints);
        cached = convertToDouble(rule);

        // Cache it.
        pointsAndWeightsDouble.put(numberOfPoints, cached);
    }

    // Return a copy.
    return new Pair<double[], double[]>(cached.getFirst().clone(),
                                        cached.getSecond().clone());
}
 
Example 9
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 10
Source File: BaseRuleFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets a copy of the quadrature rule with the given number of integration
 * points.
 *
 * @param numberOfPoints Number of integration points.
 * @return a copy of the integration rule.
 * @throws NotStrictlyPositiveException if {@code numberOfPoints < 1}.
 * @throws DimensionMismatchException if the elements of the rule pair do not
 * have the same length.
 */
public Pair<double[], double[]> getRule(int numberOfPoints)
    throws NotStrictlyPositiveException, DimensionMismatchException {

    if (numberOfPoints <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS,
                                               numberOfPoints);
    }

    // Try to obtain the rule from the cache.
    Pair<double[], double[]> cached = pointsAndWeightsDouble.get(numberOfPoints);

    if (cached == null) {
        // Rule not computed yet.

        // Compute the rule.
        final Pair<T[], T[]> rule = getRuleInternal(numberOfPoints);
        cached = convertToDouble(rule);

        // Cache it.
        pointsAndWeightsDouble.put(numberOfPoints, cached);
    }

    // Return a copy.
    return new Pair<double[], double[]>(cached.getFirst().clone(),
                                        cached.getSecond().clone());
}
 
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.math3.exception.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[]) {
    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;

    MathArrays.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: 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 13
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 14
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 15
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.math3.exception.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[]) {
    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;

    MathArrays.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 16
Source File: AkimaSplineInterpolator.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a Hermite cubic spline interpolation from the set of (x,y) value
 * pairs and their derivatives. This is modeled off of the
 * InterpolateHermiteSorted method in the Math.NET CubicSpline class.
 *
 * @param xvals x values for interpolation
 * @param yvals y values for interpolation
 * @param firstDerivatives first derivative values of the function
 * @return polynomial that fits the function
 */
private PolynomialSplineFunction interpolateHermiteSorted(double[] xvals,
                                                          double[] yvals,
                                                          double[] firstDerivatives) {
    if (xvals.length != yvals.length) {
        throw new DimensionMismatchException(xvals.length, yvals.length);
    }

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

    final int minimumLength = 2;
    if (xvals.length < minimumLength) {
        throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS,
                                            xvals.length, minimumLength,
                                            true);
    }

    final int size = xvals.length - 1;
    final PolynomialFunction[] polynomials = new PolynomialFunction[size];
    final double[] coefficients = new double[4];

    for (int i = 0; i < polynomials.length; i++) {
        final double w = xvals[i + 1] - xvals[i];
        final double w2 = w * w;

        final double yv = yvals[i];
        final double yvP = yvals[i + 1];

        final double fd = firstDerivatives[i];
        final double fdP = firstDerivatives[i + 1];

        coefficients[0] = yv;
        coefficients[1] = firstDerivatives[i];
        coefficients[2] = (3 * (yvP - yv) / w - 2 * fd - fdP) / w;
        coefficients[3] = (2 * (yv - yvP) / w + fd + fdP) / w2;
        polynomials[i] = new PolynomialFunction(coefficients);
    }

    return new PolynomialSplineFunction(xvals, polynomials);

}
 
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 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 18
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 19
Source File: IterativeLegendreGaussIntegrator.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Builds an integrator with given accuracies and iterations counts.
 *
 * @param n Number of integration points.
 * @param relativeAccuracy Relative accuracy of the result.
 * @param absoluteAccuracy Absolute accuracy of the result.
 * @param minimalIterationCount Minimum number of iterations.
 * @param maximalIterationCount Maximum number of iterations.
 * @throws NotStrictlyPositiveException if minimal number of iterations
 * or number of points are not strictly positive.
 * @throws NumberIsTooSmallException if maximal number of iterations
 * is smaller than or equal to the minimal number of iterations.
 */
public IterativeLegendreGaussIntegrator(final int n,
                                        final double relativeAccuracy,
                                        final double absoluteAccuracy,
                                        final int minimalIterationCount,
                                        final int maximalIterationCount)
    throws NotStrictlyPositiveException, NumberIsTooSmallException {
    super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
    if (n <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS, n);
    }
   numberOfPoints = n;
}
 
Example 20
Source File: IterativeLegendreGaussIntegrator.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Builds an integrator with given accuracies and iterations counts.
 *
 * @param n Number of integration points.
 * @param relativeAccuracy Relative accuracy of the result.
 * @param absoluteAccuracy Absolute accuracy of the result.
 * @param minimalIterationCount Minimum number of iterations.
 * @param maximalIterationCount Maximum number of iterations.
 * @throws NotStrictlyPositiveException if minimal number of iterations
 * or number of points are not strictly positive.
 * @throws NumberIsTooSmallException if maximal number of iterations
 * is smaller than or equal to the minimal number of iterations.
 */
public IterativeLegendreGaussIntegrator(final int n,
                                        final double relativeAccuracy,
                                        final double absoluteAccuracy,
                                        final int minimalIterationCount,
                                        final int maximalIterationCount)
    throws NotStrictlyPositiveException, NumberIsTooSmallException {
    super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
    if (n <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS, n);
    }
   numberOfPoints = n;
}