--- jsr166/src/test/loops/IntegrateGamma.java 2010/09/19 12:55:37 1.1 +++ jsr166/src/test/loops/IntegrateGamma.java 2015/01/15 18:42:39 1.10 @@ -1,20 +1,18 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ import java.util.concurrent.*; /** * Adapted from FJTask version. - * Sample program using Guassian Quadrature for numerical integration. - * Inspired by a + * Sample program using Gaussian Quadrature for numerical integration. + * Inspired by a * Filaments * demo program. - * */ - public class IntegrateGamma { /** for time conversion */ static final long NPS = (1000L * 1000 * 1000); @@ -30,7 +28,7 @@ public class IntegrateGamma { start = new Double(args[1]).doubleValue(); if (args.length > 2) end = new Double(args[2]).doubleValue(); - if (args.length > 3) + if (args.length > 3) exp = Integer.parseInt(args[3]); } catch (Exception e) { @@ -38,11 +36,11 @@ public class IntegrateGamma { return; } - ForkJoinPool g = procs == 0? new ForkJoinPool() : + ForkJoinPool g = (procs == 0) ? new ForkJoinPool() : new ForkJoinPool(procs); System.out.println("Integrating from " + start + " to " + end + " exponent: " + exp + " parallelism " + g.getParallelism()); - + Function f = new SampleFunction(exp); for (int i = 0; i < 10; ++i) { Integrator integrator = new Integrator(f, 0.001, g); @@ -51,7 +49,7 @@ public class IntegrateGamma { double elapsed = elapsedTime(last); System.out.printf("time: %7.3f", elapsed); System.out.println(" Answer = " + result); - } + } System.out.println(g); g.shutdown(); } @@ -66,20 +64,20 @@ public class IntegrateGamma { classes declared as static within Integrate */ - /** A function to be integrated **/ + /** A function to be integrated */ static interface Function { double compute(double x); } /** * Sample from filaments demo. - * Computes (2*n-1)*(x^(2*n-1)) for all odd values - **/ + * Computes (2*n-1)*(x^(2*n-1)) for all odd values. + */ static class SampleFunction implements Function { final int n; SampleFunction(int n) { this.n = n; } - public double compute(double x) { + public double compute(double x) { double power = x; double xsq = x * x; double val = power; @@ -93,7 +91,6 @@ public class IntegrateGamma { } } - static class Integrator { final Function f; // The function to integrate final double errorTolerance; @@ -116,27 +113,26 @@ public class IntegrateGamma { return q.area; } - - /** + /** * FJTask to recursively perform the quadrature. * Algorithm: * Compute the area from lower bound to the center point of interval, * and from the center point to the upper bound. If this * differs from the value from lower to upper by more than * the error tolerance, recurse on each half. - **/ + */ final class Quad extends RecursiveAction { final double left; // lower bound final double right; // upper bound final double f_left; // value of the function evaluated at left final double f_right; // value of the function evaluated at right - + // Area initialized with original estimate from left to right. // It is replaced with refined value. volatile double area; - - Quad(double left, double right, - double f_left, double f_right, + + Quad(double left, double right, + double f_left, double f_right, double area) { this.left = left; this.right = right; @@ -144,32 +140,30 @@ public class IntegrateGamma { this.f_right = f_right; this.area = area; } - + public void compute() { double center = 0.5 * (left + right); - double f_center = f.compute(center); - - double leftArea = 0.5 * (center - left) * (f_left + f_center); + double f_center = f.compute(center); + + double leftArea = 0.5 * (center - left) * (f_left + f_center); double rightArea = 0.5 * (right - center) * (f_center + f_right); double sum = leftArea + rightArea; - + double diff = sum - area; if (diff < 0) diff = -diff; - - if (diff >= errorTolerance) { + + if (diff >= errorTolerance) { Quad q1 = new Quad(left, center, f_left, f_center, leftArea); q1.fork(); Quad q2 = new Quad(center, right, f_center, f_right, rightArea); q2.compute(); q1.join(); - sum = q1.area + q2.area; + sum = q1.area + q2.area; } - + area = sum; } } } } - -