Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compute log factorial of an array of numbers

The quantity to be computed is log(k!), where k could be 4000 or even higher, but of course the log will compensate. I tried computing sum(log(k)) which is the same.

So, I am given an large array with integers and I want to efficiently compute sum(log(k)). This was my attempt:

integers = np.asarray([435, 535, 242,])

score = np.sum(np.log(np.arange(1,integers+1)))

This would work, except that np.arange would generate an array of different size for each integer, so when I run that, it gives me an error (as it should).

The problem could be easily solved with a for loop as follows:

scores = []
for i in range(integers.shape[0]):
    score = np.sum(np.log(np.arange(1,integer[i]+1)))
    scores.append(score)

but that's too slow. My actual integers has millions of value to be computed.

Is there an efficient implementation for this that basically that doesn't need a for loop? I was thinking of a lambda function or something like that, but I am not really sure how to apply it. Any help is appreciated!

like image 874
Schach21 Avatar asked Sep 14 '25 13:09

Schach21


1 Answers

How about math.lgamma? Gamma function is factorial, and lgamma is log of gamma.

You don't need to compute factorial and then log.

There is also gammaln in the SciPy

Code, Python 3.9 x64 Win 10

import numpy as np
from scipy.special import gammaln

startf = 1 # start of factorial sequence
stopf  = 400 # end of of factorial sequence

q = gammaln(range(startf+1, stopf+1)) # n! = G(n+1)
print(q)

looks reasonable to me

like image 171
Severin Pappadeux Avatar answered Sep 16 '25 01:09

Severin Pappadeux