Sqrt implements UnivariateDifferentiableFunction, DifferentiableUnivariateFunction { public double value(double x) { return FastMath.sqrt(x); }  double value(double x); @Deprecated UnivariateFunction derivative(); DerivativeStructure value(final DerivativeStructure t);  }
@Test public void testComparison() { final Sqrt s = new Sqrt(); final UnivariateFunction f = new UnivariateFunction() { public double value(double x) { return FastMath.sqrt(x); } }; for (double x = 1e-30; x < 1e10; x *= 2) { final double fX = f.value(x); final double sX = s.value(x); Assert.assertEquals("x=" + x, fX, sX, 0); } }
@Test public void testDerivativeComparison() { final UnivariateDifferentiableFunction sPrime = new Sqrt(); final UnivariateFunction f = new UnivariateFunction() { public double value(double x) { return 1 / (2 * FastMath.sqrt(x)); } }; for (double x = 1e-30; x < 1e10; x *= 2) { final double fX = f.value(x); final double sX = sPrime.value(new DerivativeStructure(1, 1, 0, x)).getPartialDerivative(1); Assert.assertEquals("x=" + x, fX, sX, FastMath.ulp(fX)); } }
@Test public void testDerivativesHighOrder() { DerivativeStructure s = new Sqrt().value(new DerivativeStructure(1, 5, 0, 1.2)); Assert.assertEquals(1.0954451150103322269, s.getPartialDerivative(0), 1.0e-16); Assert.assertEquals(0.45643546458763842789, s.getPartialDerivative(1), 1.0e-16); Assert.assertEquals(-0.1901814435781826783, s.getPartialDerivative(2), 1.0e-16); Assert.assertEquals(0.23772680447272834785, s.getPartialDerivative(3), 1.0e-16); Assert.assertEquals(-0.49526417598485072465, s.getPartialDerivative(4), 1.0e-16); Assert.assertEquals(1.4445205132891479465, s.getPartialDerivative(5), 5.0e-16); }