Ask Your Question
1

How to symbolic differentiate a sum of N unknown variables?

asked 2022-05-15 16:44:50 +0200

hanatok gravatar image

updated 2022-05-27 23:57:04 +0200

I want to take the derivative of the following function with the variable beta:

$$ \sum_{k=1}^n \frac{\beta X_i \sin\theta_i}{1+\beta^2 X_i} $$

In maxima, I can do it by:

diff(sum(β*X[i]*sin(θ[i])/(1+β*β*X[i]*X[i]), i, 1, N), β);

How can I do it via sagemath? I have tried the following code:

X = function('X')
theta = function('theta')
var('beta, k, n')
from sage.calculus.calculus import symbolic_sum
E(beta) = symbolic_sum(beta*sin(theta(k))*X(k)/(1+beta*beta*X(k)*X(k)), k, 1, n)
print(E)
print(simplify(derivative(E, beta)))

But the result is different from the one of maxima:

beta |--> beta*sum(X(k)*sin(theta(k))/(beta^2*X(k)^2 + 1), k, 1, n)
beta |--> -2*beta^2*n*X(k)^3*sin(theta(k))/(beta^2*X(k)^2 + 1)^2 + sum(X(k)*sin(theta(k))/(beta^2*X(k)^2 + 1), k, 1, n)

How should I do it correctly?

edit retag flag offensive close merge delete

Comments

This is a known bug: https://trac.sagemath.org/ticket/32161

hanatok gravatar imagehanatok ( 2022-05-28 16:49:47 +0200 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2022-05-18 01:06:25 +0200

Emmanuel Charpentier gravatar image

Workaround :

sage: maxima_calculus.diff(*map(lambda u:u._maxima_(), (E(beta), beta)))._sage_()
-2*beta^2*sum(X(k)^2*sin(theta(k))/(beta^2*X(k) + 1)^2, k, 1, n) + sum(X(k)*sin(theta(k))/(beta^2*X(k) + 1), k, 1, n)

i. e. :

$$-2 \, \beta^{2} {\sum_{k=1}^{n} \frac{X\left(k\right)^{2} \sin\left(\theta\left(k\right)\right)}{{\left(\beta^{2} X\left(k\right) + 1\right)}^{2}}} + {\sum_{k=1}^{n} \frac{X\left(k\right) \sin\left(\theta\left(k\right)\right)}{\beta^{2} X\left(k\right) + 1}}$$

which is, up to the order of the terms, what Maxima returns...

What Sage does :

sage: E(beta).diff(beta)
-(2*beta^2*X(k)^2*sin(theta(k))/(beta^2*X(k) + 1)^2 - X(k)*sin(theta(k))/(beta^2*X(k) + 1))*D[0](sum)(beta*X(k)*sin(theta(k))/(beta^2*X(k) + 1), k, 1, n)

contains D[0](sum)(beta*X(k)*sin(theta(k))/(beta^2*X(k) + 1), which is pure, unadulterated, two-hundred-proof nonsense...

This is a bug and should be reported as such.

HTH,

edit flag offensive delete link more

Comments

Thanks! Your workaround works.

hanatok gravatar imagehanatok ( 2022-05-27 23:52:20 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-05-15 16:44:50 +0200

Seen: 224 times

Last updated: May 27 '22