List of usage examples for org.apache.commons.math3.analysis.polynomials PolynomialFunctionLagrangeForm value
public double value(double z)
From source file:com.opengamma.strata.math.impl.util.CommonsMathWrapper.java
/** * Unwraps a Lagrange.//from w w w. j a v a 2 s. c om * * @param lagrange a Commons polynomial in Lagrange form * @return an OG 1-D function mapping doubles to doubles */ public static Function<Double, Double> unwrap(PolynomialFunctionLagrangeForm lagrange) { ArgChecker.notNull(lagrange, "lagrange"); return new Function<Double, Double>() { @Override public Double apply(Double x) { try { return lagrange.value(x); } catch (DimensionMismatchException | NonMonotonicSequenceException | NumberIsTooSmallException e) { throw new MathException(e); } } }; }
From source file:uk.ac.diamond.scisoft.analysis.processing.operations.oned.SplineBaselineOperation.java
/** * Performs the subtraction of the spline baseline. * <p>// w ww.j a va2 s.c om * Given the x position of the knot points, return a copy of the data which * has had a spline subtracted such that the knot points become zero. * @param input * input data * @param knots * position of the zeroes in the new data * @return * data with the baseline subtracted. */ private Dataset subtractSplineBaseline(Dataset input, Dataset knots) { if (knots == null || knots.getSize() <= 0)// || knots.getShape().length < 2 ) // Without sufficient data points for a fit, return the original data return input; Dataset xaxis = null; // Get the axis, or create one. if (AbstractOperation.getFirstAxes(input) != null && AbstractOperation.getFirstAxes(input).length != 0 && AbstractOperation.getFirstAxes(input)[0] != null) xaxis = DatasetUtils.sliceAndConvertLazyDataset(AbstractOperation.getFirstAxes(input)[0]); else xaxis = DoubleDataset.createRange(input.getSize()); // y values at the xs Dataset ys = Maths.interpolate(xaxis, input, knots, null, null); Dataset ybase = null; switch (knots.getSize()) { case 0: ybase = DoubleDataset.zeros(xaxis); break; case 1: // Subtract a constant ybase = Maths.multiply(ys.getDouble(0), DoubleDataset.ones(xaxis)); break; case 2: // Subtract a linear fit ybase = Interpolation1D.linearInterpolation(knots, ys, xaxis); // Extrapolating function for the linear fit PolynomialFunctionLagrangeForm linearLagrange = new PolynomialFunctionLagrangeForm( new double[] { knots.getDouble(0), knots.getDouble(1) }, new double[] { ys.getDouble(0), ys.getDouble(1) }); // Calculate the values at the extrapolated points for (int i = 0; i < xaxis.getSize(); i++) if ((xaxis.getDouble(i) < knots.min().doubleValue()) || (xaxis.getDouble(i) >= knots.max().doubleValue())) ybase.set(linearLagrange.value(xaxis.getDouble(i)), i); break; default: // Spline interpolation between the outer most knots ybase = Interpolation1D.splineInterpolation(knots, ys, xaxis); // Extrapolation final int degree = 3; double[] lowerInterval = new double[degree + 1]; double[] lowerValues = new double[degree + 1]; double[] upperInterval = new double[degree + 1]; double[] upperValues = new double[degree + 1]; // Get 4 x and y values for the end intervals. the end points, the // next-to-end points and two equispaced points in between. for (int i = 0; i <= degree; i++) { lowerInterval[i] = ((degree - i) * knots.getDouble(0) + i * knots.getDouble(1)) / degree; lowerValues[i] = Interpolation1D.splineInterpolation(knots, ys, new DoubleDataset(Arrays.copyOfRange(lowerInterval, i, i + 1), 1)).getDouble(0); upperInterval[i] = ((degree - i) * knots.getDouble(knots.getSize() - 2) + i * knots.getDouble(knots.getSize() - 1)) / degree; upperValues[i] = Interpolation1D.splineInterpolation(knots, ys, new DoubleDataset(Arrays.copyOfRange(upperInterval, i, i + 1), 1)).getDouble(0); } // Lower (smaller value) and upper (larger value) extrapolating // functions. 4 points define a cubic, to match the cubic spline // used in the interpolation PolynomialFunctionLagrangeForm lowerLagrange = new PolynomialFunctionLagrangeForm(lowerInterval, lowerValues); PolynomialFunctionLagrangeForm upperLagrange = new PolynomialFunctionLagrangeForm(upperInterval, upperValues); // Loop through all points. If the x value falls outside the knots, // replace the y-value of the baseline with the interpolated value. for (int i = 0; i < xaxis.getSize(); i++) if (xaxis.getDouble(i) < knots.min().doubleValue()) ybase.set(lowerLagrange.value(xaxis.getDouble(i)), i); else if (xaxis.getDouble(i) >= knots.max().doubleValue()) ybase.set(upperLagrange.value(xaxis.getDouble(i)), i); break; } // Finally, perform the subtraction. return Maths.subtract(input, ybase); }