1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.ode;
18
19 import java.lang.reflect.Array;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23
24 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
25 import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
26 import org.apache.commons.math4.legacy.exception.MaxCountExceededException;
27 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class JacobianMatrices {
56
57
58 private ExpandableStatefulODE efode;
59
60
61 private int index;
62
63
64 private MainStateJacobianProvider jode;
65
66
67 private ParameterizedODE pode;
68
69
70 private int stateDim;
71
72
73 private ParameterConfiguration[] selectedParameters;
74
75
76 private List<ParameterJacobianProvider> jacobianProviders;
77
78
79 private int paramDim;
80
81
82 private boolean dirtyParameter;
83
84
85 private double[] matricesData;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public JacobianMatrices(final FirstOrderDifferentialEquations fode, final double[] hY,
103 final String... parameters)
104 throws DimensionMismatchException {
105 this(new MainStateJacobianWrapper(fode, hY), parameters);
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120 public JacobianMatrices(final MainStateJacobianProvider jode,
121 final String... parameters) {
122
123 this.efode = null;
124 this.index = -1;
125
126 this.jode = jode;
127 this.pode = null;
128
129 this.stateDim = jode.getDimension();
130
131 if (parameters == null) {
132 selectedParameters = null;
133 paramDim = 0;
134 } else {
135 this.selectedParameters = new ParameterConfiguration[parameters.length];
136 for (int i = 0; i < parameters.length; ++i) {
137 selectedParameters[i] = new ParameterConfiguration(parameters[i], Double.NaN);
138 }
139 paramDim = parameters.length;
140 }
141 this.dirtyParameter = false;
142
143 this.jacobianProviders = new ArrayList<>();
144
145
146
147 matricesData = new double[(stateDim + paramDim) * stateDim];
148 for (int i = 0; i < stateDim; ++i) {
149 matricesData[i * (stateDim + 1)] = 1.0;
150 }
151 }
152
153
154
155
156
157
158
159
160
161 public void registerVariationalEquations(final ExpandableStatefulODE expandable)
162 throws DimensionMismatchException, MismatchedEquations {
163
164
165 final FirstOrderDifferentialEquations ode = (jode instanceof MainStateJacobianWrapper) ?
166 ((MainStateJacobianWrapper) jode).ode :
167 jode;
168 if (expandable.getPrimary() != ode) {
169 throw new MismatchedEquations();
170 }
171
172 efode = expandable;
173 index = efode.addSecondaryEquations(new JacobiansSecondaryEquations());
174 efode.setSecondaryState(index, matricesData);
175 }
176
177
178
179
180 public void addParameterJacobianProvider(final ParameterJacobianProvider provider) {
181 jacobianProviders.add(provider);
182 }
183
184
185
186
187 public void setParameterizedODE(final ParameterizedODE parameterizedOde) {
188 this.pode = parameterizedOde;
189 dirtyParameter = true;
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209 public void setParameterStep(final String parameter, final double hP)
210 throws UnknownParameterException {
211
212 for (ParameterConfiguration param: selectedParameters) {
213 if (parameter.equals(param.getParameterName())) {
214 param.setHP(hP);
215 dirtyParameter = true;
216 return;
217 }
218 }
219
220 throw new UnknownParameterException(parameter);
221 }
222
223
224
225
226
227
228
229
230
231 public void setInitialMainStateJacobian(final double[][] dYdY0)
232 throws DimensionMismatchException {
233
234
235 checkDimension(stateDim, dYdY0);
236 checkDimension(stateDim, dYdY0[0]);
237
238
239 int i = 0;
240 for (final double[] row : dYdY0) {
241 System.arraycopy(row, 0, matricesData, i, stateDim);
242 i += stateDim;
243 }
244
245 if (efode != null) {
246 efode.setSecondaryState(index, matricesData);
247 }
248 }
249
250
251
252
253
254
255
256
257
258
259
260 public void setInitialParameterJacobian(final String pName, final double[] dYdP)
261 throws UnknownParameterException, DimensionMismatchException {
262
263
264 checkDimension(stateDim, dYdP);
265
266
267 int i = stateDim * stateDim;
268 for (ParameterConfiguration param: selectedParameters) {
269 if (pName.equals(param.getParameterName())) {
270 System.arraycopy(dYdP, 0, matricesData, i, stateDim);
271 if (efode != null) {
272 efode.setSecondaryState(index, matricesData);
273 }
274 return;
275 }
276 i += stateDim;
277 }
278
279 throw new UnknownParameterException(pName);
280 }
281
282
283
284
285 public void getCurrentMainSetJacobian(final double[][] dYdY0) {
286
287
288 double[] p = efode.getSecondaryState(index);
289
290 int j = 0;
291 for (int i = 0; i < stateDim; i++) {
292 System.arraycopy(p, j, dYdY0[i], 0, stateDim);
293 j += stateDim;
294 }
295 }
296
297
298
299
300
301 public void getCurrentParameterJacobian(String pName, final double[] dYdP) {
302
303
304 double[] p = efode.getSecondaryState(index);
305
306 int i = stateDim * stateDim;
307 for (ParameterConfiguration param: selectedParameters) {
308 if (param.getParameterName().equals(pName)) {
309 System.arraycopy(p, i, dYdP, 0, stateDim);
310 return;
311 }
312 i += stateDim;
313 }
314 }
315
316
317
318
319
320
321 private void checkDimension(final int expected, final Object array)
322 throws DimensionMismatchException {
323 int arrayDimension = (array == null) ? 0 : Array.getLength(array);
324 if (arrayDimension != expected) {
325 throw new DimensionMismatchException(arrayDimension, expected);
326 }
327 }
328
329
330
331
332
333
334
335 private final class JacobiansSecondaryEquations implements SecondaryEquations {
336
337
338 @Override
339 public int getDimension() {
340 return stateDim * (stateDim + paramDim);
341 }
342
343
344 @Override
345 public void computeDerivatives(final double t, final double[] y, final double[] yDot,
346 final double[] z, final double[] zDot)
347 throws MaxCountExceededException, DimensionMismatchException {
348
349
350 if (dirtyParameter && paramDim != 0) {
351 jacobianProviders.add(new ParameterJacobianWrapper(jode, pode, selectedParameters));
352 dirtyParameter = false;
353 }
354
355
356
357
358
359 double[][] dFdY = new double[stateDim][stateDim];
360 jode.computeMainStateJacobian(t, y, yDot, dFdY);
361
362
363 for (int i = 0; i < stateDim; ++i) {
364 final double[] dFdYi = dFdY[i];
365 for (int j = 0; j < stateDim; ++j) {
366 double s = 0;
367 final int startIndex = j;
368 int zIndex = startIndex;
369 for (int l = 0; l < stateDim; ++l) {
370 s += dFdYi[l] * z[zIndex];
371 zIndex += stateDim;
372 }
373 zDot[startIndex + i * stateDim] = s;
374 }
375 }
376
377 if (paramDim != 0) {
378
379 double[] dFdP = new double[stateDim];
380 int startIndex = stateDim * stateDim;
381 for (ParameterConfiguration param: selectedParameters) {
382 boolean found = false;
383 for (int k = 0 ; !found && k < jacobianProviders.size(); ++k) {
384 final ParameterJacobianProvider provider = jacobianProviders.get(k);
385 if (provider.isSupported(param.getParameterName())) {
386 provider.computeParameterJacobian(t, y, yDot,
387 param.getParameterName(), dFdP);
388 for (int i = 0; i < stateDim; ++i) {
389 final double[] dFdYi = dFdY[i];
390 int zIndex = startIndex;
391 double s = dFdP[i];
392 for (int l = 0; l < stateDim; ++l) {
393 s += dFdYi[l] * z[zIndex];
394 zIndex++;
395 }
396 zDot[startIndex + i] = s;
397 }
398 found = true;
399 }
400 }
401 if (! found) {
402 Arrays.fill(zDot, startIndex, startIndex + stateDim, 0.0);
403 }
404 startIndex += stateDim;
405 }
406 }
407 }
408 }
409
410
411
412
413 private static final class MainStateJacobianWrapper implements MainStateJacobianProvider {
414
415
416 private final FirstOrderDifferentialEquations ode;
417
418
419 private final double[] hY;
420
421
422
423
424
425
426
427 MainStateJacobianWrapper(final FirstOrderDifferentialEquations ode,
428 final double[] hY)
429 throws DimensionMismatchException {
430 this.ode = ode;
431 this.hY = hY.clone();
432 if (hY.length != ode.getDimension()) {
433 throw new DimensionMismatchException(ode.getDimension(), hY.length);
434 }
435 }
436
437
438 @Override
439 public int getDimension() {
440 return ode.getDimension();
441 }
442
443
444 @Override
445 public void computeDerivatives(double t, double[] y, double[] yDot)
446 throws MaxCountExceededException, DimensionMismatchException {
447 ode.computeDerivatives(t, y, yDot);
448 }
449
450
451 @Override
452 public void computeMainStateJacobian(double t, double[] y, double[] yDot, double[][] dFdY)
453 throws MaxCountExceededException, DimensionMismatchException {
454
455 final int n = ode.getDimension();
456 final double[] tmpDot = new double[n];
457
458 for (int j = 0; j < n; ++j) {
459 final double savedYj = y[j];
460 y[j] += hY[j];
461 ode.computeDerivatives(t, y, tmpDot);
462 for (int i = 0; i < n; ++i) {
463 dFdY[i][j] = (tmpDot[i] - yDot[i]) / hY[j];
464 }
465 y[j] = savedYj;
466 }
467 }
468 }
469
470
471
472
473
474 public static class MismatchedEquations extends MathIllegalArgumentException {
475
476
477 private static final long serialVersionUID = 20120902L;
478
479
480 public MismatchedEquations() {
481 super(LocalizedFormats.UNMATCHED_ODE_IN_EXPANDED_SET);
482 }
483 }
484 }
485