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 javax.annotation.Nullable;
41
42 import static java.lang.String.format;
43 import static java.util.Arrays.asList;
44 import static java.util.Arrays.copyOf;
45 import static java.util.Arrays.fill;
46 import static java.util.Arrays.stream;
47
48 import java.util.List;
49
50
51
52
53
54
55 public class CompositeCurve extends FunctionDataCurve {
56
57
58 private final FunctionDataCurve[] curves;
59
60 private final double[] coefficients;
61
62 private final int nCurves;
63
64
65
66
67
68
69
70
71 public CompositeCurve(List<FunctionDataCurve> componentCurves, @Nullable List<Double> coefficients) {
72 assert !componentCurves.isEmpty();
73
74 nCurves = componentCurves.size();
75 this.curves = new FunctionDataCurve[nCurves];
76 componentCurves.toArray(this.curves);
77 assert (coefficients == null || nCurves == coefficients.size());
78
79 if (coefficients == null) {
80 this.coefficients = new double[nCurves];
81 fill(this.coefficients, 1.0);
82 } else {
83 this.coefficients = coefficients.stream().mapToDouble(Double::doubleValue).toArray();
84 }
85
86 FunctionDataCurve curve0 = curves[0];
87 lb = curve0.lowerBound();
88 ub = curve0.upperBound();
89 halfWidthEnd = curve0.halfWidthEnds();
90 x = curve0.getX();
91 double sep = curve0.binWidth();
92
93 boolean isInvalid =
94 stream(curves)
95 .anyMatch(
96 (FunctionDataCurve c) -> {
97 if (lb != c.lowerBound()) {
98 return true;
99 }
100 if (ub != c.upperBound()) {
101 return true;
102 }
103 if (halfWidthEnd != c.halfWidthEnds()) {
104 return true;
105 }
106 return sep != c.binWidth();
107 });
108 if (isInvalid) {
109 throw new IllegalArgumentException(
110 " Not all curves passed to CompositeCurve had the same x[] points!");
111 }
112
113 int nPoints = curve0.numPoints();
114 points = new double[nPoints];
115 for (int i = 0; i < nPoints; i++) {
116 points[i] = 0.0;
117 for (int j = 0; j < nCurves; j++) {
118 points[i] += (this.coefficients[j] * curves[j].getFxPoint(i));
119 }
120 }
121 }
122
123
124 @Override
125 public double fX(double x) {
126 return valAt(x);
127 }
128
129
130
131
132
133
134 public List<FunctionDataCurve> getSubCurves() {
135 return asList(curves);
136 }
137
138
139
140
141
142
143 public double[] getWeights() {
144 return copyOf(coefficients, coefficients.length);
145 }
146
147
148 @Override
149 public double integralAt(double x) {
150 double val = 0.0;
151 for (int i = 0; i < nCurves; i++) {
152 val += (curves[i].integralAt(x) * coefficients[i]);
153 }
154 return val;
155 }
156
157
158 @Override
159 public String toString() {
160 StringBuilder sb =
161 new StringBuilder(
162 format(
163 "Composite curve with %d points from lower bound %9.3g and upper bound %9.3g",
164 points.length, lb, ub));
165 if (halfWidthEnd) {
166 sb.append(" and half-width start/end bins.\nComponent curves:\n");
167 }
168 for (FunctionDataCurve curve : curves) {
169 sb.append(curve.toString());
170 }
171 return sb.toString();
172 }
173
174
175 private double valAt(double x) {
176 double val = 0.0;
177 for (int i = 0; i < nCurves; i++) {
178 val += (curves[i].fX(x) * coefficients[i]);
179 }
180 return val;
181 }
182 }