1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.numerics.integrate;
39
40 import static java.lang.String.format;
41 import static java.lang.System.arraycopy;
42 import static org.apache.commons.math3.util.FastMath.abs;
43 import static org.apache.commons.math3.util.FastMath.max;
44 import static org.apache.commons.math3.util.FastMath.ulp;
45
46
47
48
49
50
51
52 public abstract class FunctionDataCurve implements DataSet {
53
54
55 protected double lb;
56
57 protected double ub;
58
59 protected double[] points;
60
61 protected double[] x;
62
63 protected boolean halfWidthEnd;
64
65
66
67
68 public FunctionDataCurve() {
69 lb = 0.0;
70 ub = 0.0;
71 points = new double[0];
72 x = new double[0];
73 halfWidthEnd = false;
74 }
75
76
77
78
79
80
81
82
83 public static boolean approxEquals(double x1, double x2) {
84 return (approxEquals(x1, x2, 10.0));
85 }
86
87
88
89
90
91
92
93
94
95 public static boolean approxEquals(double x1, double x2, double ulpMultiple) {
96 double diff = abs(x1 - x2);
97
98 double ulp = ulp(max(abs(x1), abs(x2)));
99 return (diff < (ulp * ulpMultiple));
100 }
101
102
103
104
105
106
107 public double analyticalIntegral() {
108 return analyticalIntegral(lowerBound(), upperBound());
109 }
110
111
112
113
114
115
116
117
118 public double analyticalIntegral(double lb, double ub) {
119 return integralAt(ub) - integralAt(lb);
120 }
121
122
123 @Override
124 public double binWidth() {
125 double divisor = halfWidthEnds() ? (double) (points.length - 2) : (double) (points.length - 1);
126 return (ub - lb) / divisor;
127 }
128
129
130
131
132
133
134
135 public abstract double fX(double x);
136
137
138 @Override
139 public double[] getAllFxPoints() {
140 int nPoints = points.length;
141 double[] retArray = new double[nPoints];
142 arraycopy(points, 0, retArray, 0, nPoints);
143 return retArray;
144 }
145
146
147 @Override
148 public double getFxPoint(int index) {
149 return points[index];
150 }
151
152
153 @Override
154 public double[] getX() {
155 double[] copyX = new double[x.length];
156 arraycopy(x, 0, copyX, 0, x.length);
157 return copyX;
158 }
159
160
161 @Override
162 public boolean halfWidthEnds() {
163 return halfWidthEnd;
164 }
165
166
167
168
169
170
171
172 public abstract double integralAt(double x);
173
174
175 @Override
176 public double lowerBound() {
177 return lb;
178 }
179
180
181 @Override
182 public int numPoints() {
183 return points.length;
184 }
185
186
187 @Override
188 public String toString() {
189 StringBuilder sb =
190 new StringBuilder(
191 format(
192 "Function f(x) curve with %d points from lower bound %9.3g and upper bound %9.3g",
193 points.length, lb, ub));
194 if (halfWidthEnd) {
195 sb.append(" and half-width start/end bins");
196 }
197 sb.append(".");
198 return sb.toString();
199 }
200
201
202 @Override
203 public double upperBound() {
204 return ub;
205 }
206
207
208
209
210 protected final void assertXIntegrity() {
211 assert ub > lb;
212 int nX = numPoints();
213 double sep = binWidth();
214 if (halfWidthEnd) {
215 assert x.length == nX;
216 assert lb == x[0];
217 assert ub == x[nX - 1];
218
219 assert approxEquals(x[1], lb + 0.5 * sep);
220 assert approxEquals(x[nX - 2], (ub - 0.5 * sep));
221
222 for (int i = 2; i < (nX - 2); i++) {
223 double target = lb + 0.5 * sep;
224 target += ((i - 1) * sep);
225 assert approxEquals(x[i], target);
226 }
227 } else {
228 for (int i = 0; i < x.length; i++) {
229 assert approxEquals(x[i], x[0] + i * sep);
230 }
231 }
232 }
233 }