1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.analysis.interpolation;
23
24 import java.lang.reflect.Array;
25
26 import org.hipparchus.Field;
27 import org.hipparchus.CalculusFieldElement;
28 import org.hipparchus.analysis.polynomials.FieldPolynomialFunction;
29 import org.hipparchus.analysis.polynomials.FieldPolynomialSplineFunction;
30 import org.hipparchus.analysis.polynomials.PolynomialFunction;
31 import org.hipparchus.analysis.polynomials.PolynomialSplineFunction;
32 import org.hipparchus.exception.LocalizedCoreFormats;
33 import org.hipparchus.exception.MathIllegalArgumentException;
34 import org.hipparchus.exception.NullArgumentException;
35 import org.hipparchus.util.FastMath;
36 import org.hipparchus.util.MathArrays;
37 import org.hipparchus.util.Precision;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class AkimaSplineInterpolator
58 implements UnivariateInterpolator, FieldUnivariateInterpolator {
59
60
61 private static final int MINIMUM_NUMBER_POINTS = 5;
62
63
64 private final boolean useModifiedWeights;
65
66
67
68
69
70
71
72
73 public AkimaSplineInterpolator() {
74 this(false);
75 }
76
77
78
79
80
81
82
83
84
85
86
87 public AkimaSplineInterpolator(final boolean useModifiedWeights) {
88 this.useModifiedWeights = useModifiedWeights;
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 @Override
105 public PolynomialSplineFunction interpolate(double[] xvals,
106 double[] yvals)
107 throws MathIllegalArgumentException {
108 if (xvals == null ||
109 yvals == null) {
110 throw new NullArgumentException();
111 }
112
113 MathArrays.checkEqualLength(xvals, yvals);
114
115 if (xvals.length < MINIMUM_NUMBER_POINTS) {
116 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_OF_POINTS,
117 xvals.length,
118 MINIMUM_NUMBER_POINTS, true);
119 }
120
121 MathArrays.checkOrder(xvals);
122
123 final int numberOfDiffAndWeightElements = xvals.length - 1;
124
125 final double[] differences = new double[numberOfDiffAndWeightElements];
126 final double[] weights = new double[numberOfDiffAndWeightElements];
127
128 for (int i = 0; i < differences.length; i++) {
129 differences[i] = (yvals[i + 1] - yvals[i]) / (xvals[i + 1] - xvals[i]);
130 }
131
132 for (int i = 1; i < weights.length; i++) {
133 weights[i] = FastMath.abs(differences[i] - differences[i - 1]);
134 if (useModifiedWeights) {
135
136 weights[i] += FastMath.abs(differences[i] + differences[i - 1]);
137 }
138 }
139
140
141 final double[] firstDerivatives = new double[xvals.length];
142
143 for (int i = 2; i < firstDerivatives.length - 2; i++) {
144 final double wP = weights[i + 1];
145 final double wM = weights[i - 1];
146 if (Precision.equals(wP, 0.0) &&
147 Precision.equals(wM, 0.0)) {
148 final double xv = xvals[i];
149 final double xvP = xvals[i + 1];
150 final double xvM = xvals[i - 1];
151 firstDerivatives[i] = (((xvP - xv) * differences[i - 1]) + ((xv - xvM) * differences[i])) / (xvP - xvM);
152 } else {
153 firstDerivatives[i] = ((wP * differences[i - 1]) + (wM * differences[i])) / (wP + wM);
154 }
155 }
156
157 firstDerivatives[0] = differentiateThreePoint(xvals, yvals, 0, 0, 1, 2);
158 firstDerivatives[1] = differentiateThreePoint(xvals, yvals, 1, 0, 1, 2);
159 firstDerivatives[xvals.length - 2] = differentiateThreePoint(xvals, yvals, xvals.length - 2,
160 xvals.length - 3, xvals.length - 2,
161 xvals.length - 1);
162 firstDerivatives[xvals.length - 1] = differentiateThreePoint(xvals, yvals, xvals.length - 1,
163 xvals.length - 3, xvals.length - 2,
164 xvals.length - 1);
165
166 return interpolateHermiteSorted(xvals, yvals, firstDerivatives);
167
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 @Override
186 public <T extends CalculusFieldElement<T>> FieldPolynomialSplineFunction<T> interpolate(final T[] xvals,
187 final T[] yvals)
188 throws MathIllegalArgumentException {
189 if (xvals == null ||
190 yvals == null) {
191 throw new NullArgumentException();
192 }
193
194 MathArrays.checkEqualLength(xvals, yvals);
195
196 if (xvals.length < MINIMUM_NUMBER_POINTS) {
197 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_OF_POINTS,
198 xvals.length,
199 MINIMUM_NUMBER_POINTS, true);
200 }
201
202 MathArrays.checkOrder(xvals);
203
204 final Field<T> field = xvals[0].getField();
205 final int numberOfDiffAndWeightElements = xvals.length - 1;
206
207 final T[] differences = MathArrays.buildArray(field, numberOfDiffAndWeightElements);
208 final T[] weights = MathArrays.buildArray(field, numberOfDiffAndWeightElements);
209
210 for (int i = 0; i < differences.length; i++) {
211 differences[i] = yvals[i + 1].subtract(yvals[i]).divide(xvals[i + 1].subtract(xvals[i]));
212 }
213
214 for (int i = 1; i < weights.length; i++) {
215 weights[i] = FastMath.abs(differences[i].subtract(differences[i - 1]));
216 }
217
218
219 final T[] firstDerivatives = MathArrays.buildArray(field, xvals.length);
220
221 for (int i = 2; i < firstDerivatives.length - 2; i++) {
222 final T wP = weights[i + 1];
223 final T wM = weights[i - 1];
224 if (Precision.equals(wP.getReal(), 0.0) &&
225 Precision.equals(wM.getReal(), 0.0)) {
226 final T xv = xvals[i];
227 final T xvP = xvals[i + 1];
228 final T xvM = xvals[i - 1];
229 firstDerivatives[i] = xvP.subtract(xv).multiply(differences[i - 1]).
230 add(xv.subtract(xvM).multiply(differences[i])).
231 divide(xvP.subtract(xvM));
232 } else {
233 firstDerivatives[i] = wP.multiply(differences[i - 1]).
234 add(wM.multiply(differences[i])).
235 divide(wP.add(wM));
236 }
237 }
238
239 firstDerivatives[0] = differentiateThreePoint(xvals, yvals, 0, 0, 1, 2);
240 firstDerivatives[1] = differentiateThreePoint(xvals, yvals, 1, 0, 1, 2);
241 firstDerivatives[xvals.length - 2] = differentiateThreePoint(xvals, yvals, xvals.length - 2,
242 xvals.length - 3, xvals.length - 2,
243 xvals.length - 1);
244 firstDerivatives[xvals.length - 1] = differentiateThreePoint(xvals, yvals, xvals.length - 1,
245 xvals.length - 3, xvals.length - 2,
246 xvals.length - 1);
247
248 return interpolateHermiteSorted(xvals, yvals, firstDerivatives);
249
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 private double differentiateThreePoint(double[] xvals, double[] yvals,
266 int indexOfDifferentiation,
267 int indexOfFirstSample,
268 int indexOfSecondsample,
269 int indexOfThirdSample) {
270 final double x0 = yvals[indexOfFirstSample];
271 final double x1 = yvals[indexOfSecondsample];
272 final double x2 = yvals[indexOfThirdSample];
273
274 final double t = xvals[indexOfDifferentiation] - xvals[indexOfFirstSample];
275 final double t1 = xvals[indexOfSecondsample] - xvals[indexOfFirstSample];
276 final double t2 = xvals[indexOfThirdSample] - xvals[indexOfFirstSample];
277
278 final double a = (x2 - x0 - (t2 / t1 * (x1 - x0))) / (t2 * t2 - t1 * t2);
279 final double b = (x1 - x0 - a * t1 * t1) / t1;
280
281 return (2 * a * t) + b;
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 private <T extends CalculusFieldElement<T>> T differentiateThreePoint(T[] xvals, T[] yvals,
300 int indexOfDifferentiation,
301 int indexOfFirstSample,
302 int indexOfSecondsample,
303 int indexOfThirdSample) {
304 final T x0 = yvals[indexOfFirstSample];
305 final T x1 = yvals[indexOfSecondsample];
306 final T x2 = yvals[indexOfThirdSample];
307
308 final T t = xvals[indexOfDifferentiation].subtract(xvals[indexOfFirstSample]);
309 final T t1 = xvals[indexOfSecondsample].subtract(xvals[indexOfFirstSample]);
310 final T t2 = xvals[indexOfThirdSample].subtract(xvals[indexOfFirstSample]);
311
312 final T a = x2.subtract(x0).subtract(t2.divide(t1).multiply(x1.subtract(x0))).
313 divide(t2.multiply(t2).subtract(t1.multiply(t2)));
314 final T b = x1.subtract(x0).subtract(a.multiply(t1).multiply(t1)).divide(t1);
315
316 return a.multiply(t).multiply(2).add(b);
317 }
318
319
320
321
322
323
324
325
326
327
328
329 private PolynomialSplineFunction interpolateHermiteSorted(double[] xvals,
330 double[] yvals,
331 double[] firstDerivatives) {
332 MathArrays.checkEqualLength(xvals, yvals);
333 MathArrays.checkEqualLength(xvals, firstDerivatives);
334
335 final int minimumLength = 2;
336 if (xvals.length < minimumLength) {
337 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_OF_POINTS,
338 xvals.length, minimumLength,
339 true);
340 }
341
342 final int size = xvals.length - 1;
343 final PolynomialFunction[] polynomials = new PolynomialFunction[size];
344 final double[] coefficients = new double[4];
345
346 for (int i = 0; i < polynomials.length; i++) {
347 final double w = xvals[i + 1] - xvals[i];
348 final double w2 = w * w;
349
350 final double yv = yvals[i];
351 final double yvP = yvals[i + 1];
352
353 final double fd = firstDerivatives[i];
354 final double fdP = firstDerivatives[i + 1];
355
356 coefficients[0] = yv;
357 coefficients[1] = firstDerivatives[i];
358 coefficients[2] = (3 * (yvP - yv) / w - 2 * fd - fdP) / w;
359 coefficients[3] = (2 * (yv - yvP) / w + fd + fdP) / w2;
360 polynomials[i] = new PolynomialFunction(coefficients);
361 }
362
363 return new PolynomialSplineFunction(xvals, polynomials);
364
365 }
366
367
368
369
370
371
372
373
374
375
376
377
378 private <T extends CalculusFieldElement<T>> FieldPolynomialSplineFunction<T> interpolateHermiteSorted(T[] xvals,
379 T[] yvals,
380 T[] firstDerivatives) {
381 MathArrays.checkEqualLength(xvals, yvals);
382 MathArrays.checkEqualLength(xvals, firstDerivatives);
383
384 final int minimumLength = 2;
385 if (xvals.length < minimumLength) {
386 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_OF_POINTS,
387 xvals.length, minimumLength,
388 true);
389 }
390
391 final Field<T> field = xvals[0].getField();
392 final int size = xvals.length - 1;
393 @SuppressWarnings("unchecked")
394 final FieldPolynomialFunction<T>[] polynomials =
395 (FieldPolynomialFunction<T>[]) Array.newInstance(FieldPolynomialFunction.class, size);
396 final T[] coefficients = MathArrays.buildArray(field, 4);
397
398 for (int i = 0; i < polynomials.length; i++) {
399 final T w = xvals[i + 1].subtract(xvals[i]);
400 final T w2 = w.multiply(w);
401
402 final T yv = yvals[i];
403 final T yvP = yvals[i + 1];
404
405 final T fd = firstDerivatives[i];
406 final T fdP = firstDerivatives[i + 1];
407
408 coefficients[0] = yv;
409 coefficients[1] = firstDerivatives[i];
410 final T ratio = yvP.subtract(yv).divide(w);
411 coefficients[2] = ratio.multiply(+3).subtract(fd.add(fd)).subtract(fdP).divide(w);
412 coefficients[3] = ratio.multiply(-2).add(fd).add(fdP).divide(w2);
413 polynomials[i] = new FieldPolynomialFunction<>(coefficients);
414 }
415
416 return new FieldPolynomialSplineFunction<>(xvals, polynomials);
417
418 }
419 }