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.*; |
8 |
|
|
9 |
|
/** |
10 |
|
* Adapted from FJTask version. |
11 |
< |
* Sample program using Guassian Quadrature for numerical integration. |
11 |
> |
* Sample program using Gaussian Quadrature for numerical integration. |
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); |
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()); |
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; |
91 |
|
} |
92 |
|
} |
93 |
|
|
96 |
– |
|
94 |
|
static class Integrator { |
95 |
|
final Function f; // The function to integrate |
96 |
|
final double errorTolerance; |
113 |
|
return q.area; |
114 |
|
} |
115 |
|
|
119 |
– |
|
116 |
|
/** |
117 |
|
* FJTask to recursively perform the quadrature. |
118 |
|
* Algorithm: |
120 |
|
* and from the center point to the upper bound. If this |
121 |
|
* differs from the value from lower to upper by more than |
122 |
|
* the error tolerance, recurse on each half. |
123 |
< |
**/ |
123 |
> |
*/ |
124 |
|
final class Quad extends RecursiveAction { |
125 |
|
final double left; // lower bound |
126 |
|
final double right; // upper bound |
167 |
|
} |
168 |
|
|
169 |
|
} |
174 |
– |
|
175 |
– |
|