Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method stage in class TrapezoidIntegrator rewrite as override. Remove not using Exception #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.exception.NumberIsTooLargeException;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.exception.TooManyEvaluationsException;
import org.apache.commons.math4.util.FastMath;

/**
Expand Down Expand Up @@ -58,7 +57,7 @@ public RombergIntegrator(final double relativeAccuracy,
final double absoluteAccuracy,
final int minimalIterationCount,
final int maximalIterationCount)
throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException {
throws NumberIsTooSmallException, NumberIsTooLargeException {
super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
if (maximalIterationCount > ROMBERG_MAX_ITERATIONS_COUNT) {
throw new NumberIsTooLargeException(maximalIterationCount,
Expand All @@ -80,7 +79,7 @@ public RombergIntegrator(final double relativeAccuracy,
*/
public RombergIntegrator(final int minimalIterationCount,
final int maximalIterationCount)
throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException {
throws NumberIsTooSmallException, NumberIsTooLargeException {
super(minimalIterationCount, maximalIterationCount);
if (maximalIterationCount > ROMBERG_MAX_ITERATIONS_COUNT) {
throw new NumberIsTooLargeException(maximalIterationCount,
Expand All @@ -99,14 +98,14 @@ public RombergIntegrator() {
/** {@inheritDoc} */
@Override
protected double doIntegrate()
throws TooManyEvaluationsException, MaxCountExceededException {
throws MaxCountExceededException {

final int m = iterations.getMaximalCount() + 1;
double previousRow[] = new double[m];
double currentRow[] = new double[m];
double[] previousRow = new double[m];
double[] currentRow = new double[m];

TrapezoidIntegrator qtrap = new TrapezoidIntegrator();
currentRow[0] = qtrap.stage(this, 0);
currentRow[0] = qtrap.stage(this);
iterations.increment();
double olds = currentRow[0];
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.commons.math4.analysis.integration;

import org.apache.commons.math4.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.exception.NumberIsTooLargeException;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.util.FastMath;
Expand Down Expand Up @@ -44,8 +43,6 @@ public class SimpsonIntegrator extends BaseAbstractUnivariateIntegrator {
* @param minimalIterationCount minimum number of iterations
* @param maximalIterationCount maximum number of iterations
* (must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT})
* @exception NotStrictlyPositiveException if minimal number of iterations
* is not strictly positive
* @exception NumberIsTooSmallException if maximal number of iterations
* is lesser than or equal to the minimal number of iterations
* @exception NumberIsTooLargeException if maximal number of iterations
Expand All @@ -55,7 +52,7 @@ public SimpsonIntegrator(final double relativeAccuracy,
final double absoluteAccuracy,
final int minimalIterationCount,
final int maximalIterationCount)
throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException {
throws NumberIsTooSmallException, NumberIsTooLargeException {
super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) {
throw new NumberIsTooLargeException(maximalIterationCount,
Expand All @@ -68,16 +65,14 @@ public SimpsonIntegrator(final double relativeAccuracy,
* @param minimalIterationCount minimum number of iterations
* @param maximalIterationCount maximum number of iterations
* (must be less than or equal to {@link #SIMPSON_MAX_ITERATIONS_COUNT})
* @exception NotStrictlyPositiveException if minimal number of iterations
* is not strictly positive
* @exception NumberIsTooSmallException if maximal number of iterations
* is lesser than or equal to the minimal number of iterations
* @exception NumberIsTooLargeException if maximal number of iterations
* is greater than {@link #SIMPSON_MAX_ITERATIONS_COUNT}
*/
public SimpsonIntegrator(final int minimalIterationCount,
final int maximalIterationCount)
throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException {
throws NumberIsTooSmallException, NumberIsTooLargeException {
super(minimalIterationCount, maximalIterationCount);
if (maximalIterationCount > SIMPSON_MAX_ITERATIONS_COUNT) {
throw new NumberIsTooLargeException(maximalIterationCount,
Expand All @@ -100,7 +95,7 @@ protected double doIntegrate() {
// So we set the first sum using two trapezoid stages.
final TrapezoidIntegrator qtrap = new TrapezoidIntegrator();

final double s0 = qtrap.stage(this, 0);
final double s0 = qtrap.stage(this);
double oldt = qtrap.stage(this, 1);
double olds = (4 * oldt - s0) / 3.0;
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
*/
package org.apache.commons.math4.analysis.integration;

import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.apache.commons.math4.exception.MaxCountExceededException;
import org.apache.commons.math4.exception.NotStrictlyPositiveException;
import org.apache.commons.math4.exception.NumberIsTooLargeException;
import org.apache.commons.math4.exception.NumberIsTooSmallException;
import org.apache.commons.math4.exception.TooManyEvaluationsException;
import org.apache.commons.math4.exception.*;
import org.apache.commons.math4.util.FastMath;

/**
Expand Down Expand Up @@ -59,7 +54,7 @@ public TrapezoidIntegrator(final double relativeAccuracy,
final double absoluteAccuracy,
final int minimalIterationCount,
final int maximalIterationCount)
throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException {
throws NumberIsTooSmallException, NumberIsTooLargeException {
super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
if (maximalIterationCount > TRAPEZOID_MAX_ITERATIONS_COUNT) {
throw new NumberIsTooLargeException(maximalIterationCount,
Expand All @@ -80,7 +75,7 @@ public TrapezoidIntegrator(final double relativeAccuracy,
*/
public TrapezoidIntegrator(final int minimalIterationCount,
final int maximalIterationCount)
throws NotStrictlyPositiveException, NumberIsTooSmallException, NumberIsTooLargeException {
throws NumberIsTooSmallException, NumberIsTooLargeException {
super(minimalIterationCount, maximalIterationCount);
if (maximalIterationCount > TRAPEZOID_MAX_ITERATIONS_COUNT) {
throw new NumberIsTooLargeException(maximalIterationCount,
Expand All @@ -97,9 +92,9 @@ public TrapezoidIntegrator() {
}

/**
* Compute the n-th stage integral of trapezoid rule. This function
* should only be called by API <code>integrate()</code> in the package.
* To save time it does not verify arguments - caller does.
* Compute the n-th stage (from first to up) integral of trapezoid rule.
* This function should only be called by API <code>integrate()</code>
* in the package. To save time it does not verify arguments - caller does.
* <p>
* The interval is divided equally into 2^n sections rather than an
* arbitrary m sections because this configuration can best utilize the
Expand All @@ -112,39 +107,58 @@ public TrapezoidIntegrator() {
* is exceeded.
*/
double stage(final BaseAbstractUnivariateIntegrator baseIntegrator, final int n)
throws TooManyEvaluationsException {

if (n == 0) {
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();
// spacing between adjacent new points
final double spacing = (max - min) / np;
double x = min + 0.5 * spacing; // the first new point
for (long i = 0; i < np; i++) {
sum += baseIntegrator.computeObjectiveValue(x);
x += spacing;
}
// add the new sum to previously calculated result
s = 0.5 * (s + sum * spacing);
return s;
throws TooManyEvaluationsException {

if (n == 0)
throw new NumberIsTooSmallException(0, 1, true);

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();
// spacing between adjacent new points
final double spacing = (max - min) / np;
double x = min + 0.5 * spacing; // the first new point
for (long i = 0; i < np; i++) {
sum += baseIntegrator.computeObjectiveValue(x);
x += spacing;
}
// add the new sum to previously calculated result
s = 0.5 * (s + sum * spacing);
return s;
}

/**
* Compute the zero stage integral of trapezoid rule. This function
* should only be called by API <code>integrate()</code> in the package.
* To save time it does not verify arguments - caller does.
* <p>
* The interval is divided equally into 2^n sections rather than an
* arbitrary m sections because this configuration can best utilize the
* already computed values.</p>
*
* @param baseIntegrator integrator holding integration parameters
* @return the value of n-th stage integral
* @throws TooManyEvaluationsException if the maximal number of evaluations
* is exceeded.
*/
double stage(final BaseAbstractUnivariateIntegrator baseIntegrator)
throws TooManyEvaluationsException {

final double max = baseIntegrator.getMax();
final double min = baseIntegrator.getMin();
s = 0.5 * (max - min) *
(baseIntegrator.computeObjectiveValue(min) +
baseIntegrator.computeObjectiveValue(max));
return s;
}

/** {@inheritDoc} */
@Override
protected double doIntegrate()
throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException {
throws MathIllegalArgumentException, MaxCountExceededException {

double oldt = stage(this, 0);
double oldt = stage(this);
iterations.increment();
while (true) {
final int i = iterations.getCount();
Expand Down