I'm trying to find the Taylor series expansion of sin(x). My first attempt:

>>> import numpy as np

>>> import sympy as sy # I use "sp" as abbreviation for SciPy... 

 

>>> sy.mpmath.taylor(sy.sin,0,0)

[mpf('0.0')]

 

>>> sy.mpmath.taylor(sy.sin,np.pi,0)

[mpf('0.0')]

 

# For some strange reason, I cannot expand around sy.pi... 

 

>>> sy.mpmath.taylor(sy.sin,np.pi,1)

[mpf('0.0'), mpf('-0.99999999999998623')]

 

# ok…

 

>>> sy.mpmath.taylor(sy.sin,np.pi,2)

[mpf('0.0'), mpf('-1.0'), mpf('0.0')]

 

# ok

 

>>> sy.mpmath.taylor(sy.sin,np.pi,3)

Out[8]: 

[mpf('0.0'),

mpf('-1.0'),

mpf('31845.854976072664'),

mpf('2.5611218906685583e+23')]

 
# What on **earth** is this???
--
I then found that I can use the series method. This method seems to work...

>>> import sympy as sy
>>> import numpy as np
>>> x = sy.Symbol('x')
>>> sin0 = sy.series(sy.sin(x),x0=sy.pi/2,n=0)
>>> sin0
 O(1, (x, pi/2))
>>> e_sin0 = sin0.removeO()
>>> e_sin0
0
>>> e_sin3 = sy.series(sy.sin(x),x0=sy.pi/2,n=3).removeO()
>>> e_sin3
-(x - pi/2)**2/2 + 1

# Fine. This seems to work.
# I can now use method "lambdify" to turn these expressions into 
numpy-compatible functions...

>>> f0 = sy.lambdify(x,e_sin0,"numpy")
>>> f3 = sy.lambdify(x,e_sin3,"numpy")

>>> y = np.linspace(-2,2,5)

>>> f0(y)
 0
>>> f3(y)
 array([-5.3752932 , -2.30449688, -0.23370055,  0.83709578,  0.9078921 ])

# There is a bug in "lambdify" -- if the expression does not depend on x, 
then the response is a scalar even with array input. I think the resulting 
function should respond with an array of the same shape as the input array.

# Of course, I can get around this problem by defining...
>>> f0 = sy.lambdify(x,e_sin0 + x,"numpy")
>>> f00 = lambda x: f0(x)-x

# But this is neither not elegant, nor very user friendly


-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/4509d8f8-6465-4018-8ce2-7ae3f77d0fac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to