1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.ode;
24
25 import java.io.Serializable;
26
27 import org.hipparchus.CalculusFieldElement;
28 import org.hipparchus.exception.LocalizedCoreFormats;
29 import org.hipparchus.exception.MathIllegalArgumentException;
30 import org.hipparchus.util.MathArrays;
31 import org.hipparchus.util.MathUtils;
32
33
34
35
36
37
38
39
40
41
42 public class FieldEquationsMapper<T extends CalculusFieldElement<T>> implements Serializable {
43
44
45 private static final long serialVersionUID = 20151114L;
46
47
48 private final int[] start;
49
50
51
52
53
54
55
56
57
58 FieldEquationsMapper(final FieldEquationsMapper<T> mapper, final int dimension) {
59 final int index = (mapper == null) ? 0 : mapper.getNumberOfEquations();
60 this.start = new int[index + 2];
61 if (mapper == null) {
62 start[0] = 0;
63 } else {
64 System.arraycopy(mapper.start, 0, start, 0, index + 1);
65 }
66 start[index + 1] = start[index] + dimension;
67 }
68
69
70
71
72 public int getNumberOfEquations() {
73 return start.length - 1;
74 }
75
76
77
78
79
80
81
82 public int getTotalDimension() {
83 return start[start.length - 1];
84 }
85
86
87
88
89
90
91
92
93 public FieldODEStateAndDerivative<T> mapStateAndDerivative(final T t, final T[] y, final T[] yDot)
94 throws MathIllegalArgumentException {
95
96 if (y.length != getTotalDimension()) {
97 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
98 y.length, getTotalDimension());
99 }
100
101 if (yDot.length != getTotalDimension()) {
102 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
103 yDot.length, getTotalDimension());
104 }
105
106 final int n = getNumberOfEquations();
107 final T[] state = extractEquationData(0, y);
108 final T[] derivative = extractEquationData(0, yDot);
109 if (n < 2) {
110 return new FieldODEStateAndDerivative<T>(t, state, derivative);
111 } else {
112 final T[][] secondaryState = MathArrays.buildArray(t.getField(), n - 1, -1);
113 final T[][] secondaryDerivative = MathArrays.buildArray(t.getField(), n - 1, -1);
114 for (int index = 1; index < getNumberOfEquations(); ++index) {
115 secondaryState[index - 1] = extractEquationData(index, y);
116 secondaryDerivative[index - 1] = extractEquationData(index, yDot);
117 }
118 return new FieldODEStateAndDerivative<T>(t, state, derivative, secondaryState, secondaryDerivative);
119 }
120 }
121
122
123
124
125
126
127
128
129
130
131 public T[] extractEquationData(final int index, final T[] complete)
132 throws MathIllegalArgumentException {
133 checkIndex(index);
134 final int begin = start[index];
135 final int end = start[index + 1];
136 if (complete.length < end) {
137 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
138 complete.length, end);
139 }
140 final int dimension = end - begin;
141 final T[] equationData = MathArrays.buildArray(complete[0].getField(), dimension);
142 System.arraycopy(complete, begin, equationData, 0, dimension);
143 return equationData;
144 }
145
146
147
148
149
150
151
152
153
154 public void insertEquationData(final int index, T[] equationData, T[] complete)
155 throws MathIllegalArgumentException {
156 checkIndex(index);
157 final int begin = start[index];
158 final int end = start[index + 1];
159 final int dimension = end - begin;
160 if (complete.length < end) {
161 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
162 complete.length, end);
163 }
164 if (equationData.length != dimension) {
165 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
166 equationData.length, dimension);
167 }
168 System.arraycopy(equationData, 0, complete, begin, dimension);
169 }
170
171
172
173
174
175
176 private void checkIndex(final int index) throws MathIllegalArgumentException {
177 MathUtils.checkRangeInclusive(index, 0, start.length - 2);
178 }
179
180 }