1 <?xml version="1.0" encoding="UTF-8"?>
3 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) 2008 - INRIA
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
6 * Copyright (C) 2020 - Samuel GOUGEON
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
16 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
17 xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns4="http://www.w3.org/1999/xhtml"
18 xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org"
19 xml:id="gamma" xml:lang="en">
21 <refname>gamma</refname>
22 <refpurpose>gamma function, complete or incomplete normalized</refpurpose>
30 y = gamma(x, .., "upper")
34 <title>Arguments</title>
39 array of positive or negative real numbers
41 <literal>gamma(u)</literal> and <literal>gamma(x,…)</literal> can be overloaded
42 for complex numbers with <literal>%s_gamma_user()</literal>, and for other
43 <varname>a</varname> types with the usual overload naming rule.
50 arrays of positive real numbers.
51 If at least one input is not scalar, scalar ones are expanded to its size.
52 If several inputs are not scalar, they must have the same size.
59 array of real numbers, with the size of <varname>u</varname>
60 or of (the non-scalar) <varname>x</varname>, <varname>a</varname>,
61 or <varname>b</varname>.
68 <title>Description</title>
70 <literal>gamma(…)</literal> computes and yields the complete or incomplete gamma
71 function for each element of its input(s), in an element-wise way. The complete
72 gamma function extends the factorial one to non-integer real positive or negative
73 numbers, as <literal>gamma(u+1)=u*gamma(u)</literal>.
76 <emphasis role="bold">gamma(u)</emphasis> computes
77 <latex style="display" fontsize="18" alt="Γ(u)= ∫_0→∞ t^{u-1}.exp(-t).dt">
78 \Gamma(u)=\int_0^\infty\! t^{u-1}e^{-t}\,dt
82 <title>Incomplete normalized integrals</title>
84 <emphasis role="bold">gamma(x, a)</emphasis> computes the integral
85 <latex style="display" fontsize="18" alt="P(x,a)= ∫_0→x t^{a-1}.exp(-t).dt / Γ(a)">
86 P(x,a)=\frac{1}{\Gamma(a)}\int_0^x\! t^{a-1}e^{-t}\,dt
90 <emphasis role="bold">gamma(x, a, b)</emphasis> computes the generalized integral
91 <latex style="display" fontsize="18" alt="P(x,a,b)= ∫_0→x t^{a-1}.exp(-bt).dt . b^a / Γ(a)">
92 P(x,a,b)=\frac{b^a}{\Gamma(a)}\int_0^x\! t^{a-1}e^{-b\,t}\,dt
96 <emphasis role="bold">gamma(x, a, "upper")</emphasis> computes accurately the
97 complementary integral
98 <latex style="display" fontsize="18" alt="Q(x,a)= ∫_x→∞ t^{a-1}.exp(-t).dt / Γ(a) = 1-P(x,a)">
99 Q(x,a)=1-P(x,a)=\frac{1}{\Gamma(a)}\int_x^\infty\! t^{a-1}e^{-t}\,dt
101 even for big x and P(x,a)→1. Finally,
104 <emphasis role="bold">gamma(x, a, b, "upper")</emphasis> computes the generalized
105 complementary integral
106 <latex style="display" fontsize="18" alt="Q(x,a,b)= ∫_x→∞ t^{a-1}.exp(-bt).dt . b^a / Γ(a)">
107 Q(x,a,b)=\frac{b^a}{\Gamma(a)}\int_x^\infty\! t^{a-1}e^{-b\,t}\,dt
112 The inverse incomplete normalized gamma function can be computed with
113 <emphasis role="bold">x = cdfgam("X", a, b, y, 1-y)</emphasis>,
114 that is the <varname>x</varname> bound such that
115 <latex alt="y=∫_0→x t^{a-1}.exp(-bt).dt . b^a / Γ(a)">
116 y=\frac{b^a}{\Gamma(a)}\int_0^x\! t^{a-1}e^{-b\,t}\,dt
120 Calling <emphasis role="bold">x = cdfgam("X", a, b, z-1, z)</emphasis> with
121 <literal>z=1-y</literal> will be preferred when 0.5 < y < 1, to get a full
122 accuracy on <varname>x</varname>.
128 <title>Examples</title>
130 Gamma as the extension of the factorial function to non-integer numbers:
132 <programlisting role="example"><![CDATA[
133 [gamma(2:7) ; factorial(1:6)]
135 gamma(1.5:7) ./ gamma(0.5:6)
138 --> [gamma(2:7) ; factorial(1:6)]
140 1. 2. 6. 24. 120. 720.
141 1. 2. 6. 24. 120. 720.
145 0.8862269 1.3293404 3.323351 11.631728 52.342778 287.88528
147 --> gamma(1.5:7) ./ gamma(0.5:6)
149 0.5 1.5 2.5 3.5 4.5 5.5
153 Graph of the Gamma function around 0:
155 <programlisting role="example"><![CDATA[
157 x = linspace(a,b,40000);
160 plot2d(x, y, style=0, axesflag=5, rect=[a,-10,b,10])
161 title("$\Gamma(u)$", "fontsize",3.5)
162 xgrid(color("grey60"))
166 x = linspace(a,b,40000);
169 plot2d(x, y, style=0, axesflag=5, rect=[a,-10,b,10])
170 title("$\Gamma(u)$", "fontsize",3.5)
171 xgrid(color("grey60"))
172 gcf().axes_size = [550,400];
176 Incomplete normalized P(x,a) gamma function:
178 <programlisting role="example"><![CDATA[
181 [X, A] = ndgrid(x, a);
184 gcf().color_map = coolcolormap(100);
186 title("$P(x,a)=\frac{1}{\Gamma(a)}\int_0^x\! t^{a-1}e^{-t}\,dt$","fontsize",3.5)
187 xlabel(["" "a"], "fontsize",2)
188 ylabel("x", "fontsize",2)
189 zlabel("P(x,a)", "fontsize",2)
195 [X, A] = ndgrid(x, a);
198 gcf().color_map = coolcolormap(100);
200 title("$P(x,a)=\frac{1}{\Gamma(a)}\int_0^x\! t^{a-1}e^{-t}\,dt$","fontsize",3.5)
201 xlabel(["" "a"], "fontsize",2)
202 ylabel("x", "fontsize",2)
203 zlabel("P(x,a)", "fontsize",2)
205 gca().rotation_angles = [64 18];
209 Incomplete generalized normalized P(x,a,b) function:
211 <programlisting role="example"><![CDATA[
214 [A, B] = ndgrid(a, b);
217 gcf().color_map = parulacolormap(100);
219 title("$P(x,a,b)=\frac{b^a}{\Gamma(a)}\int_0^x\! t^{a-1}e^{-b\,t}\,dt\quad for\quad x=1$","fontsize",3.7)
220 xlabel("b", "fontsize",2)
221 ylabel("a", "fontsize",2)
223 gca().rotation_angles = [58 75];
229 [A, B] = ndgrid(a, b);
232 gcf().color_map = parulacolormap(100);
234 title("$P(x,a,b)=\frac{b^a}{\Gamma(a)}\int_0^x\! t^{a-1}e^{-b\,t}\,dt\quad for\quad x=1$","fontsize",3.7)
235 xlabel("b", "fontsize",2)
236 ylabel("a", "fontsize",2)
238 gca().rotation_angles = [58 75];
242 <refsection role="see also">
243 <title>See also</title>
244 <simplelist type="inline">
246 <link linkend="gammaln">gammaln</link>
249 <link linkend="dlgamma">dlgamma</link>
252 <link linkend="cdfgam">cdfgam</link>
255 <link linkend="factorial">factorial</link>
260 <title>History</title>
263 <revnumber>5.4.0</revnumber>
264 <revremark>Overloading allowed for list, mlist, tlist and hypermatrix types.</revremark>
267 <revnumber>6.0.2</revnumber>
271 The input can now be an hypermatrix.
274 <literal>gamma</literal> can now be overloaded for complex numbers.
280 <revnumber>6.1.1</revnumber>
281 <revremark>gamma(x,..) incomplete versions added.</revremark>