--- jsr166/src/test/loops/IntegrateGamma.java 2010/09/19 12:55:37 1.1 +++ jsr166/src/test/loops/IntegrateGamma.java 2010/09/20 20:42:37 1.2 @@ -9,10 +9,10 @@ import java.util.concurrent.*; /** * Adapted from FJTask version. * Sample program using Guassian Quadrature for numerical integration. - * Inspired by a + * Inspired by a * Filaments * demo program. - * + * */ public class IntegrateGamma { @@ -30,7 +30,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 +38,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 +51,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(); } @@ -116,8 +116,8 @@ 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, @@ -130,13 +130,13 @@ public class IntegrateGamma { 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,27 +144,27 @@ 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; } } @@ -172,4 +172,4 @@ public class IntegrateGamma { } - +