ID EN
Math Functions

Special

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Fungsi matematika khusus yang berkaitan dengan fungsi beta dan gamma.

Syntax

R
beta(a, b)
lbeta(a, b)<p></p><p>gamma(x)
lgamma(x)
psigamma(x, deriv = 0)
digamma(x)
trigamma(x)</p><p>choose(n, k)
lchoose(n, k)
factorial(x)
lfactorial(x)</p>

Arguments

Parameter Deskripsi
a, b non-negative numeric vectors.
x, n numeric vectors.
k, deriv integer vectors.

Details

Fungsi beta dan lbeta mengembalikan fungsi beta dan logaritma natural dari fungsi beta, $$B(a,b) = \frac{\Gamma(a)\Gamma(b)}{\Gamma(a+b)}.$$ Definisi formalnya adalah $$B(a, b) = \int_0^1 t^{a-1} (1-t)^{b-1} dt$$ (bagian Abramowitz dan Stegun 6.2.1, halaman 258). Perhatikan bahwa ini hanya didefinisikan dalam R untuk a dan b non-negatif, dan tidak terbatas jika keduanya nol. Fungsi gamma dan lgamma mengembalikan fungsi gamma \(\Gamma(x)\) dan logaritma natural dari nilai absolut fungsi gamma. Fungsi gamma didefinisikan oleh (Abramowitz dan Stegun bagian 6.1.1, halaman 255) $$\Gamma(x) = \int_0^\infty t^{x-1} e^{-t} dt$$ untuk semua x nyata kecuali bilangan bulat nol dan negatif (saat NaN dikembalikan). Akan ada peringatan tentang kemungkinan hilangnya presisi untuk nilai yang terlalu dekat (

Contoh

Example
R
# NOT RUN {
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)

# }

See Also

Arithmetic for simple sqrt for miscellaneous mathematical functions and Bessel for the real Bessel functions. For the incomplete gamma function see pgamma.