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 , 5 ⎦

In[25]: [b*c*num**c for num in data]Out[25]: 
⎑      cccc    ⎀
⎣bβ‹…c, 2 β‹…bβ‹…c, 3 β‹…bβ‹…c, 4 β‹…bβ‹…c, 5 β‹…bβ‹…c⎦

In[26]: [log(b*c*num**c) for num in data]Out[26]: 
⎑             βŽ› c    ⎞     βŽ› c    ⎞     βŽ› c    ⎞     βŽ› c    ⎞⎀
⎣log(bβ‹…c), log⎝2 β‹…bβ‹…c⎠, log⎝3 β‹…bβ‹…c⎠, log⎝4 β‹…bβ‹…c⎠, log⎝5 β‹…bβ‹…c⎠⎦

In[27]: sum([log(b*c*num**c) for num in data])                              
Out[27]: 
              βŽ› c    ⎞      βŽ› c    ⎞      βŽ› c    ⎞      βŽ› c    ⎞
log(bβ‹…c) + log⎝2 β‹…bβ‹…c⎠ + log⎝3 β‹…bβ‹…c⎠ + log⎝4 β‹…bβ‹…c⎠ + log⎝5 β‹…bβ‹…c⎠

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]: 
5
─
bIn[30]: diff(sum([log(b*c*num**c) for num in data]),c)                      
Out[30]: 
     -c βŽ› cc  ⎞    -c βŽ› cc  ⎞    -c βŽ› c15  β‹…βŽ5 β‹…bβ‹…cβ‹…log(5) + 5 β‹…b⎠   4  β‹…βŽ4 β‹…bβ‹…cβ‹…log(4) + 4 β‹…b⎠   3  β‹…βŽ3 β‹…bβ‹…cβ‹…l
─ + ────────────────────────── + ────────────────────────── + ─────────────
cbβ‹…cbβ‹…cbβ‹…

         c  ⎞    -c βŽ› cc  ⎞
og(3) + 3 β‹…b⎠   2  β‹…βŽ2 β‹…bβ‹…cβ‹…log(2) + 2 β‹…b⎠
───────────── + ──────────────────────────
cbβ‹…c

[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"