# Built-in Special Mathematical Functions in R

Along with other usual built-in mathematical functions, R programming language provides some special built-in mathematical functions. All these functions are vectorised. In this tutorial you will learn some built-in special mathematical functions in R and how to use these special mathematical functions in R.

## Built-in Special Mathematical Functions

R provides some special mathematical functions related to beta and gamma functions.

Function Operation Performed
factorial(x) Factorial of x i.e. x!
lfactorial(x) Logarithm of Factorial of x i.e. log(x!)
choose(n,k) Binomial coefficient
lchoose(n,k) Logarithm of Binomial Coefficient
gamma(a) Gamma function
lgamma(a) Logarithm of gamma function
digamma(x) First derivative of log of gamma function
trigamma(x) Second derivative of log of gamma function
psigamma(x,deriv=0) Polygamma function (for higher derivatives)
beta(a,b) Beta function
lbeta(a,b) Logarithm of beta function

## Examples of Special Mathematical in R

Let us discuss all these special mathematical function with examples.

### Factorial in R

The factorial of a non-negative integer $n$ is denoted by $n!$ and is defined as the product of all positive integers less than or equal to $n$. That is

$$n! = n\cdot (n-1) \cdot (n-2) \cdots 3 \cdot 2 \cdot 1$$

For example, $5! = 5\times 4\times 3\times 2\times 1= 120$

# compute factorial of 5
factorial(5)
 120

### Logarithm of factorial in R

The logarithm of factorial of a non-negative integer x in R is the natural logarithm of factorial of x.

# compute natural log of 5 factorial
lfactorial(5)
 4.787492
# 5!  = 120 so lfactorial(5) = log(120)
log(120)
 4.787492

### Combination in R

The number of combination of $n$ distinct objects, taken $r$ at a time is $\binom{n}{r}=\frac{n!}{r!(n-r)!}$.

For example, $\binom{5}{2} = \frac{5!}{2!(5-2)!}= 10$

# binomial coefficient
choose(5,2)
 10

### Logarithm of Combination in R

# logarithm of binomial coefficient
lchoose(5,2)
 2.302585

### Gamma Function in R

Gamma Function is also known as Eulerian integral of second kind. It is denoted by $\Gamma(n)$ and is defined as

 \begin{aligned} \Gamma (n) &=\int_0^\infty t^{n-1} e^{-t}\; dt\\ &=(n-1)!;\; n>0; \end{aligned}

# gamma 5 is (5-1) = 4!
gamma(5)
 24

### Logarithm of Gamma Function in R

The logarithm of gamma function is the natural logarithm of the gamma function.

# natural logarithm of gamma 5
# gamma(5) = 24 so lgamma(5) = log(24)
lgamma(5)
 3.178054
log(24)
 3.178054

### Digamma, trigamma and polygamma functions in R

#### Digamma Function in R

The digamma function is the first derivative of logarithm of gamma function.

That is,

 \begin{aligned} \text{digamma}(x) &= \psi(x)\\ &=\frac{d}{dx}(\ln \Gamma(x))\\ &=\frac{\Gamma^{\prime}(x)}{\Gamma (x)}. \end{aligned}

# digamma function of 10
digamma(10)
 2.251753

#### Trigamma Function in R

The trigamma function is the second derivative of logarithm of gamma function.

That is,

 \begin{aligned} \text{trigamma}(x) &= \psi_1(x)\\ &=\frac{d^2}{dx^2}(\ln \Gamma(x))\\ &=\frac{\Gamma^{\prime}(x)}{\Gamma (x)}. \end{aligned}

# trigamma function of 10
trigamma(10)
 0.1051663

#### Polygamma Function in R

The polygamma function (also known as Psigamma function) of order $m$ is defined as the $(m+1)^{th}$ derivative of the logarithm of the gamma function.

That is,

 \begin{aligned} \psi^{(m)}(x)&:=\frac{d^{m+1}}{dx^{m+1}}(\ln \Gamma(x)). \end{aligned}

# polygamma function of 10 of order 4
psigamma(10,deriv=4)
 -0.0007299312

### Beta function in R

Beta Function is also known as Eulerian integral of first kind. It is denoted by $B(m,n)$ and is defined as

 \begin{aligned} B(m,n)&=\int_0^1 t^{m-1} (1-t)^{n-1}\; dt\\ &=\dfrac{\Gamma(m)\Gamma(n)}{\Gamma(m+n)};\; m,n > 0. \end{aligned}

where

• $B(m,n)$ : Beta Function
• $m$ : First shape parameter
• $n$ : Second shape parameter
# beta function with m=4 and n=5
beta(4,5)
 0.003571429

### Logarithm of Beta function in R

Logarithm of beta Function is the natural logarithm of a beta function.

# logarithm of beta function with m=4 and n=5
lbeta(4,5)
 -5.63479

## Endnote

In this tutorial you learned about some special mathematical functions in R and how to use these functions in R.