Hello everyone. First time here, so please be forgiving of any missteps on my part. This is an idea I have thought about for a while, and I believe it could be quite useful. Would it be possible to enhance the standard range() type to allow for elementary operations on ranges? For example : range(10)  range(5) => range(5, 10) range(3, 5, 2) + range(3, 6, 2) => range(3, 6, 2) I believe this could be a fair first step into allowing for a whole range of mathematical operations in abstract algebra.
On Mon, 1 Aug 2022 at 13:50, <arvindsistla@gmail.com> wrote:
Hello everyone. First time here, so please be forgiving of any missteps on my part. This is an idea I have thought about for a while, and I believe it could be quite useful. Would it be possible to enhance the standard range() type to allow for elementary operations on ranges? For example : range(10)  range(5) => range(5, 10) range(3, 5, 2) + range(3, 6, 2) => range(3, 6, 2)
There are a lot of complex cases you'd need to consider. What would the value of range(3, 15, 2) + range(8, 12, 2) be? It's not a range in the sense of being describable as (start, end, step). And simply saying "that's not allowed" wouldn't really work, as it would be far too hard to work with if operations could fail unexpectedly like this. In reality, this feels more like you're after set algebra, which Python already has. I believe this could be a fair first step into allowing for a whole range
of mathematical operations in abstract algebra.
For cases where the number of elements in the range is not ridiculously large, simply converting to sets is probably sufficient:
set(range(10))  set(range(5)) == set(range(5, 10)) True set(range(3,5,2))  set(range(3,6,2)) == set(range(3,6,2)) True
For sufficiently large ranges, there are bitset classes on PyPI that might be more memory efficient. Or if you're looking for some other types of operation (unbounded ranges, or specialised abstract algebra operations) a custom class (maybe even published on PyPI) would probably be a better approach than trying to get something that specialised added to the stdlib. Paul
For cases where the number of elements in the range is not ridiculously large, simply converting to sets is probably sufficient:
Sets don’t preserve order, which may or may not be important— but as the IP didn’t provide a use case, I have no idea. To the OP: this idea isn’t likely y on be accepted into Python itself without some compelling use cases. But it’s nifty idea . A great candidate for a prototype library that you can use to work out the details and demonstrate its utility. Once you’ve done that, it may (or may not) make sense to propose an extension to tbe builtin range object. CHB
set(range(10))  set(range(5)) == set(range(5, 10)) True set(range(3,5,2))  set(range(3,6,2)) == set(range(3,6,2)) True
For sufficiently large ranges, there are bitset classes on PyPI that might be more memory efficient. Or if you're looking for some other types of operation (unbounded ranges, or specialised abstract algebra operations) a custom class (maybe even published on PyPI) would probably be a better approach than trying to get something that specialised added to the stdlib.
Paul _______________________________________________ Pythonideas mailing list  pythonideas@python.org To unsubscribe send an email to pythonideasleave@python.org https://mail.python.org/mailman3/lists/pythonideas.python.org/ Message archived at https://mail.python.org/archives/list/pythonideas@python.org/message/EMDUKI... Code of Conduct: http://python.org/psf/codeofconduct/
 Christopher Barker, PhD (Chris) Python Language Consulting  Teaching  Scientific Software Development  Desktop GUI and Web Development  wxPython, numpy, scipy, Cython
Thanks so much Christopher. I have several ideas for use cases, but you're right, it's probably better off as a library. That's exactly what I'll do now.
You're right, sets does make sense in these specific use cases, but I want to preserve order too. You're also right, a custom class is a much better idea. Thanks so much.
On Mon, Aug 1, 2022, at 09:19, Paul Moore wrote:
There are a lot of complex cases you'd need to consider. What would the value of range(3, 15, 2) + range(8, 12, 2) be? It's not a range in the sense of being describable as (start, end, step). And simply saying "that's not allowed" wouldn't really work, as it would be far too hard to work with if operations could fail unexpectedly like this. In reality, this feels more like you're after set algebra, which Python already has.
Maybe it would make sense for the type of the result to devolve into a sorted set (do we have a sorted set type now?) if it's not representable as a range.
These kind of problems can easily be handled with numpy:
import numpy as np print(np.union1d(np.arange(3,15,2), np.arange(8,12,2))) [ 3 5 7 8 9 10 11 13]
Op 3/08/2022 om 21:23 schreef Random832:
On Mon, Aug 1, 2022, at 09:19, Paul Moore wrote:
There are a lot of complex cases you'd need to consider. What would the value of range(3, 15, 2) + range(8, 12, 2) be? It's not a range in the sense of being describable as (start, end, step). And simply saying "that's not allowed" wouldn't really work, as it would be far too hard to work with if operations could fail unexpectedly like this. In reality, this feels more like you're after set algebra, which Python already has.
Maybe it would make sense for the type of the result to devolve into a sorted set (do we have a sorted set type now?) if it's not representable as a range. _______________________________________________ Pythonideas mailing list  pythonideas@python.org To unsubscribe send an email to pythonideasleave@python.org https://mail.python.org/mailman3/lists/pythonideas.python.org/ Message archived at https://mail.python.org/archives/list/pythonideas@python.org/message/PKGCHW... Code of Conduct: http://python.org/psf/codeofconduct/
On Mon, 1 Aug 2022 at 13:50, <arvindsistla@gmail.com> wrote:
Hello everyone. First time here, so please be forgiving of any missteps on my part. This is an idea I have thought about for a while, and I believe it could be quite useful. Would it be possible to enhance the standard range() type to allow for elementary operations on ranges? For example : range(10)  range(5) => range(5, 10) range(3, 5, 2) + range(3, 6, 2) => range(3, 6, 2)
SymPy has a symbolic Range object that interacts with its other Set types: In [9]: from sympy import * In [10]: Range(10)  Range(5) Out[10]: {0, 1, …, 9} \ {0, 1, …, 4} In [11]: list(_) Out[11]: [5, 6, 7, 8, 9] In [12]: Range(3, 5, 2) + Range(3, 6, 2) Out[12]: {3} ∪ {3, 5} That last example could be simplified but I guess the simplification code for it has not been added. https://docs.sympy.org/latest/modules/sets.html#range The Range type is a bit weird because other SymPy Sets are not considered to be ordered but Range is. Some operations will not preserve the ordering because it isn't really well defined in general set arithmetic: In [35]: Range(10) Out[35]: {0, 1, …, 9} In [36]: Range(10)[::1] Out[36]: {9, 8, …, 0} In [37]: Range(10) & Range(10)[::1] Out[37]: {0, 1, …, 9} In [38]: Range(10)[::1] & Range(10) Out[38]: {0, 1, …, 9}
I believe this could be a fair first step into allowing for a whole range of mathematical operations in abstract algebra.
Take a look at SymPy's sets module. There are various other kinds of sets and things such as: In [26]: Range(2) * Range(3) Out[26]: {0, 1} × {0, 1, 2} In [27]: list(_) Out[27]: [(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (1, 2)] In [28]: Range(10)  {3} Out[28]: {0, 1, …, 9} \ {3} I don't think it makes sense to make any changes to the builtin range objects for these kinds of operations.  Oscar
participants (6)

arvindsistla＠gmail.com

Benedict Verhegghe

Christopher Barker

Oscar Benjamin

Paul Moore

Random832