如何计算Scala中的泊松数
没有标准的库函数来执行此操作。我怎样才能有效地完成它?我翻译成Scala:如何计算Scala中的泊松数,scala,statistics,Scala,Statistics,没有标准的库函数来执行此操作。我怎样才能有效地完成它?我翻译成Scala: def recursive_poisson_helper(m:Long, r:Long, p:Double, i:Long):Double = { if (r == i) { p } else { recursive_poisson_helper(m, r, (p * m) / (i + 1), i + 1) } } def efficient_poisson(m:Long, r:Long)
def recursive_poisson_helper(m:Long, r:Long, p:Double, i:Long):Double = {
if (r == i) {
p
} else {
recursive_poisson_helper(m, r, (p * m) / (i + 1), i + 1)
}
}
def efficient_poisson(m:Long, r:Long): Double = {
val p = math.exp(-m)
recursive_poisson_helper(m, r, p, 0)
}
我翻译成Scala:
def recursive_poisson_helper(m:Long, r:Long, p:Double, i:Long):Double = {
if (r == i) {
p
} else {
recursive_poisson_helper(m, r, (p * m) / (i + 1), i + 1)
}
}
def efficient_poisson(m:Long, r:Long): Double = {
val p = math.exp(-m)
recursive_poisson_helper(m, r, p, 0)
}
Scala Breeze在其stats.distributions包中有一个泊松类
case class Poisson(mean: Double)(implicit rand: RandBasis = Rand)
Scala Breeze在其stats.distributions包中有一个泊松类
case class Poisson(mean: Double)(implicit rand: RandBasis = Rand)
i
可能也应该是一个Long
,否则您不应该真正测试是否相等。而且函数名不遵循Scalas样式指南,这里有所有这些不必要的花括号,递归的\u poisson\u helper
应该是一个内部函数。i
可能也应该是一个Long
,否则就不应该真正测试是否相等。而且函数名不遵循Scalas样式指南,这里有所有这些不必要的花括号,递归\u poisson\u helper
应该是一个内部函数。