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