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 java.io.Serializable;
20
21 import org.hipparchus.Field;
22 import org.hipparchus.exception.LocalizedCoreFormats;
23 import org.hipparchus.exception.MathIllegalArgumentException;
24 import org.hipparchus.util.FastMath;
25
26
27
28
29
30
31
32 public class DSFactory implements Serializable {
33
34
35 private static final long serialVersionUID = 20161222L;
36
37
38 private final transient DSCompiler compiler;
39
40
41 private final transient DSField derivativeField;
42
43
44
45
46
47 public DSFactory(final int parameters, final int order) {
48 this.compiler = DSCompiler.getCompiler(parameters, order);
49 this.derivativeField = new DSField(constant(0.0), constant(1.0), constant(FastMath.PI));
50 }
51
52
53
54
55 public DSField getDerivativeField() {
56 return derivativeField;
57 }
58
59
60
61
62
63 public DerivativeStructure constant(double value) {
64 final DerivativeStructure ds = new DerivativeStructure(this);
65 ds.setDerivativeComponent(0, value);
66 return ds;
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public DerivativeStructure variable(final int index, final double value)
82 throws MathIllegalArgumentException {
83
84 if (index >= getCompiler().getFreeParameters()) {
85 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE_BOUND_EXCLUDED,
86 index, getCompiler().getFreeParameters());
87 }
88
89 final DerivativeStructure ds = new DerivativeStructure(this);
90 ds.setDerivativeComponent(0, value);
91
92 if (getCompiler().getOrder() > 0) {
93
94 ds.setDerivativeComponent(DSCompiler.getCompiler(index, getCompiler().getOrder()).getSize(), 1.0);
95 }
96
97 return ds;
98
99 }
100
101
102
103
104
105
106
107
108
109
110 @SafeVarargs
111 public final DerivativeStructure build(final double ... derivatives)
112 throws MathIllegalArgumentException {
113
114 if (derivatives.length != compiler.getSize()) {
115 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
116 derivatives.length, compiler.getSize());
117 }
118
119 return new DerivativeStructure(this, derivatives);
120
121 }
122
123
124
125
126
127 DerivativeStructure build() {
128 return new DerivativeStructure(this);
129 }
130
131
132
133
134 public DSCompiler getCompiler() {
135 return compiler;
136 }
137
138
139
140
141
142 void checkCompatibility(final DSFactory factory) throws MathIllegalArgumentException {
143 compiler.checkCompatibility(factory.compiler);
144 }
145
146
147
148
149
150 private Object writeReplace() {
151 return new DataTransferObject(compiler.getFreeParameters(), compiler.getOrder());
152 }
153
154
155 private static class DataTransferObject implements Serializable {
156
157
158 private static final long serialVersionUID = 20161222L;
159
160
161
162
163 private final int variables;
164
165
166
167
168 private final int order;
169
170
171
172
173
174 DataTransferObject(final int variables, final int order) {
175 this.variables = variables;
176 this.order = order;
177 }
178
179
180
181
182 private Object readResolve() {
183 return new DSFactory(variables, order);
184 }
185
186 }
187
188
189
190 public static class DSField implements Field<DerivativeStructure> {
191
192
193 private final DerivativeStructure zero;
194
195
196 private final DerivativeStructure one;
197
198
199 private final DerivativeStructure pi;
200
201
202
203
204
205
206 DSField(final DerivativeStructure zero, final DerivativeStructure one, final DerivativeStructure pi) {
207 this.zero = zero;
208 this.one = one;
209 this.pi = pi;
210 }
211
212
213 @Override
214 public DerivativeStructure getZero() {
215 return zero;
216 }
217
218
219 @Override
220 public DerivativeStructure getOne() {
221 return one;
222 }
223
224
225
226
227
228
229
230
231 public DerivativeStructure getPi() {
232 return pi;
233 }
234
235
236 @Override
237 public Class<DerivativeStructure> getRuntimeClass() {
238 return DerivativeStructure.class;
239 }
240
241
242 @Override
243 public boolean equals(final Object other) {
244 if (this == other) {
245 return true;
246 } else if (other instanceof DSField) {
247 DSFactory lhsFactory = zero.getFactory();
248 DSFactory rhsFactory = ((DSField) other).zero.getFactory();
249 return lhsFactory.compiler == rhsFactory.compiler;
250 } else {
251 return false;
252 }
253 }
254
255
256 @Override
257 public int hashCode() {
258 final DSCompiler compiler = zero.getFactory().getCompiler();
259 return 0x9943b886 ^ (compiler.getFreeParameters() << 16 & compiler.getOrder());
260 }
261
262 }
263
264 }