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 |
整數向量。 |
細節
函數 beta
和 lbeta
返回 beta 函數和 beta 函數的自然對數,
正式的定義是
(Abramowitz 和 Stegun 第 6.2.1 節,第 258 頁)。請注意,它僅定義在R對於非負數a
和b
,如果其中一個為零,則為無限。
函數gamma
和lgamma
返回伽瑪函數 以及伽瑪函數絕對值的自然對數。伽馬函數由(Abramowitz 和 Stegun 第 6.1.1 節,第 255 頁)定義
對於所有真實的x
除了零和負整數(當NaN
被返回)。如果值太接近(大約在 ) 為小於‘的負整數-10’。
factorial(x)
(非負整數x
為 )定義為gamma(x+1)
,lfactorial
定義為lgamma(x+1)
。
函數digamma
和trigamma
返回伽馬函數對數的一階和二階導數。 psigamma(x, deriv)
( deriv >= 0
) 計算 的 deriv
階導數。
psigamma()
函數通常稱為 ‘polygamma’ 函數,例如Abramowitz 和 Stegun(第 6.4.1 節,第 260 頁);更高階的導數(deriv = 2:4
)有時也被稱為‘tetragamma’, ‘pentagamma’和‘hexagamma’。 及其派生物
函數choose
和lchoose
返回二項式係數及其絕對值的對數。注意choose(n, k)
為所有實數定義 和整數 。為了 它被定義為 , 作為 為了 並作為 對於負數 。非整數值k
四舍五入為整數,並帶有警告。
choose(*, k)
使用直接算術(而不是[l]gamma
調用)小k
,出於速度和準確性的原因。注意函數combn
(包utils
)用於枚舉所有可能的組合。
gamma
、 lgamma
、 digamma
和 trigamma
函數是 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)
來源
gamma
、lgamma
、beta
和 lbeta
基於洛斯阿拉莫斯科學實驗室的 W. Fullerton(現已作為 SLATEC 的一部分提供)對 Fortran 子例程的 C 翻譯。
x >= 0
的 digamma
、 trigamma
和 psigamma
基於
阿莫斯,D.E.(1983)。用於 psi 函數導數的可移植 Fortran 子例程,算法 610,ACM Transactions on Mathematical Software 9(4), 494-502。
對於x < 0
和deriv <= 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 Sys.getenv 獲取環境變量
- R Sys.localeconv 查找當前語言環境中數字和貨幣表示形式的詳細信息
- R Sys.setFileTime 設置文件時間
- R Startup R 會話開始時的初始化
- R Sys.info 提取係統和用戶信息
- R Syntax 運算符語法和優先級
- R Signals 中斷 R 的執行
- R Sys.sleep 暫停執行一段時間
- R Sys.setenv 設置或取消設置環境變量
- R Sys.getpid 獲取 R 會話的進程 ID
- R Sys.readlink 讀取文件符號鏈接
- R Sys.glob 文件路徑上的通配符擴展
- R Sys.which 查找可執行文件的完整路徑
- R S3method 注冊S3方法
- R Sys.time 獲取當前日期和時間
- R file.path 構造文件路徑
- R grep 模式匹配和替換
- R getwd 獲取或設置工作目錄
- R vector 向量 - 創建、強製等
- R lapply 對列表或向量應用函數
- R dump R 對象的文本表示
- R rank 樣本排名
- R getDLLRegisteredRoutines DLL 中 C/Fortran 例程的反射信息
- R pushBack 將文本推回連接
- R strsplit 分割字符向量的元素
注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Special Functions of Mathematics。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。