Skip to content Skip to sidebar Skip to footer

Error Using 'exp' In Sympy -typeerror And Attribute Error Is Displayed

I want solve differential equation using sympy on Python3. My equation is relatively simple with two variables. However, the equation has log, power, and exp operators. Whether I u

Solution 1:

In an isympy session (similar to your imports), plus a np import:

In [12]: data = [1,2,3,4,5]                                                  

In [13]: np.power(data,c)                                                    
Out[13]: array([1, 2**c, 3**c, 4**c, 5**c], dtype=object)

In [14]: b*c*np.power(data,c)                                                
Out[14]: array([b*c, 2**c*b*c, 3**c*b*c, 4**c*b*c, 5**c*b*c], dtype=object)

So far these work. When numpy functions and operators encounter an object dtype array (non-numeric ones), they try to apply corresponding operators or methods of the objects. b and c as symbols do respond to ** and *.

But np.log applied to the object array fails with your error message. The elements of the array a sympy Mul objects:

In [17]: type(Out[14][0])                                                    
Out[17]: sympy.core.mul.Mul
In [18]: Out[14][0].log()                                                    
---------------------------------------------------------------------------
AttributeError: 'Mul' object has no attribute 'log'

Same for np.exp.

math.log expects a number, so it won't work with array or the sympy objects either.

sympy.log(Out[14][0]) works - the argument is a sympy Mul. But it doesn't work with Out[14] which a numpy array.

===

I know numpy a lot better than sympy. But I was able to get this sequence of calculations to work:

In[24]: [d**c for d in data]     # listcomprehensionOut[24]: 
⎡    cccc⎤
⎣1, 2 , 3 , 4 , 5In[25]: [b*c*num**c for num in data]Out[25]: 
⎡      cccc    ⎤
⎣bc, 2bc, 3bc, 4bc, 5bcIn[26]: [log(b*c*num**c) for num in data]Out[26]: 
⎡             ⎛ c    ⎞     ⎛ c    ⎞     ⎛ c    ⎞     ⎛ c    ⎞⎤
⎣log(b⋅c), log2bc⎠, log3bc⎠, log4bc⎠, log5bc⎠⎦

In[27]: sum([log(b*c*num**c) for num in data])                              
Out[27]: 
              ⎛ c    ⎞      ⎛ c    ⎞      ⎛ c    ⎞      ⎛ clog(b⋅c) + log2bc⎠ + log3bc⎠ + log4bc⎠ + log5bc

sympy.sum expects an iterable, which this list qualifies.

Now I can do the sympy.diff

In[29]: diff(sum([log(b*c*num**c) for num in data]),b)                      
Out[29]: 
5bIn[30]: diff(sum([log(b*c*num**c) for num in data]),c)                      
Out[30]: 
     -ccc-ccc-cc15  ⋅⎝5bclog(5) + 5b4  ⋅⎝4bclog(4) + 4b3  ⋅⎝3bcl
─ + ────────────────────────── + ────────────────────────── + ─────────────
cbcbcbc-cccog(3) + 3b2  ⋅⎝2bclog(2) + 2b⎠
───────────── + ──────────────────────────
cbc

[log(item) for item in Out[14]] produces the same output as Out[26]. Out[14] is simply the object array equivalent of the Out[25] list.

Solution 2:

As per @hpaulj suggestion, I was able to solve this by using list comprehension. The working code is below:

f =-(-n +sum([sym.log(b*c*(num**(c-1))*sym.exp(-b*(num**c)))for num in data]))

Post a Comment for "Error Using 'exp' In Sympy -typeerror And Attribute Error Is Displayed"