ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/IntegrateGamma.java
Revision: 1.7
Committed: Sun Oct 21 06:14:12 2012 UTC (11 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.6: +0 -1 lines
Log Message:
delete trailing empty lines of javadoc

File Contents

# Content
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/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.
12 * Inspired by a
13 * <A href="http://www.cs.uga.edu/~dkl/filaments/dist.html"> Filaments</A>
14 * demo program.
15 */
16
17 public class IntegrateGamma {
18 /** for time conversion */
19 static final long NPS = (1000L * 1000 * 1000);
20 public static void main(String[] args) {
21 int procs = 0;
22 double start = 1.0;
23 double end = 96.0;
24 int exp = 5;
25 try {
26 if (args.length > 0)
27 procs = Integer.parseInt(args[0]);
28 if (args.length > 1)
29 start = new Double(args[1]).doubleValue();
30 if (args.length > 2)
31 end = new Double(args[2]).doubleValue();
32 if (args.length > 3)
33 exp = Integer.parseInt(args[3]);
34 }
35 catch (Exception e) {
36 System.out.println("Usage: java IntegrateGamma <threads> <lower bound> <upper bound> <exponent>\n (for example 2 1 48 5).");
37 return;
38 }
39
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
45 Function f = new SampleFunction(exp);
46 for (int i = 0; i < 10; ++i) {
47 Integrator integrator = new Integrator(f, 0.001, g);
48 long last = System.nanoTime();
49 double result = integrator.integral(start, end);
50 double elapsed = elapsedTime(last);
51 System.out.printf("time: %7.3f", elapsed);
52 System.out.println(" Answer = " + result);
53 }
54 System.out.println(g);
55 g.shutdown();
56 }
57
58 static double elapsedTime(long startTime) {
59 return (double)(System.nanoTime() - startTime) / NPS;
60 }
61
62 /*
63 This is all set up as if it were part of a more serious
64 framework, but is for now just a demo, with all
65 classes declared as static within Integrate
66 */
67
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 */
77 static class SampleFunction implements Function {
78 final int n;
79 SampleFunction(int n) { this.n = n; }
80
81 public double compute(double x) {
82 double power = x;
83 double xsq = x * x;
84 double val = power;
85 double di = 1.0;
86 for (int i = n - 1; i > 0; --i) {
87 di += 2.0;
88 power *= xsq;
89 val += di * power;
90 }
91 return val;
92 }
93 }
94
95
96 static class Integrator {
97 final Function f; // The function to integrate
98 final double errorTolerance;
99 final ForkJoinPool g;
100
101 Integrator(Function f, double errorTolerance, ForkJoinPool g) {
102 this.f = f;
103 this.errorTolerance = errorTolerance;
104 this.g = g;
105 }
106
107 double integral(double lowerBound, double upperBound) {
108 double f_lower = f.compute(lowerBound);
109 double f_upper = f.compute(upperBound);
110 double initialArea = 0.5 * (upperBound-lowerBound) * (f_upper + f_lower);
111 Quad q = new Quad(lowerBound, upperBound,
112 f_lower, f_upper,
113 initialArea);
114 g.invoke(q);
115 return q.area;
116 }
117
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 */
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
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,
139 double area) {
140 this.left = left;
141 this.right = right;
142 this.f_left = f_left;
143 this.f_right = f_right;
144 this.area = area;
145 }
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);
152 double rightArea = 0.5 * (right - center) * (f_center + f_right);
153 double sum = leftArea + rightArea;
154
155 double diff = sum - area;
156 if (diff < 0) diff = -diff;
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;
165 }
166
167 area = sum;
168 }
169 }
170 }
171
172 }