LegendrePolynomials.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.util.CombinatoricsUtils;
  19. import org.hipparchus.util.FastMath;

  20. /**
  21.  * Computes the P<sub>nm</sub>(t) coefficients.
  22.  * <p>
  23.  * The computation of the Legendre polynomials is performed following:
  24.  * Heiskanen and Moritz, Physical Geodesy, 1967, eq. 1-62
  25.  * </p>
  26.  * @since 11.0
  27.  * @author Bryan Cazabonne
  28.  */
  29. public class LegendrePolynomials {

  30.     /** Array for the Legendre polynomials. */
  31.     private double[][] pCoef;

  32.     /** Create Legendre polynomials for the given degree and order.
  33.      * @param degree degree of the spherical harmonics
  34.      * @param order order of the spherical harmonics
  35.      * @param t argument for polynomials calculation
  36.      */
  37.     public LegendrePolynomials(final int degree, final int order,
  38.                                final double t) {

  39.         // Initialize array
  40.         this.pCoef = new double[degree + 1][order + 1];

  41.         final double t2 = t * t;

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

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

  45.                 // r = int((n - m) / 2)
  46.                 final int r = (int) (n - m) / 2;
  47.                 double sum = 0.;
  48.                 for (int k = 0; k <= r; k++) {
  49.                     final double term = FastMath.pow(-1.0, k) * CombinatoricsUtils.factorialDouble(2 * n - 2 * k) /
  50.                                     (CombinatoricsUtils.factorialDouble(k) * CombinatoricsUtils.factorialDouble(n - k) *
  51.                                      CombinatoricsUtils.factorialDouble(n - m - 2 * k)) *
  52.                                      FastMath.pow(t, n - m - 2 * k);
  53.                     sum = sum + term;
  54.                 }

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

  56.             }

  57.         }

  58.     }

  59.     /** Return the coefficient P<sub>nm</sub>.
  60.      * @param n index
  61.      * @param m index
  62.      * @return The coefficient P<sub>nm</sub>
  63.      */
  64.     public double getPnm(final int n, final int m) {
  65.         return pCoef[n][m];
  66.     }

  67. }