sxrange / xsrange possible bug

52 views
Skip to first unread message

Mariah Lenox

unread,
Sep 15, 2009, 1:53:20 PM9/15/09
to sage-s...@googlegroups.com
Perhaps I do not understand sxrange / xsrange correctly. The
reference manual entries for them seem to be similar. Is
there a difference? (If one is just an alias for the other,
it would be best to say so. If they are different, the reference
manual needs to emphasize the difference.)

Also the reference manual entry for

sage.misc.misc.sxrange

has the note

: This function is called xsrange to distinguish it from the builtin
Python xrange command.

Should it say "sxrange" rather than "xsrange"?

Finally here is my attempt to use sxrange (xsrange
gives similar output):

e = 1
while true:
print Integer(10^e)
for i in sxrange(Integer(2),Integer(10^e)):
if i == 3:
break
e += 1

gives

10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
10000000000
100000000000
1000000000000
10000000000000
100000000000000
1000000000000000
10000000000000000
100000000000000000
1000000000000000000
10000000000000000000
Traceback (most recent call last):
File "zzzbug.py", line 7, in <module>
for i in sxrange(Integer(_sage_const_2 ),Integer(_sage_const_10 **e)):
File "/home/mariah/sage/sage-4.1.1-x86_64-Linux-core2-fc/local/lib/python2.6/site-packages/sage/misc/misc.py",
line 1059, in generic_xsrange
for k in xrange(icount):
OverflowError: long int too large to convert to int

I really want an iterator for the "for i in ..." loop.

Is this a bug? Is there a better way to do what I want?

--
Mariah

William Stein

unread,
Sep 15, 2009, 2:27:26 PM9/15/09
to sage-s...@googlegroups.com
On Tue, Sep 15, 2009 at 10:53 AM, Mariah Lenox <mariah...@gmail.com> wrote:
>
> Perhaps I do not understand sxrange / xsrange correctly.  The
> reference manual entries for them seem to be similar.  Is
> there a difference?  (If one is just an alias for the other,
> it would be best to say so. If they are different, the reference
> manual needs to emphasize the difference.)

The file misc.py has this line:

sxrange = xsrange

Any chance you could post a patch with a statement that they are
aliases added to the docs?

Yes, I think that is a bug given the point of xrange, caused by an optimization.

> Is there a better way to do what I want?

Here is a way to do what you want:

e = 1
while true:
print Integer(10^e)

i = Integer(2)
while i < Integer(10^e):


if i == 3:
break

print type(i)
i += 1
e += 1

William Stein

unread,
Sep 15, 2009, 2:40:45 PM9/15/09
to Robert Bradshaw, sage-support
On Tue, Sep 15, 2009 at 11:38 AM, Robert Bradshaw
<robe...@math.washington.edu> wrote:
> On Sep 15, 2009, at 11:28 AM, William Stein wrote:
>
>> You might have some thought about this... at least I think it might be
>> caused by your optimizations to [a..b].  This is really a question from ccr,
>> by the way...
>>
>> Forwarded conversation
>> Subject: [sage-support] sxrange / xsrange possible bug
>> ------------------------

>>
>> From: Mariah Lenox <mariah...@gmail.com>
>> Date: Tue, Sep 15, 2009 at 10:53 AM
>> To: sage-s...@googlegroups.com
>>
>>
>>
>> Perhaps I do not understand sxrange / xsrange correctly.  The
>> reference manual entries for them seem to be similar.  Is
>> there a difference?  (If one is just an alias for the other,
>> it would be best to say so. If they are different, the reference
>> manual needs to emphasize the difference.)
>
> sage: xsrange is sxrange
>  True
> This is because we use the Python xrange under the hood, which can't handle
> anything that big... It should probably be re-written to be an (optimized)
> while loop.

>
>> I really want an iterator for the "for i in ..." loop.
>>
>> Is this a bug?   Is there a better way to do what I want?
>
> How about
>
> sage: for a in (1..):

Using your ellipsis implementation does not actually work to fix the
problem that she had, much to my surprise:

