SymPy -- issues with - Taylor series (mpmath) and Lambdify

87 views
Skip to first unread message

bern...@gmail.com

unread,
Apr 28, 2017, 10:40:26 AM4/28/17
to sympy
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


Aaron Meurer

unread,
Apr 28, 2017, 4:10:33 PM4/28/17
to sy...@googlegroups.com
The first issue I would report to the mpmath list. https://groups.google.com/forum/#!forum/mpmath. But first:

-  make sure you are using the latest version of SymPy (1.0). It no longer includes mpmath, so you should use mpmath directly instead of sympy.mpmath.
- don't mix SymPy and mpmath objects. Mpmath doesn't know about sympy objects. You should use mpmath.pi instead of sympy.pi.  For most users, I would recommend only using SymPy functions (like Float and evalf), which are wrappers to mpmath, and know about SymPy objects.

For the lambdify thing, it's a known issue https://github.com/sympy/sympy/issues/5642. For the most part, it shouldn't be an issue in practice because NumPy's broadcasting rules will make f0(y) act as if it were a an array in the appropriate contexts. However, that isn't to say that the issue shouldn't be fixed.

Aaron Meurer

--
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 sympy+unsubscribe@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
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 all
Reply to author
Forward
0 new messages