2

I find myself wanting to use approxmations provided as part of the mpmath package, but getting confused on exactly what they are supposed to be doing:

http://docs.sympy.org/dev/modules/mpmath/calculus/approximation.html

What exactly is the difference between a sympy expression and a sympy.mpmath expression ?

If I want a taylor approximation to a symbolic expression without understanding what mpmath package is doing I can do the following:

#Imports
import sympy
import sympy.parsing
import sympy.parsing.sympy_parser
import Library_TaylorApproximation

#Create a sympy expression to approximate
ExampleStringExpression = 'sin(x)'
ExampleSympyExpression = sympy.parsing.sympy_parser.parse_expr(ExampleStringExpression)


#Create a taylor expantion sympy expression around the point x=0
SympyTaylorApproximation = sympy.series( 
    ExampleSympyExpression,
    sympy.Symbol('x'),
    1, 
    4,
    ).removeO()

#Cast the sympy expressions to python functions which can be evaluated:
VariableNames = [str(var) for var in SympyTaylorApproximation.free_symbols]
PythonFunctionOriginal =  sympy.lambdify(VariableNames, ExampleSympyExpression)
PythonFunctionApproximation = sympy.lambdify(VariableNames, SympyTaylorApproximation)

#Evaluate the approximation and the original at a point:
print PythonFunctionOriginal(2)
print PythonFunctionApproximation(2)

#>>> 0.909297426826
#>>> 0.870987413961

However, if I try to do the same thing with mpmath based on the documentation:

TaylorCoefficients = sympy.mpmath.taylor(ExampleSympyExpression, 1, 4 )
print 'TaylorCoefficients', TaylorCoefficients

#>>> TypeError: 'sin' object is not callable

I can try to cram the python function in there (which is callable):

TaylorCoefficients = sympy.mpmath.taylor(PythonFunctionOriginal, 1, 4 )
print 'TaylorCoefficients', TaylorCoefficients

#>>> TaylorCoefficients [mpf('0.8414709848078965'), mpf('0.0'), mpf('0.0'), mpf('0.0'), mpf('-8.3694689805155739e+57')]

But the above does not make any sense, because I know that derivatives cannot be taken of a python function.

I can call the mpmath function sin:

TaylorCoefficients = sympy.mpmath.taylor(sympy.mpmath.sin, 1, 4 )
print 'TaylorCoefficients', TaylorCoefficients
#>>> TaylorCoefficients [mpf('0.8414709848078965'), mpf('0.54030230586813977'), mpf('-0.42073549240394825'), mpf('-0.090050384311356632'), mpf('0.035061291033662352')]

But then I cannot do manipulations on it the way I would want too -> like If I want

SinTimesCos = sympy.mpmath.sin*sympy.mpmath.cos
TaylorCoefficients = sympy.mpmath.taylor(SinTimesCos, 1, 4 )
print 'TaylorCoefficients', TaylorCoefficients
#>>> TypeError: unsupported operand type(s) for *: 'function' and 'function'

Exactly WHAT is an mpmath function ?

It is not a sympy expression, and it is also not a python function. How do I do manipulations on arbitrary expressions?

It would appear that I cannot take approximations of arbitrary sympy expressions in the documentation. http://docs.sympy.org/dev/modules/mpmath/calculus/approximation.html

How do I take arbitrary approximations ( Pade / Cheby Chev / Fourier ) to arbitrary sympy expressions?

EDIT:

So an example of what I am looking for is the following approximation:

#Start with a sympy expression of (a, b, x)
expressionString = 'cos(a*x)*sin(b*x)*(x**2)'
expressionSympy = sympy.parsing.sympy_parser.parse_expr(expressionString)

#Do not want to decide on value of `a or b` in advance.
#Do want approximation with respect to x:

wantedSympyExpression = SympyChebyChev( expressionSympy, sympy.Symbol('x') ) 

Result could either be a list of coefficient expressions that are functions of a, and b:

wantedSympyExpressionCoefficients = [ Coef0Expression(a,b), Coef1Expression(a,b), ... , CoefNExpression(a,b)]

OR the result could be the entire sympy expression itself (which is itself a function of a, b):

wantedSympyExpression = Coef0Expression(a,b) + Coef1Expression(a,b) *(x**2) + ... + CoefNExpression(a,b) (x**N)

Note that a and b are not chosen in advance of performing the approximation.

D A
  • 3,130
  • 4
  • 25
  • 41

2 Answers2

1

mpmath functions are ordinary Python functions. They simply do their math in arbitrary-precision arithmetic.

But the above does not make any sense, because I know that derivatives cannot be taken of a python function.

You can't take the derivative symbolically, but you can compute an approximation of the derivative by evaluating the function several times and using numerical differentiation techniques. This is what sympy.mpmath.taylor does. Quoting the docs:

The coefficients are computed using high-order numerical differentiation. The function must be possible to evaluate to arbitrary precision.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • So - my `PythonFunctionOriginal` cannot be evaluated to arbitrary precision? – D A Nov 12 '15 at 01:39
  • @DAdams: Correct. That's most likely why the higher-order terms of the taylor series ended up as zero. – user2357112 Nov 12 '15 at 02:31
  • This answer was extremely insightful regarding an explanation of what npmath functions are doing. Perhaps we could migrate this answer, and the question `What exactly is the difference between a sympy expression and a sympy.mpmath expression ?` to another page? – D A Nov 16 '15 at 19:08
1

If you have a SymPy expression and want to evaluate it to arbitrary precision, use evalf, like

sympy.sin(1).evalf(100)

You can use sin(x).evalf(100, subs={x:1}) to replace the x with 1 before evaluating. evalf uses mpmath under the hood, so this will give you the same result that mpmath would, but without having to use mpmath directly.

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • I'm unclear what your issue is. Can you show an example? If it doesn't fit in a comment you can open a new question. – asmeurer Nov 16 '15 at 18:18
  • I am hoping to generate a set of symbolic coefficients for the chebychev/pade/fourier of an arbitrary symbolic expression. I edited the last few lines of the question for clarity. – D A Nov 16 '15 at 18:51
  • It would be better to write that as a new question. This answers your original question ("what is the difference between a sympy expression and an mpmath expression?"). – asmeurer Nov 16 '15 at 19:55