1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.analysis.interpolation;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22
23 import org.apache.commons.math4.legacy.analysis.differentiation.DerivativeStructure;
24 import org.apache.commons.math4.legacy.analysis.differentiation.UnivariateDifferentiableVectorFunction;
25 import org.apache.commons.math4.legacy.analysis.polynomials.PolynomialFunction;
26 import org.apache.commons.math4.legacy.exception.MathArithmeticException;
27 import org.apache.commons.math4.legacy.exception.NoDataException;
28 import org.apache.commons.math4.legacy.exception.ZeroException;
29 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
30 import org.apache.commons.numbers.combinatorics.Factorial;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class HermiteInterpolator implements UnivariateDifferentiableVectorFunction {
49
50
51 private final List<Double> abscissae;
52
53
54 private final List<double[]> topDiagonal;
55
56
57 private final List<double[]> bottomDiagonal;
58
59
60
61 public HermiteInterpolator() {
62 this.abscissae = new ArrayList<>();
63 this.topDiagonal = new ArrayList<>();
64 this.bottomDiagonal = new ArrayList<>();
65 }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public void addSamplePoint(final double x, final double[] ... value)
87 throws ZeroException, MathArithmeticException {
88
89 if (value.length > 20) {
90 throw new MathArithmeticException(LocalizedFormats.NUMBER_TOO_LARGE, value.length, 20);
91 }
92 for (int i = 0; i < value.length; ++i) {
93 final double[] y = value[i].clone();
94 if (i > 1) {
95 double inv = 1.0 / Factorial.value(i);
96 for (int j = 0; j < y.length; ++j) {
97 y[j] *= inv;
98 }
99 }
100
101
102 final int n = abscissae.size();
103 bottomDiagonal.add(n - i, y);
104 double[] bottom0 = y;
105 for (int j = i; j < n; ++j) {
106 final double[] bottom1 = bottomDiagonal.get(n - (j + 1));
107 final double inv = 1.0 / (x - abscissae.get(n - (j + 1)));
108 if (Double.isInfinite(inv)) {
109 throw new ZeroException(LocalizedFormats.DUPLICATED_ABSCISSA_DIVISION_BY_ZERO, x);
110 }
111 for (int k = 0; k < y.length; ++k) {
112 bottom1[k] = inv * (bottom0[k] - bottom1[k]);
113 }
114 bottom0 = bottom1;
115 }
116
117
118 topDiagonal.add(bottom0.clone());
119
120
121 abscissae.add(x);
122 }
123 }
124
125
126
127
128
129 public PolynomialFunction[] getPolynomials()
130 throws NoDataException {
131
132
133 checkInterpolation();
134
135
136 final PolynomialFunction zero = polynomial(0);
137 PolynomialFunction[] polynomials = new PolynomialFunction[topDiagonal.get(0).length];
138 for (int i = 0; i < polynomials.length; ++i) {
139 polynomials[i] = zero;
140 }
141 PolynomialFunction coeff = polynomial(1);
142
143
144 for (int i = 0; i < topDiagonal.size(); ++i) {
145 double[] tdi = topDiagonal.get(i);
146 for (int k = 0; k < polynomials.length; ++k) {
147 polynomials[k] = polynomials[k].add(coeff.multiply(polynomial(tdi[k])));
148 }
149 coeff = coeff.multiply(polynomial(-abscissae.get(i), 1.0));
150 }
151
152 return polynomials;
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166 @Override
167 public double[] value(double x) throws NoDataException {
168
169
170 checkInterpolation();
171
172 final double[] value = new double[topDiagonal.get(0).length];
173 double valueCoeff = 1;
174 for (int i = 0; i < topDiagonal.size(); ++i) {
175 double[] dividedDifference = topDiagonal.get(i);
176 for (int k = 0; k < value.length; ++k) {
177 value[k] += dividedDifference[k] * valueCoeff;
178 }
179 final double deltaX = x - abscissae.get(i);
180 valueCoeff *= deltaX;
181 }
182
183 return value;
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197 @Override
198 public DerivativeStructure[] value(final DerivativeStructure x)
199 throws NoDataException {
200
201
202 checkInterpolation();
203
204 final DerivativeStructure[] value = new DerivativeStructure[topDiagonal.get(0).length];
205 Arrays.fill(value, x.getField().getZero());
206 DerivativeStructure valueCoeff = x.getField().getOne();
207 for (int i = 0; i < topDiagonal.size(); ++i) {
208 double[] dividedDifference = topDiagonal.get(i);
209 for (int k = 0; k < value.length; ++k) {
210 value[k] = value[k].add(valueCoeff.multiply(dividedDifference[k]));
211 }
212 final DerivativeStructure deltaX = x.subtract(abscissae.get(i));
213 valueCoeff = valueCoeff.multiply(deltaX);
214 }
215
216 return value;
217 }
218
219
220
221
222
223 private void checkInterpolation() throws NoDataException {
224 if (abscissae.isEmpty()) {
225 throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE);
226 }
227 }
228
229
230
231
232
233 private PolynomialFunction polynomial(double ... c) {
234 return new PolynomialFunction(c);
235 }
236 }