1   /* Copyright 2002-2025 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   * @param <T> type of the field elements
34   */
35  public class FieldLegendrePolynomials<T extends CalculusFieldElement<T>> {
36  
37      /** Array for the Legendre polynomials. */
38      private T[][] pCoef;
39  
40      /** Create Legendre polynomials for the given degree and order.
41       * @param degree degree of the spherical harmonics
42       * @param order order of the spherical harmonics
43       * @param  t argument for polynomials calculation
44       */
45      public FieldLegendrePolynomials(final int degree, final int order,
46                                      final T t) {
47  
48          // Field
49          final Field<T> field = t.getField();
50  
51          // Initialize array
52          this.pCoef = MathArrays.buildArray(field, degree + 1, order + 1);
53  
54          final T t2 = t.square();
55  
56          for (int n = 0; n <= degree; n++) {
57  
58              // m shall be <= n (Heiskanen and Moritz, 1967, pp 21)
59              for (int m = 0; m <= FastMath.min(n, order); m++) {
60  
61                  // r = int((n - m) / 2)
62                  final int r = (int) (n - m) / 2;
63                  T sum = field.getZero();
64                  for (int k = 0; k <= r; k++) {
65                      final T term = FastMath.pow(t, n - m - 2 * k).
66                                     multiply(FastMath.pow(-1.0, k) * CombinatoricsUtils.factorialDouble(2 * n - 2 * k) /
67                                                                             (CombinatoricsUtils.factorialDouble(k) * CombinatoricsUtils.factorialDouble(n - k) *
68                                                                                             CombinatoricsUtils.factorialDouble(n - m - 2 * k)));
69                      sum = sum.add(term);
70                  }
71  
72                  pCoef[n][m] = FastMath.pow(t2.negate().add(1.0), 0.5 * m).multiply(FastMath.pow(2, -n)).multiply(sum);
73  
74              }
75  
76          }
77  
78      }
79  
80      /** Return the coefficient P<sub>nm</sub>.
81       * @param n index
82       * @param m index
83       * @return The coefficient P<sub>nm</sub>
84       */
85      public T getPnm(final int n, final int m) {
86          return pCoef[n][m];
87      }
88  
89  }