List of usage examples for org.apache.commons.math3.analysis.differentiation UnivariateDifferentiableFunction value
DerivativeStructure value(DerivativeStructure t) throws MathIllegalArgumentException;
From source file:com.bwc.ora.models.Lrp.java
/** * Get the local maximums from a collection of Points. * * @param lrpSeries//from w w w . j a v a 2 s . c om * @param seriesTitle * @return */ public static XYSeries getMaximumsWithHiddenPeaks(XYSeries lrpSeries, String seriesTitle) { XYSeries maxPoints = new XYSeries(seriesTitle); //convert to x and y coordinate arrays double[][] xyline = lrpSeries.toArray(); //use a spline interpolator to converts points into an equation UnivariateInterpolator interpolator = new SplineInterpolator(); UnivariateFunction function = interpolator.interpolate(xyline[0], xyline[1]); // create a differentiator using 5 points and 0.01 step FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(5, 0.01); // create a new function that computes both the value and the derivatives // using DerivativeStructure UnivariateDifferentiableFunction completeF = differentiator.differentiate(function); // now we can compute the value and its derivatives // here we decided to display up to second order derivatives, // because we feed completeF with order 2 DerivativeStructure instances //find local minima in second derivative, these indicate the peaks (and hidden peaks) //of the input for (double x = xyline[0][0] + 1; x < xyline[0][xyline[0].length - 1] - 1; x += 0.5) { DerivativeStructure xDSc = new DerivativeStructure(1, 2, 0, x); DerivativeStructure xDSl = new DerivativeStructure(1, 2, 0, x - 0.5); DerivativeStructure xDSr = new DerivativeStructure(1, 2, 0, x + 0.5); DerivativeStructure yDSc = completeF.value(xDSc); DerivativeStructure yDSl = completeF.value(xDSl); DerivativeStructure yDSr = completeF.value(xDSr); double c2d = yDSc.getPartialDerivative(2); if (c2d < yDSl.getPartialDerivative(2) && c2d < yDSr.getPartialDerivative(2)) { maxPoints.add((int) Math.round(x), yDSc.getValue()); } } return maxPoints; }
From source file:org.orekit.bodies.OneAxisEllipsoidTest.java
@Test public void testMovingGeodeticPoint() throws OrekitException { final OneAxisEllipsoid earth = new OneAxisEllipsoid(Constants.WGS84_EARTH_EQUATORIAL_RADIUS, Constants.WGS84_EARTH_FLATTENING, FramesFactory.getITRF(IERSConventions.IERS_2010, true)); double lat0 = FastMath.toRadians(60.0); double lon0 = FastMath.toRadians(25.0); double alt0 = 100.0; double lat1 = 1.0e-3; double lon1 = -2.0e-3; double alt1 = 1.2; double lat2 = -1.0e-5; double lon2 = -3.0e-5; double alt2 = -0.01; final DerivativeStructure latDS = new DerivativeStructure(1, 2, lat0, lat1, lat2); final DerivativeStructure lonDS = new DerivativeStructure(1, 2, lon0, lon1, lon2); final DerivativeStructure altDS = new DerivativeStructure(1, 2, alt0, alt1, alt2); // direct computation of position, velocity and acceleration PVCoordinates pv = earth.transform(new FieldGeodeticPoint<DerivativeStructure>(latDS, lonDS, altDS)); // finite differences computation FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(5, 0.1); UnivariateDifferentiableFunction fx = differentiator.differentiate(new UnivariateFunction() { public double value(double dt) { GeodeticPoint gp = new GeodeticPoint(latDS.taylor(dt), lonDS.taylor(dt), altDS.taylor(dt)); return earth.transform(gp).getX(); }//w ww.ja va2s .c om }); UnivariateDifferentiableFunction fy = differentiator.differentiate(new UnivariateFunction() { public double value(double dt) { GeodeticPoint gp = new GeodeticPoint(latDS.taylor(dt), lonDS.taylor(dt), altDS.taylor(dt)); return earth.transform(gp).getY(); } }); UnivariateDifferentiableFunction fz = differentiator.differentiate(new UnivariateFunction() { public double value(double dt) { GeodeticPoint gp = new GeodeticPoint(latDS.taylor(dt), lonDS.taylor(dt), altDS.taylor(dt)); return earth.transform(gp).getZ(); } }); DerivativeStructure dtZero = new DerivativeStructure(1, 2, 0, 0.0); DerivativeStructure xDS = fx.value(dtZero); DerivativeStructure yDS = fy.value(dtZero); DerivativeStructure zDS = fz.value(dtZero); Assert.assertEquals(xDS.getValue(), pv.getPosition().getX(), 2.0e-20 * FastMath.abs(xDS.getValue())); Assert.assertEquals(xDS.getPartialDerivative(1), pv.getVelocity().getX(), 2.0e-12 * FastMath.abs(xDS.getPartialDerivative(1))); Assert.assertEquals(xDS.getPartialDerivative(2), pv.getAcceleration().getX(), 2.0e-9 * FastMath.abs(xDS.getPartialDerivative(2))); Assert.assertEquals(yDS.getValue(), pv.getPosition().getY(), 2.0e-20 * FastMath.abs(yDS.getValue())); Assert.assertEquals(yDS.getPartialDerivative(1), pv.getVelocity().getY(), 2.0e-12 * FastMath.abs(yDS.getPartialDerivative(1))); Assert.assertEquals(yDS.getPartialDerivative(2), pv.getAcceleration().getY(), 2.0e-9 * FastMath.abs(yDS.getPartialDerivative(2))); Assert.assertEquals(zDS.getValue(), pv.getPosition().getZ(), 2.0e-20 * FastMath.abs(zDS.getValue())); Assert.assertEquals(zDS.getPartialDerivative(1), pv.getVelocity().getZ(), 2.0e-12 * FastMath.abs(zDS.getPartialDerivative(1))); Assert.assertEquals(zDS.getPartialDerivative(2), pv.getAcceleration().getZ(), 2.0e-9 * FastMath.abs(zDS.getPartialDerivative(2))); }
From source file:org.orekit.utils.IERSConventionsTest.java
private void checkDerivative(final TimeFunction<DerivativeStructure> function, final AbsoluteDate date, final double span, final double sampleStep, final double h, final double tolerance) { UnivariateDifferentiableFunction differentiated = new FiniteDifferencesDifferentiator(4, h) .differentiate(new UnivariateFunction() { @Override//from ww w .jav a2 s . c om public double value(final double dt) { return function.value(date.shiftedBy(dt)).getValue(); } }); for (double dt = 0; dt < span; dt += sampleStep) { DerivativeStructure yRef = differentiated.value(new DerivativeStructure(1, 1, 0, dt)); DerivativeStructure y = function.value(date.shiftedBy(dt)); Assert.assertEquals(yRef.getPartialDerivative(1), y.getPartialDerivative(1), tolerance); } }