/** * Returns a function that calculates the first derivative. * <p> * The method used is central finite difference, with $\epsilon = 10^{-5}$. * Implementing classes can override this method to return a function that * is the exact functional representation of the first derivative. * * @return a function that calculates the first derivative of this function */ public default DoubleFunction1D derivative() { return derivative(FiniteDifferenceType.CENTRAL, 1e-5); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testDerivativeNullType() { F1.derivative(null, EPS); }
/** * Uses the {@link DoubleFunction1D#derivative()} method. This method uses an initial * guess for the root, rather than bounds. * * @param function The function, 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(DoubleFunction1D function, Double x) { ArgChecker.notNull(function, "function"); return getRoot(function, function.derivative(), x); }
/** * Uses the {@link DoubleFunction1D#derivative()} method. <i>x<sub>1</sub></i> and * <i>x<sub>2</sub></i> do not have to be increasing. * * @param function The function, 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(DoubleFunction1D function, Double x1, Double x2) { ArgChecker.notNull(function, "function"); return getRoot(function, function.derivative(), 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 testDerivative() { final double x = RANDOM.nextDouble(); assertEquals(3 * C[3] * Math.pow(x, 2) + 2 * C[2] * x + C[1], F.derivative().applyAsDouble(x), EPS); } }
@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); } } }
@Test public void testDerivative() { assertEquals(F1.derivative().applyAsDouble(X), DF1.applyAsDouble(X), 1e-3); assertEquals(F2.derivative().applyAsDouble(X), DF2.applyAsDouble(X), 1e-3); assertEquals(F1.derivative(FiniteDifferenceType.CENTRAL, 1e-5).applyAsDouble(X), DF1.applyAsDouble(X), 1e-3); assertEquals(F2.derivative(FiniteDifferenceType.CENTRAL, 1e-5).applyAsDouble(X), DF2.applyAsDouble(X), 1e-3); assertEquals(F1.derivative(FiniteDifferenceType.FORWARD, 1e-5).applyAsDouble(X), DF1.applyAsDouble(X), 1e-3); assertEquals(F2.derivative(FiniteDifferenceType.FORWARD, 1e-5).applyAsDouble(X), DF2.applyAsDouble(X), 1e-3); assertEquals(F1.derivative(FiniteDifferenceType.BACKWARD, 1e-5).applyAsDouble(X), DF1.applyAsDouble(X), 1e-3); assertEquals(F2.derivative(FiniteDifferenceType.BACKWARD, 1e-5).applyAsDouble(X), DF2.applyAsDouble(X), 1e-3); assertEquals(F3.derivative().applyAsDouble(X), DF1.applyAsDouble(X), 1e-15); assertEquals(F4.derivative().applyAsDouble(X), DF2.applyAsDouble(X), 1e-15); }
@Override public Pair<DoubleFunction1D, DoubleFunction1D>[] getPolynomialsAndFirstDerivative(int n) { ArgChecker.isTrue(n >= 0); @SuppressWarnings("unchecked") Pair<DoubleFunction1D, DoubleFunction1D>[] polynomials = new Pair[n + 1]; DoubleFunction1D p, dp; for (int i = 0; i <= n; i++) { if (i == 0) { polynomials[i] = Pair.of(getOne(), getZero()); } else if (i == 1) { polynomials[i] = Pair.of(getX(), getOne()); } else { p = (polynomials[i - 1].getFirst() .multiply(getX()) .multiply(2 * i - 1) .subtract(polynomials[i - 2].getFirst().multiply(i - 1))).multiply(1. / i); dp = p.derivative(); polynomials[i] = Pair.of(p, dp); } } return polynomials; }
DoubleFunction1D temp3 = p2.multiply(getD(alpha, beta, j)); p = (temp1.add(temp2).add(temp3)).divide(getA(alpha, beta, j)); dp = p.derivative(); polynomials[i] = Pair.of(p, dp);