sympy

Poul Riis priisdk at gmail.com
Thu Mar 31 06:57:10 EDT 2016


Den onsdag den 30. marts 2016 kl. 17.59.49 UTC+2 skrev Steven D'Aprano:
> On Thu, 31 Mar 2016 02:23 am, Poul Riis wrote:
> 
> > What I intend to do is to let sympy find the derivative of some
> > welldefined function and next define the foundation derivative as a normal
> > function so that I can calculate numerical values or even make a graph.
> 
> 
> I'm glad you explained what you *actually* wanted, because I was going to
> guess that you wanted to evaluate the derivative at x = 3:
> 
> 
> py> ftext.evalf(subs={x:3})
> -0.0600000000000000
> 
> 
> 
> -- 
> Steven

... However, the sympy way seems to be about 70 times slower than using the derivative calculated 'by hand' (try the example below).
Can it be done in a more efficient way?

Poul Riis



from sympy import *
from time import *
x=Symbol('x')
ftext=diff(sin(x),x)

def fmsympy(t): 
   return ftext.evalf(subs={x:t})

def fm(t):
    return cos(t)

nloop=10000
tstart=time()
# nloop evaluations with sympy
for i in range(0,nloop):
    a=fmsympy(1)
dt1=time()-tstart

# nloop evaluations without sympy
tstart=time()
for i in range(0,nloop):
    a=fm(1)
dt2=time()-tstart

print(nloop,' evaluations with sympy   : dt1 =',dt1)
print(nloop,' evaluations without sympy: dt2 =',dt2)





More information about the Python-list mailing list