RdFieldDuplication.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.CalculusFieldElement;
  19. import org.hipparchus.complex.Complex;
  20. import org.hipparchus.complex.FieldComplex;
  21. import org.hipparchus.util.FastMath;

  22. /** Duplication algorithm for Carlson R<sub>D</sub> elliptic integral.
  23.  * @param <T> type of the field elements (really {@link Complex} or {@link FieldComplex})
  24.  * @since 2.0
  25.  */
  26. class RdFieldDuplication<T extends CalculusFieldElement<T>> extends FieldDuplication<T> {

  27.     /** Partial sum. */
  28.     private T sum;

  29.     /** Simple constructor.
  30.      * @param x first symmetric variable of the integral
  31.      * @param y second symmetric variable of the integral
  32.      * @param z third symmetric variable of the integral
  33.      */
  34.     RdFieldDuplication(final T x, final T y, final T z) {
  35.         super(x, y, z);
  36.         sum = x.getField().getZero();
  37.     }

  38.     /** {@inheritDoc} */
  39.     @Override
  40.     protected void initialMeanPoint(final T[] va) {
  41.         va[3] = va[0].add(va[1]).add(va[2].multiply(3)).divide(5.0);
  42.     }

  43.     /** {@inheritDoc} */
  44.     @Override
  45.     protected T convergenceCriterion(final T r, final T max) {
  46.         return max.divide(FastMath.sqrt(FastMath.sqrt(FastMath.sqrt(r.multiply(0.25)))));
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     protected void update(final int m, final T[] vaM, final T[] sqrtM, final  double fourM) {

  51.         // equation 2.29 in Carlson[1995]
  52.         final T lambdaA = sqrtM[0].multiply(sqrtM[1]);
  53.         final T lambdaB = sqrtM[0].multiply(sqrtM[2]);
  54.         final T lambdaC = sqrtM[1].multiply(sqrtM[2]);

  55.         // running sum in equation 2.34 in Carlson[1995]
  56.         final T lambda = lambdaA.add(lambdaB).add(lambdaC);
  57.         sum = sum.add(vaM[2].add(lambda).multiply(sqrtM[2]).multiply(fourM).reciprocal());

  58.         // equations 2.29 and 2.30 in Carlson[1995]
  59.         vaM[0] = vaM[0].linearCombination(0.25, vaM[0], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // xₘ
  60.         vaM[1] = vaM[1].linearCombination(0.25, vaM[1], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // yₘ
  61.         vaM[2] = vaM[2].linearCombination(0.25, vaM[2], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // zₘ
  62.         vaM[3] = vaM[3].linearCombination(0.25, vaM[3], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // aₘ

  63.     }

  64.     /** {@inheritDoc} */
  65.     @Override
  66.     protected T evaluate(final T[] va0, final T aM, final  double fourM) {

  67.         // compute symmetric differences
  68.         final T inv   = aM.multiply(fourM).reciprocal();
  69.         final T bigX  = va0[3].subtract(va0[0]).multiply(inv);
  70.         final T bigY  = va0[3].subtract(va0[1]).multiply(inv);
  71.         final T bigZ  = bigX.add(bigY).divide(-3);
  72.         final T bigXY = bigX.multiply(bigY);
  73.         final T bigZ2 = bigZ.multiply(bigZ);

  74.         // compute elementary symmetric functions (we already know e1 = 0 by construction)
  75.         final T e2  = bigXY.subtract(bigZ2.multiply(6));
  76.         final T e3  = bigXY.multiply(3).subtract(bigZ2.multiply(8)).multiply(bigZ);
  77.         final T e4  = bigXY.subtract(bigZ2).multiply(3).multiply(bigZ2);
  78.         final T e5  = bigXY.multiply(bigZ2).multiply(bigZ);

  79.         final T e2e2   = e2.multiply(e2);
  80.         final T e2e3   = e2.multiply(e3);
  81.         final T e2e4   = e2.multiply(e4);
  82.         final T e2e5   = e2.multiply(e5);
  83.         final T e3e3   = e3.multiply(e3);
  84.         final T e3e4   = e3.multiply(e4);
  85.         final T e2e2e2 = e2e2.multiply(e2);
  86.         final T e2e2e3 = e2e2.multiply(e3);

  87.         // evaluate integral using equation 19.36.1 in DLMF
  88.         // (which add more terms than equation 2.7 in Carlson[1995])
  89.         final T poly = e3e4.add(e2e5).multiply(RdRealDuplication.E3_E4_P_E2_E5).
  90.                        add(e2e2e3.multiply(RdRealDuplication.E2_E2_E3)).
  91.                        add(e2e4.multiply(RdRealDuplication.E2_E4)).
  92.                        add(e3e3.multiply(RdRealDuplication.E3_E3)).
  93.                        add(e2e2e2.multiply(RdRealDuplication.E2_E2_E2)).
  94.                        add(e5.multiply(RdRealDuplication.E5)).
  95.                        add(e2e3.multiply(RdRealDuplication.E2_E3)).
  96.                        add(e4.multiply(RdRealDuplication.E4)).
  97.                        add(e2e2.multiply(RdRealDuplication.E2_E2)).
  98.                        add(e3.multiply(RdRealDuplication.E3)).
  99.                        add(e2.multiply(RdRealDuplication.E2)).
  100.                        add(RdRealDuplication.CONSTANT).
  101.                        divide(RdRealDuplication.DENOMINATOR);
  102.         final T polyTerm = poly.divide(aM.multiply(FastMath.sqrt(aM)).multiply(fourM));

  103.         return polyTerm.add(sum.multiply(3));

  104.     }

  105. }