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.List;
21
22 import org.apache.commons.math4.legacy.core.FieldElement;
23 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
24 import org.apache.commons.math4.legacy.exception.MathArithmeticException;
25 import org.apache.commons.math4.legacy.exception.NoDataException;
26 import org.apache.commons.math4.legacy.exception.NullArgumentException;
27 import org.apache.commons.math4.legacy.exception.ZeroException;
28 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
29 import org.apache.commons.math4.legacy.core.MathArrays;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class FieldHermiteInterpolator<T extends FieldElement<T>> {
50
51
52 private final List<T> abscissae;
53
54
55 private final List<T[]> topDiagonal;
56
57
58 private final List<T[]> bottomDiagonal;
59
60
61
62 public FieldHermiteInterpolator() {
63 this.abscissae = new ArrayList<>();
64 this.topDiagonal = new ArrayList<>();
65 this.bottomDiagonal = new ArrayList<>();
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 @SafeVarargs
90 public final void addSamplePoint(final T x, final T[] ... value)
91 throws ZeroException, MathArithmeticException,
92 DimensionMismatchException, NullArgumentException {
93
94 NullArgumentException.check(x);
95 T factorial = x.getField().getOne();
96 for (int i = 0; i < value.length; ++i) {
97
98 final T[] y = value[i].clone();
99 if (i > 1) {
100 factorial = factorial.multiply(i);
101 final T inv = factorial.reciprocal();
102 for (int j = 0; j < y.length; ++j) {
103 y[j] = y[j].multiply(inv);
104 }
105 }
106
107
108 final int n = abscissae.size();
109 bottomDiagonal.add(n - i, y);
110 T[] bottom0 = y;
111 for (int j = i; j < n; ++j) {
112 final T[] bottom1 = bottomDiagonal.get(n - (j + 1));
113 if (x.equals(abscissae.get(n - (j + 1)))) {
114 throw new ZeroException(LocalizedFormats.DUPLICATED_ABSCISSA_DIVISION_BY_ZERO, x);
115 }
116 final T inv = x.subtract(abscissae.get(n - (j + 1))).reciprocal();
117 for (int k = 0; k < y.length; ++k) {
118 bottom1[k] = inv.multiply(bottom0[k].subtract(bottom1[k]));
119 }
120 bottom0 = bottom1;
121 }
122
123
124 topDiagonal.add(bottom0.clone());
125
126
127 abscissae.add(x);
128 }
129 }
130
131
132
133
134
135
136
137 public T[] value(T x) throws NoDataException, NullArgumentException {
138
139
140 NullArgumentException.check(x);
141 if (abscissae.isEmpty()) {
142 throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE);
143 }
144
145 final T[] value = MathArrays.buildArray(x.getField(), topDiagonal.get(0).length);
146 T valueCoeff = x.getField().getOne();
147 for (int i = 0; i < topDiagonal.size(); ++i) {
148 T[] dividedDifference = topDiagonal.get(i);
149 for (int k = 0; k < value.length; ++k) {
150 value[k] = value[k].add(dividedDifference[k].multiply(valueCoeff));
151 }
152 final T deltaX = x.subtract(abscissae.get(i));
153 valueCoeff = valueCoeff.multiply(deltaX);
154 }
155
156 return value;
157 }
158
159
160
161
162
163
164
165
166
167 public T[][] derivatives(T x, int order) throws NoDataException, NullArgumentException {
168
169
170 NullArgumentException.check(x);
171 if (abscissae.isEmpty()) {
172 throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE);
173 }
174
175 final T zero = x.getField().getZero();
176 final T one = x.getField().getOne();
177 final T[] tj = MathArrays.buildArray(x.getField(), order + 1);
178 tj[0] = zero;
179 for (int i = 0; i < order; ++i) {
180 tj[i + 1] = tj[i].add(one);
181 }
182
183 final T[][] derivatives =
184 MathArrays.buildArray(x.getField(), order + 1, topDiagonal.get(0).length);
185 final T[] valueCoeff = MathArrays.buildArray(x.getField(), order + 1);
186 valueCoeff[0] = x.getField().getOne();
187 for (int i = 0; i < topDiagonal.size(); ++i) {
188 T[] dividedDifference = topDiagonal.get(i);
189 final T deltaX = x.subtract(abscissae.get(i));
190 for (int j = order; j >= 0; --j) {
191 for (int k = 0; k < derivatives[j].length; ++k) {
192 derivatives[j][k] =
193 derivatives[j][k].add(dividedDifference[k].multiply(valueCoeff[j]));
194 }
195 valueCoeff[j] = valueCoeff[j].multiply(deltaX);
196 if (j > 0) {
197 valueCoeff[j] = valueCoeff[j].add(tj[j].multiply(valueCoeff[j - 1]));
198 }
199 }
200 }
201
202 return derivatives;
203 }
204 }