在 R 编程中计算计算的 nCr 值的自然对数 - lchoose()函数
R语言中的lchoose()
函数用于返回nCr值的自然对数。此函数等于 log(choose(x))。
Syntax: lchoose(n, r)
Parameters:
n: Number of elements
r: Number of combinations
示例 1:
# R program to illustrate
# lchoose function
# Calling lchoose() function
answer1 <- lchoose(3, 2)
answer2 <- lchoose(2, 2)
answer3 <- lchoose(7, 3)
print(answer1)
print(answer2)
print(answer3)
输出:
[1] 1.098612
[1] 0
[1] 3.555348
示例 2:
# R program to illustrate
# lchoose function
# Calling lchoose() function which
# is equivalent to log(choose(x))
answer1 <- log(choose(5, 1))
answer1_2 <- lchoose(5, 1)
answer2 <- log(choose(4, 2))
answer2_2 <- lchoose(4, 2)
# Printing values
print(answer1)
print(answer1_2)
print(answer2)
print(answer2_2)
输出:
[1] 1.609438
[1] 1.609438
[1] 1.791759
[1] 1.791759
这里,在上面的代码中, log(choose())
和lchoose()
的输出是相等的,这表明两个函数是等价的。