/** {@inheritDoc} */ public double integrate(final int maxEval, final UnivariateFunction f, final double lower, final double upper) throws TooManyEvaluationsException, MaxCountExceededException, MathIllegalArgumentException, NullArgumentException { // Initialization. setup(maxEval, f, lower, upper); // Perform computation. return doIntegrate(); }
/** {@inheritDoc} */ public double value(double x) throws MathIllegalArgumentException, TooManyEvaluationsException { return computeObjectiveValue(x); } };
/** * Compute the n-th stage integral. * @param n number of steps * @return the value of n-th stage integral * @throws TooManyEvaluationsException if the maximum number of evaluations * is exceeded. */ private double stage(final int n) throws TooManyEvaluationsException { // set up the step for the current stage final double step = (getMax() - getMin()) / n; final double halfStep = step / 2.0; // integrate over all elementary steps double midPoint = getMin() + halfStep; double sum = 0.0; for (int i = 0; i < n; ++i) { for (int j = 0; j < abscissas.length; ++j) { sum += weights[j] * computeObjectiveValue(midPoint + halfStep * abscissas[j]); } midPoint += step; } return halfStep * sum; }
/** {@inheritDoc} */ @Override protected double doIntegrate() throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { final double min = getMin(); final double diff = getMax() - min; final double midPoint = min + 0.5 * diff; double oldt = diff * computeObjectiveValue(midPoint); while (true) { incrementCount(); final int i = getIterations(); final double t = stage(i, oldt, min, diff); if (i >= getMinimalIterationCount()) { final double delta = FastMath.abs(t - oldt); final double rLimit = getRelativeAccuracy() * (FastMath.abs(oldt) + FastMath.abs(t)) * 0.5; if ((delta <= rLimit) || (delta <= getAbsoluteAccuracy())) { return t; } } oldt = t; } }
throws TooManyEvaluationsException, MaxCountExceededException { final int m = getMaximalIterationCount() + 1; double previousRow[] = new double[m]; double currentRow[] = new double[m]; TrapezoidIntegrator qtrap = new TrapezoidIntegrator(); currentRow[0] = qtrap.stage(this, 0); incrementCount(); double olds = currentRow[0]; while (true) { final int i = getIterations(); currentRow = tmpRow; currentRow[0] = qtrap.stage(this, i); incrementCount(); for (int j = 1; j <= i; j++) { if (i >= getMinimalIterationCount()) { final double delta = FastMath.abs(s - olds); final double rLimit = getRelativeAccuracy() * (FastMath.abs(olds) + FastMath.abs(s)) * 0.5; if ((delta <= rLimit) || (delta <= getAbsoluteAccuracy())) { return s;
/** {@inheritDoc} */ @Override protected double doIntegrate() throws TooManyEvaluationsException, MaxCountExceededException { TrapezoidIntegrator qtrap = new TrapezoidIntegrator(); if (getMinimalIterationCount() == 1) { return (4 * qtrap.stage(this, 1) - qtrap.stage(this, 0)) / 3.0; } // Simpson's rule requires at least two trapezoid stages. double olds = 0; double oldt = qtrap.stage(this, 0); while (true) { final double t = qtrap.stage(this, getIterations()); incrementCount(); final double s = (4 * t - oldt) / 3.0; if (getIterations() >= getMinimalIterationCount()) { final double delta = FastMath.abs(s - olds); final double rLimit = getRelativeAccuracy() * (FastMath.abs(olds) + FastMath.abs(s)) * 0.5; if ((delta <= rLimit) || (delta <= getAbsoluteAccuracy())) { return s; } } olds = s; oldt = t; } }
/** {@inheritDoc} */ @Override protected double doIntegrate() throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { double oldt = stage(this, 0); incrementCount(); while (true) { final int i = getIterations(); final double t = stage(this, i); if (i >= getMinimalIterationCount()) { final double delta = FastMath.abs(t - oldt); final double rLimit = getRelativeAccuracy() * (FastMath.abs(oldt) + FastMath.abs(t)) * 0.5; if ((delta <= rLimit) || (delta <= getAbsoluteAccuracy())) { return t; } } oldt = t; incrementCount(); } }
/** {@inheritDoc} */ @Override protected double doIntegrate() throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { // compute first estimate with a single step double oldt = stage(1); int n = 2; while (true) { // improve integral with a larger number of steps final double t = stage(n); // estimate error final double delta = FastMath.abs(t - oldt); final double limit = FastMath.max(getAbsoluteAccuracy(), getRelativeAccuracy() * (FastMath.abs(oldt) + FastMath.abs(t)) * 0.5); // check convergence if ((getIterations() + 1 >= getMinimalIterationCount()) && (delta <= limit)) { return t; } // prepare next iteration double ratio = FastMath.min(4, FastMath.pow(delta / limit, 0.5 / abscissas.length)); n = FastMath.max((int) (ratio * n), n + 1); oldt = t; incrementCount(); } }
/** {@inheritDoc} */ @Override protected double doIntegrate() throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { // Compute first estimate with a single step. double oldt = stage(1); int n = 2; while (true) { // Improve integral with a larger number of steps. final double t = stage(n); // Estimate the error. final double delta = FastMath.abs(t - oldt); final double limit = FastMath.max(getAbsoluteAccuracy(), getRelativeAccuracy() * (FastMath.abs(oldt) + FastMath.abs(t)) * 0.5); // check convergence if (getIterations() + 1 >= getMinimalIterationCount() && delta <= limit) { return t; } // Prepare next iteration. final double ratio = FastMath.min(4, FastMath.pow(delta / limit, 0.5 / numberOfPoints)); n = FastMath.max((int) (ratio * n), n + 1); oldt = t; incrementCount(); } }
UnivariateIntegrator integrator = new RombergIntegrator(); for (int i = 0; i < xs.length - 1; i++) { double delta = integrator.integrate(1000000, new UnivariateFunction() { @Override public double value(double v) {
final double min = getMin(); final double max = getMax(); final double step = (max - min) / n; final GaussIntegrator g = FACTORY.legendreHighPrecision(numberOfPoints, a, b); sum += g.integrate(f);
final double max = baseIntegrator.getMax(); final double min = baseIntegrator.getMin(); s = 0.5 * (max - min) * (baseIntegrator.computeObjectiveValue(min) + baseIntegrator.computeObjectiveValue(max)); return s; } else { final long np = 1L << (n-1); // number of new points in this stage double sum = 0; final double max = baseIntegrator.getMax(); final double min = baseIntegrator.getMin(); sum += baseIntegrator.computeObjectiveValue(x); x += spacing;
public double mean(){ UnivariateFunction f = new UnivariateFunction() { @Override public double value(double x) { return (x*densityFunction.value(x)); } }; return integrator.integrate(5000, f, quadPoints[0]-3*h, quadPoints[nPoints-1]+3*h); }
sum += computeObjectiveValue(x); x += spacing;
/** * Compute the area of a sine intensity by using numerical approximation. The * range for which the area is computed is defined by a lower bound of * <code>0</code> and an upper bound of <code>length</code>. * @param s The intensity function to integrate. * @param lb The lower bound of the range. * @param ub The upper bound of the range. * @return The area. */ public static double areaByIntegration(IntensityFunction s, double lb, double ub) { final UnivariateIntegrator ri = new RombergIntegrator(16, 32); final double val = ri.integrate(10000000, asUnivariateFunction(s), lb, ub); return val; }
/** {@inheritDoc} */ public double integrate(final int maxEval, final UnivariateFunction f, final double lower, final double upper) throws TooManyEvaluationsException, MaxCountExceededException, MathIllegalArgumentException, NullArgumentException { // Initialization. setup(maxEval, f, lower, upper); // Perform computation. return doIntegrate(); }
public double variance(){ final double dMean = mean(); UnivariateFunction f = new UnivariateFunction() { @Override public double value(double x) { return Math.pow(x-dMean,2)*densityFunction.value(x); } }; return integrator.integrate(5000, f, quadPoints[0]-3*h, quadPoints[nPoints-1]+3*h); }
/** {@inheritDoc} */ public double value(double x) throws MathIllegalArgumentException, TooManyEvaluationsException { return computeObjectiveValue(x); } };
/** {@inheritDoc} */ public double integrate(final int maxEval, final UnivariateFunction f, final double lower, final double upper) throws TooManyEvaluationsException, MaxCountExceededException, MathIllegalArgumentException, NullArgumentException { // Initialization. setup(maxEval, f, lower, upper); // Perform computation. return doIntegrate(); }
/** {@inheritDoc} */ public double value(double x) throws MathIllegalArgumentException, TooManyEvaluationsException { return computeObjectiveValue(x); } };