How to improve performance of this numerical computation in Haskell?

前端 未结 2 864
时光取名叫无心
时光取名叫无心 2020-12-23 16:25

I\'m in the middle of porting David Blei\'s original C implementation of Latent Dirichlet Allocation to Haskell, and I\'m trying to decide whether to leave some of the low-l

2条回答
  •  借酒劲吻你
    2020-12-23 17:06

    Before the optimization work, I wouldn't say that your original translation is the most idiomatic way to express in Haskell what the C code is doing.

    How would the optimization process have proceeded if we started with the following instead:

    trigamma :: Double -> Double
    trigamma x = foldl' (+) p' . map invSq . take 6 . iterate (+ 1) $ x
    where
      invSq y = 1 / (y * y)
      x' = x + 6
      p  = invSq x'
      p' =(((((0.075757575757576*p-0.033333333333333)*p+0.0238095238095238)
                  *p-0.033333333333333)*p+0.166666666666667)*p+1)/x'+0.5*p
    

提交回复
热议问题