@Test(expectedExceptions = IllegalArgumentException.class) public void testNull1DFunction() { CommonsMathWrapper.wrapUnivariate((Function<Double, Double>) null); }
/** * {@inheritDoc} * @throws MathException If the Commons method could not evaluate the function; * if the Commons method could not converge. */ @Override public Double getRoot(Function<Double, Double> function, Double xLow, Double xHigh) { checkInputs(function, xLow, xHigh); UnivariateFunction wrapped = CommonsMathWrapper.wrapUnivariate(function); try { return _ridder.solve(MAX_ITER, wrapped, xLow, xHigh); } catch (TooManyEvaluationsException | NoBracketingException e) { throw new MathException(e); } }
/** * Romberg integration method. Note that the Commons implementation fails if the lower bound is larger than the upper - * in this case, the bounds are reversed and the result negated. * @param f The function to integrate, not null * @param lower The lower bound, not null * @param upper The upper bound, not null * @return The result of the integration */ @Override public Double integrate(Function<Double, Double> f, Double lower, Double upper) { ArgChecker.notNull(f, "f"); ArgChecker.notNull(lower, "lower bound"); ArgChecker.notNull(upper, "upper bound"); try { if (lower < upper) { return integrator.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), lower, upper); } log.info("Upper bound was less than lower bound; swapping bounds and negating result"); return -integrator.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), upper, lower); } catch (MaxCountExceededException | MathIllegalArgumentException e) { throw new MathException(e); } }
/** * Trapezoid integration method. Note that the Commons implementation fails if the lower bound is larger than the upper - * in this case, the bounds are reversed and the result negated. * {@inheritDoc} */ @Override public Double integrate(Function<Double, Double> f, Double lower, Double upper) { ArgChecker.notNull(f, "f"); ArgChecker.notNull(lower, "lower"); ArgChecker.notNull(upper, "upper"); try { if (lower < upper) { return INTEGRATOR.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), lower, upper); } log.info("Upper bound was less than lower bound; swapping bounds and negating result"); return -INTEGRATOR.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), upper, lower); } catch (MaxCountExceededException | MathIllegalArgumentException e) { throw new MathException(e); } }
/** * Simpson's integration method. * <p> * Note that the Commons implementation fails if the lower bound is larger than the upper - * in this case, the bounds are reversed and the result negated. * * @param f The function to integrate, not null * @param lower The lower bound, not null * @param upper The upper bound, not null * @return The result of the integration */ @Override public Double integrate(Function<Double, Double> f, Double lower, Double upper) { ArgChecker.notNull(f, "function"); ArgChecker.notNull(lower, "lower bound"); ArgChecker.notNull(upper, "upper bound"); try { if (lower < upper) { return integrator.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), lower, upper); } log.info("Upper bound was less than lower bound; swapping bounds and negating result"); return -integrator.integrate(MAX_EVAL, CommonsMathWrapper.wrapUnivariate(f), upper, lower); } catch (NumberIsTooSmallException | NumberIsTooLargeException e) { throw new MathException(e); } }
@Test public void test1DFunction() { UnivariateFunction commons = CommonsMathWrapper.wrapUnivariate(OG_FUNCTION_1D); for (int i = 0; i < 100; i++) { assertEquals(OG_FUNCTION_1D.apply((double) i), commons.value(i), 1e-15); } }