LegendreEllipticIntegral.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.legendre;

  18. import java.util.function.DoubleFunction;
  19. import java.util.function.Function;

  20. import org.hipparchus.CalculusFieldElement;
  21. import org.hipparchus.analysis.CalculusFieldUnivariateFunction;
  22. import org.hipparchus.complex.Complex;
  23. import org.hipparchus.complex.ComplexUnivariateIntegrator;
  24. import org.hipparchus.complex.FieldComplex;
  25. import org.hipparchus.complex.FieldComplexUnivariateIntegrator;
  26. import org.hipparchus.special.elliptic.carlson.CarlsonEllipticIntegral;
  27. import org.hipparchus.util.FastMath;
  28. import org.hipparchus.util.MathUtils;

  29. /** Complete and incomplete elliptic integrals in Legendre form.
  30.  * <p>
  31.  * The elliptic integrals are related to Jacobi elliptic functions.
  32.  * </p>
  33.  * <p>
  34.  * <em>
  35.  * Beware that when computing elliptic integrals in the complex plane,
  36.  * many issues arise due to branch cuts. See the
  37.  * <a href="https://www.hipparchus.org/hipparchus-core/special.html#Elliptic_functions_and_integrals">user guide</a>
  38.  * for a thorough explanation.
  39.  * </em>
  40.  * </p>
  41.  * <p>
  42.  * There are different conventions to interpret the arguments of
  43.  * Legendre elliptic integrals. In mathematical texts, these conventions show
  44.  * up using the separator between arguments. So for example for the incomplete
  45.  * integral of the first kind F we have:
  46.  * </p>
  47.  * <ul>
  48.  *   <li>F(φ, k): the first argument φ is an angle and the second argument k
  49.  *       is the elliptic modulus: this is the trigonometric form of the integral</li>
  50.  *   <li>F(φ; m): the first argument φ is an angle and the second argument m=k²
  51.  *       is the parameter: this is also a trigonometric form of the integral</li>
  52.  *   <li>F(x|m): the first argument x=sin(φ) is not an angle anymore and the
  53.  *       second argument m=k² is the parameter: this is the Legendre form</li>
  54.  *   <li>F(φ\α): the first argument φ is an angle and the second argument α is the
  55.  *       modular angle</li>
  56.  * </ul>
  57.  * <p>
  58.  * As we have no separator in a method call, we have to adopt one convention
  59.  * and stick to it. In Hipparchus, we adopted the Legendre form (i.e. F(x|m),
  60.  * with x=sin(φ) and m=k². These conventions are consistent with Wolfram Alpha
  61.  * functions EllipticF, EllipticE, ElliptiPI…
  62.  * </p>
  63.  * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  64.  * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheFirstKind.html">Complete Elliptic Integrals of the First Kind (MathWorld)</a>
  65.  * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheSecondKind.html">Complete Elliptic Integrals of the Second Kind (MathWorld)</a>
  66.  * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheFirstKind.html">Elliptic Integrals of the First Kind (MathWorld)</a>
  67.  * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheSecondKind.html">Elliptic Integrals of the Second Kind (MathWorld)</a>
  68.  * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheThirdKind.html">Elliptic Integrals of the Third Kind (MathWorld)</a>
  69.  * @since 2.0
  70.  */
  71. public class LegendreEllipticIntegral { // NOPMD - this class has a high number of methods, it is normal

  72.     /** Private constructor for a utility class.
  73.      */
  74.     private LegendreEllipticIntegral() {
  75.         // nothing to do
  76.     }

  77.     /** Get the nome q.
  78.      * @param m parameter (m=k² where k is the elliptic modulus)
  79.      * @return nome q
  80.      */
  81.     public static double nome(final double m) {
  82.         if (m < 1.0e-16) {
  83.             // first terms of infinite series in Abramowitz and Stegun 17.3.21
  84.             final double m16 = m * 0.0625;
  85.             return m16 * (1 + 8 * m16);
  86.         } else {
  87.             return FastMath.exp(-FastMath.PI * bigKPrime(m) / bigK(m));
  88.         }
  89.     }

  90.     /** Get the nome q.
  91.      * @param m parameter (m=k² where k is the elliptic modulus)
  92.      * @param <T> the type of the field elements
  93.      * @return nome q
  94.      */
  95.     public static <T extends CalculusFieldElement<T>> T nome(final T m) {
  96.         final T one = m.getField().getOne();
  97.         if (m.norm() < 100 * one.ulp().getReal()) {
  98.             // first terms of infinite series in Abramowitz and Stegun 17.3.21
  99.             final T m16 = m.multiply(0.0625);
  100.             return m16.multiply(m16.multiply(8).add(1));
  101.         } else {
  102.             return FastMath.exp(bigKPrime(m).divide(bigK(m)).multiply(one.getPi().negate()));
  103.         }
  104.     }

  105.     /** Get the complete elliptic integral of the first kind K(m).
  106.      * <p>
  107.      * The complete elliptic integral of the first kind K(m) is
  108.      * \[
  109.      *    \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1-m \sin^2\theta}}
  110.      * \]
  111.      * it corresponds to the real quarter-period of Jacobi elliptic functions
  112.      * </p>
  113.      * <p>
  114.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  115.      * Carlson elliptic integrals}.
  116.      * </p>
  117.      * @param m parameter (m=k² where k is the elliptic modulus)
  118.      * @return complete elliptic integral of the first kind K(m)
  119.      * @see #bigKPrime(double)
  120.      * @see #bigF(double, double)
  121.      * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheFirstKind.html">Complete Elliptic Integrals of the First Kind (MathWorld)</a>
  122.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  123.      */
  124.     public static double bigK(final double m) {
  125.         if (m < 1.0e-8) {
  126.             // first terms of infinite series in Abramowitz and Stegun 17.3.11
  127.             return (1 + 0.25 * m) * MathUtils.SEMI_PI;
  128.         } else {
  129.             return CarlsonEllipticIntegral.rF(0, 1.0 - m, 1);
  130.         }
  131.     }

  132.     /** Get the complete elliptic integral of the first kind K(m).
  133.      * <p>
  134.      * The complete elliptic integral of the first kind K(m) is
  135.      * \[
  136.      *    \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1-m \sin^2\theta}}
  137.      * \]
  138.      * it corresponds to the real quarter-period of Jacobi elliptic functions
  139.      * </p>
  140.      * <p>
  141.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  142.      * Carlson elliptic integrals}.
  143.      * </p>
  144.      * @param m parameter (m=k² where k is the elliptic modulus)
  145.      * @param <T> the type of the field elements
  146.      * @return complete elliptic integral of the first kind K(m)
  147.      * @see #bigKPrime(CalculusFieldElement)
  148.      * @see #bigF(CalculusFieldElement, CalculusFieldElement)
  149.      * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheFirstKind.html">Complete Elliptic Integrals of the First Kind (MathWorld)</a>
  150.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  151.      */
  152.     public static <T extends CalculusFieldElement<T>> T bigK(final T m) {
  153.         final T zero = m.getField().getZero();
  154.         final T one  = m.getField().getOne();
  155.         if (m.norm() < 1.0e7 * one.ulp().getReal()) {

  156.             // first terms of infinite series in Abramowitz and Stegun 17.3.11
  157.             return one.add(m.multiply(0.25)).multiply(zero.getPi().multiply(0.5));

  158.         } else {
  159.             return CarlsonEllipticIntegral.rF(zero, one.subtract(m), one);
  160.         }
  161.     }

  162.     /** Get the complete elliptic integral of the first kind K(m).
  163.      * <p>
  164.      * The complete elliptic integral of the first kind K(m) is
  165.      * \[
  166.      *    \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1-m \sin^2\theta}}
  167.      * \]
  168.      * it corresponds to the real quarter-period of Jacobi elliptic functions
  169.      * </p>
  170.      * <p>
  171.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  172.      * Carlson elliptic integrals}.
  173.      * </p>
  174.      * @param m parameter (m=k² where k is the elliptic modulus)
  175.      * @return complete elliptic integral of the first kind K(m)
  176.      * @see #bigKPrime(Complex)
  177.      * @see #bigF(Complex, Complex)
  178.      * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheFirstKind.html">Complete Elliptic Integrals of the First Kind (MathWorld)</a>
  179.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  180.      */
  181.     public static Complex bigK(final Complex m) {
  182.         if (m.norm() < 1.0e-8) {
  183.             // first terms of infinite series in Abramowitz and Stegun 17.3.11
  184.             return Complex.ONE.add(m.multiply(0.25)).multiply(MathUtils.SEMI_PI);
  185.         } else {
  186.             return CarlsonEllipticIntegral.rF(Complex.ZERO, Complex.ONE.subtract(m), Complex.ONE);
  187.         }
  188.     }

  189.     /** Get the complete elliptic integral of the first kind K(m).
  190.      * <p>
  191.      * The complete elliptic integral of the first kind K(m) is
  192.      * \[
  193.      *    \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1-m \sin^2\theta}}
  194.      * \]
  195.      * it corresponds to the real quarter-period of Jacobi elliptic functions
  196.      * </p>
  197.      * <p>
  198.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  199.      * Carlson elliptic integrals}.
  200.      * </p>
  201.      * @param m parameter (m=k² where k is the elliptic modulus)
  202.      * @param <T> the type of the field elements
  203.      * @return complete elliptic integral of the first kind K(m)
  204.      * @see #bigKPrime(FieldComplex)
  205.      * @see #bigF(FieldComplex, FieldComplex)
  206.      * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheFirstKind.html">Complete Elliptic Integrals of the First Kind (MathWorld)</a>
  207.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  208.      */
  209.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> bigK(final FieldComplex<T> m) {
  210.         final FieldComplex<T> zero = m.getField().getZero();
  211.         final FieldComplex<T> one  = m.getField().getOne();
  212.         if (m.norm() < 1.0e7 * one.ulp().getReal()) {

  213.             // first terms of infinite series in Abramowitz and Stegun 17.3.11
  214.             return one.add(m.multiply(0.25)).multiply(zero.getPi().multiply(0.5));

  215.         } else {
  216.             return CarlsonEllipticIntegral.rF(zero, one.subtract(m), one);
  217.         }
  218.     }

  219.     /** Get the complete elliptic integral of the first kind K'(m).
  220.      * <p>
  221.      * The complete elliptic integral of the first kind K'(m) is
  222.      * \[
  223.      *    \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1-(1-m) \sin^2\theta}}
  224.      * \]
  225.      * it corresponds to the imaginary quarter-period of Jacobi elliptic functions
  226.      * </p>
  227.      * <p>
  228.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  229.      * Carlson elliptic integrals}.
  230.      * </p>
  231.      * @param m parameter (m=k² where k is the elliptic modulus)
  232.      * @return complete elliptic integral of the first kind K'(m)
  233.      * @see #bigK(double)
  234.      * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheFirstKind.html">Complete Elliptic Integrals of the First Kind (MathWorld)</a>
  235.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  236.      */
  237.     public static double bigKPrime(final double m) {
  238.         return CarlsonEllipticIntegral.rF(0, m, 1);
  239.     }

  240.     /** Get the complete elliptic integral of the first kind K'(m).
  241.      * <p>
  242.      * The complete elliptic integral of the first kind K'(m) is
  243.      * \[
  244.      *    \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1-(1-m) \sin^2\theta}}
  245.      * \]
  246.      * it corresponds to the imaginary quarter-period of Jacobi elliptic functions
  247.      * </p>
  248.      * <p>
  249.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  250.      * Carlson elliptic integrals}.
  251.      * </p>
  252.      * @param m parameter (m=k² where k is the elliptic modulus)
  253.      * @param <T> the type of the field elements
  254.      * @return complete elliptic integral of the first kind K'(m)
  255.      * @see #bigK(CalculusFieldElement)
  256.      * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheFirstKind.html">Complete Elliptic Integrals of the First Kind (MathWorld)</a>
  257.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  258.      */
  259.     public static <T extends CalculusFieldElement<T>> T bigKPrime(final T m) {
  260.         final T zero = m.getField().getZero();
  261.         final T one  = m.getField().getOne();
  262.         return CarlsonEllipticIntegral.rF(zero, m, one);
  263.     }

  264.     /** Get the complete elliptic integral of the first kind K'(m).
  265.      * <p>
  266.      * The complete elliptic integral of the first kind K'(m) is
  267.      * \[
  268.      *    \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1-(1-m) \sin^2\theta}}
  269.      * \]
  270.      * it corresponds to the imaginary quarter-period of Jacobi elliptic functions
  271.      * </p>
  272.      * <p>
  273.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  274.      * Carlson elliptic integrals}.
  275.      * </p>
  276.      * @param m parameter (m=k² where k is the elliptic modulus)
  277.      * @return complete elliptic integral of the first kind K'(m)
  278.      * @see #bigK(Complex)
  279.      * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheFirstKind.html">Complete Elliptic Integrals of the First Kind (MathWorld)</a>
  280.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  281.      */
  282.     public static Complex bigKPrime(final Complex m) {
  283.         return CarlsonEllipticIntegral.rF(Complex.ZERO, m, Complex.ONE);
  284.     }

  285.     /** Get the complete elliptic integral of the first kind K'(m).
  286.      * <p>
  287.      * The complete elliptic integral of the first kind K'(m) is
  288.      * \[
  289.      *    \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1-(1-m) \sin^2\theta}}
  290.      * \]
  291.      * it corresponds to the imaginary quarter-period of Jacobi elliptic functions
  292.      * </p>
  293.      * <p>
  294.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  295.      * Carlson elliptic integrals}.
  296.      * </p>
  297.      * @param m parameter (m=k² where k is the elliptic modulus)
  298.      * @param <T> the type of the field elements
  299.      * @return complete elliptic integral of the first kind K'(m)
  300.      * @see #bigK(FieldComplex)
  301.      * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheFirstKind.html">Complete Elliptic Integrals of the First Kind (MathWorld)</a>
  302.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  303.      */
  304.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> bigKPrime(final FieldComplex<T> m) {
  305.         final FieldComplex<T> zero = m.getField().getZero();
  306.         final FieldComplex<T> one  = m.getField().getOne();
  307.         return CarlsonEllipticIntegral.rF(zero, m, one);
  308.     }

  309.     /** Get the complete elliptic integral of the second kind E(m).
  310.      * <p>
  311.      * The complete elliptic integral of the second kind E(m) is
  312.      * \[
  313.      *    \int_0^{\frac{\pi}{2}} \sqrt{1-m \sin^2\theta} d\theta
  314.      * \]
  315.      * </p>
  316.      * <p>
  317.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  318.      * Carlson elliptic integrals}.
  319.      * </p>
  320.      * @param m parameter (m=k² where k is the elliptic modulus)
  321.      * @return complete elliptic integral of the second kind E(m)
  322.      * @see #bigE(double, double)
  323.      * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheSecondKind.html">Complete Elliptic Integrals of the Second Kind (MathWorld)</a>
  324.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  325.      */
  326.     public static double bigE(final double m) {
  327.         return CarlsonEllipticIntegral.rG(0, 1 - m, 1) * 2;
  328.     }

  329.     /** Get the complete elliptic integral of the second kind E(m).
  330.      * <p>
  331.      * The complete elliptic integral of the second kind E(m) is
  332.      * \[
  333.      *    \int_0^{\frac{\pi}{2}} \sqrt{1-m \sin^2\theta} d\theta
  334.      * \]
  335.      * </p>
  336.      * <p>
  337.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  338.      * Carlson elliptic integrals}.
  339.      * </p>
  340.      * @param m parameter (m=k² where k is the elliptic modulus)
  341.      * @param <T> the type of the field elements
  342.      * @return complete elliptic integral of the second kind E(m)
  343.      * @see #bigE(CalculusFieldElement, CalculusFieldElement)
  344.      * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheSecondKind.html">Complete Elliptic Integrals of the Second Kind (MathWorld)</a>
  345.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  346.      */
  347.     public static <T extends CalculusFieldElement<T>> T bigE(final T m) {
  348.         final T zero = m.getField().getZero();
  349.         final T one  = m.getField().getOne();
  350.         return CarlsonEllipticIntegral.rG(zero, one.subtract(m), one).multiply(2);
  351.     }

  352.     /** Get the complete elliptic integral of the second kind E(m).
  353.      * <p>
  354.      * The complete elliptic integral of the second kind E(m) is
  355.      * \[
  356.      *    \int_0^{\frac{\pi}{2}} \sqrt{1-m \sin^2\theta} d\theta
  357.      * \]
  358.      * </p>
  359.      * <p>
  360.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  361.      * Carlson elliptic integrals}.
  362.      * </p>
  363.      * @param m parameter (m=k² where k is the elliptic modulus)
  364.      * @return complete elliptic integral of the second kind E(m)
  365.      * @see #bigE(Complex, Complex)
  366.      * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheSecondKind.html">Complete Elliptic Integrals of the Second Kind (MathWorld)</a>
  367.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  368.      */
  369.     public static Complex bigE(final Complex m) {
  370.         return CarlsonEllipticIntegral.rG(Complex.ZERO,
  371.                                           Complex.ONE.subtract(m),
  372.                                           Complex.ONE).multiply(2);
  373.     }

  374.     /** Get the complete elliptic integral of the second kind E(m).
  375.      * <p>
  376.      * The complete elliptic integral of the second kind E(m) is
  377.      * \[
  378.      *    \int_0^{\frac{\pi}{2}} \sqrt{1-m \sin^2\theta} d\theta
  379.      * \]
  380.      * </p>
  381.      * <p>
  382.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  383.      * Carlson elliptic integrals}.
  384.      * </p>
  385.      * @param m parameter (m=k² where k is the elliptic modulus)
  386.      * @param <T> the type of the field elements
  387.      * @return complete elliptic integral of the second kind E(m)
  388.      * @see #bigE(FieldComplex, FieldComplex)
  389.      * @see <a href="https://mathworld.wolfram.com/CompleteEllipticIntegraloftheSecondKind.html">Complete Elliptic Integrals of the Second Kind (MathWorld)</a>
  390.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  391.      */
  392.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> bigE(final FieldComplex<T> m) {
  393.         final FieldComplex<T> zero = m.getField().getZero();
  394.         final FieldComplex<T> one  = m.getField().getOne();
  395.         return CarlsonEllipticIntegral.rG(zero, one.subtract(m), one).multiply(2);
  396.     }

  397.     /** Get the complete elliptic integral D(m) = [K(m) - E(m)]/m.
  398.      * <p>
  399.      * The complete elliptic integral D(m) is
  400.      * \[
  401.      *    \int_0^{\frac{\pi}{2}} \frac{\sin^2\theta}{\sqrt{1-m \sin^2\theta}} d\theta
  402.      * \]
  403.      * </p>
  404.      * <p>
  405.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  406.      * Carlson elliptic integrals}.
  407.      * </p>
  408.      * @param m parameter (m=k² where k is the elliptic modulus)
  409.      * @return complete elliptic integral D(m)
  410.      * @see #bigD(double, double)
  411.      */
  412.     public static double bigD(final double m) {
  413.         return CarlsonEllipticIntegral.rD(0, 1 - m, 1) / 3;
  414.     }

  415.     /** Get the complete elliptic integral D(m) = [K(m) - E(m)]/m.
  416.      * <p>
  417.      * The complete elliptic integral D(m) is
  418.      * \[
  419.      *    \int_0^{\frac{\pi}{2}} \frac{\sin^2\theta}{\sqrt{1-m \sin^2\theta}} d\theta
  420.      * \]
  421.      * </p>
  422.      * <p>
  423.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  424.      * Carlson elliptic integrals}.
  425.      * </p>
  426.      * @param m parameter (m=k² where k is the elliptic modulus)
  427.      * @param <T> the type of the field elements
  428.      * @return complete elliptic integral D(m)
  429.      * @see #bigD(CalculusFieldElement, CalculusFieldElement)
  430.      */
  431.     public static <T extends CalculusFieldElement<T>> T bigD(final T m) {
  432.         final T zero = m.getField().getZero();
  433.         final T one  = m.getField().getOne();
  434.         return CarlsonEllipticIntegral.rD(zero, one.subtract(m), one).divide(3);
  435.     }

  436.     /** Get the complete elliptic integral D(m) = [K(m) - E(m)]/m.
  437.      * <p>
  438.      * The complete elliptic integral D(m) is
  439.      * \[
  440.      *    \int_0^{\frac{\pi}{2}} \frac{\sin^2\theta}{\sqrt{1-m \sin^2\theta}} d\theta
  441.      * \]
  442.      * </p>
  443.      * <p>
  444.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  445.      * Carlson elliptic integrals}.
  446.      * </p>
  447.      * @param m parameter (m=k² where k is the elliptic modulus)
  448.      * @return complete elliptic integral D(m)
  449.      * @see #bigD(Complex, Complex)
  450.      */
  451.     public static Complex bigD(final Complex m) {
  452.         return CarlsonEllipticIntegral.rD(Complex.ZERO, Complex.ONE.subtract(m), Complex.ONE).divide(3);
  453.     }

  454.     /** Get the complete elliptic integral D(m) = [K(m) - E(m)]/m.
  455.      * <p>
  456.      * The complete elliptic integral D(m) is
  457.      * \[
  458.      *    \int_0^{\frac{\pi}{2}} \frac{\sin^2\theta}{\sqrt{1-m \sin^2\theta}} d\theta
  459.      * \]
  460.      * </p>
  461.      * <p>
  462.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  463.      * Carlson elliptic integrals}.
  464.      * </p>
  465.      * @param m parameter (m=k² where k is the elliptic modulus)
  466.      * @param <T> the type of the field elements
  467.      * @return complete elliptic integral D(m)
  468.      * @see #bigD(FieldComplex, FieldComplex)
  469.      */
  470.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> bigD(final FieldComplex<T> m) {
  471.         final FieldComplex<T> zero = m.getField().getZero();
  472.         final FieldComplex<T> one  = m.getField().getOne();
  473.         return CarlsonEllipticIntegral.rD(zero, one.subtract(m), one).divide(3);
  474.     }

  475.     /** Get the complete elliptic integral of the third kind Π(n, m).
  476.      * <p>
  477.      * The complete elliptic integral of the third kind Π(n, m) is
  478.      * \[
  479.      *    \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1-m \sin^2\theta}(1-n \sin^2\theta)}
  480.      * \]
  481.      * </p>
  482.      * <p>
  483.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  484.      * Carlson elliptic integrals}.
  485.      * </p>
  486.      * @param n elliptic characteristic
  487.      * @param m parameter (m=k² where k is the elliptic modulus)
  488.      * @return complete elliptic integral of the third kind Π(n, m)
  489.      * @see #bigPi(double, double, double)
  490.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheThirdKind.html">Elliptic Integrals of the Third Kind (MathWorld)</a>
  491.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  492.      */
  493.     public static double bigPi(final double n, final double m) {
  494.         final double kPrime2 = 1 - m;
  495.         final double delta   = n * (m - n) * (n - 1);
  496.         return CarlsonEllipticIntegral.rF(0, kPrime2, 1) +
  497.                CarlsonEllipticIntegral.rJ(0, kPrime2, 1, 1 - n, delta) * n / 3;
  498.     }

  499.     /** Get the complete elliptic integral of the third kind Π(n, m).
  500.      * <p>
  501.      * The complete elliptic integral of the third kind Π(n, m) is
  502.      * \[
  503.      *    \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1-m \sin^2\theta}(1-n \sin^2\theta)}
  504.      * \]
  505.      * </p>
  506.      * <p>
  507.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  508.      * Carlson elliptic integrals}.
  509.      * </p>
  510.      * @param n elliptic characteristic
  511.      * @param m parameter (m=k² where k is the elliptic modulus)
  512.      * @param <T> the type of the field elements
  513.      * @return complete elliptic integral of the third kind Π(n, m)
  514.      * @see #bigPi(CalculusFieldElement, CalculusFieldElement, CalculusFieldElement)
  515.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheThirdKind.html">Elliptic Integrals of the Third Kind (MathWorld)</a>
  516.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  517.      */
  518.     public static <T extends CalculusFieldElement<T>> T bigPi(final T n, final T m) {
  519.         final T zero    = m.getField().getZero();
  520.         final T one     = m.getField().getOne();
  521.         final T kPrime2 = one.subtract(m);
  522.         final T delta   = n.multiply(m.subtract(n)).multiply(n.subtract(1));
  523.         return CarlsonEllipticIntegral.rF(zero, kPrime2, one).
  524.                add(CarlsonEllipticIntegral.rJ(zero, kPrime2, one, one.subtract(n), delta).multiply(n).divide(3));
  525.     }

  526.     /** Get the complete elliptic integral of the third kind Π(n, m).
  527.      * <p>
  528.      * The complete elliptic integral of the third kind Π(n, m) is
  529.      * \[
  530.      *    \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1-m \sin^2\theta}(1-n \sin^2\theta)}
  531.      * \]
  532.      * </p>
  533.      * <p>
  534.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  535.      * Carlson elliptic integrals}.
  536.      * </p>
  537.      * @param n elliptic characteristic
  538.      * @param m parameter (m=k² where k is the elliptic modulus)
  539.      * @return complete elliptic integral of the third kind Π(n, m)
  540.      * @see #bigPi(Complex, Complex, Complex)
  541.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheThirdKind.html">Elliptic Integrals of the Third Kind (MathWorld)</a>
  542.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  543.      */
  544.     public static Complex bigPi(final Complex n, final Complex m) {
  545.         final Complex kPrime2 = Complex.ONE.subtract(m);
  546.         final Complex delta   = n.multiply(m.subtract(n)).multiply(n.subtract(1));
  547.         return CarlsonEllipticIntegral.rF(Complex.ZERO, kPrime2, Complex.ONE).
  548.                add(CarlsonEllipticIntegral.rJ(Complex.ZERO, kPrime2, Complex.ONE, Complex.ONE.subtract(n), delta).multiply(n).divide(3));
  549.     }

  550.     /** Get the complete elliptic integral of the third kind Π(n, m).
  551.      * <p>
  552.      * The complete elliptic integral of the third kind Π(n, m) is
  553.      * \[
  554.      *    \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1-m \sin^2\theta}(1-n \sin^2\theta)}
  555.      * \]
  556.      * </p>
  557.      * <p>
  558.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  559.      * Carlson elliptic integrals}.
  560.      * </p>
  561.      * @param n elliptic characteristic
  562.      * @param m parameter (m=k² where k is the elliptic modulus)
  563.      * @param <T> the type of the field elements
  564.      * @return complete elliptic integral of the third kind Π(n, m)
  565.      * @see #bigPi(FieldComplex, FieldComplex, FieldComplex)
  566.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheThirdKind.html">Elliptic Integrals of the Third Kind (MathWorld)</a>
  567.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  568.      */
  569.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> bigPi(final FieldComplex<T> n, final FieldComplex<T> m) {
  570.         final FieldComplex<T> zero    = m.getField().getZero();
  571.         final FieldComplex<T> one     = m.getField().getOne();
  572.         final FieldComplex<T> kPrime2 = one.subtract(m);
  573.         final FieldComplex<T> delta   = n.multiply(m.subtract(n)).multiply(n.subtract(1));
  574.         return CarlsonEllipticIntegral.rF(zero, kPrime2, one).
  575.                add(CarlsonEllipticIntegral.rJ(zero, kPrime2, one, one.subtract(n), delta).multiply(n).divide(3));
  576.     }

  577.     /** Get the incomplete elliptic integral of the first kind F(φ, m).
  578.      * <p>
  579.      * The incomplete elliptic integral of the first kind F(φ, m) is
  580.      * \[
  581.      *    \int_0^{\phi} \frac{d\theta}{\sqrt{1-m \sin^2\theta}}
  582.      * \]
  583.      * </p>
  584.      * <p>
  585.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  586.      * Carlson elliptic integrals}.
  587.      * </p>
  588.      * @param phi amplitude (i.e. upper bound of the integral)
  589.      * @param m parameter (m=k² where k is the elliptic modulus)
  590.      * @return incomplete elliptic integral of the first kind F(φ, m)
  591.      * @see #bigK(double)
  592.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheFirstKind.html">Elliptic Integrals of the First Kind (MathWorld)</a>
  593.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  594.      */
  595.     public static double bigF(final double phi, final double m) {

  596.         // argument reduction
  597.         final DoubleArgumentReduction ar = new DoubleArgumentReduction(phi, m, LegendreEllipticIntegral::bigK);

  598.         // integrate part between 0 and π/2
  599.         final double cM1 = ar.csc2 - 1.0;
  600.         final double cMm = ar.csc2 - m;
  601.         final double incomplete =  CarlsonEllipticIntegral.rF(cM1, cMm, ar.csc2);

  602.         // combine complete and incomplete parts
  603.         return ar.negate ? ar.complete - incomplete : ar.complete + incomplete;

  604.     }

  605.     /** Get the incomplete elliptic integral of the first kind F(φ, m).
  606.      * <p>
  607.      * The incomplete elliptic integral of the first kind F(φ, m) is
  608.      * \[
  609.      *    \int_0^{\phi} \frac{d\theta}{\sqrt{1-m \sin^2\theta}}
  610.      * \]
  611.      * </p>
  612.      * <p>
  613.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  614.      * Carlson elliptic integrals}.
  615.      * </p>
  616.      * @param phi amplitude (i.e. upper bound of the integral)
  617.      * @param m parameter (m=k² where k is the elliptic modulus)
  618.      * @param <T> the type of the field elements
  619.      * @return incomplete elliptic integral of the first kind F(φ, m)
  620.      * @see #bigK(CalculusFieldElement)
  621.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheFirstKind.html">Elliptic Integrals of the First Kind (MathWorld)</a>
  622.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  623.      */
  624.     public static <T extends CalculusFieldElement<T>> T bigF(final T phi, final T m) {

  625.         // argument reduction
  626.         final FieldArgumentReduction<T> ar = new FieldArgumentReduction<>(phi, m, LegendreEllipticIntegral::bigK);

  627.         // integrate part between 0 and π/2
  628.         final T cM1        = ar.csc2.subtract(1);
  629.         final T cMm        = ar.csc2.subtract(m);
  630.         final T incomplete = CarlsonEllipticIntegral.rF(cM1, cMm, ar.csc2);

  631.         // combine complete and incomplete parts
  632.         return ar.negate ? ar.complete.subtract(incomplete) : ar.complete.add(incomplete);

  633.     }

  634.     /** Get the incomplete elliptic integral of the first kind F(φ, m).
  635.      * <p>
  636.      * <em>
  637.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  638.      * are considered experimental for now, they have known issues.
  639.      * </em>
  640.      * </p>
  641.      * <p>
  642.      * The incomplete elliptic integral of the first kind F(φ, m) is
  643.      * \[
  644.      *    \int_0^{\phi} \frac{d\theta}{\sqrt{1-m \sin^2\theta}}
  645.      * \]
  646.      * </p>
  647.      * <p>
  648.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  649.      * Carlson elliptic integrals}.
  650.      * </p>
  651.      * @param phi amplitude (i.e. upper bound of the integral)
  652.      * @param m parameter (m=k² where k is the elliptic modulus)
  653.      * @return incomplete elliptic integral of the first kind F(φ, m)
  654.      * @see #bigK(Complex)
  655.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheFirstKind.html">Elliptic Integrals of the First Kind (MathWorld)</a>
  656.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  657.      */
  658.     public static Complex bigF(final Complex phi, final Complex m) {

  659.         // argument reduction
  660.         final FieldArgumentReduction<Complex> ar = new FieldArgumentReduction<>(phi, m, LegendreEllipticIntegral::bigK);

  661.         // integrate part between 0 and π/2
  662.         final Complex cM1        = ar.csc2.subtract(1);
  663.         final Complex cMm        = ar.csc2.subtract(m);
  664.         final Complex incomplete = CarlsonEllipticIntegral.rF(cM1, cMm, ar.csc2);

  665.         // combine complete and incomplete parts
  666.         return ar.negate ? ar.complete.subtract(incomplete) : ar.complete.add(incomplete);

  667.     }

  668.     /** Get the incomplete elliptic integral of the first kind F(φ, m) using numerical integration.
  669.      * <p>
  670.      * <em>
  671.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  672.      * are considered experimental for now, they have known issues.
  673.      * </em>
  674.      * </p>
  675.      * <p>
  676.      * The incomplete elliptic integral of the first kind F(φ, m) is
  677.      * \[
  678.      *    \int_0^{\phi} \frac{d\theta}{\sqrt{1-m \sin^2\theta}}
  679.      * \]
  680.      * </p>
  681.      * <p>
  682.      * The algorithm for evaluating the functions is based on numerical integration.
  683.      * If integration path comes too close to a pole of the integrand, then integration will fail
  684.      * with a {@link org.hipparchus.exception.MathIllegalStateException MathIllegalStateException}
  685.      * even for very large {@code maxEval}. This is normal behavior.
  686.      * </p>
  687.      * @param phi amplitude (i.e. upper bound of the integral)
  688.      * @param m parameter (m=k² where k is the elliptic modulus)
  689.      * @param integrator integrator to use
  690.      * @param maxEval maximum number of evaluations (real and imaginary
  691.      * parts are evaluated separately, so up to twice this number may be used)
  692.      * @return incomplete elliptic integral of the first kind F(φ, m)
  693.      * @see #bigK(Complex)
  694.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheFirstKind.html">Elliptic Integrals of the First Kind (MathWorld)</a>
  695.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  696.      */
  697.     public static Complex bigF(final Complex phi, final Complex m,
  698.                                final ComplexUnivariateIntegrator integrator, final int maxEval) {
  699.         return integrator.integrate(maxEval, new First<>(m), phi.getField().getZero(), phi);
  700.     }

  701.     /** Get the incomplete elliptic integral of the first kind F(φ, m).
  702.      * <p>
  703.      * <em>
  704.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  705.      * are considered experimental for now, they have known issues.
  706.      * </em>
  707.      * </p>
  708.      * <p>
  709.      * The incomplete elliptic integral of the first kind F(φ, m) is
  710.      * \[
  711.      *    \int_0^{\phi} \frac{d\theta}{\sqrt{1-m \sin^2\theta}}
  712.      * \]
  713.      * </p>
  714.      * <p>
  715.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  716.      * Carlson elliptic integrals}.
  717.      * </p>
  718.      * @param phi amplitude (i.e. upper bound of the integral)
  719.      * @param m parameter (m=k² where k is the elliptic modulus)
  720.      * @param <T> the type of the field elements
  721.      * @return incomplete elliptic integral of the first kind F(φ, m)
  722.      * @see #bigK(CalculusFieldElement)
  723.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheFirstKind.html">Elliptic Integrals of the First Kind (MathWorld)</a>
  724.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  725.      */
  726.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> bigF(final FieldComplex<T> phi, final FieldComplex<T> m) {

  727.         // argument reduction
  728.         final FieldArgumentReduction<FieldComplex<T>> ar = new FieldArgumentReduction<>(phi, m, LegendreEllipticIntegral::bigK);

  729.         // integrate part between 0 and π/2
  730.         final FieldComplex<T> cM1        = ar.csc2.subtract(1);
  731.         final FieldComplex<T> cMm        = ar.csc2.subtract(m);
  732.         final FieldComplex<T> incomplete = CarlsonEllipticIntegral.rF(cM1, cMm, ar.csc2);

  733.         // combine complete and incomplete parts
  734.         return ar.negate ? ar.complete.subtract(incomplete) : ar.complete.add(incomplete);

  735.     }

  736.     /** Get the incomplete elliptic integral of the first kind F(φ, m).
  737.      * <p>
  738.      * <em>
  739.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  740.      * are considered experimental for now, they have known issues.
  741.      * </em>
  742.      * </p>
  743.      * <p>
  744.      * The incomplete elliptic integral of the first kind F(φ, m) is
  745.      * \[
  746.      *    \int_0^{\phi} \frac{d\theta}{\sqrt{1-m \sin^2\theta}}
  747.      * \]
  748.      * </p>
  749.      * <p>
  750.      * The algorithm for evaluating the functions is based on numerical integration.
  751.      * If integration path comes too close to a pole of the integrand, then integration will fail
  752.      * with a {@link org.hipparchus.exception.MathIllegalStateException MathIllegalStateException}
  753.      * even for very large {@code maxEval}. This is normal behavior.
  754.      * </p>
  755.      * @param phi amplitude (i.e. upper bound of the integral)
  756.      * @param m parameter (m=k² where k is the elliptic modulus)
  757.      * @param integrator integrator to use
  758.      * @param maxEval maximum number of evaluations (real and imaginary
  759.      * parts are evaluated separately, so up to twice this number may be used)
  760.      * @param <T> the type of the field elements
  761.      * @return incomplete elliptic integral of the first kind F(φ, m)
  762.      * @see #bigK(CalculusFieldElement)
  763.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheFirstKind.html">Elliptic Integrals of the First Kind (MathWorld)</a>
  764.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  765.      */
  766.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> bigF(final FieldComplex<T> phi, final FieldComplex<T> m,
  767.                                                                            final FieldComplexUnivariateIntegrator<T> integrator,
  768.                                                                            final int maxEval) {
  769.         return integrator.integrate(maxEval, new First<>(m), phi.getField().getZero(), phi);
  770.     }

  771.     /** Get the incomplete elliptic integral of the second kind E(φ, m).
  772.      * <p>
  773.      * The incomplete elliptic integral of the second kind E(φ, m) is
  774.      * \[
  775.      *    \int_0^{\phi} \sqrt{1-m \sin^2\theta} d\theta
  776.      * \]
  777.      * </p>
  778.      * <p>
  779.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  780.      * Carlson elliptic integrals}.
  781.      * </p>
  782.      * @param phi amplitude (i.e. upper bound of the integral)
  783.      * @param m parameter (m=k² where k is the elliptic modulus)
  784.      * @return incomplete elliptic integral of the second kind E(φ, m)
  785.      * @see #bigE(double)
  786.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheSecondKind.html">Elliptic Integrals of the Second Kind (MathWorld)</a>
  787.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  788.      */
  789.     public static double bigE(final double phi, final double m) {

  790.         // argument reduction
  791.         final DoubleArgumentReduction ar = new DoubleArgumentReduction(phi, m, LegendreEllipticIntegral::bigE);

  792.         // integrate part between 0 and π/2
  793.         final double cM1        = ar.csc2 - 1.0;
  794.         final double cMm        = ar.csc2 - m;
  795.         final double incomplete = CarlsonEllipticIntegral.rF(cM1, cMm, ar.csc2) -
  796.                                   CarlsonEllipticIntegral.rD(cM1, cMm, ar.csc2) * (m / 3);

  797.         // combine complete and incomplete parts
  798.         return ar.negate ? ar.complete - incomplete : ar.complete + incomplete;

  799.     }

  800.     /** Get the incomplete elliptic integral of the second kind E(φ, m).
  801.      * <p>
  802.      * The incomplete elliptic integral of the second kind E(φ, m) is
  803.      * \[
  804.      *    \int_0^{\phi} \sqrt{1-m \sin^2\theta} d\theta
  805.      * \]
  806.      * </p>
  807.      * <p>
  808.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  809.      * Carlson elliptic integrals}.
  810.      * </p>
  811.      * @param phi amplitude (i.e. upper bound of the integral)
  812.      * @param m parameter (m=k² where k is the elliptic modulus)
  813.      * @param <T> the type of the field elements
  814.      * @return incomplete elliptic integral of the second kind E(φ, m)
  815.      * @see #bigE(CalculusFieldElement)
  816.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheSecondKind.html">Elliptic Integrals of the Second Kind (MathWorld)</a>
  817.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  818.      */
  819.     public static <T extends CalculusFieldElement<T>> T bigE(final T phi, final T m) {

  820.         // argument reduction
  821.         final FieldArgumentReduction<T> ar = new FieldArgumentReduction<>(phi, m, LegendreEllipticIntegral::bigE);

  822.         // integrate part between 0 and π/2
  823.         final T cM1        = ar.csc2.subtract(1);
  824.         final T cMm        = ar.csc2.subtract(m);
  825.         final T incomplete = CarlsonEllipticIntegral.rF(cM1, cMm, ar.csc2).
  826.                              subtract(CarlsonEllipticIntegral.rD(cM1, cMm, ar.csc2).multiply(m.divide(3)));

  827.         // combine complete and incomplete parts
  828.         return ar.negate ? ar.complete.subtract(incomplete) : ar.complete.add(incomplete);

  829.     }

  830.     /** Get the incomplete elliptic integral of the second kind E(φ, m).
  831.      * <p>
  832.      * <em>
  833.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  834.      * are considered experimental for now, they have known issues.
  835.      * </em>
  836.      * </p>
  837.      * <p>
  838.      * The incomplete elliptic integral of the second kind E(φ, m) is
  839.      * \[
  840.      *    \int_0^{\phi} \sqrt{1-m \sin^2\theta} d\theta
  841.      * \]
  842.      * </p>
  843.      * <p>
  844.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  845.      * Carlson elliptic integrals}.
  846.      * </p>
  847.      * @param phi amplitude (i.e. upper bound of the integral)
  848.      * @param m parameter (m=k² where k is the elliptic modulus)
  849.      * @return incomplete elliptic integral of the second kind E(φ, m)
  850.      * @see #bigE(Complex)
  851.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheSecondKind.html">Elliptic Integrals of the Second Kind (MathWorld)</a>
  852.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  853.      */
  854.     public static Complex bigE(final Complex phi, final Complex m) {

  855.         // argument reduction
  856.         final FieldArgumentReduction<Complex> ar = new FieldArgumentReduction<>(phi, m, LegendreEllipticIntegral::bigE);

  857.         // integrate part between 0 and π/2
  858.         final Complex cM1        = ar.csc2.subtract(1);
  859.         final Complex cMm        = ar.csc2.subtract(m);
  860.         final Complex incomplete = CarlsonEllipticIntegral.rF(cM1, cMm, ar.csc2).
  861.                                    subtract(CarlsonEllipticIntegral.rD(cM1, cMm, ar.csc2).multiply(m.divide(3)));

  862.         // combine complete and incomplete parts
  863.         return ar.negate ? ar.complete.subtract(incomplete) : ar.complete.add(incomplete);

  864.     }

  865.     /** Get the incomplete elliptic integral of the second kind E(φ, m) using numerical integration.
  866.      * <p>
  867.      * <em>
  868.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  869.      * are considered experimental for now, they have known issues.
  870.      * </em>
  871.      * </p>
  872.      * <p>
  873.      * The incomplete elliptic integral of the second kind E(φ, m) is
  874.      * \[
  875.      *    \int_0^{\phi} \sqrt{1-m \sin^2\theta} d\theta
  876.      * \]
  877.      * </p>
  878.      * <p>
  879.      * The algorithm for evaluating the functions is based on numerical integration.
  880.      * If integration path comes too close to a pole of the integrand, then integration will fail
  881.      * with a {@link org.hipparchus.exception.MathIllegalStateException MathIllegalStateException}
  882.      * even for very large {@code maxEval}. This is normal behavior.
  883.      * </p>
  884.      * @param phi amplitude (i.e. upper bound of the integral)
  885.      * @param m parameter (m=k² where k is the elliptic modulus)
  886.      * @param integrator integrator to use
  887.      * @param maxEval maximum number of evaluations (real and imaginary
  888.      * parts are evaluated separately, so up to twice this number may be used)
  889.      * @return incomplete elliptic integral of the second kind E(φ, m)
  890.      * @see #bigE(Complex)
  891.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheSecondKind.html">Elliptic Integrals of the Second Kind (MathWorld)</a>
  892.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  893.      */
  894.     public static Complex bigE(final Complex phi, final Complex m,
  895.                                final ComplexUnivariateIntegrator integrator, final int maxEval) {
  896.         return integrator.integrate(maxEval, new Second<>(m), phi.getField().getZero(), phi);
  897.     }

  898.     /** Get the incomplete elliptic integral of the second kind E(φ, m).
  899.      * <p>
  900.      * <em>
  901.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  902.      * are considered experimental for now, they have known issues.
  903.      * </em>
  904.      * </p>
  905.      * <p>
  906.      * The incomplete elliptic integral of the second kind E(φ, m) is
  907.      * \[
  908.      *    \int_0^{\phi} \sqrt{1-m \sin^2\theta} d\theta
  909.      * \]
  910.      * </p>
  911.      * <p>
  912.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  913.      * Carlson elliptic integrals}.
  914.      * </p>
  915.      * @param phi amplitude (i.e. upper bound of the integral)
  916.      * @param m parameter (m=k² where k is the elliptic modulus)
  917.      * @param <T> the type of the field elements
  918.      * @return incomplete elliptic integral of the second kind E(φ, m)
  919.      * @see #bigE(FieldComplex)
  920.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheSecondKind.html">Elliptic Integrals of the Second Kind (MathWorld)</a>
  921.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  922.      */
  923.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> bigE(final FieldComplex<T> phi, final FieldComplex<T> m) {

  924.         // argument reduction
  925.         final FieldArgumentReduction<FieldComplex<T>> ar = new FieldArgumentReduction<>(phi, m, LegendreEllipticIntegral::bigE);

  926.         // integrate part between 0 and π/2
  927.         final FieldComplex<T> cM1        = ar.csc2.subtract(1);
  928.         final FieldComplex<T> cMm        = ar.csc2.subtract(m);
  929.         final FieldComplex<T> incomplete = CarlsonEllipticIntegral.rF(cM1, cMm, ar.csc2).
  930.                                            subtract(CarlsonEllipticIntegral.rD(cM1, cMm, ar.csc2).multiply(m.divide(3)));

  931.         // combine complete and incomplete parts
  932.         return ar.negate ? ar.complete.subtract(incomplete) : ar.complete.add(incomplete);

  933.     }

  934.     /** Get the incomplete elliptic integral of the second kind E(φ, m).
  935.      * <p>
  936.      * <em>
  937.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  938.      * are considered experimental for now, they have known issues.
  939.      * </em>
  940.      * </p>
  941.      * <p>
  942.      * The incomplete elliptic integral of the second kind E(φ, m) is
  943.      * \[
  944.      *    \int_0^{\phi} \sqrt{1-m \sin^2\theta} d\theta
  945.      * \]
  946.      * </p>
  947.      * <p>
  948.      * The algorithm for evaluating the functions is based on numerical integration.
  949.      * If integration path comes too close to a pole of the integrand, then integration will fail
  950.      * with a {@link org.hipparchus.exception.MathIllegalStateException MathIllegalStateException}
  951.      * even for very large {@code maxEval}. This is normal behavior.
  952.      * </p>
  953.      * @param phi amplitude (i.e. upper bound of the integral)
  954.      * @param m parameter (m=k² where k is the elliptic modulus)
  955.      * @param integrator integrator to use
  956.      * @param maxEval maximum number of evaluations (real and imaginary
  957.      * parts are evaluated separately, so up to twice this number may be used)
  958.      * @param <T> the type of the field elements
  959.      * @return incomplete elliptic integral of the second kind E(φ, m)
  960.      * @see #bigE(FieldComplex)
  961.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheSecondKind.html">Elliptic Integrals of the Second Kind (MathWorld)</a>
  962.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  963.      */
  964.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> bigE(final FieldComplex<T> phi, final FieldComplex<T> m,
  965.                                                                            final FieldComplexUnivariateIntegrator<T> integrator,
  966.                                                                            final int maxEval) {
  967.         return integrator.integrate(maxEval, new Second<>(m), phi.getField().getZero(), phi);
  968.     }

  969.     /** Get the incomplete elliptic integral D(φ, m) = [F(φ, m) - E(φ, m)]/m.
  970.      * <p>
  971.      * The incomplete elliptic integral D(φ, m) is
  972.      * \[
  973.      *    \int_0^{\phi} \frac{\sin^2\theta}{\sqrt{1-m \sin^2\theta}} d\theta
  974.      * \]
  975.      * </p>
  976.      * <p>
  977.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  978.      * Carlson elliptic integrals}.
  979.      * </p>
  980.      * @param phi amplitude (i.e. upper bound of the integral)
  981.      * @param m parameter (m=k² where k is the elliptic modulus)
  982.      * @return incomplete elliptic integral D(φ, m)
  983.      * @see #bigD(double)
  984.      */
  985.     public static double bigD(final double phi, final double m) {

  986.         // argument reduction
  987.         final DoubleArgumentReduction ar = new DoubleArgumentReduction(phi, m, LegendreEllipticIntegral::bigD);

  988.         // integrate part between 0 and π/2
  989.         final double cM1        = ar.csc2 - 1.0;
  990.         final double cMm        = ar.csc2 - m;
  991.         final double incomplete = CarlsonEllipticIntegral.rD(cM1, cMm, ar.csc2) / 3;

  992.         // combine complete and incomplete parts
  993.         return ar.negate ? ar.complete - incomplete : ar.complete + incomplete;

  994.     }

  995.     /** Get the incomplete elliptic integral D(φ, m) = [F(φ, m) - E(φ, m)]/m.
  996.      * <p>
  997.      * The incomplete elliptic integral D(φ, m) is
  998.      * \[
  999.      *    \int_0^{\phi} \frac{\sin^2\theta}{\sqrt{1-m \sin^2\theta}} d\theta
  1000.      * \]
  1001.      * </p>
  1002.      * <p>
  1003.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  1004.      * Carlson elliptic integrals}.
  1005.      * </p>
  1006.      * @param phi amplitude (i.e. upper bound of the integral)
  1007.      * @param m parameter (m=k² where k is the elliptic modulus)
  1008.      * @param <T> the type of the field elements
  1009.      * @return incomplete elliptic integral D(φ, m)
  1010.      * @see #bigD(CalculusFieldElement)
  1011.      */
  1012.     public static <T extends CalculusFieldElement<T>> T bigD(final T phi, final T m) {

  1013.         // argument reduction
  1014.         final FieldArgumentReduction<T> ar = new FieldArgumentReduction<>(phi, m, LegendreEllipticIntegral::bigD);

  1015.         // integrate part between 0 and π/2
  1016.         final T cM1        = ar.csc2.subtract(1);
  1017.         final T cMm        = ar.csc2.subtract(m);
  1018.         final T incomplete = CarlsonEllipticIntegral.rD(cM1, cMm, ar.csc2).divide(3);

  1019.         // combine complete and incomplete parts
  1020.         return ar.negate ? ar.complete.subtract(incomplete) : ar.complete.add(incomplete);

  1021.     }

  1022.     /** Get the incomplete elliptic integral D(φ, m) = [F(φ, m) - E(φ, m)]/m.
  1023.      * <p>
  1024.      * <em>
  1025.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  1026.      * are considered experimental for now, they have known issues.
  1027.      * </em>
  1028.      * </p>
  1029.      * <p>
  1030.      * The incomplete elliptic integral D(φ, m) is
  1031.      * \[
  1032.      *    \int_0^{\phi} \frac{\sin^2\theta}{\sqrt{1-m \sin^2\theta}} d\theta
  1033.      * \]
  1034.      * </p>
  1035.      * <p>
  1036.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  1037.      * Carlson elliptic integrals}.
  1038.      * </p>
  1039.      * @param phi amplitude (i.e. upper bound of the integral)
  1040.      * @param m parameter (m=k² where k is the elliptic modulus)
  1041.      * @return incomplete elliptic integral D(φ, m)
  1042.      * @see #bigD(Complex)
  1043.      */
  1044.     public static Complex bigD(final Complex phi, final Complex m) {

  1045.         // argument reduction
  1046.         final FieldArgumentReduction<Complex> ar = new FieldArgumentReduction<>(phi, m, LegendreEllipticIntegral::bigD);

  1047.         // integrate part between 0 and π/2
  1048.         final Complex cM1        = ar.csc2.subtract(1);
  1049.         final Complex cMm        = ar.csc2.subtract(m);
  1050.         final Complex incomplete = CarlsonEllipticIntegral.rD(cM1, cMm, ar.csc2).divide(3);

  1051.         // combine complete and incomplete parts
  1052.         return ar.negate ? ar.complete.subtract(incomplete) : ar.complete.add(incomplete);

  1053.     }

  1054.     /** Get the incomplete elliptic integral D(φ, m) = [F(φ, m) - E(φ, m)]/m.
  1055.      * <p>
  1056.      * <em>
  1057.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  1058.      * are considered experimental for now, they have known issues.
  1059.      * </em>
  1060.      * </p>
  1061.      * <p>
  1062.      * The incomplete elliptic integral D(φ, m) is
  1063.      * \[
  1064.      *    \int_0^{\phi} \frac{\sin^2\theta}{\sqrt{1-m \sin^2\theta}} d\theta
  1065.      * \]
  1066.      * </p>
  1067.      * <p>
  1068.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  1069.      * Carlson elliptic integrals}.
  1070.      * </p>
  1071.      * @param phi amplitude (i.e. upper bound of the integral)
  1072.      * @param m parameter (m=k² where k is the elliptic modulus)
  1073.      * @param <T> the type of the field elements
  1074.      * @return incomplete elliptic integral D(φ, m)
  1075.      * @see #bigD(CalculusFieldElement)
  1076.      */
  1077.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> bigD(final FieldComplex<T> phi, final FieldComplex<T> m) {

  1078.         // argument reduction
  1079.         final FieldArgumentReduction<FieldComplex<T>> ar = new FieldArgumentReduction<>(phi, m, LegendreEllipticIntegral::bigD);

  1080.         // integrate part between 0 and π/2
  1081.         final FieldComplex<T> cM1        = ar.csc2.subtract(1);
  1082.         final FieldComplex<T> cMm        = ar.csc2.subtract(m);
  1083.         final FieldComplex<T> incomplete = CarlsonEllipticIntegral.rD(cM1, cMm, ar.csc2).divide(3);

  1084.         // combine complete and incomplete parts
  1085.         return ar.negate ? ar.complete.subtract(incomplete) : ar.complete.add(incomplete);

  1086.     }

  1087.     /** Get the incomplete elliptic integral of the third kind Π(n, φ, m).
  1088.      * <p>
  1089.      * The incomplete elliptic integral of the third kind Π(n, φ, m) is
  1090.      * \[
  1091.      *    \int_0^{\phi} \frac{d\theta}{\sqrt{1-m \sin^2\theta}(1-n \sin^2\theta)}
  1092.      * \]
  1093.      * </p>
  1094.      * <p>
  1095.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  1096.      * Carlson elliptic integrals}.
  1097.      * </p>
  1098.      * @param n elliptic characteristic
  1099.      * @param phi amplitude (i.e. upper bound of the integral)
  1100.      * @param m parameter (m=k² where k is the elliptic modulus)
  1101.      * @return incomplete elliptic integral of the third kind Π(n, φ, m)
  1102.      * @see #bigPi(double, double)
  1103.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheThirdKind.html">Elliptic Integrals of the Third Kind (MathWorld)</a>
  1104.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  1105.      */
  1106.     public static double bigPi(final double n, final double phi, final double m) {

  1107.         // argument reduction
  1108.         final DoubleArgumentReduction ar = new DoubleArgumentReduction(phi, m, parameter -> bigPi(n, parameter));

  1109.         // integrate part between 0 and π/2
  1110.         final double cM1        = ar.csc2 - 1.0;
  1111.         final double cMm        = ar.csc2 - m;
  1112.         final double cMn        = ar.csc2 - n;
  1113.         final double delta      = n * (m - n) * (n - 1);
  1114.         final double incomplete = CarlsonEllipticIntegral.rF(cM1, cMm, ar.csc2) +
  1115.                                   CarlsonEllipticIntegral.rJ(cM1, cMm, ar.csc2, cMn, delta) * n / 3;

  1116.         // combine complete and incomplete parts
  1117.         return ar.negate ? ar.complete - incomplete : ar.complete + incomplete;

  1118.     }

  1119.     /** Get the incomplete elliptic integral of the third kind Π(n, φ, m).
  1120.      * <p>
  1121.      * The incomplete elliptic integral of the third kind Π(n, φ, m) is
  1122.      * \[
  1123.      *    \int_0^{\phi} \frac{d\theta}{\sqrt{1-m \sin^2\theta}(1-n \sin^2\theta)}
  1124.      * \]
  1125.      * </p>
  1126.      * <p>
  1127.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  1128.      * Carlson elliptic integrals}.
  1129.      * </p>
  1130.      * @param n elliptic characteristic
  1131.      * @param phi amplitude (i.e. upper bound of the integral)
  1132.      * @param m parameter (m=k² where k is the elliptic modulus)
  1133.      * @param <T> the type of the field elements
  1134.      * @return incomplete elliptic integral of the third kind Π(n, φ, m)
  1135.      * @see #bigPi(CalculusFieldElement, CalculusFieldElement)
  1136.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheThirdKind.html">Elliptic Integrals of the Third Kind (MathWorld)</a>
  1137.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  1138.      */
  1139.     public static <T extends CalculusFieldElement<T>> T bigPi(final T n, final T phi, final T m) {

  1140.         // argument reduction
  1141.         final FieldArgumentReduction<T> ar = new FieldArgumentReduction<>(phi, m, parameter -> bigPi(n, parameter));

  1142.         // integrate part between 0 and π/2
  1143.         final T cM1        = ar.csc2.subtract(1);
  1144.         final T cMm        = ar.csc2.subtract(m);
  1145.         final T cMn        = ar.csc2.subtract(n);
  1146.         final T delta      = n.multiply(m.subtract(n)).multiply(n.subtract(1));
  1147.         final T incomplete = CarlsonEllipticIntegral.rF(cM1, cMm, ar.csc2).
  1148.                              add(CarlsonEllipticIntegral.rJ(cM1, cMm, ar.csc2, cMn, delta).multiply(n).divide(3));

  1149.         // combine complete and incomplete parts
  1150.         return ar.negate ? ar.complete.subtract(incomplete) : ar.complete.add(incomplete);

  1151.     }

  1152.     /** Get the incomplete elliptic integral of the third kind Π(n, φ, m).
  1153.      * <p>
  1154.      * <em>
  1155.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  1156.      * are considered experimental for now, they have known issues.
  1157.      * </em>
  1158.      * </p>
  1159.      * <p>
  1160.      * The incomplete elliptic integral of the third kind Π(n, φ, m) is
  1161.      * \[
  1162.      *    \int_0^{\phi} \frac{d\theta}{\sqrt{1-m \sin^2\theta}(1-n \sin^2\theta)}
  1163.      * \]
  1164.      * </p>
  1165.      * <p>
  1166.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  1167.      * Carlson elliptic integrals}.
  1168.      * </p>
  1169.      * @param n elliptic characteristic
  1170.      * @param phi amplitude (i.e. upper bound of the integral)
  1171.      * @param m parameter (m=k² where k is the elliptic modulus)
  1172.      * @return incomplete elliptic integral of the third kind Π(n, φ, m)
  1173.      * @see #bigPi(Complex, Complex)
  1174.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheThirdKind.html">Elliptic Integrals of the Third Kind (MathWorld)</a>
  1175.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  1176.      */
  1177.     public static Complex bigPi(final Complex n, final Complex phi, final Complex m) {

  1178.         // argument reduction
  1179.         final FieldArgumentReduction<Complex> ar = new FieldArgumentReduction<>(phi, m, parameter -> bigPi(n, parameter));

  1180.         // integrate part between 0 and π/2
  1181.         final Complex cM1        = ar.csc2.subtract(1);
  1182.         final Complex cMm        = ar.csc2.subtract(m);
  1183.         final Complex cMn        = ar.csc2.subtract(n);
  1184.         final Complex delta      = n.multiply(m.subtract(n)).multiply(n.subtract(1));
  1185.         final Complex incomplete = CarlsonEllipticIntegral.rF(cM1, cMm, ar.csc2).
  1186.                                    add(CarlsonEllipticIntegral.rJ(cM1, cMm, ar.csc2, cMn, delta).multiply(n).divide(3));

  1187.         // combine complete and incomplete parts
  1188.         return ar.negate ? ar.complete.subtract(incomplete) : ar.complete.add(incomplete);

  1189.     }

  1190.     /** Get the incomplete elliptic integral of the third kind Π(n, φ, m) using numerical integration.
  1191.      * <p>
  1192.      * <em>
  1193.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  1194.      * are considered experimental for now, they have known issues.
  1195.      * </em>
  1196.      * </p>
  1197.      * <p>
  1198.      * The incomplete elliptic integral of the third kind Π(n, φ, m) is
  1199.      * \[
  1200.      *    \int_0^{\phi} \frac{d\theta}{\sqrt{1-m \sin^2\theta}(1-n \sin^2\theta)}
  1201.      * \]
  1202.      * </p>
  1203.      * <p>
  1204.      * The algorithm for evaluating the functions is based on numerical integration.
  1205.      * If integration path comes too close to a pole of the integrand, then integration will fail
  1206.      * with a {@link org.hipparchus.exception.MathIllegalStateException MathIllegalStateException}
  1207.      * even for very large {@code maxEval}. This is normal behavior.
  1208.      * </p>
  1209.      * @param n elliptic characteristic
  1210.      * @param phi amplitude (i.e. upper bound of the integral)
  1211.      * @param m parameter (m=k² where k is the elliptic modulus)
  1212.      * @param integrator integrator to use
  1213.      * @param maxEval maximum number of evaluations (real and imaginary
  1214.      * @return incomplete elliptic integral of the third kind Π(n, φ, m)
  1215.      * @see #bigPi(Complex, Complex)
  1216.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheThirdKind.html">Elliptic Integrals of the Third Kind (MathWorld)</a>
  1217.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  1218.      */
  1219.     public static Complex bigPi(final Complex n, final Complex phi, final Complex m,
  1220.                                 final ComplexUnivariateIntegrator integrator, final int maxEval) {
  1221.          return integrator.integrate(maxEval, new Third<>(n, m), phi.getField().getZero(), phi);
  1222.     }

  1223.     /** Get the incomplete elliptic integral of the third kind Π(n, φ, m).
  1224.      * <p>
  1225.      * <em>
  1226.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  1227.      * are considered experimental for now, they have known issues.
  1228.      * </em>
  1229.      * </p>
  1230.      * <p>
  1231.      * The incomplete elliptic integral of the third kind Π(n, φ, m) is
  1232.      * \[
  1233.      *    \int_0^{\phi} \frac{d\theta}{\sqrt{1-m \sin^2\theta}(1-n \sin^2\theta)}
  1234.      * \]
  1235.      * </p>
  1236.      * <p>
  1237.      * The algorithm for evaluating the functions is based on {@link CarlsonEllipticIntegral
  1238.      * Carlson elliptic integrals}.
  1239.      * </p>
  1240.      * @param n elliptic characteristic
  1241.      * @param phi amplitude (i.e. upper bound of the integral)
  1242.      * @param m parameter (m=k² where k is the elliptic modulus)
  1243.      * @param <T> the type of the field elements
  1244.      * @return incomplete elliptic integral of the third kind Π(n, φ, m)
  1245.      * @see #bigPi(FieldComplex, FieldComplex)
  1246.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheThirdKind.html">Elliptic Integrals of the Third Kind (MathWorld)</a>
  1247.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  1248.      */
  1249.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> bigPi(final FieldComplex<T> n,
  1250.                                                                             final FieldComplex<T> phi,
  1251.                                                                             final FieldComplex<T> m) {

  1252.         // argument reduction
  1253.         final FieldArgumentReduction<FieldComplex<T>> ar = new FieldArgumentReduction<>(phi, m, parameter -> bigPi(n, parameter));

  1254.         // integrate part between 0 and π/2
  1255.         final FieldComplex<T> cM1        = ar.csc2.subtract(1);
  1256.         final FieldComplex<T> cMm        = ar.csc2.subtract(m);
  1257.         final FieldComplex<T> cMn        = ar.csc2.subtract(n);
  1258.         final FieldComplex<T> delta      = n.multiply(m.subtract(n)).multiply(n.subtract(1));
  1259.         final FieldComplex<T> incomplete = CarlsonEllipticIntegral.rF(cM1, cMm, ar.csc2).
  1260.                                            add(CarlsonEllipticIntegral.rJ(cM1, cMm, ar.csc2, cMn, delta).multiply(n).divide(3));

  1261.         // combine complete and incomplete parts
  1262.         return ar.negate ? ar.complete.subtract(incomplete) : ar.complete.add(incomplete);

  1263.     }

  1264.     /** Get the incomplete elliptic integral of the third kind Π(n, φ, m).
  1265.      * <p>
  1266.      * <em>
  1267.      * BEWARE! Elliptic integrals for complex numbers in the incomplete case
  1268.      * are considered experimental for now, they have known issues.
  1269.      * </em>
  1270.      * </p>
  1271.      * <p>
  1272.      * The incomplete elliptic integral of the third kind Π(n, φ, m) is
  1273.      * \[
  1274.      *    \int_0^{\phi} \frac{d\theta}{\sqrt{1-m \sin^2\theta}(1-n \sin^2\theta)}
  1275.      * \]
  1276.      * </p>
  1277.      * <p>
  1278.      * The algorithm for evaluating the functions is based on numerical integration.
  1279.      * If integration path comes too close to a pole of the integrand, then integration will fail
  1280.      * with a {@link org.hipparchus.exception.MathIllegalStateException MathIllegalStateException}
  1281.      * even for very large {@code maxEval}. This is normal behavior.
  1282.      * </p>
  1283.      * @param n elliptic characteristic
  1284.      * @param phi amplitude (i.e. upper bound of the integral)
  1285.      * @param m parameter (m=k² where k is the elliptic modulus)
  1286.      * @param integrator integrator to use
  1287.      * @param maxEval maximum number of evaluations (real and imaginary
  1288.      * parts are evaluated separately, so up to twice this number may be used)
  1289.      * @param <T> the type of the field elements
  1290.      * @return incomplete elliptic integral of the third kind Π(n, φ, m)
  1291.      * @see #bigPi(FieldComplex, FieldComplex)
  1292.      * @see <a href="https://mathworld.wolfram.com/EllipticIntegraloftheThirdKind.html">Elliptic Integrals of the Third Kind (MathWorld)</a>
  1293.      * @see <a href="https://en.wikipedia.org/wiki/Elliptic_integral">Elliptic Integrals (Wikipedia)</a>
  1294.      */
  1295.     public static <T extends CalculusFieldElement<T>> FieldComplex<T> bigPi(final FieldComplex<T> n,
  1296.                                                                             final FieldComplex<T> phi,
  1297.                                                                             final FieldComplex<T> m,
  1298.                                                                             final FieldComplexUnivariateIntegrator<T> integrator,
  1299.                                                                             final int maxEval) {
  1300.         return integrator.integrate(maxEval, new Third<>(n, m), phi.getField().getZero(), phi);
  1301.     }

  1302.     /** Argument reduction for an incomplete integral. */
  1303.     private static class DoubleArgumentReduction {

  1304.         /** Complete part. */
  1305.         private final double complete;

  1306.         /** Squared cosecant of the Jacobi amplitude. */
  1307.         private final double csc2;

  1308.         /** Indicator for negated Jacobi amplitude. */
  1309.         private boolean negate;

  1310.         /** Simple constructor.
  1311.          * @param phi amplitude (i.e. upper bound of the integral)
  1312.          * @param m parameter (m=k² where k is the elliptic modulus)
  1313.          * @param integral provider for complete integral
  1314.          */
  1315.         DoubleArgumentReduction(final double phi, final double m, final DoubleFunction<Double> integral) {
  1316.             final double sin = FastMath.sin(phi);
  1317.             final int    p   = (int) FastMath.rint(phi / FastMath.PI);
  1318.             complete         = p == 0 ? 0 : integral.apply(m) * 2 * p;
  1319.             negate           = sin < 0 ^ (p & 0x1) == 1;
  1320.             csc2             = 1.0 / (sin * sin);
  1321.         }

  1322.     }

  1323.     /** Argument reduction for an incomplete integral.
  1324.      * @param <T> type fo the field elements
  1325.      */
  1326.     private static class FieldArgumentReduction<T extends CalculusFieldElement<T>> {

  1327.         /** Complete part. */
  1328.         private final T complete;

  1329.         /** Squared cosecant of the Jacobi amplitude. */
  1330.         private final T csc2;

  1331.         /** Indicator for negated Jacobi amplitude. */
  1332.         private boolean negate;

  1333.         /** Simple constructor.
  1334.          * @param phi amplitude (i.e. upper bound of the integral)
  1335.          * @param m parameter (m=k² where k is the elliptic modulus)
  1336.          * @param integral provider for complete integral
  1337.          */
  1338.         FieldArgumentReduction(final T phi, final T m, final Function<T, T> integral) {
  1339.             final T   sin = FastMath.sin(phi);
  1340.             final int p   = (int) FastMath.rint(phi.getReal() / FastMath.PI);
  1341.             complete      = p == 0 ? phi.getField().getZero() : integral.apply(m).multiply(2 * p);
  1342.             negate        = sin.getReal() < 0 ^ (p & 0x1) == 1;
  1343.             csc2          = sin.multiply(sin).reciprocal();
  1344.         }

  1345.     }

  1346.     /** Integrand for elliptic integrals of the first kind.
  1347.      * @param <T> type of the field elements
  1348.      */
  1349.     private static class First<T extends CalculusFieldElement<T>> implements CalculusFieldUnivariateFunction<T> {

  1350.         /** Parameter. */
  1351.         private final T m;

  1352.         /** Simple constructor.
  1353.          * @param m parameter (m=k² where k is the elliptic modulus)
  1354.          */
  1355.         First(final T m) {
  1356.             this.m = m;
  1357.         }

  1358.         /** {@inheritDoc} */
  1359.         @Override
  1360.         public T value(final T theta) {
  1361.             final T sin  = theta.sin();
  1362.             final T sin2 = sin.multiply(sin);
  1363.             return sin2.multiply(m).negate().add(1).sqrt().reciprocal();
  1364.         }

  1365.     }

  1366.     /** Integrand for elliptic integrals of the second kind.
  1367.      * @param <T> type of the field elements
  1368.      */
  1369.     private static class Second<T extends CalculusFieldElement<T>> implements CalculusFieldUnivariateFunction<T> {

  1370.         /** Parameter. */
  1371.         private final T m;

  1372.         /** Simple constructor.
  1373.          * @param m parameter (m=k² where k is the elliptic modulus)
  1374.          */
  1375.         Second(final T m) {
  1376.             this.m = m;
  1377.         }

  1378.         /** {@inheritDoc} */
  1379.         @Override
  1380.         public T value(final T theta) {
  1381.             final T sin = theta.sin();
  1382.             final T sin2 = sin.multiply(sin);
  1383.             return sin2.multiply(m).negate().add(1).sqrt();
  1384.         }

  1385.     }

  1386.     /** Integrand for elliptic integrals of the third kind.
  1387.      * @param <T> type of the field elements
  1388.      */
  1389.     private static class Third<T extends CalculusFieldElement<T>> implements CalculusFieldUnivariateFunction<T> {

  1390.         /** Elliptic characteristic. */
  1391.         private final T n;

  1392.         /** Parameter. */
  1393.         private final T m;

  1394.         /** Simple constructor.
  1395.          * @param n elliptic characteristic
  1396.          * @param m parameter (m=k² where k is the elliptic modulus)
  1397.          */
  1398.         Third(final T n, final T m) {
  1399.             this.n = n;
  1400.             this.m = m;
  1401.         }

  1402.         /** {@inheritDoc} */
  1403.         @Override
  1404.         public T value(final T theta) {
  1405.             final T sin  = theta.sin();
  1406.             final T sin2 = sin.multiply(sin);
  1407.             final T d1   = sin2.multiply(m).negate().add(1).sqrt();
  1408.             final T da   = sin2.multiply(n).negate().add(1);
  1409.             return d1.multiply(da).reciprocal();
  1410.         }

  1411.     }
  1412. }