Sum of partial derivatives of a product over a symbolic number of variables

I would like SymPy to evaluate an expression like the following:

formula

How would I define characters and expressions so that SymPy can handle this beautifully? I would like to save Nas a symbol, i.e. Do not make the final end list x. I tried various combinations IndexedBaseand Sum/ Productbut did not work correctly.

+4
source share
1 answer

Ideally, it would be like this:

x = IndexedBase("x")
i, j, N = symbols("i j N")
expr = Sum(Product(exp(-x[j]**2), (j, 1, N)).diff(x[i]), (i, 1, N))

Not rated yet expr

Sum(Derivative(Product(exp(-x[j]**2), (j, 1, N)), x[i]), (i, 1, N)) 

The method doitcan be used to evaluate it. Unfortunately, product differentiation still doesn’t quite work: expr.doit()returns

N*Derivative(Product(exp(-x[j]**2), (j, 1, N)), x[i])

Rewriting the product as a sum before differentiation helps:

expr = Sum(Product(exp(-x[j]**2), (j, 1, N)).rewrite(Sum).diff(x[i]), (i, 1, N))
expr.doit()

returns

Sum(Piecewise((-2*exp(Sum(log(exp(-x[j]**2)), (j, 1, N)))*x[i], (1 <= i) & (i <= N)), (0, True)), (i, 1, N))

. , Piecewise, log(exp(...)), . SymPy , (1 <= i) & (i <= N) True , , log(exp x[j] . , :

e = expr.doit()
p = next(iter(e.atoms(Piecewise)))
e = expand_log(e.xreplace({p: p.args[0][0]}), force=True)

e

Sum(-2*exp(Sum(-x[j]**2, (j, 1, N)))*x[i], (i, 1, N))

exp(Sum(..)), .

+3

Source: https://habr.com/ru/post/1692540/


All Articles