Error when plotting `sum(i for i in range(x))`

55 views
Skip to first unread message

Georgi Guninski

unread,
Jan 22, 2025, 1:47:26 AM1/22/25
to sage-...@googlegroups.com
sage: def f1(x): return sum(i for i in range(x))
sage: f1(20)
190
sage: pl1=plot(f1,2,4);pl1
verbose 0 (3965: plot.py, generate_plot_points) WARNING: When
plotting, failed to evaluate function at 200 points.
verbose 0 (3965: plot.py, generate_plot_points) Last error message:
''float' object cannot be interpreted as an integer'

dmo...@deductivepress.ca

unread,
Jan 22, 2025, 1:53:08 AM1/22/25
to sage-devel
The error message is correct -- you should not expect to be able to plot this function, because it is only defined for integer values of x, not all x between 2 and 4.  For example:

sage: f1(2.1)
<snip>
TypeError: 'sage.rings.real_mpfr.RealLiteral' object cannot be interpreted as an integer

kcrisman

unread,
Jan 22, 2025, 10:43:41 AM1/22/25
to sage-devel
If you want to plot a list of points, there are several ways to do so, including points() and list_plot().

def f1(x): return sum(i for i in range(x))
L = [(n,f1(n)) for n in [2..4]]
list_plot(L) # or points(L)
Reply all
Reply to author
Forward
0 new messages