Java Code Examples for org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction#value()

The following examples show how to use org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction#value() . 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: 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 2
Source File: MapRouteDrawer.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a double array containing y coordinates of a {@linkplain PolynomialSplineFunction} with
 * the above specified {@code DETAIL_LEVEL}.
 *
 * @param fuction The {@linkplain PolynomialSplineFunction} with the values
 * @param index the parameterized array to indicate the maximum Values
 * @return an array of double-precision y values of the specified function
 */
protected double[] getCoords(final PolynomialSplineFunction fuction, final double[] index) {
  final double defaultCoordSize = index[index.length - 1];
  final double[] coords = new double[(int) Math.round(DETAIL_LEVEL * defaultCoordSize) + 1];
  final double stepSize = fuction.getKnots()[fuction.getKnots().length - 1] / coords.length;
  double curValue = 0;
  for (int i = 0; i < coords.length; i++) {
    coords[i] = fuction.value(curValue);
    curValue += stepSize;
  }
  return coords;
}
 
Example 3
Source File: StlDecomposition.java    From stl-java with Apache License 2.0 4 votes vote down vote up
private TimesAndValues padEdges(double[] ts, double[] xs) {
  // Find step between times
  double step = Math.abs(ts[1] - ts[0]);
  // Times (assuming uniform
  double[] paddedTimes = new double[ts.length + 2];
  System.arraycopy(ts, 0, paddedTimes, 1, ts.length);
  paddedTimes[0] = paddedTimes[1] - step;
  paddedTimes[paddedTimes.length - 1] = paddedTimes[paddedTimes.length - 2] + step;

  // Series
  double[] paddedSeries = new double[xs.length + 2];
  System.arraycopy(xs, 0, paddedSeries, 1, xs.length);

  // Use Loess at ends to pad
  // n.b. For some reason, this can result in NaN values - perhaps similar to
  // https://issues.apache.org/jira/browse/MATH-296. If we see NaN, just "extrapolate" by copying
  // the end points :(

  double left = paddedSeries[1];
  double right = paddedSeries[paddedSeries.length - 2];

  double bandwidth = 0.3;
  if (ts.length * bandwidth > 2) {
    PolynomialSplineFunction loess = new LoessInterpolator(bandwidth, 2).interpolate(ts, xs);

    double loessLeft = loess.value(ts[0]);
    if (!Double.isNaN(loessLeft)) {
      left = loessLeft;
    }

    double loessRight = loess.value(ts[ts.length - 1]);
    if (!Double.isNaN(loessRight)) {
      right = loessRight;
    }
  }

  paddedSeries[0] = left;
  paddedSeries[paddedSeries.length - 1] = right;

  return new TimesAndValues(paddedTimes, paddedSeries);
}
 
Example 4
Source File: CorrelationDriftEstimator.java    From thunderstorm with GNU General Public License v3.0 4 votes vote down vote up
public static PolynomialSplineFunction addLinearExtrapolationToBorders(PolynomialSplineFunction spline, int minFrame, int maxFrame) {
    PolynomialFunction[] polynomials = spline.getPolynomials();
    double[] knots = spline.getKnots();

    boolean addToBeginning = knots[0] != minFrame;
    boolean addToEnd = knots[knots.length - 1] != maxFrame;
    int sizeIncrease = 0 + (addToBeginning ? 1 : 0) + (addToEnd ? 1 : 0);
    if(!addToBeginning && !addToEnd) {
        return spline; //do nothing
    }

    //construct new knots and polynomial arrays
    double[] newKnots = new double[knots.length + sizeIncrease];
    PolynomialFunction[] newPolynomials = new PolynomialFunction[polynomials.length + sizeIncrease];
    //add to beginning
    if(addToBeginning) {
        //add knot
        newKnots[0] = minFrame;
        System.arraycopy(knots, 0, newKnots, 1, knots.length);
        //add function
        double derivativeAtFirstKnot = polynomials[0].derivative().value(0);
        double valueAtFirstKnot = spline.value(knots[0]);
        PolynomialFunction beginningFunction = new PolynomialFunction(new double[]{valueAtFirstKnot - (knots[0] - minFrame) * derivativeAtFirstKnot, derivativeAtFirstKnot});
        newPolynomials[0] = beginningFunction;
        System.arraycopy(polynomials, 0, newPolynomials, 1, polynomials.length);
    } else {
        System.arraycopy(knots, 0, newKnots, 0, knots.length);
        System.arraycopy(polynomials, 0, newPolynomials, 0, polynomials.length);
    }
    //add to end
    if(addToEnd) {
        //add knot
        newKnots[newKnots.length - 1] = maxFrame;
        //add function
        double derivativeAtLastKnot = polynomials[polynomials.length - 1].polynomialDerivative().value(knots[knots.length - 1] - knots[knots.length - 2]);
        double valueAtLastKnot = spline.value(knots[knots.length - 1]);
        PolynomialFunction endFunction = new PolynomialFunction(new double[]{valueAtLastKnot, derivativeAtLastKnot});
        newPolynomials[newPolynomials.length - 1] = endFunction;
    }

    return new PolynomialSplineFunction(newKnots, newPolynomials);

}
 
Example 5
Source File: SeaWaterInitialDelta234UTableModel.java    From ET_Redux with Apache License 2.0 4 votes vote down vote up
public double calculateAr234U_238Uisw(double ageInYears) {
        LinearInterpolator linearInterpolator = new LinearInterpolator();
        double[] dates = getArrayOfDates();
        double[] deltas = getArrayOfDeltasAsRatios();
        PolynomialSplineFunction psfSeaWater = linearInterpolator.interpolate(dates, deltas);

        double retVal = 0;
        try {
            retVal = psfSeaWater.value(ageInYears);
        } catch (Exception e) {
//            System.out.println("calculateAr234U_238Uisw error with age = " + ageInYears);
            retVal = 0;
        }

        return retVal;
    }
 
Example 6
Source File: SeaWaterInitialDelta234UTableModel.java    From ET_Redux with Apache License 2.0 4 votes vote down vote up
public double calculateAr234U_238UiswUnct(double ageInYears) {
        LinearInterpolator linearInterpolator = new LinearInterpolator();
        double[] dates = getArrayOfDates();
        double[] deltasUnct = getArrayOfDeltasAsRatioUncertainties();
        PolynomialSplineFunction psfSeaWaterUnct = linearInterpolator.interpolate(dates, deltasUnct);

        double retVal = 0;
        try {
            retVal = psfSeaWaterUnct.value(ageInYears);
        } catch (Exception e) {
//            System.out.println("calculateAr234U_238UiswUnct error with age = " + ageInYears);
            retVal = 0;
        }

        return retVal;
    }
 
Example 7
Source File: Interpolation.java    From mars-sim with GNU General Public License v3.0 2 votes vote down vote up
public double[] linearInterp(double[] x, double[] y, double[] xi) {
   LinearInterpolator li = new LinearInterpolator(); // or other interpolator
   PolynomialSplineFunction psf = li.interpolate(x, y);

   double[] yi = new double[xi.length];
   for (int i = 0; i < xi.length; i++) {
       yi[i] = psf.value(xi[i]);
   }
   return yi;
}