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.8 by jsr166, Sun Oct 21 06:40:21 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   */
17
16   public class IntegrateGamma {
17      /** for time conversion */
18      static final long NPS = (1000L * 1000 * 1000);
# Line 30 | Line 28 | public class IntegrateGamma {
28                  start = new Double(args[1]).doubleValue();
29              if (args.length > 2)
30                  end = new Double(args[2]).doubleValue();
31 <            if (args.length > 3)
31 >            if (args.length > 3)
32                  exp = Integer.parseInt(args[3]);
33          }
34          catch (Exception e) {
# Line 38 | Line 36 | public class IntegrateGamma {
36              return;
37          }
38  
39 <        ForkJoinPool g = procs == 0? new ForkJoinPool() :
39 >        ForkJoinPool g = (procs == 0) ? new ForkJoinPool() :
40              new ForkJoinPool(procs);
41  
42          System.out.println("Integrating from " + start + " to " + end + " exponent: " + exp + " parallelism " + g.getParallelism());
43 <        
43 >
44          Function f = new SampleFunction(exp);
45          for (int i = 0; i < 10; ++i) {
46              Integrator integrator = new Integrator(f, 0.001, g);
# Line 51 | Line 49 | public class IntegrateGamma {
49              double elapsed = elapsedTime(last);
50              System.out.printf("time: %7.3f", elapsed);
51              System.out.println(" Answer = " + result);
52 <        }        
52 >        }
53          System.out.println(g);
54          g.shutdown();
55      }
# Line 66 | Line 64 | public class IntegrateGamma {
64        classes declared as static within Integrate
65      */
66  
67 <    /** A function to be integrated **/
67 >    /** A function to be integrated */
68      static interface Function {
69          double compute(double x);
70      }
71  
72      /**
73       * Sample from filaments demo.
74 <     * Computes (2*n-1)*(x^(2*n-1)) for all odd values
75 <     **/
74 >     * Computes (2*n-1)*(x^(2*n-1)) for all odd values.
75 >     */
76      static class SampleFunction implements Function {
77          final int n;
78          SampleFunction(int n) { this.n = n; }
79  
80 <        public double compute(double x)  {
80 >        public double compute(double x) {
81              double power = x;
82              double xsq = x * x;
83              double val = power;
# Line 116 | Line 114 | public class IntegrateGamma {
114              return q.area;
115          }
116  
117 <    
118 <        /**
117 >
118 >        /**
119           * FJTask to recursively perform the quadrature.
120           * Algorithm:
121           *  Compute the area from lower bound to the center point of interval,
122           *  and from the center point to the upper bound. If this
123           *  differs from the value from lower to upper by more than
124           *  the error tolerance, recurse on each half.
125 <         **/
125 >         */
126          final class Quad extends RecursiveAction {
127              final double left;       // lower bound
128              final double right;      // upper bound
129              final double f_left;     // value of the function evaluated at left
130              final double f_right;    // value of the function evaluated at right
131 <    
131 >
132              // Area initialized with original estimate from left to right.
133              // It is replaced with refined value.
134              volatile double area;
135 <    
136 <            Quad(double left, double right,
137 <                 double f_left, double f_right,
135 >
136 >            Quad(double left, double right,
137 >                 double f_left, double f_right,
138                   double area) {
139                  this.left = left;
140                  this.right = right;
# Line 144 | Line 142 | public class IntegrateGamma {
142                  this.f_right = f_right;
143                  this.area = area;
144              }
145 <      
145 >
146              public void compute() {
147                  double center = 0.5 * (left + right);
148 <                double f_center = f.compute(center);
149 <        
150 <                double leftArea  = 0.5 * (center - left)  * (f_left + f_center);
148 >                double f_center = f.compute(center);
149 >
150 >                double leftArea  = 0.5 * (center - left)  * (f_left + f_center);
151                  double rightArea = 0.5 * (right - center) * (f_center + f_right);
152                  double sum = leftArea + rightArea;
153 <        
153 >
154                  double diff = sum - area;
155                  if (diff < 0) diff = -diff;
156 <        
157 <                if (diff >= errorTolerance) {
156 >
157 >                if (diff >= errorTolerance) {
158                      Quad q1 = new Quad(left,   center, f_left,   f_center, leftArea);
159                      q1.fork();
160                      Quad q2 = new Quad(center, right,  f_center, f_right,  rightArea);
161                      q2.compute();
162                      q1.join();
163 <                    sum = q1.area + q2.area;
163 >                    sum = q1.area + q2.area;
164                  }
165 <        
165 >
166                  area = sum;
167              }
168          }
169      }
170  
171   }
174
175  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines