List of usage examples for org.apache.commons.math3.analysis.differentiation FiniteDifferencesDifferentiator differentiate
public UnivariateDifferentiableMatrixFunction differentiate(final UnivariateMatrixFunction function)
The returned object cannot compute derivatives to arbitrary orders.
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 o m * @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 w w. j a va 2 s. c o m }); 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.orbits.KeplerianParametersTest.java
@Test public void testKeplerianDerivatives() { final KeplerianOrbit o = new KeplerianOrbit(new PVCoordinates(new Vector3D(-4947831., -3765382., -3708221.), new Vector3D(-2079., 5291., -7842.)), FramesFactory.getEME2000(), date, 3.9860047e14); final Vector3D p = o.getPVCoordinates().getPosition(); final Vector3D v = o.getPVCoordinates().getVelocity(); final Vector3D a = o.getPVCoordinates().getAcceleration(); // check that despite we did not provide acceleration, it got recomputed Assert.assertEquals(7.605422, a.getNorm(), 1.0e-6); FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(8, 0.1); // check velocity is the derivative of position double vx = differentiator.differentiate(new UnivariateFunction() { public double value(double dt) { return o.shiftedBy(dt).getPVCoordinates().getPosition().getX(); }//from w w w .j ava 2 s . c o m }).value(new DerivativeStructure(1, 1, 0, 0.0)).getPartialDerivative(1); Assert.assertEquals(o.getPVCoordinates().getVelocity().getX(), vx, 3.0e-12 * v.getNorm()); double vy = differentiator.differentiate(new UnivariateFunction() { public double value(double dt) { return o.shiftedBy(dt).getPVCoordinates().getPosition().getY(); } }).value(new DerivativeStructure(1, 1, 0, 0.0)).getPartialDerivative(1); Assert.assertEquals(o.getPVCoordinates().getVelocity().getY(), vy, 3.0e-12 * v.getNorm()); double vz = differentiator.differentiate(new UnivariateFunction() { public double value(double dt) { return o.shiftedBy(dt).getPVCoordinates().getPosition().getZ(); } }).value(new DerivativeStructure(1, 1, 0, 0.0)).getPartialDerivative(1); Assert.assertEquals(o.getPVCoordinates().getVelocity().getZ(), vz, 3.0e-12 * v.getNorm()); // check acceleration is the derivative of velocity double ax = differentiator.differentiate(new UnivariateFunction() { public double value(double dt) { return o.shiftedBy(dt).getPVCoordinates().getVelocity().getX(); } }).value(new DerivativeStructure(1, 1, 0, 0.0)).getPartialDerivative(1); Assert.assertEquals(o.getPVCoordinates().getAcceleration().getX(), ax, 3.0e-12 * a.getNorm()); double ay = differentiator.differentiate(new UnivariateFunction() { public double value(double dt) { return o.shiftedBy(dt).getPVCoordinates().getVelocity().getY(); } }).value(new DerivativeStructure(1, 1, 0, 0.0)).getPartialDerivative(1); Assert.assertEquals(o.getPVCoordinates().getAcceleration().getY(), ay, 3.0e-12 * a.getNorm()); double az = differentiator.differentiate(new UnivariateFunction() { public double value(double dt) { return o.shiftedBy(dt).getPVCoordinates().getVelocity().getZ(); } }).value(new DerivativeStructure(1, 1, 0, 0.0)).getPartialDerivative(1); Assert.assertEquals(o.getPVCoordinates().getAcceleration().getZ(), az, 3.0e-12 * a.getNorm()); // check jerk is the derivative of acceleration final double r2 = p.getNormSq(); final double r = FastMath.sqrt(r2); Vector3D keplerianJerk = new Vector3D(-3 * Vector3D.dotProduct(p, v) / r2, a, -a.getNorm() / r, v); double jx = differentiator.differentiate(new UnivariateFunction() { public double value(double dt) { return o.shiftedBy(dt).getPVCoordinates().getAcceleration().getX(); } }).value(new DerivativeStructure(1, 1, 0, 0.0)).getPartialDerivative(1); Assert.assertEquals(keplerianJerk.getX(), jx, 3.0e-12 * keplerianJerk.getNorm()); double jy = differentiator.differentiate(new UnivariateFunction() { public double value(double dt) { return o.shiftedBy(dt).getPVCoordinates().getAcceleration().getY(); } }).value(new DerivativeStructure(1, 1, 0, 0.0)).getPartialDerivative(1); Assert.assertEquals(keplerianJerk.getY(), jy, 3.0e-12 * keplerianJerk.getNorm()); double jz = differentiator.differentiate(new UnivariateFunction() { public double value(double dt) { return o.shiftedBy(dt).getPVCoordinates().getAcceleration().getZ(); } }).value(new DerivativeStructure(1, 1, 0, 0.0)).getPartialDerivative(1); Assert.assertEquals(keplerianJerk.getZ(), jz, 3.0e-12 * keplerianJerk.getNorm()); }
From source file:org.orekit.utils.FieldPVCoordinatesTest.java
@Test public void testNormalize() { RandomGenerator generator = new Well19937a(0x7ede9376e4e1ab5al); FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(5, 1.0e-3); for (int i = 0; i < 200; ++i) { final FieldPVCoordinates<DerivativeStructure> pv = randomPVCoordinates(generator, 1e6, 1e3, 1.0); DerivativeStructure x = differentiator.differentiate(new UnivariateFunction() { public double value(double t) { return pv.shiftedBy(t).getPosition().normalize().getX().getValue(); }//from w w w.j av a 2 s . c o m }).value(new DerivativeStructure(1, 2, 0, 0.0)); DerivativeStructure y = differentiator.differentiate(new UnivariateFunction() { public double value(double t) { return pv.shiftedBy(t).getPosition().normalize().getY().getValue(); } }).value(new DerivativeStructure(1, 2, 0, 0.0)); DerivativeStructure z = differentiator.differentiate(new UnivariateFunction() { public double value(double t) { return pv.shiftedBy(t).getPosition().normalize().getZ().getValue(); } }).value(new DerivativeStructure(1, 2, 0, 0.0)); FieldPVCoordinates<DerivativeStructure> normalized = pv.normalize(); Assert.assertEquals(x.getValue(), normalized.getPosition().getX().getValue(), 1.0e-16); Assert.assertEquals(y.getValue(), normalized.getPosition().getY().getValue(), 1.0e-16); Assert.assertEquals(z.getValue(), normalized.getPosition().getZ().getValue(), 1.0e-16); Assert.assertEquals(x.getPartialDerivative(1), normalized.getVelocity().getX().getValue(), 3.0e-13); Assert.assertEquals(y.getPartialDerivative(1), normalized.getVelocity().getY().getValue(), 3.0e-13); Assert.assertEquals(z.getPartialDerivative(1), normalized.getVelocity().getZ().getValue(), 3.0e-13); Assert.assertEquals(x.getPartialDerivative(2), normalized.getAcceleration().getX().getValue(), 6.0e-10); Assert.assertEquals(y.getPartialDerivative(2), normalized.getAcceleration().getY().getValue(), 6.0e-10); Assert.assertEquals(z.getPartialDerivative(2), normalized.getAcceleration().getZ().getValue(), 6.0e-10); } }
From source file:org.orekit.utils.PVCoordinatesTest.java
@Test public void testNormalize() { RandomGenerator generator = new Well19937a(0xb2011ffd25412067l); FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(5, 1.0e-3); for (int i = 0; i < 200; ++i) { final PVCoordinates pv = randomPVCoordinates(generator, 1e6, 1e3, 1.0); DerivativeStructure x = differentiator.differentiate(new UnivariateFunction() { public double value(double t) { return pv.shiftedBy(t).getPosition().normalize().getX(); }/*from w ww.j a va2 s . c o m*/ }).value(new DerivativeStructure(1, 2, 0, 0.0)); DerivativeStructure y = differentiator.differentiate(new UnivariateFunction() { public double value(double t) { return pv.shiftedBy(t).getPosition().normalize().getY(); } }).value(new DerivativeStructure(1, 2, 0, 0.0)); DerivativeStructure z = differentiator.differentiate(new UnivariateFunction() { public double value(double t) { return pv.shiftedBy(t).getPosition().normalize().getZ(); } }).value(new DerivativeStructure(1, 2, 0, 0.0)); PVCoordinates normalized = pv.normalize(); Assert.assertEquals(x.getValue(), normalized.getPosition().getX(), 1.0e-16); Assert.assertEquals(y.getValue(), normalized.getPosition().getY(), 1.0e-16); Assert.assertEquals(z.getValue(), normalized.getPosition().getZ(), 1.0e-16); Assert.assertEquals(x.getPartialDerivative(1), normalized.getVelocity().getX(), 3.0e-13); Assert.assertEquals(y.getPartialDerivative(1), normalized.getVelocity().getY(), 3.0e-13); Assert.assertEquals(z.getPartialDerivative(1), normalized.getVelocity().getZ(), 3.0e-13); Assert.assertEquals(x.getPartialDerivative(2), normalized.getAcceleration().getX(), 6.0e-10); Assert.assertEquals(y.getPartialDerivative(2), normalized.getAcceleration().getY(), 6.0e-10); Assert.assertEquals(z.getPartialDerivative(2), normalized.getAcceleration().getZ(), 6.0e-10); } }
From source file:org.orekit.utils.PVCoordinatesTest.java
@Test public void testCrossProduct() { RandomGenerator generator = new Well19937a(0x85c592b3be733d23l); FiniteDifferencesDifferentiator differentiator = new FiniteDifferencesDifferentiator(5, 1.0e-3); for (int i = 0; i < 200; ++i) { final PVCoordinates pv1 = randomPVCoordinates(generator, 1.0, 1.0, 1.0); final PVCoordinates pv2 = randomPVCoordinates(generator, 1.0, 1.0, 1.0); DerivativeStructure x = differentiator.differentiate(new UnivariateFunction() { public double value(double t) { return Vector3D.crossProduct(pv1.shiftedBy(t).getPosition(), pv2.shiftedBy(t).getPosition()) .getX();/*from w w w . j a v a 2 s .c o m*/ } }).value(new DerivativeStructure(1, 2, 0, 0.0)); DerivativeStructure y = differentiator.differentiate(new UnivariateFunction() { public double value(double t) { return Vector3D.crossProduct(pv1.shiftedBy(t).getPosition(), pv2.shiftedBy(t).getPosition()) .getY(); } }).value(new DerivativeStructure(1, 2, 0, 0.0)); DerivativeStructure z = differentiator.differentiate(new UnivariateFunction() { public double value(double t) { return Vector3D.crossProduct(pv1.shiftedBy(t).getPosition(), pv2.shiftedBy(t).getPosition()) .getZ(); } }).value(new DerivativeStructure(1, 2, 0, 0.0)); PVCoordinates product = PVCoordinates.crossProduct(pv1, pv2); Assert.assertEquals(x.getValue(), product.getPosition().getX(), 1.0e-16); Assert.assertEquals(y.getValue(), product.getPosition().getY(), 1.0e-16); Assert.assertEquals(z.getValue(), product.getPosition().getZ(), 1.0e-16); Assert.assertEquals(x.getPartialDerivative(1), product.getVelocity().getX(), 9.0e-10); Assert.assertEquals(y.getPartialDerivative(1), product.getVelocity().getY(), 9.0e-10); Assert.assertEquals(z.getPartialDerivative(1), product.getVelocity().getZ(), 9.0e-10); Assert.assertEquals(x.getPartialDerivative(2), product.getAcceleration().getX(), 3.0e-9); Assert.assertEquals(y.getPartialDerivative(2), product.getAcceleration().getY(), 3.0e-9); Assert.assertEquals(z.getPartialDerivative(2), product.getAcceleration().getZ(), 3.0e-9); } }