Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

irange --- also add range that includes the endpoints by default #706

Closed
williamstein opened this issue Sep 20, 2007 · 4 comments
Closed

Comments

@williamstein
Copy link
Contributor

From Jaap Spies (see attached):


sage: v = irange(0,5); v
[0, 1, 2, 3, 4, 5]
sage: v = irange(1,10); v
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sage: v = irange(10,-1,-1); v
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1]
sage: v = irange(1,8, 1/2); v
[1, 3/2, 2, 5/2, 3, 7/2, 4, 9/2, 5, 11/2, 6, 13/2, 7, 15/2, 8]
sage: v = irange(1,2, 0.4); v
[1, 1.40000000000000, 1.80000000000000]
sage: v = irange(1, 2, 0.5); v
[1, 1.50000000000000, 2]
sage: v = irange(1, 2, -0.5); v
[]
sage: v = irange(2, -2, -0.5); v
[2, 1.50000000000000, 1.00000000000000, 0.500000000000000, 0.000000000000000, -0.500000000000000, -1.00000000000000, -1.50000000000000, -2]
sage: v = irange(10,1); v
[]
sage: v = irange(10,10); v
[10]
sage: v = irange(10); v
Traceback (most recent call last):
...
TypeError: irange() takes at least 2 arguments (1 given)
sage: v = irange(0.5, 2.5, 0.5); v
[0.500000000000000, 1.00000000000000, 1.50000000000000, 2.00000000000000, 2.50000000000000]
sage: [n^2 for n in irange(-1, 10)]
[1, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

And this one from the calculus thread!
> --  I think that the Python convention of not including the upper bound
>> > in a sum is a real problem.
>> >
>> > sage: sum(i for i in range(1,10))
>> > 45
>> >
>> > I understand this is a fundamental convention in Python, and that it is
>> > very
>> > natural for people used to malloc(), but I worry that this will be a
>> > constant
>> > headache for students (and professors!).


sage: sum(i for i in irange(1, 10))
55

I think including this is a good idea, modulo serious optimization issues.

Component: basic arithmetic

Issue created by migration from https://trac.sagemath.org/ticket/706

@williamstein
Copy link
Contributor Author

Attachment: irange_improved.hg.gz

@williamstein

This comment has been minimized.

@jaapspies
Copy link
Member

Attachment: irange.hg.gz

Replying to @williamstein:

From Jaap Spies (see attached):


sage: v = irange(0,5); v
[0, 1, 2, 3, 4, 5]
sage: v = irange(1,10); v
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sage: v = irange(10,-1,-1); v
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1]
sage: v = irange(1,8, 1/2); v
[1, 3/2, 2, 5/2, 3, 7/2, 4, 9/2, 5, 11/2, 6, 13/2, 7, 15/2, 8]
sage: v = irange(1,2, 0.4); v
[1, 1.40000000000000, 1.80000000000000]
sage: v = irange(1, 2, 0.5); v
[1, 1.50000000000000, 2]
sage: v = irange(1, 2, -0.5); v
[]
sage: v = irange(2, -2, -0.5); v
[2, 1.50000000000000, 1.00000000000000, 0.500000000000000, 0.000000000000000, -0.500000000000000, -1.00000000000000, -1.50000000000000, -2]
sage: v = irange(10,1); v
[]
sage: v = irange(10,10); v
[10]
sage: v = irange(10); v
Traceback (most recent call last):
...
TypeError: irange() takes at least 2 arguments (1 given)
sage: v = irange(0.5, 2.5, 0.5); v
[0.500000000000000, 1.00000000000000, 1.50000000000000, 2.00000000000000, 2.50000000000000]
sage: [n^2 for n in irange(-1, 10)]
[1, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

And this one from the calculus thread!
> --  I think that the Python convention of not including the upper bound
>> > in a sum is a real problem.
>> >
>> > sage: sum(i for i in range(1,10))
>> > 45
>> >
>> > I understand this is a fundamental convention in Python, and that it is
>> > very
>> > natural for people used to malloc(), but I worry that this will be a
>> > constant
>> > headache for students (and professors!).


sage: sum(i for i in irange(1, 10))
55

I think including this is a good idea, modulo serious optimization issues.

irange now only depends on srange. So ticket #701 will solve this issue.

patch 'irange_improved.hg' is relative to 'irange.hg'

@williamstein
Copy link
Contributor Author

comment:5

I think #702 is actually much nicer to use, so for now I think
irange isn't needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants