ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/IntegrateGamma.java
(Generate patch)

Comparing jsr166/src/test/loops/IntegrateGamma.java (file contents):
Revision 1.1 by dl, Sun Sep 19 12:55:37 2010 UTC vs.
Revision 1.7 by jsr166, Sun Oct 21 06:14:12 2012 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import java.util.concurrent.*;
# Line 9 | Line 9 | import java.util.concurrent.*;
9   /**
10   * Adapted from FJTask version.
11   * Sample program using Guassian Quadrature for numerical integration.
12 < * Inspired by a
12 > * Inspired by a
13   * <A href="http://www.cs.uga.edu/~dkl/filaments/dist.html"> Filaments</A>
14   * demo program.
15 *
15   */
16  
17   public class IntegrateGamma {
# Line 30 | Line 29 | public class IntegrateGamma {
29                  start = new Double(args[1]).doubleValue();
30              if (args.length > 2)
31                  end = new Double(args[2]).doubleValue();
32 <            if (args.length > 3)
32 >            if (args.length > 3)
33                  exp = Integer.parseInt(args[3]);
34          }
35          catch (Exception e) {
# Line 38 | Line 37 | public class IntegrateGamma {
37              return;
38          }
39  
40 <        ForkJoinPool g = procs == 0? new ForkJoinPool() :
40 >        ForkJoinPool g = (procs == 0) ? new ForkJoinPool() :
41              new ForkJoinPool(procs);
42  
43          System.out.println("Integrating from " + start + " to " + end + " exponent: " + exp + " parallelism " + g.getParallelism());
44 <        
44 >
45          Function f = new SampleFunction(exp);
46          for (int i = 0; i < 10; ++i) {
47              Integrator integrator = new Integrator(f, 0.001, g);
# Line 51 | Line 50 | public class IntegrateGamma {
50              double elapsed = elapsedTime(last);
51              System.out.printf("time: %7.3f", elapsed);
52              System.out.println(" Answer = " + result);
53 <        }        
53 >        }
54          System.out.println(g);
55          g.shutdown();
56      }
# Line 66 | Line 65 | public class IntegrateGamma {
65        classes declared as static within Integrate
66      */
67  
68 <    /** A function to be integrated **/
68 >    /** A function to be integrated */
69      static interface Function {
70          double compute(double x);
71      }
72  
73      /**
74       * Sample from filaments demo.
75 <     * Computes (2*n-1)*(x^(2*n-1)) for all odd values
76 <     **/
75 >     * Computes (2*n-1)*(x^(2*n-1)) for all odd values.
76 >     */
77      static class SampleFunction implements Function {
78          final int n;
79          SampleFunction(int n) { this.n = n; }
80  
81 <        public double compute(double x)  {
81 >        public double compute(double x) {
82              double power = x;
83              double xsq = x * x;
84              double val = power;
# Line 116 | Line 115 | public class IntegrateGamma {
115              return q.area;
116          }
117  
118 <    
119 <        /**
118 >
119 >        /**
120           * FJTask to recursively perform the quadrature.
121           * Algorithm:
122           *  Compute the area from lower bound to the center point of interval,
123           *  and from the center point to the upper bound. If this
124           *  differs from the value from lower to upper by more than
125           *  the error tolerance, recurse on each half.
126 <         **/
126 >         */
127          final class Quad extends RecursiveAction {
128              final double left;       // lower bound
129              final double right;      // upper bound
130              final double f_left;     // value of the function evaluated at left
131              final double f_right;    // value of the function evaluated at right
132 <    
132 >
133              // Area initialized with original estimate from left to right.
134              // It is replaced with refined value.
135              volatile double area;
136 <    
137 <            Quad(double left, double right,
138 <                 double f_left, double f_right,
136 >
137 >            Quad(double left, double right,
138 >                 double f_left, double f_right,
139                   double area) {
140                  this.left = left;
141                  this.right = right;
# Line 144 | Line 143 | public class IntegrateGamma {
143                  this.f_right = f_right;
144                  this.area = area;
145              }
146 <      
146 >
147              public void compute() {
148                  double center = 0.5 * (left + right);
149 <                double f_center = f.compute(center);
150 <        
151 <                double leftArea  = 0.5 * (center - left)  * (f_left + f_center);
149 >                double f_center = f.compute(center);
150 >
151 >                double leftArea  = 0.5 * (center - left)  * (f_left + f_center);
152                  double rightArea = 0.5 * (right - center) * (f_center + f_right);
153                  double sum = leftArea + rightArea;
154 <        
154 >
155                  double diff = sum - area;
156                  if (diff < 0) diff = -diff;
157 <        
158 <                if (diff >= errorTolerance) {
157 >
158 >                if (diff >= errorTolerance) {
159                      Quad q1 = new Quad(left,   center, f_left,   f_center, leftArea);
160                      q1.fork();
161                      Quad q2 = new Quad(center, right,  f_center, f_right,  rightArea);
162                      q2.compute();
163                      q1.join();
164 <                    sum = q1.area + q2.area;
164 >                    sum = q1.area + q2.area;
165                  }
166 <        
166 >
167                  area = sum;
168              }
169          }
170      }
171  
172   }
174
175  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines