ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 15 Nov 2015 15:04:53 -0600Error when using 'for in range()' but not ' for in [..]'http://ask.sagemath.org/question/30733/error-when-using-for-in-range-but-not-for-in/I've just started using SageMath today, and I've been pleasantly surprised at how good it is. I've been working through tutorials and playing around with plotting functions without any major problems until I wrote a series of nested for-loops using range values.
I'm getting the error 'ValueError("plot start point and end point must be different")', but only when the outer loop uses 'for r1 in range(3,6)', not when I do exactly the same thing using 'for r1 in [3..5]'.
Am I doing something newbie-naive here, or is this a bug?
Here's the full script:
t = var('t')
g = Graphics()
for r1 in [3..5]: # This works as expected
#for r1 in range(3,6): # This produces error: File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/plot/misc.py", line 136, in setup_for_eval_on_grid raise ValueError("plot start point and end point must be different")
g = g + circle((0, 0), r1, rgbcolor=(0.5,0.5,0.5))
for r2 in range(1, r1):
f = floor((r1 + r2) / 2)
for d in range(1, f):
print "r1 =",r1, " r2 =", r2, " d =",d; # debug msg
g = g + parametric_plot(((r1 - r2) * cos(t) + d * cos((r1 - r2) * t / r2), (r1 - r2) * sin(t) - d * sin((r1 - r2) * t / r2)), (t, 0, r2 / r1 * (pi ^ pi)), rgbcolor=(r2/r1, d / f, 0.5))
g.show()Sun, 15 Nov 2015 00:32:19 -0600http://ask.sagemath.org/question/30733/error-when-using-for-in-range-but-not-for-in/Answer by tmonteil for <p>I've just started using SageMath today, and I've been pleasantly surprised at how good it is. I've been working through tutorials and playing around with plotting functions without any major problems until I wrote a series of nested for-loops using range values.</p>
<p>I'm getting the error 'ValueError("plot start point and end point must be different")', but only when the outer loop uses 'for r1 in range(3,6)', not when I do exactly the same thing using 'for r1 in [3..5]'.</p>
<p>Am I doing something newbie-naive here, or is this a bug?</p>
<p>Here's the full script:</p>
<pre><code>t = var('t')
g = Graphics()
for r1 in [3..5]: # This works as expected
#for r1 in range(3,6): # This produces error: File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/plot/misc.py", line 136, in setup_for_eval_on_grid raise ValueError("plot start point and end point must be different")
g = g + circle((0, 0), r1, rgbcolor=(0.5,0.5,0.5))
for r2 in range(1, r1):
f = floor((r1 + r2) / 2)
for d in range(1, f):
print "r1 =",r1, " r2 =", r2, " d =",d; # debug msg
g = g + parametric_plot(((r1 - r2) * cos(t) + d * cos((r1 - r2) * t / r2), (r1 - r2) * sin(t) - d * sin((r1 - r2) * t / r2)), (t, 0, r2 / r1 * (pi ^ pi)), rgbcolor=(r2/r1, d / f, 0.5))
g.show()
</code></pre>
http://ask.sagemath.org/question/30733/error-when-using-for-in-range-but-not-for-in/?answer=30745#post-id-30745The reason is that when you use `[3..5]`, the iterator produces Sage Integers:
sage: for r1 in [3..5]:
....: print type(r1)
<type 'sage.rings.integer.Integer'>
<type 'sage.rings.integer.Integer'>
<type 'sage.rings.integer.Integer'>
while when you use `range(3,6)`, the iterator produces Python ints:
sage: for r1 in range(3,6):
print type(r1)
<type 'int'>
<type 'int'>
<type 'int'>
Now when you write `r2 / r1`, if one of them is a Sage Integer, you get a rational nomber. If both are Python ints, then you get an int, that is the rounded fraction:
sage: ZZ(3)/int(2)
3/2
sage: int(3)/ZZ(2)
3/2
sage: ZZ(3)/ZZ(2)
3/2
sage: int(3)/int(2)
1
Hence, when both `r1` and `r2` are produced by `range()`, with `r2 < r1`, the value of `r2/r1* (pi ^ pi))` will be `0`, hence your plot starts and ends with the same value, which explains the error.
To solve the problem, you can use `srange()` function, which produces Sage integers instead of Pythoin ints, this will solve your problem.
Sun, 15 Nov 2015 10:49:05 -0600http://ask.sagemath.org/question/30733/error-when-using-for-in-range-but-not-for-in/?answer=30745#post-id-30745Comment by Philstix for <p>The reason is that when you use <code>[3..5]</code>, the iterator produces Sage Integers:</p>
<pre><code>sage: for r1 in [3..5]:
....: print type(r1)
<type 'sage.rings.integer.Integer'>
<type 'sage.rings.integer.Integer'>
<type 'sage.rings.integer.Integer'>
</code></pre>
<p>while when you use <code>range(3,6)</code>, the iterator produces Python ints:</p>
<pre><code>sage: for r1 in range(3,6):
print type(r1)
<type 'int'>
<type 'int'>
<type 'int'>
</code></pre>
<p>Now when you write <code>r2 / r1</code>, if one of them is a Sage Integer, you get a rational nomber. If both are Python ints, then you get an int, that is the rounded fraction:</p>
<pre><code>sage: ZZ(3)/int(2)
3/2
sage: int(3)/ZZ(2)
3/2
sage: ZZ(3)/ZZ(2)
3/2
sage: int(3)/int(2)
1
</code></pre>
<p>Hence, when both <code>r1</code> and <code>r2</code> are produced by <code>range()</code>, with <code>r2 < r1</code>, the value of <code>r2/r1* (pi ^ pi))</code> will be <code>0</code>, hence your plot starts and ends with the same value, which explains the error.</p>
<p>To solve the problem, you can use <code>srange()</code> function, which produces Sage integers instead of Pythoin ints, this will solve your problem.</p>
http://ask.sagemath.org/question/30733/error-when-using-for-in-range-but-not-for-in/?comment=30748#post-id-30748Ah, that explains it, thank you.
I had read that the Python interpreter would automatically cast the integers to floats when dividing, but I now realise that was referring to Python 3, and I am using Python 2.7.Sun, 15 Nov 2015 15:04:53 -0600http://ask.sagemath.org/question/30733/error-when-using-for-in-range-but-not-for-in/?comment=30748#post-id-30748