EVOLUTION-MANAGER
Edit File: Special.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Special Functions of Mathematics</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for Special {base}"><tr><td>Special {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Special Functions of Mathematics</h2> <h3>Description</h3> <p>Special mathematical functions related to the beta and gamma functions. </p> <h3>Usage</h3> <pre> beta(a, b) lbeta(a, b) gamma(x) lgamma(x) psigamma(x, deriv = 0) digamma(x) trigamma(x) choose(n, k) lchoose(n, k) factorial(x) lfactorial(x) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>a, b</code></td> <td> <p>non-negative numeric vectors.</p> </td></tr> <tr valign="top"><td><code>x, n</code></td> <td> <p>numeric vectors.</p> </td></tr> <tr valign="top"><td><code>k, deriv</code></td> <td> <p>integer vectors.</p> </td></tr> </table> <h3>Details</h3> <p>The functions <code>beta</code> and <code>lbeta</code> return the beta function and the natural logarithm of the beta function, </p> <p style="text-align: center;"><i>B(a,b) = Γ(a)Γ(b)/Γ(a+b).</i></p> <p>The formal definition is </p> <p style="text-align: center;"><i>integral_0^1 t^(a-1) (1-t)^(b-1) dt</i></p> <p>(Abramowitz and Stegun section 6.2.1, page 258). Note that it is only defined in <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> for non-negative <code>a</code> and <code>b</code>, and is infinite if either is zero. </p> <p>The functions <code>gamma</code> and <code>lgamma</code> return the gamma function <i>Γ(x)</i> and the natural logarithm of <em>the absolute value of</em> the gamma function. The gamma function is defined by (Abramowitz and Stegun section 6.1.1, page 255) </p> <p style="text-align: center;"><i>Γ(x) = integral_0^Inf t^(x-1) exp(-t) dt</i></p> <p>for all real <code>x</code> except zero and negative integers (when <code>NaN</code> is returned). There will be a warning on possible loss of precision for values which are too close (within about <i>1e-8</i>) to a negative integer less than <span class="samp">-10</span>. </p> <p><code>factorial(x)</code> (<i>x!</i> for non-negative integer <code>x</code>) is defined to be <code>gamma(x+1)</code> and <code>lfactorial</code> to be <code>lgamma(x+1)</code>. </p> <p>The functions <code>digamma</code> and <code>trigamma</code> return the first and second derivatives of the logarithm of the gamma function. <code>psigamma(x, deriv)</code> (<code>deriv >= 0</code>) computes the <code>deriv</code>-th derivative of <i>ψ(x)</i>. </p> <p style="text-align: center;"><i>digamma(x) = ψ(x) = d/dx{ln Γ(x)} = Γ'(x) / Γ(x)</i></p> <p><i>ψ</i> and its derivatives, the <code>psigamma()</code> functions, are often called the ‘polygamma’ functions, e.g. in Abramowitz and Stegun (section 6.4.1, page 260); and higher derivatives (<code>deriv = 2:4</code>) have occasionally been called ‘tetragamma’, ‘pentagamma’, and ‘hexagamma’. </p> <p>The functions <code>choose</code> and <code>lchoose</code> return binomial coefficients and the logarithms of their absolute values. Note that <code>choose(n, k)</code> is defined for all real numbers <i>n</i> and integer <i>k</i>. For <i>k ≥ 1</i> it is defined as <i>n(n-1)…(n-k+1) / k!</i>, as <i>1</i> for <i>k = 0</i> and as <i>0</i> for negative <i>k</i>. Non-integer values of <code>k</code> are rounded to an integer, with a warning. <br /> <code>choose(*, k)</code> uses direct arithmetic (instead of <code>[l]gamma</code> calls) for small <code>k</code>, for speed and accuracy reasons. Note the function <code><a href="../../utils/html/combn.html">combn</a></code> (package <span class="pkg">utils</span>) for enumeration of all possible combinations. </p> <p>The <code>gamma</code>, <code>lgamma</code>, <code>digamma</code> and <code>trigamma</code> functions are <a href="InternalMethods.html">internal generic</a> <a href="Primitive.html">primitive</a> functions: methods can be defined for them individually or via the <code><a href="groupGeneric.html">Math</a></code> group generic. </p> <h3>Source</h3> <p><code>gamma</code>, <code>lgamma</code>, <code>beta</code> and <code>lbeta</code> are based on C translations of Fortran subroutines by W. Fullerton of Los Alamos Scientific Laboratory (now available as part of SLATEC). </p> <p><code>digamma</code>, <code>trigamma</code> and <code>psigamma</code> are based on </p> <p>Amos, D. E. (1983). A portable Fortran subroutine for derivatives of the psi function, Algorithm 610, <em>ACM Transactions on Mathematical Software</em> <b>9(4)</b>, 494–502. </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. (For <code>gamma</code> and <code>lgamma</code>.) </p> <p>Abramowitz, M. and Stegun, I. A. (1972) <em>Handbook of Mathematical Functions</em>. New York: Dover. <a href="https://en.wikipedia.org/wiki/Abramowitz_and_Stegun">https://en.wikipedia.org/wiki/Abramowitz_and_Stegun</a> provides links to the full text which is in public domain.<br /> Chapter 6: Gamma and Related Functions. </p> <h3>See Also</h3> <p><code><a href="Arithmetic.html">Arithmetic</a></code> for simple, <code><a href="MathFun.html">sqrt</a></code> for miscellaneous mathematical functions and <code><a href="Bessel.html">Bessel</a></code> for the real Bessel functions. </p> <p>For the incomplete gamma function see <code><a href="../../stats/html/GammaDist.html">pgamma</a></code>. </p> <h3>Examples</h3> <pre> require(graphics) choose(5, 2) for (n in 0:10) print(choose(n, k = 0:n)) factorial(100) lfactorial(10000) ## gamma has 1st order poles at 0, -1, -2, ... ## this will generate loss of precision warnings, so turn off op <- options("warn") options(warn = -1) x <- sort(c(seq(-3, 4, length.out = 201), outer(0:-3, (-1:1)*1e-6, "+"))) plot(x, gamma(x), ylim = c(-20,20), col = "red", type = "l", lwd = 2, main = expression(Gamma(x))) abline(h = 0, v = -3:0, lty = 3, col = "midnightblue") options(op) x <- seq(0.1, 4, length.out = 201); dx <- diff(x)[1] par(mfrow = c(2, 3)) for (ch in c("", "l","di","tri","tetra","penta")) { is.deriv <- nchar(ch) >= 2 nm <- paste0(ch, "gamma") if (is.deriv) { dy <- diff(y) / dx # finite difference der <- which(ch == c("di","tri","tetra","penta")) - 1 nm2 <- paste0("psigamma(*, deriv = ", der,")") nm <- if(der >= 2) nm2 else paste(nm, nm2, sep = " ==\n") y <- psigamma(x, deriv = der) } else { y <- get(nm)(x) } plot(x, y, type = "l", main = nm, col = "red") abline(h = 0, col = "lightgray") if (is.deriv) lines(x[-1], dy, col = "blue", lty = 2) } par(mfrow = c(1, 1)) ## "Extended" Pascal triangle: fN <- function(n) formatC(n, width=2) for (n in -4:10) { cat(fN(n),":", fN(choose(n, k = -2:max(3, n+2)))) cat("\n") } ## R code version of choose() [simplistic; warning for k < 0]: mychoose <- function(r, k) ifelse(k <= 0, (k == 0), sapply(k, function(k) prod(r:(r-k+1))) / factorial(k)) k <- -1:6 cbind(k = k, choose(1/2, k), mychoose(1/2, k)) ## Binomial theorem for n = 1/2 ; ## sqrt(1+x) = (1+x)^(1/2) = sum_{k=0}^Inf choose(1/2, k) * x^k : k <- 0:10 # 10 is sufficient for ~ 9 digit precision: sqrt(1.25) sum(choose(1/2, k)* .25^k) </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>