Search code examples
pythonsympysymbolic-integration

partial integration of a two dimensional gaussian function


I want to carry out the following partial integration of a 2-D gaussian function of four variables (x, y, alpha and beta), with respect to only x and y, as follows. In the end I want the answer to be a function of alpha and beta only.

enter image description here

I wrote the following code in python to execute the above mentioned integral.

from sympy import Symbol
from sympy import integrate
from math import e
alpha = Symbol('alpha')
beta = Symbol('beta')
x = Symbol('x')
y = Symbol('y')
n = 2
value = integrate( e**( -(x - alpha)**n - (y - beta)**n  ), (x, -1, 1), (y, -1, 1) )

However I get the following error:

sympy.polys.polyerrors.DomainError: there is no ring associated with RR

The above mentioned integrate function works fine for n=1. However it breaks down for n>1.

Am I doing something wrong?


Solution

  • Welcome to SO!

    Interestingly it works when you substitute alpha and beta into the integral bounds. Try:

    from IPython.display import display
    import sympy as sy
    
    sy.init_printing()  # LaTeX like pretty printing forIPython
    
    alpha, beta, x, y = sy.symbols("alpha, beta, x, y", real=True)
    
    f = sy.exp(-x**2 - y**2)  # sy.exp() is better than the numeric constant
    val = sy.integrate(f,  (x, -1+alpha, 1+alpha), (y, -1+beta, 1+beta))
    display(val)