Python Forum

Full Version: Help trying to write a function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I have some trouble trying to define a function using the math module to write a logarithm and then derivating it in python 2.7.

I want to write the function xo=log(yo) and then being able to derivate it to obatin as output 1/yo.

Here's the code:

from math import *
from sympy import *

xo,yo,zo=symbols('xo yo zo', real=True)
xo=log(yo)

dyo1=diff(xo, yo)
print ("dxo/dyo="),dyo1
When I execute the programme I get this error in line 5:
Error:
xo=log(yo) AttributeError: 'Symbol' object has no attribute 'log'
It works for me
>>> from math import *
>>> from sympy import *
>>>  
... xo,yo,zo=symbols('xo yo zo', real=True)
>>> xo=log(yo)
>>>  
... dyo1=diff(xo, yo)
>>> print ("dxo/dyo="),dyo1
dxo/dyo= 1/yo
(Apr-12-2019, 11:32 AM)Rochense Wrote: [ -> ]Hello, I have some trouble trying to define a function using the math module to write a logarithm and then derivating it in python 2.7.

I want to write the function xo=log(yo) and then being able to derivate it to obatin as output 1/yo.

Here's the code:

from math import *
from sympy import *

xo,yo,zo=symbols('xo yo zo', real=True)
xo=log(yo)

dyo1=diff(xo, yo)
print ("dxo/dyo="),dyo1
When I execute the programme I get this error in line 5:
Error:
xo=log(yo) AttributeError: 'Symbol' object has no attribute 'log'

I found that the error appeared if I import the pylab module.

If I have this, the error appears:
from math import *
from sympy import *
from pylab import *

xo,yo,zo=symbols('xo yo zo', real=True)
xo=log(yo)

dyo1=diff(xo, yo)
print ("dxo/dyo="),dyo1
And I have just discovered that with numpy module happens the same, any idea of how to fix it?
Don't use the import * construct, it leads to inconsistencies in the current namespace. Use this for example
import math
import sympy as sy
import pylab
 
xo,yo,zo=sy.symbols('xo yo zo', real=True)
xo=sy.log(yo)
 
dyo1=sy.diff(xo, yo)
print ("dxo/dyo="),dyo1
Then you're sure that the log() function from module sympy is used and not the log() function from the numpy module imported in pylab.