1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.analysis.differentiation;
18
19 import org.hipparchus.exception.LocalizedCoreFormats;
20 import org.hipparchus.exception.MathIllegalArgumentException;
21 import org.hipparchus.linear.MatrixDecomposer;
22 import org.hipparchus.linear.MatrixUtils;
23 import org.hipparchus.linear.RealMatrix;
24 import org.hipparchus.util.MathUtils;
25
26
27
28
29
30
31
32
33
34 public class TaylorMap implements DifferentialAlgebra {
35
36
37 private final double[] point;
38
39
40 private final DerivativeStructure[] functions;
41
42
43
44
45
46
47
48
49
50 public TaylorMap(final double[] point, final DerivativeStructure[] functions) {
51 if (point == null || point.length == 0) {
52 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE,
53 point == null ? 0 : point.length);
54 }
55 if (functions == null || functions.length == 0) {
56 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE,
57 functions == null ? 0 : functions.length);
58 }
59 this.point = point.clone();
60 this.functions = functions.clone();
61 final DSFactory factory0 = functions[0].getFactory();
62 MathUtils.checkDimension(point.length, factory0.getCompiler().getFreeParameters());
63 for (int i = 1; i < functions.length; ++i) {
64 factory0.checkCompatibility(functions[i].getFactory());
65 }
66 }
67
68
69
70
71
72
73
74
75
76 public TaylorMap(final int parameters, final int order, final int nbFunctions) {
77 this(parameters, nbFunctions);
78 final DSFactory factory = new DSFactory(parameters, order);
79 for (int i = 0; i < nbFunctions; ++i) {
80 functions[i] = factory.variable(i, 0.0);
81 }
82 }
83
84
85
86
87
88 private TaylorMap(final int parameters, final int nbFunctions) {
89 this.point = new double[parameters];
90 this.functions = new DerivativeStructure[nbFunctions];
91 }
92
93
94 @Override
95 public int getFreeParameters() {
96 return point.length;
97 }
98
99
100 @Override
101 public int getOrder() {
102 return functions[0].getOrder();
103 }
104
105
106
107
108 @Deprecated
109 public int getNbParameters() {
110 return getFreeParameters();
111 }
112
113
114
115
116 public int getNbFunctions() {
117 return functions.length;
118 }
119
120
121
122
123 public double[] getPoint() {
124 return point.clone();
125 }
126
127
128
129
130
131 public DerivativeStructure getFunction(final int i) {
132 return functions[i];
133 }
134
135
136
137
138
139 private TaylorMap subtract(final TaylorMap map) {
140 final TaylorMap result = new TaylorMap(point.length, functions.length);
141 for (int i = 0; i < result.functions.length; ++i) {
142 result.functions[i] = functions[i].subtract(map.functions[i]);
143 }
144 return result;
145 }
146
147
148
149
150
151 public double[] value(final double... deltaP) {
152 final double[] value = new double[functions.length];
153 for (int i = 0; i < functions.length; ++i) {
154 value[i] = functions[i].taylor(deltaP);
155 }
156 return value;
157 }
158
159
160
161
162
163 public TaylorMap compose(final TaylorMap other) {
164
165
166 MathUtils.checkDimension(getFreeParameters(), other.getNbFunctions());
167
168 final DerivativeStructure[] composed = new DerivativeStructure[functions.length];
169 for (int i = 0; i < functions.length; ++i) {
170 composed[i] = functions[i].rebase(other.functions);
171 }
172
173 return new TaylorMap(other.point, composed);
174
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public TaylorMap invert(final MatrixDecomposer decomposer) {
196
197 final DSFactory factory = functions[0].getFactory();
198 final DSCompiler compiler = factory.getCompiler();
199 final int n = functions.length;
200
201
202 MathUtils.checkDimension(n, functions[0].getFreeParameters());
203
204
205 final int[] indirection = new int[n];
206 int linearIndex = 0;
207 for (int k = 1; linearIndex < n; ++k) {
208 if (compiler.getPartialDerivativeOrdersSum(k) == 1) {
209 indirection[linearIndex++] = k;
210 }
211 }
212
213
214 final RealMatrix linear = MatrixUtils.createRealMatrix(n, n);
215 final TaylorMap nonLinearTM = new TaylorMap(n, n);
216 for (int i = 0; i < n; ++i) {
217 nonLinearTM.functions[i] = factory.build(functions[i].getAllDerivatives());
218 nonLinearTM.functions[i].setDerivativeComponent(0, 0.0);
219 for (int j = 0; j < n; ++j) {
220 final int k = indirection[j];
221 linear.setEntry(i, j, functions[i].getDerivativeComponent(k));
222 nonLinearTM.functions[i].setDerivativeComponent(k, 0.0);
223 }
224 }
225
226
227 final RealMatrix linearInvert = decomposer.decompose(linear).getInverse();
228
229
230 final TaylorMap linearInvertTM = new TaylorMap(n, n);
231 for (int i = 0; i < n; ++i) {
232 linearInvertTM.functions[i] = new DerivativeStructure(factory);
233 for (int j = 0; j < n; ++j) {
234 linearInvertTM.functions[i].setDerivativeComponent(indirection[j], linearInvert.getEntry(i, j));
235 }
236 }
237
238
239
240 final TaylorMap identity = new TaylorMap(n, compiler.getOrder(), n);
241 TaylorMap invertTM = linearInvertTM;
242 for (int k = 1; k < compiler.getOrder(); ++k) {
243 invertTM = linearInvertTM.compose(identity.subtract(nonLinearTM.compose(invertTM)));
244 }
245
246
247 for (int i = 0; i < n; ++i) {
248 invertTM.point[i] = functions[i].getValue();
249 invertTM.functions[i].setDerivativeComponent(0, point[i]);
250 }
251
252 return invertTM;
253
254 }
255
256 }