ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 11 Sep 2020 14:02:42 +0200sage sum not python sumhttps://ask.sagemath.org/question/53399/sage-sum-not-python-sum/ I was using sum in a routine and it bombed. I found that the Python sum I wanted is different from the Sagemath sum, which is symbolic, and I kept getting the Sagemath sum. But I don't see how you differentiate the two, since they have the same name.Fri, 11 Sep 2020 03:36:06 +0200https://ask.sagemath.org/question/53399/sage-sum-not-python-sum/Answer by Emmanuel Charpentier for <p>I was using sum in a routine and it bombed. I found that the Python sum I wanted is different from the Sagemath sum, which is symbolic, and I kept getting the Sagemath sum. But I don't see how you differentiate the two, since they have the same name.</p>
https://ask.sagemath.org/question/53399/sage-sum-not-python-sum/?answer=53400#post-id-53400From `$SAGE_ROOT/local/sage-exp/src/sage/misc/functional.py` :
def symbolic_sum(expression, *args, **kwds):
"...."
if hasattr(expression, 'sum'): ## This catches possible specialized methods of some objects
return expression.sum(*args, **kwds)
elif len(args) <= 1: ## If 0 or 1 argument, call Python's sum
return sum(expression, *args)
else:
from sage.symbolic.ring import SR
return SR(expression).sum(*args, **kwds) ## Use SR.sum by default
*Then*, from `$SAGE_ROOT/local/sage-exp/src/sage/misc/all.py` :
from .functional import (additive_order,
#...
symbolic_sum as sum,
# ...
)
At compilation time, when `symbolic_sum` is defined, Python's `sum` is accessible as `sum`, and used when no or a single argument is passed to `symbolic sum`.
At run time, the global identifier `sum` points to Sage's `symbolic_sum` ; however, thanks to Python's *lexical scoping*, this function still calls Python's `sum` if necessary : the definitions in use are those present when `symbolic_sum` was defined.
HTH,Fri, 11 Sep 2020 07:17:03 +0200https://ask.sagemath.org/question/53399/sage-sum-not-python-sum/?answer=53400#post-id-53400Answer by slelievre for <p>I was using sum in a routine and it bombed. I found that the Python sum I wanted is different from the Sagemath sum, which is symbolic, and I kept getting the Sagemath sum. But I don't see how you differentiate the two, since they have the same name.</p>
https://ask.sagemath.org/question/53399/sage-sum-not-python-sum/?answer=53401#post-id-53401Sage provides some functions with the same names
as some Python built-in functions.
In the case of `sum` it tries to be unobtrusive,
as @Emmanuel Charpentier's answer explains.
To force the use of Python's `sum`:
sage: from builtins import sum as python_sum
sage: python_sum([3, 4, 5])
12
To check where the currently active `sum` is from:
sage: import_statements(sum)
from sage.misc.functional import symbolic_sum
To have `sum` be Python's and `symbolic_sum` be Sage's:
sage: from builtins import sum
sage: from sage.misc.functional import symbolic_sum
To use `symbolic_sum` as `sage_sum`:
sage: from sage.misc.functional import symbolic_sum as sage_sum
The documentation could be improved on this topic.
See the dedicated ticket:
- [Sage Trac ticket 29516: Cross-link documentation of sum, prod, product](https://trac.sagemath.org/ticket/29516)Fri, 11 Sep 2020 14:02:42 +0200https://ask.sagemath.org/question/53399/sage-sum-not-python-sum/?answer=53401#post-id-53401