Ask Your Question

Numeric evaluation of exp(x^2) in a sum TypeError

asked 2014-01-18 23:29:40 -0500

sage_user gravatar image

Define simple function of one variable with sum:

sage: r, i = var('r i')
sage: h(r) = sum(exp(i), i, -r, r)
sage: n(h(1))

OK, now change the argument of exponent to i^2:

sage: h(r) = sum(exp(i^2), i, -r, r)
sage: n(h(1))
TypeError                                 Traceback (most recent call last)
<ipython-input-128-85150c4c79b2> in <module>()
----> 1 n(h(Integer(1)))

/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/misc/functional.pyc in numerical_approx(x, prec, digits)
   1395             prec = int((digits+1) * LOG_TEN_TWO_PLUS_EPSILON) + 1
   1396     try:
-> 1397         return x._numerical_approx(prec)
   1398     except AttributeError:
   1399         from sage.rings.complex_double import is_ComplexDoubleElement

/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/ in sage.symbolic.expression.Expression._numerical_approx (sage/symbolic/expression.cpp:22617)()

TypeError: cannot evaluate symbolic expression numerically

Why is that and what am I doing wrong?

edit retag flag offensive close merge delete


Interestingly, calling n on the symbolic answer works: sage: n(sum(e^(i^2), i, -1, 1)) 6.43656365691809

sage_user gravatar imagesage_user ( 2014-01-18 23:33:02 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-01-20 03:10:44 -0500

tmonteil gravatar image

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))
edit flag offensive delete link more


I wonder whether this is worth adding as a `.n()` method for sums returned from Maxima like this.

kcrisman gravatar imagekcrisman ( 2014-01-20 09:51:54 -0500 )edit

Or add this to the `full_simplify()` method ?

tmonteil gravatar imagetmonteil ( 2014-01-21 07:36:11 -0500 )edit

No, because that should only return symbolic output. Or at least the last `.n()` bit shouldn't; maybe the other lambda function could! (After testing that it didn't do too much stuff, of course.) Please do open a ticket with this example.

kcrisman gravatar imagekcrisman ( 2014-01-21 08:48:30 -0500 )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


Asked: 2014-01-18 23:29:40 -0500

Seen: 409 times

Last updated: Jan 20 '14