RdRealDuplication.java

  1. /*
  2.  * Licensed to the Hipparchus project under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The Hipparchus project 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.  *      https://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.hipparchus.special.elliptic.carlson;

  18. import org.hipparchus.util.FastMath;
  19. import org.hipparchus.util.MathArrays;

  20. /** Duplication algorithm for Carlson R<sub>D</sub> elliptic integral.
  21.  * @since 2.0
  22.  */
  23. class RdRealDuplication extends RealDuplication {

  24.     /** Constant term in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  25.     static final double CONSTANT = 4084080;

  26.     /** Coefficient of E₂ in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  27.     static final double E2 = -875160;

  28.     /** Coefficient of E₃ in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  29.     static final double E3 = 680680;

  30.     /** Coefficient of E₂² in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  31.     static final double E2_E2 = 417690;

  32.     /** Coefficient of E₄ in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  33.     static final double E4 = -556920;

  34.     /** Coefficient of E₂E₃ in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  35.     static final double E2_E3 = -706860;

  36.     /** Coefficient of E₅ in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  37.     static final double E5 = 471240;

  38.     /** Coefficient of E₂³ in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  39.     static final double E2_E2_E2 = -255255;

  40.     /** Coefficient of E₃² in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  41.     static final double E3_E3 = 306306;

  42.     /** Coefficient of E₂E₄ in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  43.     static final double E2_E4 = 612612;

  44.     /** Coefficient of E₂²E₃ in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  45.     static final double E2_E2_E3 = 675675;

  46.     /** Coefficient of E₃E₄+E₂E₅ in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  47.     static final double E3_E4_P_E2_E5 = -540540;

  48.     /** Denominator in R<sub>J</sub> and R<sub>D</sub> polynomials. */
  49.     static final double DENOMINATOR = 4084080;

  50.     /** Partial sum. */
  51.     private double sum;

  52.     /** Simple constructor.
  53.      * @param x first symmetric variable of the integral
  54.      * @param y second symmetric variable of the integral
  55.      * @param z third symmetric variable of the integral
  56.      */
  57.     RdRealDuplication(final double x, final double y, final double z) {
  58.         super(x, y, z);
  59.         sum = 0;
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     protected void initialMeanPoint(final double[] va) {
  64.         va[3] = (va[0] + va[1] + va[2] * 3.0) / 5.0;
  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     protected double convergenceCriterion(final double r, final double max) {
  69.         return max / (FastMath.sqrt(FastMath.sqrt(FastMath.sqrt(r * 0.25))));
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     protected void update(final int m, final double[] vaM, final double[] sqrtM, final  double fourM) {

  74.         // equation 2.29 in Carlson[1995]
  75.         final double lambdaA = sqrtM[0] * sqrtM[1];
  76.         final double lambdaB = sqrtM[0] * sqrtM[2];
  77.         final double lambdaC = sqrtM[1] * sqrtM[2];

  78.         // running sum in equation 2.34 in Carlson[1995]
  79.         final double lambda = lambdaA + lambdaB + lambdaC;
  80.         sum += 1.0 / ((vaM[2] + lambda) * sqrtM[2] * fourM);

  81.         // equations 2.29 and 2.30 in Carlson[1995]
  82.         vaM[0] = MathArrays.linearCombination(0.25, vaM[0], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // xₘ
  83.         vaM[1] = MathArrays.linearCombination(0.25, vaM[1], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // yₘ
  84.         vaM[2] = MathArrays.linearCombination(0.25, vaM[2], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // zₘ
  85.         vaM[3] = MathArrays.linearCombination(0.25, vaM[3], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // aₘ

  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     protected double evaluate(final double[] va0, final double aM, final  double fourM) {

  90.         // compute symmetric differences
  91.         final double inv   = 1.0 / (aM * fourM);
  92.         final double bigX  = (va0[3] - va0[0]) * inv;
  93.         final double bigY  = (va0[3] - va0[1]) * inv;
  94.         final double bigZ  = (bigX + bigY) / -3;
  95.         final double bigXY = bigX * bigY;
  96.         final double bigZ2 = bigZ * bigZ;

  97.         // compute elementary symmetric functions (we already know e1 = 0 by construction)
  98.         final double e2  = bigXY - bigZ2 * 6;
  99.         final double e3  = (bigXY * 3 - bigZ2 * 8) * bigZ;
  100.         final double e4  = (bigXY - bigZ2) * 3 * bigZ2;
  101.         final double e5  = bigXY * bigZ2 * bigZ;

  102.         final double e2e2   =   e2 * e2;
  103.         final double e2e3   =   e2 * e3;
  104.         final double e2e4   =   e2 * e4;
  105.         final double e2e5   =   e2 * e5;
  106.         final double e3e3   =   e3 * e3;
  107.         final double e3e4   =   e3 * e4;
  108.         final double e2e2e2 = e2e2 * e2;
  109.         final double e2e2e3 = e2e2 * e3;

  110.         // evaluate integral using equation 19.36.1 in DLMF
  111.         // (which add more terms than equation 2.7 in Carlson[1995])
  112.         final double poly = ((e3e4 + e2e5) * E3_E4_P_E2_E5 +
  113.                               e2e2e3 * E2_E2_E3 +
  114.                               e2e4 * E2_E4 +
  115.                               e3e3 * E3_E3 +
  116.                               e2e2e2 * E2_E2_E2 +
  117.                               e5 * E5 +
  118.                               e2e3 * E2_E3 +
  119.                               e4 * E4 +
  120.                               e2e2 * E2_E2 +
  121.                               e3 * E3 +
  122.                               e2 * E2 +
  123.                               CONSTANT) /
  124.                              DENOMINATOR;
  125.         final double polyTerm = poly / (aM * FastMath.sqrt(aM) * fourM);

  126.         return polyTerm + sum * 3;

  127.     }

  128. }