sage: e = 1
sage: while true:
... print Integer(10^e)
... for i in (2..10^e):
... if i == 3:
... break
... e += 1


10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
10000000000
100000000000
1000000000000
10000000000000
100000000000000
1000000000000000
10000000000000000
100000000000000000
1000000000000000000
10000000000000000000

Traceback (most recent call last): if i == 3:
File "", line 3, in <module>

File "/Users/wstein/sage/build/64bit/sage/local/lib/python2.6/site-packages/sage/misc/misc.py",
line 1319, in ellipsis_iter
first = more.next()
File "/Users/wstein/sage/build/64bit/sage/local/lib/python2.6/site-packages/sage/misc/misc.py",


line 1059, in generic_xsrange
for k in xrange(icount):
OverflowError: long int too large to convert to int


>   ...:     print a
>   ...:     if a > 100: break
>   ...:
> 1
> 2
> 3
> 4
> 5
> [...]
> 101
>
> - Robert
>

--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

Mariah

unread,
Sep 16, 2009, 9:10:48 AM9/16/09
to sage-support
William,

> The file misc.py has this line:
>
> sxrange = xsrange

Ah, yes. I should have looked at the source code. Apologies.

> Any chance you could post a patch with a statement that they are
> aliases added to the docs?

How about:

---------------------------------------------------------------------------------------------
--- misc.py.orig 2009-09-15 16:25:22.983553000 -0400
+++ misc.py 2009-09-15 16:28:47.335875000 -0400
@@ -1001,8 +1001,8 @@

.. note::

- This function is called ``xsrange`` to distinguish it from the
- builtin Python ``xrange`` command.
+ ``sxrange`` is an alias for ``xsrange``. The ``x`` in the
name is to distinguish it
+ from the builtin Python ``xrange`` command.

EXAMPLES::

-----------------------------------------------------------------------------------------------

What I do not know how to do is to indicate that this is for the file

sage-4.1.1/sage/misc/misc.py

in

spkg/standard/sage-4.1.1.spkg

Is there a convention as to how to indicate this?

Mariah


Mariah

unread,
Sep 16, 2009, 9:15:39 AM9/16/09
to sage-support
Second attempt. Embarrassing typo in my first attempt.

---------------------------------------------------------------------------------------------
--- misc.py.orig        2009-09-15 16:25:22.983553000 -0400
+++ misc.py     2009-09-15 16:28:47.335875000 -0400
@@ -1001,8 +1001,8 @@

     .. note::

-       This function is called ``xsrange`` to distinguish it from the
-       builtin Python ``xrange`` command.
+       ``sxrange`` is an alias for ``xsrange``.  The ``x`` in the
name is to distinguish it
+       from the builtin Python ``srange`` command.

     EXAMPLES::

-----------------------------------------------------------------------------------------------

Mariah
Message has been deleted

Mariah

unread,
Sep 16, 2009, 10:22:13 AM9/16/09
to sage-support
Minh,

Ok, how does the following look? Like what you want?
The date looks funny.

------------------------------------------------------------
# HG changeset patch
# User mariah...@gmail.com
# Date 1253110290 14400
# Node ID 299c98688ef9ab55662e33f9b9e074de803de4c4
# Parent 684eea91ff224e5bc6259ca19f1576c4c082b9d3
user: mariah...@gmail.com
branch 'default'
changed sage/misc/misc.py

diff -r 684eea91ff22 -r 299c98688ef9 sage/misc/misc.py
--- a/sage/misc/misc.py Fri Aug 14 05:37:38 2009 -0700
+++ b/sage/misc/misc.py Wed Sep 16 10:11:30 2009 -0400
@@ -1001,7 +1001,8 @@

.. note::

- This function is called ``xsrange`` to distinguish it from the
+ ``sxrange`` is an alias for ``xsrange``. The ``s`` in the
name
+ stands for "Sage" and is to distinguish the command from the
builtin Python ``xrange`` command.

EXAMPLES::
--------------------------------------------------------------------


