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