當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R Special 數學的特殊函數

R語言 Special 位於 base 包(package)。

說明

與 beta 和 gamma 函數相關的特殊數學函數。

用法

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)

參數

a, b

非負數值向量。

x, n

數值向量。

k, deriv

整數向量。

細節

函數 betalbeta 返回 beta 函數和 beta 函數的自然對數,

正式的定義是

(Abramowitz 和 Stegun 第 6.2.1 節,第 258 頁)。請注意,它僅定義在R對於非負數ab,如果其中一個為零,則為無限。

函數gammalgamma返回伽瑪函數 以及伽瑪函數絕對值的自然對數。伽馬函數由(Abramowitz 和 Stegun 第 6.1.1 節,第 255 頁)定義

對於所有真實的x除了零和負整數(當NaN被返回)。如果值太接近(大約在 ) 為小於‘的負整數⁠-10⁠’。

factorial(x)(非負整數x )定義為gamma(x+1)lfactorial定義為lgamma(x+1)

函數digammatrigamma 返回伽馬函數對數的一階和二階導數。 psigamma(x, deriv) ( deriv >= 0 ) 計算 deriv 階導數。

及其派生物 psigamma() 函數通常稱為 ‘polygamma’ 函數,例如Abramowitz 和 Stegun(第 6.4.1 節,第 260 頁);更高階的導數(deriv = 2:4)有時也被稱為‘tetragamma’, ‘pentagamma’和‘hexagamma’。

函數chooselchoose返回二項式係數及其絕對值的對數。注意choose(n, k)為所有實數定義 和整數 。為了 它被定義為 , 作為 為了 並作為 對於負數 。非整數值k四舍五入為整數,並帶有警告。
choose(*, k)使用直接算術(而不是[l]gamma調用)小k,出於速度和準確性的原因。注意函數combn(包utils)用於枚舉所有可能的組合。

gammalgammadigammatrigamma 函數是 internal generic primitive 函數:可以單獨為它們定義方法,也可以通過 Math 組泛型為它們定義方法。

例子

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)


來源

gammalgammabetalbeta 基於洛斯阿拉莫斯科學實驗室的 W. Fullerton(現已作為 SLATEC 的一部分提供)對 Fortran 子例程的 C 翻譯。

x >= 0digammatrigammapsigamma 基於

阿莫斯,D.E.(1983)。用於 psi 函數導數的可移植 Fortran 子例程,算法 610,ACM Transactions on Mathematical Software 9(4), 494-502。

對於x < 0deriv <= 5,使用Abramowitz和Stegun的反射公式(6.4.7)。

參考

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole. (For gamma and lgamma.)

Abramowitz, M. and Stegun, I. A. (1972) Handbook of Mathematical Functions. New York: Dover. https://en.wikipedia.org/wiki/Abramowitz_and_Stegun provides links to the full text which is in public domain.
Chapter 6: Gamma and Related Functions.

也可以看看

Arithmetic 表示簡單函數,sqrt 表示各種數學函數,Bessel 表示實數貝塞爾函數。

對於不完整的 gamma 函數,請參閱 pgamma

相關用法


注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Special Functions of Mathematics。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。