1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.ode.nonstiff;
19
20 import org.apache.commons.math4.legacy.core.Field;
21 import org.apache.commons.math4.legacy.core.RealFieldElement;
22 import org.apache.commons.math4.legacy.ode.FieldEquationsMapper;
23 import org.apache.commons.math4.legacy.ode.FieldODEStateAndDerivative;
24 import org.apache.commons.math4.legacy.ode.sampling.AbstractFieldStepInterpolator;
25 import org.apache.commons.math4.legacy.core.MathArrays;
26
27
28
29
30
31
32
33
34
35
36
37 abstract class RungeKuttaFieldStepInterpolator<T extends RealFieldElement<T>>
38 extends AbstractFieldStepInterpolator<T> {
39
40
41 private final Field<T> field;
42
43
44 private final T[][] yDotK;
45
46
47
48
49
50
51
52
53
54
55
56 protected RungeKuttaFieldStepInterpolator(final Field<T> field, final boolean forward,
57 final T[][] yDotK,
58 final FieldODEStateAndDerivative<T> globalPreviousState,
59 final FieldODEStateAndDerivative<T> globalCurrentState,
60 final FieldODEStateAndDerivative<T> softPreviousState,
61 final FieldODEStateAndDerivative<T> softCurrentState,
62 final FieldEquationsMapper<T> mapper) {
63 super(forward, globalPreviousState, globalCurrentState, softPreviousState, softCurrentState, mapper);
64 this.field = field;
65 this.yDotK = MathArrays.buildArray(field, yDotK.length, -1);
66 for (int i = 0; i < yDotK.length; ++i) {
67 this.yDotK[i] = yDotK[i].clone();
68 }
69 }
70
71
72 @Override
73 protected RungeKuttaFieldStepInterpolator<T> create(boolean newForward,
74 FieldODEStateAndDerivative<T> newGlobalPreviousState,
75 FieldODEStateAndDerivative<T> newGlobalCurrentState,
76 FieldODEStateAndDerivative<T> newSoftPreviousState,
77 FieldODEStateAndDerivative<T> newSoftCurrentState,
78 FieldEquationsMapper<T> newMapper) {
79 return create(field, newForward, yDotK,
80 newGlobalPreviousState, newGlobalCurrentState,
81 newSoftPreviousState, newSoftCurrentState,
82 newMapper);
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96 protected abstract RungeKuttaFieldStepInterpolator<T> create(Field<T> newField, boolean newForward, T[][] newYDotK,
97 FieldODEStateAndDerivative<T> newGlobalPreviousState,
98 FieldODEStateAndDerivative<T> newGlobalCurrentState,
99 FieldODEStateAndDerivative<T> newSoftPreviousState,
100 FieldODEStateAndDerivative<T> newSoftCurrentState,
101 FieldEquationsMapper<T> newMapper);
102
103
104
105
106
107 @SafeVarargs
108 protected final T[] previousStateLinearCombination(final T ... coefficients) {
109 return combine(getPreviousState().getState(),
110 coefficients);
111 }
112
113
114
115
116
117 @SuppressWarnings("unchecked")
118 protected T[] currentStateLinearCombination(final T ... coefficients) {
119 return combine(getCurrentState().getState(),
120 coefficients);
121 }
122
123
124
125
126
127 @SuppressWarnings("unchecked")
128 protected T[] derivativeLinearCombination(final T ... coefficients) {
129 return combine(MathArrays.buildArray(field, yDotK[0].length), coefficients);
130 }
131
132
133
134
135
136
137 @SuppressWarnings("unchecked")
138 private T[] combine(final T[] a, final T ... coefficients) {
139 for (int i = 0; i < a.length; ++i) {
140 for (int k = 0; k < coefficients.length; ++k) {
141 a[i] = a[i].add(coefficients[k].multiply(yDotK[k][i]));
142 }
143 }
144 return a;
145 }
146 }