1 /* Copyright 2002-2021 CS GROUP
2 * Licensed to CS GROUP (CS) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * CS licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.orekit.utils;
18
19 import org.hipparchus.Field;
20 import org.hipparchus.CalculusFieldElement;
21 import org.hipparchus.util.CombinatoricsUtils;
22 import org.hipparchus.util.FastMath;
23 import org.hipparchus.util.MathArrays;
24
25 /**
26 * Computes the P<sub>nm</sub>(t) coefficients.
27 * <p>
28 * The computation of the Legendre polynomials is performed following:
29 * Heiskanen and Moritz, Physical Geodesy, 1967, eq. 1-62
30 * </p>
31 * @since 11.0
32 * @author Bryan Cazabonne
33 */
34 public class FieldLegendrePolynomials<T extends CalculusFieldElement<T>> {
35
36 /** Array for the Legendre polynomials. */
37 private T[][] pCoef;
38
39 /** Create Legendre polynomials for the given degree and order.
40 * @param degree degree of the spherical harmonics
41 * @param order order of the spherical harmonics
42 * @param t argument for polynomials calculation
43 */
44 public FieldLegendrePolynomials(final int degree, final int order,
45 final T t) {
46
47 // Field
48 final Field<T> field = t.getField();
49
50 // Initialize array
51 this.pCoef = MathArrays.buildArray(field, degree + 1, order + 1);
52
53 final T t2 = t.multiply(t);
54
55 for (int n = 0; n <= degree; n++) {
56
57 // m shall be <= n (Heiskanen and Moritz, 1967, pp 21)
58 for (int m = 0; m <= FastMath.min(n, order); m++) {
59
60 // r = int((n - m) / 2)
61 final int r = (int) (n - m) / 2;
62 T sum = field.getZero();
63 for (int k = 0; k <= r; k++) {
64 final T term = FastMath.pow(t, n - m - 2 * k).
65 multiply(FastMath.pow(-1.0, k) * CombinatoricsUtils.factorialDouble(2 * n - 2 * k) /
66 (CombinatoricsUtils.factorialDouble(k) * CombinatoricsUtils.factorialDouble(n - k) *
67 CombinatoricsUtils.factorialDouble(n - m - 2 * k)));
68 sum = sum.add(term);
69 }
70
71 pCoef[n][m] = FastMath.pow(t2.negate().add(1.0), 0.5 * m).multiply(FastMath.pow(2, -n)).multiply(sum);
72
73 }
74
75 }
76
77 }
78
79 /** Return the coefficient P<sub>nm</sub>.
80 * @param n index
81 * @param m index
82 * @return The coefficient P<sub>nm</sub>
83 */
84 public T getPnm(final int n, final int m) {
85 return pCoef[n][m];
86 }
87
88 }