/** * Uses the function and its derivative. This method uses an initial guess for the root, rather than bounds. * @param function The function, not null * @param derivative The derivative, not null * @param x The initial guess for the root, not null * @return The root * @throws MathException If the root is not found in 1000 attempts. */ public Double getRoot(Function<Double, Double> function, Function<Double, Double> derivative, Double x) { return getRoot(DoubleFunction1D.from(function), DoubleFunction1D.from(derivative), x); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testConvertNull() { DoubleFunction1D.from(null); }
/** * Uses the function and its derivative. * @param function The function, not null * @param derivative The derivative, not null * @param x1 The first bound of the root, not null * @param x2 The second bound of the root, not null * @return The root * @throws MathException If the root is not found in 1000 attempts; if the Newton * step takes the estimate for the root outside the original bounds. */ public Double getRoot(Function<Double, Double> function, Function<Double, Double> derivative, Double x1, Double x2) { checkInputs(function, x1, x2); ArgChecker.notNull(derivative, "derivative"); return getRoot(DoubleFunction1D.from(function), DoubleFunction1D.from(derivative), x1, x2); }
/** * {@inheritDoc} * @throws MathException If the root is not found in 1000 attempts; if the Newton * step takes the estimate for the root outside the original bounds. */ @Override public Double getRoot(Function<Double, Double> function, Double x1, Double x2) { ArgChecker.notNull(function, "function"); return getRoot(DoubleFunction1D.from(function), x1, x2); }
public Double getRoot(Function<Double, Double> function, Double x) { ArgChecker.notNull(function, "function"); ArgChecker.notNull(x, "x"); DoubleFunction1D f = DoubleFunction1D.from(function); return getRoot(f, f.derivative(), x); }
@Test public void testConversion() { final Function<Double, Double> f1 = x -> x * x * x + 2 * x * x - 7 * x + 12; final DoubleFunction1D f2 = DoubleFunction1D.from(f1); for (int i = 0; i < 100; i++) { final double x = Math.random(); assertEquals(f2.applyAsDouble(x), F1.applyAsDouble(x), 0); assertEquals(f2.derivative().applyAsDouble(x), F1.derivative().applyAsDouble(x), 0); } } }