Note that the first time I did "$ hg ci" I had to edit the file
that my editor threw up to remove leading "HG" in some
lines. When I repeat the command, I now get

No username found, using 'mar...@localhost.localdomain' instead

I also had to edit the patch to put in my email address.
Is there a way (environment variable?) so that hg will pick
up my email address?

Mariah

Robert Bradshaw

unread,
Sep 16, 2009, 1:53:31 PM9/16/09
to sage-s...@googlegroups.com

Yes, you can put an .hgrc file in your home directory.

- Robert


Simon King

unread,
Sep 16, 2009, 2:47:33 PM9/16/09
to sage-support
Hi Mariah,

On 16 Sep., 15:29, Minh Nguyen <nguyenmi...@gmail.com> wrote:
[...]
> Do you mean producing a proper patch file using Mercurial? If yes,
> then you can use the Mercurial that's shipped with Sage like so. From
> SAGE_ROOT, you can do something along the lines of the following
> terminal session:
>
> [mvngu@sage sage-4.1.1-sage.math]$ ./sage
> ----------------------------------------------------------------------
> | Sage Version 4.1.1, Release Date: 2009-08-14                       |
> | Type notebook() for the GUI, and license() for information.        |
> ----------------------------------------------------------------------
> sage: !bash
> [mvngu@sage sage-4.1.1-sage.math]$ cd devel/sage-main/
> [mvngu@sage sage-main]$ emacs sage/misc/misc.py
>
> # or use your favourite editor to make changes to that misc.py file

There is also a different way in Sage:
For example,
sage: from sage.misc.misc import xsrange
sage: edit(xsrange,'vi')
would open your favourite editor (vi, of course...) exactly where
xsrange is defined. I find this a very clever and helpful tool, so,
cudos to whoever implemented it!

Then you can edit it, and save.

> [mvngu@sage sage-main]$ hg ci  # now check in your changes
> No username found, using 'mv...@sage.math.washington.edu' instead
> [mvngu@sage sage-main]$ hg export tip > ~/mypatch.patch
> [mvngu@sage sage-main]$ exit
> exit
> sage: exit

This can in principle also be done differently, whithout going to a
shell: Inside a sage session, there is "hg_sage", that provides you
with methods such as hg_sage.log(), hg_sage.commit(), hg_sage.export
(nb,patchname).
Just do "hg_sage?" for getting more information.

Best regards,
Simon

Dan Aldrich

unread,
Sep 16, 2009, 2:33:20 PM9/16/09
to sage-s...@googlegroups.com
I've been using sagenb for a few weeks now and have the hang of most
of the easy things. Is there a way to generate tables of a user
defined function? I'm still switching over to Excel or the TI
calculator to do that.

Thanks,
-d


Mariah

unread,
Sep 16, 2009, 4:58:57 PM9/16/09
to sage-support
Dan,

Please start a new thread rather than change the subject line.

Thanks!

Mariah

Jason Grout

unread,
Sep 16, 2009, 5:08:58 PM9/16/09
to sage-s...@googlegroups.com

On that new thread :), can you give an example of exactly what you
mean, i.e., give the output you want and describe what you have for input?

I think this will be very easy to do, but we have to see exactly what
you mean. For example, for a list:

f(x)=cos(x)-x
values=[(i, n(f(i))) for i in [0..2*pi,step=0.1]]
print values

or for a pretty output:

html.table(values)


Jason


Dan

unread,
Sep 16, 2009, 5:23:12 PM9/16/09
to sage-support
I've been using sagenb for a few weeks now and have the hang of most
of the easy things. Is there a way to generate tables of a user
defined function? I'm still switching over to Excel or the TI
calculator to do that. TI table function lets you punch in table
values and calculates values in table.

Thanks,
-d

x = var('x')
plot ((e^(2*x) - 1)/(x),(-.75,.75))

Want to see is table showing how f(x) as x->0 approaches e

.1
.01
.001
.0001
-.1
-.01
-.001
-.0001

Dan

