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.
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.
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.
EDIT: Rereading my answer -> I thought I would fill in a few missing pieces as a service to someone someday actually using this. Below I labeled how I named my libraries, and what imports were required. I don't have the time to be a real contributor to sympy at the moment, but feel this functionality would certainly be used by other math/physics professors/students.
Note for space reasons the following two libraries are omitted, and I will throw a link to my repo at a future date.
import Library_SympyExpressionToPythonFunction
Creates a python callable function object with the same args ( number and names ) of the free variables in a sympy expression.
import Library_SympyExpressionToStringExpression
Literally just does str(SympyExpression)
#-------------------------------------------------------------------------------
Library_GenerateChebyShevPolynomial:::
#-------------------------------------------------------------------------------
import pprint
import Library_SympyExpressionToPythonFunction
import Library_SympyExpressionToStringExpression
import sympy
import sympy.core
def Main(
ApproximationSymbol = sympy.Symbol('x'),
ResultType = 'sympy',
Kind= None,
Order= None,
ReturnAll = False,
CheckArguments = True,
PrintExtra = False,
):
Result = None
if (CheckArguments):
ArgumentErrorMessage = ""
if (len(ArgumentErrorMessage) > 0 ):
if(PrintExtra):
print "ArgumentErrorMessage:\n", ArgumentErrorMessage
raise Exception(ArgumentErrorMessage)
ChebyChevPolynomials = []
ChebyChevPolynomials.append(sympy.sympify(1.))
ChebyChevPolynomials.append(ApproximationSymbol)
#Generate the polynomial with sympy:
for Term in range(Order + 1)[2:]:
Tn = ChebyChevPolynomials[Term - 1]
Tnminus1 = ChebyChevPolynomials[Term - 2]
Tnplus1 = 2*ApproximationSymbol*Tn - Tnminus1
ChebyChevPolynomials.append(Tnplus1.simplify().expand().trigsimp())
if(PrintExtra): print 'ChebyChevPolynomials'
if(PrintExtra): pprint.pprint(ChebyChevPolynomials)
if (ReturnAll):
Result = []
for SympyChebyChevPolynomial in ChebyChevPolynomials:
if (ResultType == 'python'):
Result.append(Library_SympyExpressionToPythonFunction.Main(SympyChebyChevPolynomial))
elif (ResultType == 'string'):
Result.append(Library_SympyExpressionToStringExpression.Main(SympyChebyChevPolynomial))
else:
Result.append(SympyChebyChevPolynomial)
else:
SympyExpression = ChebyChevPolynomials[Order] #the last one
#If the result type is something other than sympy, we can cast it into that type here:
if (ResultType == 'python'):
Result = Library_SympyExpressionToPythonFunction.Main(SympyExpression)
elif (ResultType == 'string'):
Result = Library_SympyExpressionToStringExpression.Main(SympyExpression)
else:
Result = SympyExpression
return Result
#-------------------------------------------------------------------------------
Library_SympyChebyShevApproximationOneDimension
#-------------------------------------------------------------------------------
import numpy
import sympy
import sympy.mpmath
import pprint
import Library_SympyExpressionToPythonFunction
import Library_GenerateChebyShevPolynomial
def Main(
SympyExpression= None,
DomainMinimumPoint= None,
DomainMaximumPoint= None,
ApproximationOrder= None,
CheckArguments = True,
PrintExtra = False,
):
#Tsymb = sympy.Symbol('t')
Xsymb = sympy.Symbol('x')
DomainStart = DomainMinimumPoint[0]
print 'DomainStart', DomainStart
DomainEnd = DomainMaximumPoint[0]
print 'DomainEnd', DomainEnd
#Transform the coefficients and the result to be on arbitrary inverval instead of from 0 to 1
DomainWidth = DomainEnd - DomainStart
DomainCenter = (DomainEnd - DomainStart) / 2.
t = (Xsymb*(DomainWidth) + DomainStart + DomainEnd) / 2.
x = (2.*Xsymb - DomainStart - DomainEnd) / (DomainWidth)
SympyExpression = SympyExpression.subs(Xsymb, t)
#GET THE COEFFICIENTS:
Coefficients = []
for CoefficientNumber in range(ApproximationOrder):
if(PrintExtra): print 'CoefficientNumber', CoefficientNumber
Coefficient = 0.0
for k in range(1, ApproximationOrder + 1):
if(PrintExtra): print ' k', k
CoefficientFunctionPart = SympyExpression.subs(Xsymb, sympy.cos( sympy.pi*( float(k) - .5 )/ float(ApproximationOrder) ) )
if(PrintExtra): print ' CoefficientFunctionPart', CoefficientFunctionPart
CeofficientCosArg = float(CoefficientNumber)*( float(k) - .5 )/ float( ApproximationOrder)
if(PrintExtra): print ' ',CoefficientNumber,'*','(',k,'-.5)/(', ApproximationOrder ,') == ', CeofficientCosArg
CoefficientCosPart = sympy.cos( sympy.pi*CeofficientCosArg )
if(PrintExtra): print ' CoefficientCosPart', CoefficientCosPart
Coefficient += CoefficientFunctionPart*CoefficientCosPart
if(PrintExtra): print 'Coefficient==', Coefficient
Coefficient = (2./ApproximationOrder)*Coefficient.evalf(10)
if(PrintExtra): print 'Coefficient==', Coefficient
Coefficients.append(Coefficient)
print '\n\nCoefficients'
pprint.pprint( Coefficients )
#GET THE POLYNOMIALS:
ChebyShevPolynomials = Library_GenerateChebyShevPolynomial.Main(
ResultType = 'sympy',
Kind= 1,
Order= ApproximationOrder-1,
ReturnAll = True,
)
print '\nChebyShevPolynomials'
pprint.pprint( ChebyShevPolynomials )
Result = 0.0 -.5*(Coefficients[0])
for Coefficient, ChebyShevPolynomial in zip(Coefficients, ChebyShevPolynomials):
Result += Coefficient*ChebyShevPolynomial
#Transform the coefficients and the result to be on arbitrary inverval instead of from 0 to 1
Result = Result.subs(Xsymb, x)
return Result
Example_SympyChebyShevApproximationOneDimension:
#------------------------------------------------------------------------------
import sympy
import sympy.mpmath
import matplotlib.pyplot as plt
import json
import pprint
import Library_GenerateBesselFunction
import Library_SympyChebyShevApproximationOneDimension
import Library_SympyExpressionToPythonFunction
import Library_GraphOneDimensionalFunction
ApproximationOrder = 10
#CREATE THE EXAMPLE EXRESSION:
Kind = 1
Order = 2
ExampleSympyExpression = sympy.sin(sympy.Symbol('x'))
"""
Library_GenerateBesselFunction.Main(
ResultType = 'sympy',
Kind = Kind,
Order = Order,
VariableNames = ['x'],
)
"""
PythonOriginalFunction = Library_SympyExpressionToPythonFunction.Main(
ExampleSympyExpression ,
FloatPrecision = 100,
)
#CREATE THE NATIVE CHEBY APPROXIMATION
ChebyDomainMin = 5.
ChebyDomainMax = 10.
ChebyDomain = [ChebyDomainMin, ChebyDomainMax]
ChebyExpandedPolynomialCoefficients, ChebyError = sympy.mpmath.chebyfit(
PythonOriginalFunction,
ChebyDomain,
ApproximationOrder,
error=True
)
print 'ChebyExpandedPolynomialCoefficients'
pprint.pprint( ChebyExpandedPolynomialCoefficients )
def PythonChebyChevApproximation(Point):
Result = sympy.mpmath.polyval(ChebyExpandedPolynomialCoefficients, Point)
return Result
#CREATE THE GENERIC ONE DIMENSIONAL CHEBY APPROXIMATION:
SympyChebyApproximation = Library_SympyChebyShevApproximationOneDimension.Main(
SympyExpression = ExampleSympyExpression*sympy.cos( sympy.Symbol('a') ),
ApproximationSymbol = sympy.Symbol('x'),
DomainMinimumPoint = [ChebyDomainMin],
DomainMaximumPoint = [ChebyDomainMax],
ApproximationOrder = ApproximationOrder
)
print 'SympyChebyApproximation', SympyChebyApproximation
SympyChebyApproximation = SympyChebyApproximation.subs(sympy.Symbol('a'), 0.0)
print 'SympyChebyApproximation', SympyChebyApproximation
PythonCastedChebyChevApproximationGeneric = Library_SympyExpressionToPythonFunction.Main(
SympyChebyApproximation ,
FloatPrecision = 100,
)
print 'PythonCastedChebyChevApproximationGeneric(1)', PythonCastedChebyChevApproximationGeneric(1.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.