1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.geometry.euclidean.threed;
23
24
25 import java.io.Serializable;
26
27 import org.hipparchus.util.FastMath;
28 import org.hipparchus.util.SinCos;
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
56
57 public class SphericalCoordinates implements Serializable {
58
59
60 private static final long serialVersionUID = 20130206L;
61
62
63 private final Vector3D v;
64
65
66 private final double r;
67
68
69 private final double theta;
70
71
72 private final double phi;
73
74
75 private double[][] jacobian;
76
77
78 private double[][] rHessian;
79
80
81 private double[][] thetaHessian;
82
83
84 private double[][] phiHessian;
85
86
87
88
89 public SphericalCoordinates(final Vector3D v) {
90
91
92 this.v = v;
93
94
95 this.r = v.getNorm();
96 this.theta = v.getAlpha();
97 this.phi = FastMath.acos(v.getZ() / r);
98
99 }
100
101
102
103
104
105
106 public SphericalCoordinates(final double r, final double theta, final double phi) {
107
108 final SinCos sinCosTheta = FastMath.sinCos(theta);
109 final SinCos sinCosPhi = FastMath.sinCos(phi);
110
111
112 this.r = r;
113 this.theta = theta;
114 this.phi = phi;
115
116
117 this.v = new Vector3D(r * sinCosTheta.cos() * sinCosPhi.sin(),
118 r * sinCosTheta.sin() * sinCosPhi.sin(),
119 r * sinCosPhi.cos());
120
121 }
122
123
124
125
126 public Vector3D getCartesian() {
127 return v;
128 }
129
130
131
132
133
134
135 public double getR() {
136 return r;
137 }
138
139
140
141
142
143
144 public double getTheta() {
145 return theta;
146 }
147
148
149
150
151
152
153 public double getPhi() {
154 return phi;
155 }
156
157
158
159
160
161
162
163
164 public double[] toCartesianGradient(final double[] sGradient) {
165
166
167 computeJacobian();
168
169
170
171 return new double[] {
172 sGradient[0] * jacobian[0][0] + sGradient[1] * jacobian[1][0] + sGradient[2] * jacobian[2][0],
173 sGradient[0] * jacobian[0][1] + sGradient[1] * jacobian[1][1] + sGradient[2] * jacobian[2][1],
174 sGradient[0] * jacobian[0][2] + sGradient[2] * jacobian[2][2]
175 };
176
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 public double[][] toCartesianHessian(final double[][] sHessian, final double[] sGradient) {
198
199 computeJacobian();
200 computeHessians();
201
202
203
204
205 final double[][] hj = new double[3][3];
206 final double[][] cHessian = new double[3][3];
207
208
209
210 hj[0][0] = sHessian[0][0] * jacobian[0][0] + sHessian[1][0] * jacobian[1][0] + sHessian[2][0] * jacobian[2][0];
211 hj[0][1] = sHessian[0][0] * jacobian[0][1] + sHessian[1][0] * jacobian[1][1] + sHessian[2][0] * jacobian[2][1];
212 hj[0][2] = sHessian[0][0] * jacobian[0][2] + sHessian[2][0] * jacobian[2][2];
213 hj[1][0] = sHessian[1][0] * jacobian[0][0] + sHessian[1][1] * jacobian[1][0] + sHessian[2][1] * jacobian[2][0];
214 hj[1][1] = sHessian[1][0] * jacobian[0][1] + sHessian[1][1] * jacobian[1][1] + sHessian[2][1] * jacobian[2][1];
215
216 hj[2][0] = sHessian[2][0] * jacobian[0][0] + sHessian[2][1] * jacobian[1][0] + sHessian[2][2] * jacobian[2][0];
217 hj[2][1] = sHessian[2][0] * jacobian[0][1] + sHessian[2][1] * jacobian[1][1] + sHessian[2][2] * jacobian[2][1];
218 hj[2][2] = sHessian[2][0] * jacobian[0][2] + sHessian[2][2] * jacobian[2][2];
219
220
221 cHessian[0][0] = jacobian[0][0] * hj[0][0] + jacobian[1][0] * hj[1][0] + jacobian[2][0] * hj[2][0];
222 cHessian[1][0] = jacobian[0][1] * hj[0][0] + jacobian[1][1] * hj[1][0] + jacobian[2][1] * hj[2][0];
223 cHessian[2][0] = jacobian[0][2] * hj[0][0] + jacobian[2][2] * hj[2][0];
224 cHessian[1][1] = jacobian[0][1] * hj[0][1] + jacobian[1][1] * hj[1][1] + jacobian[2][1] * hj[2][1];
225 cHessian[2][1] = jacobian[0][2] * hj[0][1] + jacobian[2][2] * hj[2][1];
226 cHessian[2][2] = jacobian[0][2] * hj[0][2] + jacobian[2][2] * hj[2][2];
227
228
229 cHessian[0][0] += sGradient[0] * rHessian[0][0] + sGradient[1] * thetaHessian[0][0] + sGradient[2] * phiHessian[0][0];
230 cHessian[1][0] += sGradient[0] * rHessian[1][0] + sGradient[1] * thetaHessian[1][0] + sGradient[2] * phiHessian[1][0];
231 cHessian[2][0] += sGradient[0] * rHessian[2][0] + sGradient[2] * phiHessian[2][0];
232 cHessian[1][1] += sGradient[0] * rHessian[1][1] + sGradient[1] * thetaHessian[1][1] + sGradient[2] * phiHessian[1][1];
233 cHessian[2][1] += sGradient[0] * rHessian[2][1] + sGradient[2] * phiHessian[2][1];
234 cHessian[2][2] += sGradient[0] * rHessian[2][2] + sGradient[2] * phiHessian[2][2];
235
236
237 cHessian[0][1] = cHessian[1][0];
238 cHessian[0][2] = cHessian[2][0];
239 cHessian[1][2] = cHessian[2][1];
240
241 return cHessian;
242
243 }
244
245
246
247 private void computeJacobian() {
248 if (jacobian == null) {
249
250
251 final double x = v.getX();
252 final double y = v.getY();
253 final double z = v.getZ();
254 final double rho2 = x * x + y * y;
255 final double rho = FastMath.sqrt(rho2);
256 final double r2 = rho2 + z * z;
257
258 jacobian = new double[3][3];
259
260
261 jacobian[0][0] = x / r;
262 jacobian[0][1] = y / r;
263 jacobian[0][2] = z / r;
264
265
266 jacobian[1][0] = -y / rho2;
267 jacobian[1][1] = x / rho2;
268
269
270
271 jacobian[2][0] = x * z / (rho * r2);
272 jacobian[2][1] = y * z / (rho * r2);
273 jacobian[2][2] = -rho / r2;
274
275 }
276 }
277
278
279
280 private void computeHessians() {
281
282 if (rHessian == null) {
283
284
285 final double x = v.getX();
286 final double y = v.getY();
287 final double z = v.getZ();
288 final double x2 = x * x;
289 final double y2 = y * y;
290 final double z2 = z * z;
291 final double rho2 = x2 + y2;
292 final double rho = FastMath.sqrt(rho2);
293 final double r2 = rho2 + z2;
294 final double xOr = x / r;
295 final double yOr = y / r;
296 final double zOr = z / r;
297 final double xOrho2 = x / rho2;
298 final double yOrho2 = y / rho2;
299 final double xOr3 = xOr / r2;
300 final double yOr3 = yOr / r2;
301 final double zOr3 = zOr / r2;
302
303
304 rHessian = new double[3][3];
305 rHessian[0][0] = y * yOr3 + z * zOr3;
306 rHessian[1][0] = -x * yOr3;
307 rHessian[2][0] = -z * xOr3;
308 rHessian[1][1] = x * xOr3 + z * zOr3;
309 rHessian[2][1] = -y * zOr3;
310 rHessian[2][2] = x * xOr3 + y * yOr3;
311
312
313 rHessian[0][1] = rHessian[1][0];
314 rHessian[0][2] = rHessian[2][0];
315 rHessian[1][2] = rHessian[2][1];
316
317
318 thetaHessian = new double[2][2];
319 thetaHessian[0][0] = 2 * xOrho2 * yOrho2;
320 thetaHessian[1][0] = yOrho2 * yOrho2 - xOrho2 * xOrho2;
321 thetaHessian[1][1] = -2 * xOrho2 * yOrho2;
322
323
324 thetaHessian[0][1] = thetaHessian[1][0];
325
326
327 final double rhor2 = rho * r2;
328 final double rho2r2 = rho * rhor2;
329 final double rhor4 = rhor2 * r2;
330 final double rho3r4 = rhor4 * rho2;
331 final double r2P2rho2 = 3 * rho2 + z2;
332 phiHessian = new double[3][3];
333 phiHessian[0][0] = z * (rho2r2 - x2 * r2P2rho2) / rho3r4;
334 phiHessian[1][0] = -x * y * z * r2P2rho2 / rho3r4;
335 phiHessian[2][0] = x * (rho2 - z2) / rhor4;
336 phiHessian[1][1] = z * (rho2r2 - y2 * r2P2rho2) / rho3r4;
337 phiHessian[2][1] = y * (rho2 - z2) / rhor4;
338 phiHessian[2][2] = 2 * rho * zOr3 / r;
339
340
341 phiHessian[0][1] = phiHessian[1][0];
342 phiHessian[0][2] = phiHessian[2][0];
343 phiHessian[1][2] = phiHessian[2][1];
344
345 }
346
347 }
348
349
350
351
352
353 private Object writeReplace() {
354 return new DataTransferObject(v.getX(), v.getY(), v.getZ());
355 }
356
357
358 private static class DataTransferObject implements Serializable {
359
360
361 private static final long serialVersionUID = 20130206L;
362
363
364
365
366 private final double x;
367
368
369
370
371 private final double y;
372
373
374
375
376 private final double z;
377
378
379
380
381
382
383 DataTransferObject(final double x, final double y, final double z) {
384 this.x = x;
385 this.y = y;
386 this.z = z;
387 }
388
389
390
391
392 private Object readResolve() {
393 return new SphericalCoordinates(new Vector3D(x, y, z));
394 }
395
396 }
397
398 }