FieldLegendrePolynomials.java

  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. import org.hipparchus.Field;
  19. import org.hipparchus.CalculusFieldElement;
  20. import org.hipparchus.util.CombinatoricsUtils;
  21. import org.hipparchus.util.FastMath;
  22. import org.hipparchus.util.MathArrays;

  23. /**
  24.  * Computes the P<sub>nm</sub>(t) coefficients.
  25.  * <p>
  26.  * The computation of the Legendre polynomials is performed following:
  27.  * Heiskanen and Moritz, Physical Geodesy, 1967, eq. 1-62
  28.  * </p>
  29.  * @since 11.0
  30.  * @author Bryan Cazabonne
  31.  * @param <T> type of the field elements
  32.  */
  33. public class FieldLegendrePolynomials<T extends CalculusFieldElement<T>> {

  34.     /** Array for the Legendre polynomials. */
  35.     private T[][] pCoef;

  36.     /** Create Legendre polynomials for the given degree and order.
  37.      * @param degree degree of the spherical harmonics
  38.      * @param order order of the spherical harmonics
  39.      * @param  t argument for polynomials calculation
  40.      */
  41.     public FieldLegendrePolynomials(final int degree, final int order,
  42.                                     final T t) {

  43.         // Field
  44.         final Field<T> field = t.getField();

  45.         // Initialize array
  46.         this.pCoef = MathArrays.buildArray(field, degree + 1, order + 1);

  47.         final T t2 = t.square();

  48.         for (int n = 0; n <= degree; n++) {

  49.             // m shall be <= n (Heiskanen and Moritz, 1967, pp 21)
  50.             for (int m = 0; m <= FastMath.min(n, order); m++) {

  51.                 // r = int((n - m) / 2)
  52.                 final int r = (int) (n - m) / 2;
  53.                 T sum = field.getZero();
  54.                 for (int k = 0; k <= r; k++) {
  55.                     final T term = FastMath.pow(t, n - m - 2 * k).
  56.                                    multiply(FastMath.pow(-1.0, k) * CombinatoricsUtils.factorialDouble(2 * n - 2 * k) /
  57.                                                                            (CombinatoricsUtils.factorialDouble(k) * CombinatoricsUtils.factorialDouble(n - k) *
  58.                                                                                            CombinatoricsUtils.factorialDouble(n - m - 2 * k)));
  59.                     sum = sum.add(term);
  60.                 }

  61.                 pCoef[n][m] = FastMath.pow(t2.negate().add(1.0), 0.5 * m).multiply(FastMath.pow(2, -n)).multiply(sum);

  62.             }

  63.         }

  64.     }

  65.     /** Return the coefficient P<sub>nm</sub>.
  66.      * @param n index
  67.      * @param m index
  68.      * @return The coefficient P<sub>nm</sub>
  69.      */
  70.     public T getPnm(final int n, final int m) {
  71.         return pCoef[n][m];
  72.     }

  73. }