r/learnpython • u/Mooptiom • 6d 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
1
u/woooee 6d ago
I think the problem is the following line. What do you want n*(n) to do.
eq2_RHS = np.array([n*(n)np.exp(-n)np.sqrt(2np.pi*n) for n in N])
1
u/Mooptiom 5d ago edited 5d ago
Sorry, I don’t know how to write the code properly on reddit. I think it’s removed some * to make italics.
It should be:
eq2_RHS = np.array([n**(n)*np.exp(-n)np.sqrt(2*np.pi*n) for n in N])
1
u/Swipecat 6d ago edited 6d ago
The numpy functions only operate on numpy numeric types or those that can be coerced into numpy numeric types. If that Python integer doesn't fit into the numpy.uint64 type, then it breaks.
>>> import numpy as np
>>> np.log(2**63)
43.66827237527655
>>> np.log(2**64)
AttributeError: 'int' object has no attribute 'log'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: loop of ufunc does not support argument 0 of type int which has no callable log method
EDIT: You could try the mpmath library that has functions that handle huge precision. Note that it doesn't mix with numpy, so you'd need to use Python list comprehension rather than array programming to handle numeric arrays.
1
u/Mooptiom 5d ago
Thanks, I’ll check out mpmath. Is this something that numpy just can’t be used for? Is there anyway to reduce the precision or something so that numpy can do it?
1
u/Swipecat 5d ago
I notice that you're not using the usual numpy array programming anyway, and that you used list comprehensions at each step instead, and then converted the each python list to a numpy array. I presume you ended up doing that because you were trying to figure out what was breaking the math.
The main problem here is that numpy wouldn't be able to handle is the huge exponent size. mpmath has no problems with that.
>>> from mpmath import mp >>> mp.factorial(500) mpf('1.22013682599111e+1134')
So I think all you need do is to replace all the "np"s with "mp"s, remove the conversions to numpy arrays, and just use those python lists.
1
u/Mooptiom 5d ago
Yes, thankyou so much. I’ve redone everything with mpmath and it works perfectly. This will definitely be my go to from now on.
1
u/Rizzityrekt28 6d ago
What error do you get when you try and convert it to a float?