0

This is the error I get when I try to run my code: TypeError: loop of ufunc does not support argument 0 of type Zero which has no callable exp method

Now, ufunc is a lambdified function (which contains exponentials, in case you needed it), but I don't understand what this error means.

Edit: Here is part of the code:

x, nu, t = sympy.symbols('x nu t')
phi = (sympy.exp(-(x - 4 * t)**2 / (4 * nu * (t + 1))) +
       sympy.exp(-(x - 4 * t - 2 * sympy.pi)**2 / (4 * nu * (t + 1))))

phiprime = phi.diff(x)

fig = plt.figure()

u = -2 * nu * (phiprime / phi) + 4
ufunc = lambdify((t,x,nu), u)

x = np.linspace(0, Dx * np.pi, nx)
t = 0

u = np.asarray([ufunc(t, x0, nu) for x0 in x])

At least the relevant part (the variables are declared and set before this and the animation function and plotting are after).

And here is the complete error message:

AttributeError: 'Zero' object has no attribute 'exp'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Utente\AppData\Local\Programs\Python\Python38\Step_4_NS.py", line 33, in <module>
    u = np.asarray([ufunc(t, x0, nu) for x0 in x])
  File "C:\Users\Utente\AppData\Local\Programs\Python\Python38\Step_4_NS.py", line 33, in <listcomp>
    u = np.asarray([ufunc(t, x0, nu) for x0 in x])
  File "<lambdifygenerated-1>", line 2, in _lambdifygenerated
    return (-2*nu*(-1/4*(-8*t + 2*x)*exp(-1/4*(-4*t + x)**2/(nu*(t + 1)))/(nu*(t + 1)) - 1/4*(-8*t + 2*x - 4*pi)*exp(-1/4*(-4*t + x - 2*pi)**2/(nu*(t + 1)))/(nu*(t + 1)))/(exp(-1/4*(-4*t + x - 2*pi)**2/(nu*(t + 1))) + exp(-1/4*(-4*t + x)**2/(nu*(t + 1)))) + 4)
TypeError: loop of ufunc does not support argument 0 of type Zero which has no callable exp method

So? What does that error mean and how do i fix it?

  • Some code would be helpful. It looks like you're trying to call `exp()` on a type that doesn't support it. – Axe319 Apr 14 '20 at 15:15
  • @Axe319 I added the relevant part of the code now, thanks for reminding me. – Dah_Fisicist Apr 15 '20 at 15:31
  • Do you reassign `nu` as well? Disclaimer: I am not familiar with sympy. However, I see that here `x = np.linspace(0, Dx * np.pi, nx)` you are reassigning the value of `x`. When your lambda is called, the value of `x` inside it will be a numpy array. Since you are passing `t` in as an argument it should be ok, as the lambdas scope will take precedence, which is why I asked if `nu` was redefined as well. – Axe319 Apr 15 '20 at 16:15
  • @Axe319 No, `nu` remains unchanged. Now, instead, I tried defining `x` before lambdifying, and now it gives me the following error: `Traceback (most recent call last): File "C:\Users\Utente\AppData\Local\Programs\Python\Python38\Step_4_NS.py", line 34, in u = np.asarray([ufunc(t, x0, nu) for x0 in x]) TypeError: 'Symbol' object is not iterable` Why is it not iterable? – Dah_Fisicist Apr 16 '20 at 12:38
  • You are still reusing your variable. It is not good practice to reuse variables for 2 completely different things for this specific reason. Additionally, it makes it very confusing for someone reading or debugging your code to understand. A better method would be to change `x = np.linspace(0, Dx * np.pi, nx)` to something like `array = np.linspace(0, Dx * np.pi, nx)` and `u = np.asarray([ufunc(t, x0, nu) for x0 in x])` to `u = np.asarray([ufunc(t, x0, nu) for x0 in array])`. – Axe319 Apr 16 '20 at 13:07
  • This answer here https://stackoverflow.com/a/17884418/12479639 gives a very good explanation of why you should not reuse variables. – Axe319 Apr 16 '20 at 13:12
  • @Axe319 Ok, now that error is gone, but I still have the original error message: `AttributeError: 'Zero' object has no attribute 'exp' ` – Dah_Fisicist Apr 19 '20 at 16:57

0 Answers0