Ask Your Question
2

Error when using 'for in range()' but not ' for in [..]'

asked 2015-11-15 07:32:19 +0200

Philstix gravatar image

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()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2015-11-15 17:49:05 +0200

tmonteil gravatar image

updated 2015-11-15 17:49:42 +0200

The 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.

edit flag offensive delete link more

Comments

Ah, 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.

Philstix gravatar imagePhilstix ( 2015-11-15 22:04:53 +0200 )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: 2015-11-15 07:32:19 +0200

Seen: 1,067 times

Last updated: Nov 15 '15