Deepmind have  open sourced packages for Distribution classes in torch, written in lua (see https://github.com/deepmind/torch-distributions 13 and https://github.com/deepmind/torch-randomkit 2) and they do have all the c code for at least the gamma, digamma and beta functions (see https://github.com/deepmind/torch-cephes/tree/master/cephes/cprob 5) . I hope this helps.