List of usage examples for org.apache.commons.math3.analysis.interpolation LinearInterpolator LinearInterpolator
LinearInterpolator
From source file:org.lightjason.agentspeak.action.builtin.TestCActionMathInterpolate.java
/** * test multiple interpolate// w w w .j a v a 2 s . c o m */ @Test public final void multipleinterpolate() { final List<ITerm> l_return = new ArrayList<>(); new CMultipleInterpolate() .execute(false, IContext.EMPTYPLAN, Stream .of(5, new LinearInterpolator().interpolate(new double[] { 3, 6 }, new double[] { 11, 13 }), new NevilleInterpolator().interpolate(new double[] { 2, 3, 8 }, new double[] { 11, 13, 20 })) .map(CRawTerm::from).collect(Collectors.toList()), l_return); Assert.assertEquals(l_return.size(), 2); Assert.assertEquals(l_return.get(0).<Number>raw(), 12.333333333333334); Assert.assertEquals(l_return.get(1).<Number>raw(), 16.400000000000002); }
From source file:org.meteoinfo.math.interpolate.InterpUtil.java
/** * Make linear interpolation function - PolynomialSplineFunction * @param x X data/* w w w. j a v a 2 s . com*/ * @param y Y data * @return Linear interpolation function */ public static PolynomialSplineFunction linearInterpFunc(Array x, Array y) { double[] xd = (double[]) ArrayUtil.copyToNDJavaArray(x); double[] yd = (double[]) ArrayUtil.copyToNDJavaArray(y); LinearInterpolator li = new LinearInterpolator(); PolynomialSplineFunction psf = li.interpolate(xd, yd); return psf; }
From source file:org.meteoinfo.math.interpolate.InterpUtil.java
/** * Make interpolation function//from ww w .ja v a 2 s. c o m * @param x X data * @param y Y data * @param kind Specifies the kind of interpolation as a string (linear, 'spline'). * @return Interpolation function */ public static UnivariateFunction getInterpFunc(Array x, Array y, String kind) { double[] xd = (double[]) ArrayUtil.copyToNDJavaArray(x); double[] yd = (double[]) ArrayUtil.copyToNDJavaArray(y); UnivariateInterpolator li; switch (kind) { case "spline": case "cubic": li = new SplineInterpolator(); break; case "akima": li = new AkimaSplineInterpolator(); break; case "divided": li = new DividedDifferenceInterpolator(); break; case "loess": li = new LoessInterpolator(); break; case "neville": li = new NevilleInterpolator(); break; default: li = new LinearInterpolator(); break; } UnivariateFunction psf = li.interpolate(xd, yd); return psf; }
From source file:org.micromanager.plugins.magellan.acq.AcqDurationEstimator.java
private double interpolateOrExtrapolate(double[] x, double[] y, double xVal) { if (x.length == 1) { return y[0]; }/* w ww . j a v a2s .c o m*/ LinearInterpolator interpolator = new LinearInterpolator(); PolynomialSplineFunction interpolant = interpolator.interpolate(x, y); if (xVal < interpolant.getKnots()[0]) { return interpolant.getKnots()[0]; } else if (xVal > interpolant.getKnots()[interpolant.getN() - 1]) { return interpolant.getKnots()[interpolant.getN() - 1]; } else { return interpolant.value(xVal); } }
From source file:org.micromanager.plugins.magellan.propsandcovariants.CovariantPairing.java
private void updateInterpolant() { if (!independent_.isDiscrete() && independent_.getType() != CovariantType.STRING && independentValues_.size() >= 2) { if (interpolator_ == null) { interpolator_ = new LinearInterpolator(); }//from ww w . j a v a 2 s. co m double[] xVals = new double[independentValues_.size()]; double[] yVals = new double[independentValues_.size()]; for (int i = 0; i < xVals.length; i++) { //set x value if (independent_.getType() == CovariantType.DOUBLE) { xVals[i] = independentValues_.get(i).doubleValue(); } else { xVals[i] = independentValues_.get(i).intValue(); } //set y value if (dependent_.getType() == CovariantType.DOUBLE) { yVals[i] = valueMap_.get(independentValues_.get(i)).doubleValue(); } else { yVals[i] = valueMap_.get(independentValues_.get(i)).intValue(); } } interpolant_ = interpolator_.interpolate(xVals, yVals); } }
From source file:org.roi.itlab.cassandra.random_attributes.AgeRandomGenerator.java
@Override public void buildGenerator() { double[] x = { 18.0, 25.0, 30.0, 60.0, 90.0 }; double[] y = { 1.0, 2.0, 3.0, 1.0, 0.1 }; LinearInterpolator li = new LinearInterpolator(); randomGenerator = new RandomGenerator(); randomGenerator.setMin(LICENSE_AGE); randomGenerator.setMax(MAX_AGE);// www . ja va 2 s . c om randomGenerator.setProportionalWeight(4); randomGenerator.setPsf(li.interpolate(x, y)); }
From source file:org.roi.itlab.cassandra.random_attributes.ExperienceRandomGenerator.java
@Override public void buildGenerator() { int max_exp = age - LICENSE_AGE; double[] x = { 0.0, 5.0, 30.0, 60.0 }; double[] y = { 1.0, 2.0, 4.0, 2.0 }; LinearInterpolator li = new LinearInterpolator(); randomGenerator = new RandomGenerator(); randomGenerator.setMin(MIN_EXP);/*from ww w. j a v a 2s . com*/ randomGenerator.setMax(max_exp); randomGenerator.setProportionalWeight(4); randomGenerator.setPsf(li.interpolate(x, y)); }
From source file:Richards1DSolver.Richards1DSolver.java
private 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]);//from w w w . j a v a2 s .com } return yi; }
From source file:ubic.basecode.math.linearmodels.MeanVarianceEstimator.java
/** * Similar implementation of R's stats.approxfun(..., rule = 2) where values outside the interval ['min(x)', * 'max(x)'] gets the value at the closest data extreme. Also performs sorting based on xTrain. * //w ww .j a v a 2 s .com * @param x the training set of x values * @param y the training set of y values * @param xInterpolate the set of x values to interpolate * @return yInterpolate the interpolated set of y values */ protected static double[] approx(double[] x, double[] y, double[] xInterpolate) { assert x != null; assert y != null; assert xInterpolate != null; assert x.length == y.length; double[] yInterpolate = new double[xInterpolate.length]; LinearInterpolator linearInterpolator = new LinearInterpolator(); // make sure that x is strictly increasing DoubleMatrix2D matrix = new DenseDoubleMatrix2D(x.length, 2); matrix.viewColumn(0).assign(x); matrix.viewColumn(1).assign(y); matrix = matrix.viewSorted(0); double[] sortedX = matrix.viewColumn(0).toArray(); double[] sortedY = matrix.viewColumn(1).toArray(); // make sure x is within the domain DoubleArrayList xList = new DoubleArrayList(sortedX); double x3ListMin = Descriptive.min(xList); double x3ListMax = Descriptive.max(xList); PolynomialSplineFunction fun = linearInterpolator.interpolate(sortedX, sortedY); for (int i = 0; i < xInterpolate.length; i++) { try { // approx(...,rule=2) if (xInterpolate[i] > x3ListMax) { yInterpolate[i] = fun.value(x3ListMax); } else if (xInterpolate[i] < x3ListMin) { yInterpolate[i] = fun.value(x3ListMin); } else { yInterpolate[i] = fun.value(xInterpolate[i]); } } catch (OutOfRangeException e) { // this shouldn't happen anymore yInterpolate[i] = Double.NaN; } } return yInterpolate; }
From source file:uk.ac.diamond.scisoft.analysis.dataset.function.Interpolation1D.java
public static Dataset linearInterpolation(IDataset x, IDataset y, IDataset xnew) { return interpolate(x, y, xnew, new LinearInterpolator()); }