1 | initial version |
This is somehow like the difference when between actually computing a sum or just writing $\Sigma$. In the first case Sage (maxima) is able to compute the general sum directly:
sage: h(r) = sum(exp(i), i, -r, r)
sage: h(r)
(e^(2*r + 1) - 1)*e^(-r)/(e - 1)
So, when you write:
sage: h(1)
(e^3 - 1)*e^(-1)/(e - 1)
Sage does a symbolic substitution: it replaces all r
by 1
in the expression.
But in the second case, it is not able to simplify the general sum:
sage: h(r) = sum(exp(i^2), i, -r, r)
sage: h(r)
sum(e^(i^2), i, -r, r)
So, the "sum
" that is returned in the last line is like a mathematical $\Sigma$, "i know that it is a sum, but i am not able to compute it". Now, when you write:
sage: h(1)
sum(e^(i^2), i, -1, 1)
again, Sage does a symbolic substitution: it replaces all r
by 1
in the expression. So, even if maxima is now able to compute this simpler sum, it will not, since $\Sigma$ is still a formal sum. I agree that it is bad, but this seems to be a general feature of the Symbolic Ring
.
So, what we have to do is to ask Sage/maxima to try to compute the sum after the substitution. For this, we will unfold the maxima expression and refold it by inserting "Hey, can you try to simplify this formal sum now ?":
sage: from sage.interfaces.maxima_lib import max_to_sr, sr_to_max, maxima_eval, max_ratsimp, max_simplify_sum
sage: numerical_sum = lambda f : max_to_sr(maxima_eval([[max_ratsimp],[[max_simplify_sum],sr_to_max(f)]])).n()
And now you get:
sage: numerical_sum(h(1))
6.43656365691809