ScalarFirstOrderDifferentiator implements Differentiator<Double, Double, Double> { @Override public Function1D<Double, Double> differentiate(final Function1D<Double, Double> function) { Validate.notNull(function); switch (_differenceType) { case FORWARD: return new Function1D<Double, Double>() { @SuppressWarnings("synthetic-access") @Override public Double evaluate(final Double x) { Validate.notNull(x, "x"); return (function.evaluate(x + _eps) - function.evaluate(x)) / _eps; } }; case CENTRAL: return new Function1D<Double, Double>() { @SuppressWarnings("synthetic-access") @Override public Double evaluate(final Double x) { Validate.notNull(x, "x"); return (function.evaluate(x + _eps) - function.evaluate(x - _eps)) / _twoEps; } }; case BACKWARD: return new Function1D<Double, Double>() { @SuppressWarnings("synthetic-access") @Override public Double evaluate(final Double x) { Validate.notNull(x, "x"); return (function.evaluate(x) - function.evaluate(x - _eps)) / _eps; } }; default: throw new IllegalArgumentException("Can only handle forward, backward and central differencing"); } } ScalarFirstOrderDifferentiator();  ScalarFirstOrderDifferentiator(final FiniteDifferenceType differenceType);  ScalarFirstOrderDifferentiator(final FiniteDifferenceType differenceType, final double eps); @Override Function1D<Double, Double> differentiate(final Function1D<Double, Double> function); @Override Function1D<Double, Double> differentiate(final Function1D<Double, Double> function, final Function1D<Double, Boolean> domain);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullFunction() { CENTRAL.differentiate((Function1D<Double, Double>) null); }
@Test public void test() { final double x = 0.2245; assertEquals(FORWARD.differentiate(F).evaluate(x), DX_ANALYTIC.evaluate(x), 10 * EPS); assertEquals(CENTRAL.differentiate(F).evaluate(x), DX_ANALYTIC.evaluate(x), EPS * EPS); assertEquals(BACKWARD.differentiate(F).evaluate(x), DX_ANALYTIC.evaluate(x), 10 * EPS); }
@Test public void domainTest() { final double[] x = new double[] {1.2, 0, Math.PI }; final Function1D<Double, Double> alFunc = CENTRAL.differentiate(F, DOMAIN); for (int i = 0; i < 3; i++) { assertEquals(alFunc.evaluate(x[i]), DX_ANALYTIC.evaluate(x[i]), 1e-8); } }