Ask Your Question
0

The sum function doesn't work in my Jupyter notebook. I get the following error message. Any help appreciated.

asked 2024-08-01 06:43:08 +0100

voodooguru gravatar image

Traceback (most recent call last) Cell In[106], line 2 1 number=Integer(20240801) ----> 2 print(sum(number.digits()))

TypeError: 'sage.rings.rational.Rational' object is not callable

edit retag flag offensive close merge delete

Comments

Does the error persist when you run only the following code in a new Jupyter session? (with no other prior code parts, just the following two lines.)

number=Integer(20240801) 
print(sum(number.digits()))
tolga gravatar imagetolga ( 2024-08-01 07:43:16 +0100 )edit

WorksForMe(TM) in 10.5.beta0 :

sage: number=Integer(20240801)
sage: sum(number.digits())
17

What happen(s|ned) in the 105 previous cells ?

HTH,

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2024-08-01 11:09:43 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2024-08-01 16:09:20 +0100

Max Alekseyev gravatar image

updated 2024-08-01 16:58:57 +0100

You may have introduced variable sum with a rational value somewhere earlier in your code. If that is the case, calling sum() will raise an error like the one you observe.

edit flag offensive delete link more

Comments

Indeed, one should avoid using names of builtin functions for variables, to avoid losing access to the functions. To get the function back one can do from sage.misc.functional import symbolic_sum as sum

rburing gravatar imagerburing ( 2024-08-02 12:06:49 +0100 )edit

@rburing: I believe the default value of sum is the Python's one, not Sage's symbolic_sum.

Max Alekseyev gravatar imageMax Alekseyev ( 2024-08-02 14:07:58 +0100 )edit
1

The default value is symbolic_sum, which has a fallback to Python's sum when few arguments are given. See e.g. the output of sum?? in a SageMath session.

rburing gravatar imagerburing ( 2024-08-02 15:49:55 +0100 )edit

Yes, importing the symbolic_sum as sum did the job. Thank you rburing and Max Alekseyev for helping me out. Much appreciated.

voodooguru gravatar imagevoodooguru ( 2024-08-02 16:57:34 +0100 )edit
2

@voodooguru: a better solution is to not overwrite the value of sum and use another name for the variable.

Max Alekseyev gravatar imageMax Alekseyev ( 2024-08-02 17:08:59 +0100 )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: 2024-08-01 06:43:08 +0100

Seen: 300 times

Last updated: Aug 01