I want to write a multidimensional Taylor approximation using sympy , which
- uses as much embedded code as possible,
- calculates the truncated Taylor approximation of a given function of two variables
- returns a result without a Big-O remainder, for example. in
sin(x)=x - x**3/6 + O(x**4) .
Here is what I have tried so far:
Approach 1
Naively, you can simply combine the series command twice for each variable, which, unfortunately, does not work, as shown in this example for the sin(x*cos(y)) function:
sp.sin(x*sp.cos(y)).series(x,x0=0,n=3).series(y,x0=0,n=3) >>> NotImplementedError: not sure of order of O(y**3) + O(x**3)
Approach 2
Based on this post , I first wrote the one-dimensional approximation of taylor:
def taylor_approximation(expr, x, max_order): taylor_series = expr.series(x=x, n=None) return sum([next(taylor_series) for i in range(max_order)])
Testing it with examples 1D works fine
mport sympy as sp x=sp.Symbol('x') y=sp.Symbol('y') taylor_approximation(sp.sin(x*sp.cos(y)),x,3) >>> x**5*cos(y)**5/120 - x**3*cos(y)**3/6 + x*cos(y)
However, if I know that a chained call to execute both extensions in x and y , sympy hangs
# this does not work taylor_approximation(taylor_approximation(sp.sin(x*sp.cos(y)),x,3),y,3)
Does anyone know how to fix this or achieve this in an alternative way?