Python Forum
Help trying to write a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help trying to write a function
#1
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'
Reply
#2
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
Reply
#3
(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?
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Better way to write this function SephMon 1 789 Feb-08-2023, 10:05 PM
Last Post: Gribouillis
  write new function or change the old one to work "smartter? korenron 3 1,924 Aug-09-2021, 10:36 AM
Last Post: jamesaarr
  How can I write a function with three parameters? MehmetAliKarabulut 1 2,375 Mar-04-2021, 10:47 PM
Last Post: Larz60+
  How to write a code with İF function? Aycaaxx 1 1,777 Nov-03-2020, 05:46 AM
Last Post: deanhystad
  Print/write to file function tpolim008 4 2,705 Apr-01-2020, 07:59 PM
Last Post: tpolim008
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,062 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  write an integer to file using a function xvkxvo 4 3,099 Dec-10-2019, 11:27 PM
Last Post: xvkxvo
  how can I write my function output on CSV file go127a 13 5,412 May-05-2019, 01:55 PM
Last Post: go127a
  Use the write function manonB 2 2,497 Apr-17-2019, 05:55 AM
Last Post: manonB
  'function' object has no attribute 'write' paramiko lateublegende 1 5,342 Feb-12-2019, 09:03 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020