View Javadoc
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  /**
20   * <a href="https://mathworld.wolfram.com/IncompleteGammaFunction.html">
21   * Incomplete Gamma functions</a>.
22   *
23   * <p>By definition, the lower and upper incomplete gamma functions satisfy:
24   *
25   * <p>\[ \Gamma(a) = \gamma(a, x) + \Gamma(a, x) \]
26   *
27   * <p>This code has been adapted from the <a href="https://www.boost.org/">Boost</a>
28   * {@code c++} implementation {@code <boost/math/special_functions/gamma.hpp>}.
29   *
30   * @see
31   * <a href="https://www.boost.org/doc/libs/1_77_0/libs/math/doc/html/math_toolkit/sf_gamma/igamma.html">
32   * Boost C++ Incomplete Gamma functions</a>
33   * @since 1.1
34   */
35  public final class IncompleteGamma {
36      /** No instances. */
37      private IncompleteGamma() {}
38  
39      /**
40       * <a href="http://mathworld.wolfram.com/IncompleteGammaFunction.html">
41       * Lower incomplete Gamma function</a> \( \gamma(a, x) \).
42       *
43       * <p>\[ \gamma(a,x) = \int_0^x t^{a-1}\,e^{-t}\,dt \]
44       * @since 1.1
45       */
46      public static final class Lower {
47          /** No instances. */
48          private Lower() {}
49  
50          /**
51           * Computes the lower incomplete gamma function \( \gamma(a, x) \).
52           *
53           * @param a Argument.
54           * @param x Argument.
55           * @return \( \gamma(a, x) \).
56           * @throws ArithmeticException if the series evaluation fails to converge.
57           */
58          public static double value(double a,
59                                     double x) {
60              return BoostGamma.tgammaLower(a, x);
61          }
62  
63          /**
64           * Computes the lower incomplete gamma function \( \gamma(a, x) \).
65           *
66           * @param a Argument.
67           * @param x Argument.
68           * @param epsilon Tolerance in series evaluation.
69           * @param maxIterations Maximum number of iterations in series evaluation.
70           * @return \( \gamma(a, x) \).
71           * @throws ArithmeticException if the series evaluation fails to converge.
72           */
73          public static double value(final double a,
74                                     double x,
75                                     double epsilon,
76                                     int maxIterations) {
77              return BoostGamma.tgammaLower(a, x, new Policy(epsilon, maxIterations));
78          }
79      }
80  
81      /**
82       * <a href="http://mathworld.wolfram.com/IncompleteGammaFunction.html">
83       * Upper incomplete Gamma function</a> \( \Gamma(a, x) \).
84       *
85       * <p>\[ \Gamma(a,x) = \int_x^{\infty} t^{a-1}\,e^{-t}\,dt \]
86       * @since 1.1
87       */
88      public static final class Upper {
89          /** No instances. */
90          private Upper() {}
91  
92          /**
93           * Computes the upper incomplete gamma function \( \Gamma(a, x) \).
94           *
95           * @param a Argument.
96           * @param x Argument.
97           * @return \( \Gamma(a, x) \).
98           * @throws ArithmeticException if the series evaluation fails to converge.
99           */
100         public static double value(double a,
101                                    double x) {
102             return BoostGamma.tgamma(a, x);
103         }
104 
105         /**
106          * Computes the upper incomplete gamma function \( \Gamma(a, x) \).
107          *
108          * @param a Argument.
109          * @param x Argument.
110          * @param epsilon Tolerance in series evaluation.
111          * @param maxIterations Maximum number of iterations in series evaluation.
112          * @return \( \Gamma(a, x) \).
113          * @throws ArithmeticException if the series evaluation fails to converge.
114          */
115         public static double value(double a,
116                                    double x,
117                                    double epsilon,
118                                    int maxIterations) {
119             return BoostGamma.tgamma(a, x, new Policy(epsilon, maxIterations));
120         }
121     }
122 }