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.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.hipparchus.analysis.differentiation.Derivative;
29 import org.hipparchus.analysis.differentiation.UnivariateDifferentiableVectorFunction;
30 import org.hipparchus.analysis.polynomials.PolynomialFunction;
31 import org.hipparchus.exception.LocalizedCoreFormats;
32 import org.hipparchus.exception.MathIllegalArgumentException;
33 import org.hipparchus.exception.MathRuntimeException;
34 import org.hipparchus.exception.NullArgumentException;
35 import org.hipparchus.util.CombinatoricsUtils;
36 import org.hipparchus.util.MathArrays;
37 import org.hipparchus.util.MathUtils;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class HermiteInterpolator implements UnivariateDifferentiableVectorFunction {
55
56
57 private final List<Double> abscissae;
58
59
60 private final List<double[]> topDiagonal;
61
62
63 private final List<double[]> bottomDiagonal;
64
65
66
67 public HermiteInterpolator() {
68 this.abscissae = new ArrayList<>();
69 this.topDiagonal = new ArrayList<>();
70 this.bottomDiagonal = new ArrayList<>();
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public void addSamplePoint(final double x, final double[] ... value)
93 throws MathIllegalArgumentException, MathRuntimeException {
94
95 for (int i = 0; i < value.length; ++i) {
96
97 final double[] y = value[i].clone();
98 if (i > 1) {
99 double inv = 1.0 / CombinatoricsUtils.factorial(i);
100 for (int j = 0; j < y.length; ++j) {
101 y[j] *= inv;
102 }
103 }
104
105
106 final int n = abscissae.size();
107 bottomDiagonal.add(n - i, y);
108 double[] bottom0 = y;
109 for (int j = i; j < n; ++j) {
110 final double[] bottom1 = bottomDiagonal.get(n - (j + 1));
111 final double inv = 1.0 / (x - abscissae.get(n - (j + 1)));
112 if (Double.isInfinite(inv)) {
113 throw new MathIllegalArgumentException(LocalizedCoreFormats.DUPLICATED_ABSCISSA_DIVISION_BY_ZERO, x);
114 }
115 for (int k = 0; k < y.length; ++k) {
116 bottom1[k] = inv * (bottom0[k] - bottom1[k]);
117 }
118 bottom0 = bottom1;
119 }
120
121
122 topDiagonal.add(bottom0.clone());
123
124
125 abscissae.add(x);
126
127 }
128
129 }
130
131
132
133
134
135 public PolynomialFunction[] getPolynomials()
136 throws MathIllegalArgumentException {
137
138
139 checkInterpolation();
140
141
142 final PolynomialFunction zero = polynomial(0);
143 PolynomialFunction[] polynomials = new PolynomialFunction[topDiagonal.get(0).length];
144 for (int i = 0; i < polynomials.length; ++i) {
145 polynomials[i] = zero;
146 }
147 PolynomialFunction coeff = polynomial(1);
148
149
150 for (int i = 0; i < topDiagonal.size(); ++i) {
151 double[] tdi = topDiagonal.get(i);
152 for (int k = 0; k < polynomials.length; ++k) {
153 polynomials[k] = polynomials[k].add(coeff.multiply(polynomial(tdi[k])));
154 }
155 coeff = coeff.multiply(polynomial(-abscissae.get(i), 1.0));
156 }
157
158 return polynomials;
159
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173 @Override
174 public double[] value(double x) throws MathIllegalArgumentException {
175
176
177 checkInterpolation();
178
179 final double[] value = new double[topDiagonal.get(0).length];
180 double valueCoeff = 1;
181 for (int i = 0; i < topDiagonal.size(); ++i) {
182 double[] dividedDifference = topDiagonal.get(i);
183 for (int k = 0; k < value.length; ++k) {
184 value[k] += dividedDifference[k] * valueCoeff;
185 }
186 final double deltaX = x - abscissae.get(i);
187 valueCoeff *= deltaX;
188 }
189
190 return value;
191
192 }
193
194
195 @Override
196 public <T extends Derivative<T>> T[] value(T x)
197 throws MathIllegalArgumentException {
198
199
200 checkInterpolation();
201
202 final T[] value = MathArrays.buildArray(x.getField(), topDiagonal.get(0).length);
203 Arrays.fill(value, x.getField().getZero());
204 T valueCoeff = x.getField().getOne();
205 for (int i = 0; i < topDiagonal.size(); ++i) {
206 double[] dividedDifference = topDiagonal.get(i);
207 for (int k = 0; k < value.length; ++k) {
208 value[k] = value[k].add(valueCoeff.multiply(dividedDifference[k]));
209 }
210 final T deltaX = x.subtract(abscissae.get(i));
211 valueCoeff = valueCoeff.multiply(deltaX);
212 }
213
214 return value;
215
216 }
217
218
219
220
221
222
223
224
225
226
227 public double[][] derivatives(double x, int order)
228 throws MathIllegalArgumentException, NullArgumentException {
229
230
231 MathUtils.checkNotNull(x);
232 if (abscissae.isEmpty()) {
233 throw new MathIllegalArgumentException(LocalizedCoreFormats.EMPTY_INTERPOLATION_SAMPLE);
234 }
235
236 final double[] tj = new double[order + 1];
237 tj[0] = 0.0;
238 for (int i = 0; i < order; ++i) {
239 tj[i + 1] = tj[i] + 1;
240 }
241
242 final double[][] derivatives = new double[order + 1][topDiagonal.get(0).length];
243 final double[] valueCoeff = new double[order + 1];
244 valueCoeff[0] = 1.0;
245 for (int i = 0; i < topDiagonal.size(); ++i) {
246 double[] dividedDifference = topDiagonal.get(i);
247 final double deltaX = x - abscissae.get(i);
248 for (int j = order; j >= 0; --j) {
249 for (int k = 0; k < derivatives[j].length; ++k) {
250 derivatives[j][k] += dividedDifference[k] * valueCoeff[j];
251 }
252 valueCoeff[j] *= deltaX;
253 if (j > 0) {
254 valueCoeff[j] += tj[j] * valueCoeff[j - 1];
255 }
256 }
257 }
258
259 return derivatives;
260
261 }
262
263
264
265
266
267 private void checkInterpolation() throws MathIllegalArgumentException {
268 if (abscissae.isEmpty()) {
269 throw new MathIllegalArgumentException(LocalizedCoreFormats.EMPTY_INTERPOLATION_SAMPLE);
270 }
271 }
272
273
274
275
276
277 private PolynomialFunction polynomial(double ... c) {
278 return new PolynomialFunction(c);
279 }
280
281 }