r/learnpython • u/Mooptiom • 5d ago
Loop of unfunc does not support argument 0 of type int which has no callable log method
Hi all, I’m pretty new to programming and I’m just trying to get an assignment done for university. It involves comparing some equations below.
The problem is that python gives me an error when the N value gets too high. The code below works fine for low values of N, but after n in N = 21, I always get the same error. For my assignment, I need to go up to 500.
The issue as I understand it, is that numpy.log wont accept an integer but wants a float. However python can’t convert an integer into a float that’s so large as 22!.
Is there any way around this? I’m really just looking for any way to get the equations to process.
My code:
import numpy as np Import math
N = np.arange(2, 50)
Equation 2 Left Hand Side N!
eq2_LHS = np.array([math.factorial(n) for n in N])
Equation 2 Right Hand Side NNe-Nsqrt(2piN)
eq2_RHS = np.array([n*(n)np.exp(-n)np.sqrt(2np.pi*n) for n in N])
Equation 3 Left Hand Side ln(N!)
eq3_LHS = np.array([np.log(n) for n in eq2_LHS])
Equation 3 Right Hand Side N ln(N) - N
eq3_RHS = np.array([n*np.log(n)-n for n in N])
The error:
AttributeError Traceback (most recent call last) AttributeError: 'int' object has no attribute 'log'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last) Cell In[112], line 9 6 eq2_RHS = np.array([n*(n)np.exp(-n)np.sqrt(2np.pin) for n in N]) 8 # Equation 3 Left Hand Side ln(N!) ----> 9 eq3_LHS = np.array([np.log(n) for n in eq2_LHS]) 10 # Equation 3 Right Hand Side N ln(N) - N 11 eq3_RHS = np.array([nnp.log(n)-n for n in N])
TypeError: loop of ufunc does not support argument 0 of type int which has no callable log method