org.apache.commons.math3.analysis.interpolation.SplineInterpolator Java Examples

The following examples show how to use org.apache.commons.math3.analysis.interpolation.SplineInterpolator. 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: ModifiedLoess.java    From thunderstorm with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Compute an interpolating function by performing a loess fit
 * on the data at the original abscissae and then building a cubic spline
 * with a
 * {@link org.apache.commons.math3.analysis.interpolation.SplineInterpolator}
 * on the resulting fit.
 *
 * @param xval the arguments for the interpolation points
 * @param yval the values for the interpolation points
 * @return A cubic spline built upon a loess fit to the data at the original abscissae
 * @throws NonMonotonicSequenceException if {@code xval} not sorted in
 * strictly increasing order.
 * @throws DimensionMismatchException if {@code xval} and {@code yval} have
 * different sizes.
 * @throws NoDataException if {@code xval} or {@code yval} has zero size.
 * @throws NotFiniteNumberException if any of the arguments and values are
 * not finite real numbers.
 * @throws NumberIsTooSmallException if the bandwidth is too small to
 * accomodate the size of the input data (i.e. the bandwidth must be
 * larger than 2/n).
 */
public final PolynomialSplineFunction interpolate(double[] xval,
                                                  double[] yval)
    throws NonMonotonicSequenceException,
           DimensionMismatchException,
           NoDataException,
           NotFiniteNumberException,
           NumberIsTooSmallException {
    double[] smoothed = smooth(xval, yval);
    DoubleList newX = new ArrayDoubleList();
    DoubleList newSmoothed = new ArrayDoubleList();
    newX.add(xval[0]);
    newSmoothed.add(smoothed[0]);
    for(int i = 1; i < xval.length; i++){
        if(xval[i] != xval[i-1]){
            newX.add(xval[i]);
            newSmoothed.add(smoothed[i]);
        }
    }
    xval = newX.toArray();
    smoothed = newSmoothed.toArray();
    
    return new SplineInterpolator().interpolate(xval, smoothed);
}
 
Example #2
Source File: ExtremumComputer.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
/**
 * 计算分段插值拟合的导数值
 *
 * @param input
 * @return
 */
private double[] splineDerivatives(double[] input) {
	double xStep = 1.0 / input.length;

	double[] x = new double[input.length];
	double[] y = new double[input.length];

	for (int i = 0; i < input.length; i++) {
		x[i] = i * xStep;
		y[i] = input[i];
	}

	SplineInterpolator fitter = new SplineInterpolator();
	PolynomialSplineFunction func = fitter.interpolate(x, y);

	double[] derivatives = new double[input.length];
	for (int i = 0; i < derivatives.length; i++) {
		derivatives[i] = func.derivative().value(x[i]);
	}

	return derivatives;
}
 
Example #3
Source File: SplineEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public Object doWork(Object... objects) throws IOException{

  Object first = objects[0];

  double[] x = null;
  double[] y = null;

  if(objects.length == 1) {
    //Only the y values passed
    y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
    x = new double[y.length];
    for(int i=0; i<y.length; i++) {
      x[i] = i;
    }
  } else if(objects.length == 2) {
    Object second = objects[1];
    x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
    y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();
  }

  SplineInterpolator interpolator = new SplineInterpolator();
  PolynomialSplineFunction spline = interpolator.interpolate(x, y);

  List<Number> list = new ArrayList<>();
  for(double xvalue : x) {
    list.add(spline.value(xvalue));
  }

  VectorFunction vec = new VectorFunction(spline, list);
  vec.addToContext("x", x);
  vec.addToContext("y", y);

  return vec;
}
 
Example #4
Source File: Interpolation.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
public static void test0() {
//    	 double[] x = { 0, 50, 100 };
//       double[] y = { 0, 50, 200 };
//        double[] x = new double[] {103, 152, 199, 211, 223, 235, 170,  38,  38,
//        							  38,  63,  67,  69,  72,  91, 105, 116, 128, 141,
//        							 155, 170, 185, 201, 218, 236, 239, 243, 249, 255, 255, 254};
        
//        double[] x = new double[] {38,  38,  38,  38,  62, 134, 230, 225, 239,
//        							254, 188, 147, 111,  81, 107, 119, 130, 141, 153, 
//        							166, 179, 193, 208, 223, 238, 241, 244, 249, 255, 253, 251};
        
    	double[] x = new double[] {104, 129, 130, 76, 38, 38, 38, 66, 148,
        							243, 254, 240, 226, 212, 190, 168, 177, 186, 194, 
        							203, 211, 221, 229, 238, 246, 248, 249, 252, 255, 218, 181};
        
        double[] y = new double[] {-9000, -8000, -7000, -6000, -5000, -4000, -3000, -2000, -1000, 
    		   						   0,  1000,  2000,  3000,  4000,  5000,  6000,  7000,  8000, 9000,
    		   						10000, 11000, 12000, 13000, 14000, 15000, 16000, 17000, 18000, 19000, 20000, 21000};
       
//       LinearInterpolator interp = new LinearInterpolator();
       SplineInterpolator si = new SplineInterpolator();
       PolynomialSplineFunction f = si.interpolate(x, y);

//       System.out.println("Piecewise functions:");
       Arrays.stream(f.getPolynomials()).forEach(System.out::println);

       double value = f.value(250);
       System.out.println("Elevation when x = 250: " + value);
       
    }
 
Example #5
Source File: MapRouteDrawerTest.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
@Test
void testCurve() {
  final double[] testYValues = new double[] {20, 40, 90};
  final PolynomialSplineFunction testFunction =
      new SplineInterpolator().interpolate(dummyIndex, testYValues);
  final double[] coords = spyRouteDrawer.getCoords(testFunction, dummyIndex);
  final double stepSize =
      testFunction.getKnots()[testFunction.getKnots().length - 1] / coords.length;
  assertEquals(testYValues[0] * stepSize, coords[(int) Math.round(dummyIndex[0])], 1);
  assertEquals(testYValues[1] * stepSize, coords[(int) Math.round(dummyIndex[1])], 1);
  assertEquals(testYValues[2] * stepSize, coords[(int) Math.round(dummyIndex[2])], 1);
  // TODO change the calculation so that delta = 0;
}