You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2013/06/03 15:48:23 UTC

svn commit: r1488992 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/integration/MidPointIntegrator.java

Author: erans
Date: Mon Jun  3 13:48:23 2013
New Revision: 1488992

URL: http://svn.apache.org/r1488992
Log:
Removed branching.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/integration/MidPointIntegrator.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/integration/MidPointIntegrator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/integration/MidPointIntegrator.java?rev=1488992&r1=1488991&r2=1488992&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/integration/MidPointIntegrator.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/analysis/integration/MidPointIntegrator.java Mon Jun  3 13:48:23 2013
@@ -105,47 +105,54 @@ public class MidPointIntegrator extends 
      * arbitrary m sections because this configuration can best utilize the
      * already computed values.</p>
      *
-     * @param n the stage of 1/2 refinement, n = 0 is no refinement
-     * @param previousStageResult Result from the previous call to the {@code stage}
-     * method. It is unused in the first stage (when {@code n} is equal to 0).
+     * @param n the stage of 1/2 refinement. Must be larger than 0.
+     * @param previousStageResult Result from the previous call to the
+     * {@code stage} method.
+     * @param min Lower bound of the integration interval.
+     * @param diffMaxMin Difference between the lower bound and upper bound
+     * of the integration interval.
      * @return the value of n-th stage integral
      * @throws TooManyEvaluationsException if the maximal number of evaluations
      * is exceeded.
      */
     private double stage(final int n,
-                         double previousStageResult)
+                         double previousStageResult,
+                         double min,
+                         double diffMaxMin)
         throws TooManyEvaluationsException {
 
-        final double min = getMin();
-        final double diff = getMax() - min;
-
-        if (n == 0) {
-            final double midPoint = min + 0.5 * diff;
-            return diff * computeObjectiveValue(midPoint);
-        } else {
-            final long np = 1L << (n - 1);           // number of new points in this stage
-            double sum = 0;
-            // spacing between adjacent new points
-            final double spacing = diff / np;
-            double x = min + 0.5 * spacing;    // the first new point
-            for (long i = 0; i < np; i++) {
-                sum += computeObjectiveValue(x);
-                x += spacing;
-            }
-            // add the new sum to previously calculated result
-            return 0.5 * (previousStageResult + sum * spacing);
+        // number of new points in this stage
+        final long np = 1L << (n - 1);
+        double sum = 0;
+
+        // spacing between adjacent new points
+        final double spacing = diffMaxMin / np;
+
+        // the first new point
+        double x = min + 0.5 * spacing;
+        for (long i = 0; i < np; i++) {
+            sum += computeObjectiveValue(x);
+            x += spacing;
         }
+        // add the new sum to previously calculated result
+        return 0.5 * (previousStageResult + sum * spacing);
     }
 
+
     /** {@inheritDoc} */
     protected double doIntegrate()
         throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException {
 
-        double oldt = stage(0, 0d);
-        iterations.incrementCount();
+        final double min = getMin();
+        final double diff = getMax() - min;
+        final double midPoint = min + 0.5 * diff;
+
+        double oldt = diff * computeObjectiveValue(midPoint);
+
         while (true) {
+            iterations.incrementCount();
             final int i = iterations.getCount();
-            final double t = stage(i, oldt);
+            final double t = stage(i, oldt, min, diff);
             if (i >= getMinimalIterationCount()) {
                 final double delta = FastMath.abs(t - oldt);
                 final double rLimit =
@@ -155,7 +162,6 @@ public class MidPointIntegrator extends 
                 }
             }
             oldt = t;
-            iterations.incrementCount();
         }
 
     }