RjFieldDuplication.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>J</sub> elliptic integral.
  23.  * @param <T> type of the field elements (really {@link Complex} or {@link FieldComplex})
  24.  * @since 2.0
  25.  */
  26. class RjFieldDuplication<T extends CalculusFieldElement<T>> extends FieldDuplication<T> {

  27.     /** Delta product. */
  28.     private T delta;

  29.     /** sₘ iteration parameter. */
  30.     private T sM;

  31.     /** Simple constructor.
  32.      * @param x first symmetric variable of the integral
  33.      * @param y second symmetric variable of the integral
  34.      * @param z third symmetric variable of the integral
  35.      * @param p fourth <em>not</em> symmetric variable of the integral
  36.      * @param delta precomputed value of (p-x)(p-y)(p-z)
  37.      */
  38.     RjFieldDuplication(final T x, final T y, final T z, final T p, final T delta) {
  39.         super(x, y, z, p);
  40.         this.delta = delta;
  41.     }

  42.     /** {@inheritDoc} */
  43.     @Override
  44.     protected void initialMeanPoint(final T[] va) {
  45.         va[4] = va[0].add(va[1]).add(va[2]).add(va[3].multiply(2)).divide(5.0);
  46.     }

  47.     /** {@inheritDoc} */
  48.     @Override
  49.     protected T convergenceCriterion(final T r, final T max) {
  50.         return max.divide(FastMath.sqrt(FastMath.sqrt(FastMath.sqrt(r.multiply(0.25)))));
  51.     }

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     protected void update(final int m, final T[] vaM, final T[] sqrtM, final  double fourM) {
  55.         final T dM =          sqrtM[3].add(sqrtM[0]).
  56.                      multiply(sqrtM[3].add(sqrtM[1])).
  57.                      multiply(sqrtM[3].add(sqrtM[2]));
  58.         if (m == 0) {
  59.             sM = dM.multiply(0.5);
  60.         } else {
  61.             // equation A.3 in Carlson[2000]
  62.             final T rM = sM.multiply(delta.divide(sM.multiply(sM).multiply(fourM)).add(1.0).sqrt().add(1.0));
  63.             sM = dM.multiply(rM).subtract(delta.divide(fourM * fourM)).
  64.                  divide(dM.add(rM.divide(fourM)).multiply(2));
  65.         }

  66.         // equation 2.19 in Carlson[1995]
  67.         final T lambdaA = sqrtM[0].multiply(sqrtM[1]);
  68.         final T lambdaB = sqrtM[0].multiply(sqrtM[2]);
  69.         final T lambdaC = sqrtM[1].multiply(sqrtM[2]);

  70.         // equations 2.19 and 2.20 in Carlson[1995]
  71.         vaM[0] = vaM[0].linearCombination(0.25, vaM[0], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // xₘ
  72.         vaM[1] = vaM[1].linearCombination(0.25, vaM[1], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // yₘ
  73.         vaM[2] = vaM[2].linearCombination(0.25, vaM[2], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // zₘ
  74.         vaM[3] = vaM[3].linearCombination(0.25, vaM[3], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // pₘ
  75.         vaM[4] = vaM[4].linearCombination(0.25, vaM[4], 0.25, lambdaA, 0.25, lambdaB, 0.25, lambdaC); // aₘ

  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     protected T evaluate(final T[] va0, final T aM, final  double fourM) {

  80.         // compute symmetric differences
  81.         final T inv    = aM.multiply(fourM).reciprocal();
  82.         final T bigX   = va0[4].subtract(va0[0]).multiply(inv);
  83.         final T bigY   = va0[4].subtract(va0[1]).multiply(inv);
  84.         final T bigZ   = va0[4].subtract(va0[2]).multiply(inv);
  85.         final T bigP   = bigX.add(bigY).add(bigZ).multiply(-0.5);
  86.         final T bigP2  = bigP.multiply(bigP);

  87.         // compute elementary symmetric functions (we already know e1 = 0 by construction)
  88.         final T xyz    = bigX.multiply(bigY).multiply(bigZ);
  89.         final T e2     = bigX.multiply(bigY.add(bigZ)).add(bigY.multiply(bigZ)).
  90.                                subtract(bigP.multiply(bigP).multiply(3));
  91.         final T e3     = xyz.add(bigP.multiply(2).multiply(e2.add(bigP2.multiply(2))));
  92.         final T e4     = xyz.multiply(2).add(bigP.multiply(e2.add(bigP2.multiply(3)))).multiply(bigP);
  93.         final T e5     = xyz.multiply(bigP2);

  94.         final T e2e2   = e2.multiply(e2);
  95.         final T e2e3   = e2.multiply(e3);
  96.         final T e2e4   = e2.multiply(e4);
  97.         final T e2e5   = e2.multiply(e5);
  98.         final T e3e3   = e3.multiply(e3);
  99.         final T e3e4   = e3.multiply(e4);
  100.         final T e2e2e2 = e2e2.multiply(e2);
  101.         final T e2e2e3 = e2e2.multiply(e3);

  102.         // evaluate integral using equation 19.36.1 in DLMF
  103.         // (which add more terms than equation 2.7 in Carlson[1995])
  104.         final T poly = e3e4.add(e2e5).multiply(RdRealDuplication.E3_E4_P_E2_E5).
  105.                        add(e2e2e3.multiply(RdRealDuplication.E2_E2_E3)).
  106.                        add(e2e4.multiply(RdRealDuplication.E2_E4)).
  107.                        add(e3e3.multiply(RdRealDuplication.E3_E3)).
  108.                        add(e2e2e2.multiply(RdRealDuplication.E2_E2_E2)).
  109.                        add(e5.multiply(RdRealDuplication.E5)).
  110.                        add(e2e3.multiply(RdRealDuplication.E2_E3)).
  111.                        add(e4.multiply(RdRealDuplication.E4)).
  112.                        add(e2e2.multiply(RdRealDuplication.E2_E2)).
  113.                        add(e3.multiply(RdRealDuplication.E3)).
  114.                        add(e2.multiply(RdRealDuplication.E2)).
  115.                        add(RdRealDuplication.CONSTANT).
  116.                        divide(RdRealDuplication.DENOMINATOR);
  117.         final T polyTerm = poly.divide(aM.multiply(FastMath.sqrt(aM)).multiply(fourM));

  118.         // compute a single R_C term
  119.         final T rcTerm = new RcFieldDuplication<>(poly.getField().getOne(), delta.divide(sM.multiply(sM).multiply(fourM)).add(1)).
  120.                          integral().
  121.                          multiply(3).divide(sM);

  122.         return polyTerm.add(rcTerm);

  123.     }

  124. }