unread,
Sep 16, 2009, 5:26:16 PM9/16/09
to sage-support
My apologies for posting in the other thread. Forgot to move up a
level.

Jason Grout

unread,
Sep 16, 2009, 5:31:10 PM9/16/09
to sage-s...@googlegroups.com
Dan wrote:
> My apologies for posting in the other thread. Forgot to move up a
> level.
>
> I've been using sagenb for a few weeks now and have the hang of most
> of the easy things. Is there a way to generate tables of a user
> defined function? I'm still switching over to Excel or the TI
> calculator to do that. TI table function lets you punch in table
> values and calculates values in table.
>
> Thanks,
> -d
>
> x = var('x')
> plot ((e^(2*x) - 1)/(x),(-.75,.75))
>

Try:

f(x)=(e^(2*x) - 1)/(x)
[n(f(i)) for i in [.1, .01, .001, .0001]]

Jason

Robert Bradshaw

unread,
Sep 16, 2009, 5:34:36 PM9/16/09
to sage-s...@googlegroups.com


sage: f(x) = (e^(2*x)-1)/x
sage: for t in [1..10]:
print 10^-t, f(10^-t).n()

1/10 2.21402758160170
1/100 2.02013400267558
1/1000 2.00200133400028
1/10000 2.00020001333542
1/100000 2.00002000012319
1/1000000 2.00000200001523
1/10000000 2.00000020116568
1/100000000 2.00000001490116
1/1000000000 2.00000000000000
1/10000000000 2.00000000000000

Dan Drake

unread,
Sep 16, 2009, 6:56:53 PM9/16/09
to sage-s...@googlegroups.com
On Wed, 16 Sep 2009 at 07:22AM -0700, Mariah wrote:
> Note that the first time I did "$ hg ci" I had to edit the file
> that my editor threw up to remove leading "HG" in some
> lines. When I repeat the command, I now get
>
> No username found, using 'mar...@localhost.localdomain' instead
>
> I also had to edit the patch to put in my email address.
> Is there a way (environment variable?) so that hg will pick
> up my email address?

You should definitely check out the Mercurial website, if you haven't
already: http://www.selenic.com/mercurial/ and also the Mercurial book:
http://hgbook.red-bean.com/

You sound like you're pretty comfortable with the basic diff and patch
utilities, so I'm sure you'll figure out Mercurial very quickly.

Dan

--
--- Dan Drake
----- http://mathsci.kaist.ac.kr/~drake
-------

signature.asc

Mariah

unread,
Sep 21, 2009, 10:26:42 AM9/21/09
to sage-support
Dan,

As you suggested, I took a look at the Mercurial website and
quickly found that putting the following in my .hgrc file
would make hg use my email address rather than
'mar...@localhost.localdomain'

[ui]
username = Mariah Lenox <mariah...@gmail.com>

> You sound like you're pretty comfortable with the basic diff and patch
> utilities ...

Actually, I am not. I just followed the directions that Minh gave.

Is there a "How to make a Sage patch (for newbies)" page anywhere?
If not, there should be. (And yes, if not I volunteer to write a
first draft
if a Sage developer will suggest where it should go, review, etc.)

Mariah
Message has been deleted

Mariah

unread,
Sep 22, 2009, 9:08:00 AM9/22/09
to sage-support
Minh,

> Tutorials, tips and techniques on making and managing patches should
> go in the Developers' Guide at
>
> http://www.sagemath.org/doc/developer/

Aha! This already has the stuff I was going to write up.

The reason that I never looked at this is that I do not consider
myself
a "developer". I found the Sage documentation for sxrange/xsrange
confusing,
William suggested I write a patch for the documentation, and I tried
to do so.

The section that you referenced "Producing Patches with Mercurial" is
in the second half of the Developers' Guide. I never would have found
it without your pointing me at it.

I respectfully suggest that this section be split out to a "How to
make
a Sage patch (for newbies)" ... or at the very least moved to the
beginning
of the Developers' Guide.

Mariah
Reply all
Reply to author
Forward
Message has been deleted
0 new messages