IncompleteGamma.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) 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 ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.numbers.gamma;

  18. /**
  19.  * <a href="https://mathworld.wolfram.com/IncompleteGammaFunction.html">
  20.  * Incomplete Gamma functions</a>.
  21.  *
  22.  * <p>By definition, the lower and upper incomplete gamma functions satisfy:
  23.  *
  24.  * <p>\[ \Gamma(a) = \gamma(a, x) + \Gamma(a, x) \]
  25.  *
  26.  * <p>This code has been adapted from the <a href="https://www.boost.org/">Boost</a>
  27.  * {@code c++} implementation {@code <boost/math/special_functions/gamma.hpp>}.
  28.  *
  29.  * @see
  30.  * <a href="https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/math_toolkit/sf_gamma/igamma.html">
  31.  * Boost C++ Incomplete Gamma functions</a>
  32.  * @since 1.1
  33.  */
  34. public final class IncompleteGamma {
  35.     /** No instances. */
  36.     private IncompleteGamma() {}

  37.     /**
  38.      * <a href="http://mathworld.wolfram.com/IncompleteGammaFunction.html">
  39.      * Lower incomplete Gamma function</a> \( \gamma(a, x) \).
  40.      *
  41.      * <p>\[ \gamma(a,x) = \int_0^x t^{a-1}\,e^{-t}\,dt \]
  42.      * @since 1.1
  43.      */
  44.     public static final class Lower {
  45.         /** No instances. */
  46.         private Lower() {}

  47.         /**
  48.          * Computes the lower incomplete gamma function \( \gamma(a, x) \).
  49.          *
  50.          * @param a Argument.
  51.          * @param x Argument.
  52.          * @return \( \gamma(a, x) \).
  53.          * @throws ArithmeticException if the series evaluation fails to converge.
  54.          */
  55.         public static double value(double a,
  56.                                    double x) {
  57.             return BoostGamma.tgammaLower(a, x);
  58.         }

  59.         /**
  60.          * Computes the lower incomplete gamma function \( \gamma(a, x) \).
  61.          *
  62.          * @param a Argument.
  63.          * @param x Argument.
  64.          * @param epsilon Tolerance in series evaluation.
  65.          * @param maxIterations Maximum number of iterations in series evaluation.
  66.          * @return \( \gamma(a, x) \).
  67.          * @throws ArithmeticException if the series evaluation fails to converge.
  68.          */
  69.         public static double value(final double a,
  70.                                    double x,
  71.                                    double epsilon,
  72.                                    int maxIterations) {
  73.             return BoostGamma.tgammaLower(a, x, new Policy(epsilon, maxIterations));
  74.         }
  75.     }

  76.     /**
  77.      * <a href="http://mathworld.wolfram.com/IncompleteGammaFunction.html">
  78.      * Upper incomplete Gamma function</a> \( \Gamma(a, x) \).
  79.      *
  80.      * <p>\[ \Gamma(a,x) = \int_x^{\infty} t^{a-1}\,e^{-t}\,dt \]
  81.      * @since 1.1
  82.      */
  83.     public static final class Upper {
  84.         /** No instances. */
  85.         private Upper() {}

  86.         /**
  87.          * Computes the upper incomplete gamma function \( \Gamma(a, x) \).
  88.          *
  89.          * @param a Argument.
  90.          * @param x Argument.
  91.          * @return \( \Gamma(a, x) \).
  92.          * @throws ArithmeticException if the series evaluation fails to converge.
  93.          */
  94.         public static double value(double a,
  95.                                    double x) {
  96.             return BoostGamma.tgamma(a, x);
  97.         }

  98.         /**
  99.          * Computes the upper incomplete gamma function \( \Gamma(a, x) \).
  100.          *
  101.          * @param a Argument.
  102.          * @param x Argument.
  103.          * @param epsilon Tolerance in series evaluation.
  104.          * @param maxIterations Maximum number of iterations in series evaluation.
  105.          * @return \( \Gamma(a, x) \).
  106.          * @throws ArithmeticException if the series evaluation fails to converge.
  107.          */
  108.         public static double value(double a,
  109.                                    double x,
  110.                                    double epsilon,
  111.                                    int maxIterations) {
  112.             return BoostGamma.tgamma(a, x, new Policy(epsilon, maxIterations));
  113.         }
  114.     }
  115. }