Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multivariate series expansion in sympy

Tags:

python

sympy

Does anyone know if there is a built-in function in sympy to obtain a multivariate series expansion of the form

f(x,y) = a + b*x + c*y + d*x**2 + e*x*y + f*y**2 + ...

i.e. by ascending order in all variables?

Thanks in advance.

like image 335
doetoe Avatar asked Sep 09 '13 22:09

doetoe


2 Answers

It is maybe too late but here is what I would do. It is not exactly a builtin function but it does the job. The idea is to introduce a temporary variable (eps) using substitutions and to expand the series over it. Here is an example:

import sympy
x, y , eps = sympy.symbols('x y eps')
f = sympy.exp(x-y)
f.subs(x,x*eps).subs(y,y*eps).series(eps).removeO().subs(eps,1)

Note that using this technique you can have "asymmetric" expansions in x and y. For instance: f.subs(x,x*eps).subs(y,y*eps**2) ...

like image 96
JBD Avatar answered Nov 25 '22 14:11

JBD


The short answer is that currently (sympy build 0.7.5), there is no built-in function in sympy that will handle multivariate series expansions.

There appears to be support for series expansion of multivariate functions in one variable only. You can see this in the docstring of _eval_nseries in the function documentation here. If this is important to you, you can comment on the Issue Tracker or join the mailinglist.

So, to be clear, this works:

In [1]: import sympy as sp
In [2]: x, y = sp.symbols('x,y')
In [3]: g = sp.exp(-x*y)
In [4]: g
Out[4]: exp(-x*y)
In [5]: g.series(x, 0)
Out[5]: 1 - x*y + x**2*y**2/2 - x**3*y**3/6 + x**4*y**4/24 - x**5*y**5/120 + O(x**6)
In [6]: g.series(y, 0)
Out[6]: 1 - x*y + x**2*y**2/2 - x**3*y**3/6 + x**4*y**4/24 - x**5*y**5/120 + O(y**6)

but there is none of your desired functionality in any of the following:

In [7]: g.series((x, y), (0, 0))
Out[7]: exp(-x*y)

In [8]: g.series((x, 0), (y, 0))
Out[8]: exp(-x*y)

In [9]: g.series(x, 0, y, 0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-20c1ab732928> in <module>()
----> 1 g.series(x, 0, y, 0)

/usr/lib/python2.7/dist-packages/sympy/core/expr.pyc in series(self, x, x0, n, dir, logx)
   2401                 return self
   2402 
-> 2403         if len(dir) != 1 or dir not in '+-':
   2404             raise ValueError("Dir must be '+' or '-'")
   2405 

TypeError: object of type 'int' has no len()

In [10]: g.series(x, y, 0, 0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-32b57736cd3d> in <module>()
----> 1 g.series(x, y, 0, 0)

/usr/lib/python2.7/dist-packages/sympy/core/expr.pyc in series(self, x, x0, n, dir, logx)
   2401                 return self
   2402 
-> 2403         if len(dir) != 1 or dir not in '+-':
   2404             raise ValueError("Dir must be '+' or '-'")
   2405 

TypeError: object of type 'int' has no len()
like image 41
wflynny Avatar answered Nov 25 '22 14:11

wflynny