Because it's not backward-incompatible with the behaviour of positive indices.

Le jeu. 26 nov. 2020 à 05:56, Mathew M. Noel via Python-ideas <python-ideas@python.org> a écrit :


Why not use list_name[-n%N] whenever you need to use negative indices and raise an index out of bounds exception with negative indices like other programming languages?





From: Calvin Spealman <cspealma@redhat.com>
Sent: Thursday, November 26, 2020 1:00 AM
To: Mathew M. Noel
Cc: python-ideas@python.org; mail@pradyunsg.me
Subject: Re: [Python-ideas] Getting rid of FOR loops and simplifying cicular conviolutions with Circular Indexing
 
days_of_the_week[14 % 7]

There ya go!

On Wed, Nov 25, 2020 at 12:51 PM Mathew M. Noel via Python-ideas <python-ideas@python.org> wrote:

If circular indexing is used then instead of using a double FOR loop to go through a list M times we can iterate from 0 to M*N (where N is the length of the list) !!!


Almost all Machine Learning (ML) algorithms iterate for some predefined epochs over a large data-set. So a double FOR loop is extremely common in ML. Using circular indexing gets rid of this extra FOR loop. If we have to iterate 2 times you can iterate using range(-n,n) but in most cases you need to iterate over 10 or more epochs in ML.


Most scientific applications of Python involve an outer FOR loop which progressively refines an approximation with an inner FOR loop by going through a list of items. So circular indexing is useful. In the following I discuss increasingly compelling reasons for adopting a circular indexing scheme in Python.


Python uses an index of -1 to index the last element in a list. Since -1 occurs before 0 we might think of the elements of the linear list are being bent into a circle making the last element occur before the 0th element. Consider a list with n elements: it would be perfectly reasonable to address the element 0 of the list using an index of n since n occurs after n-1 (if we assume that the list is bent into a circle). This feature can prove to be extremely useful. Consider the following example:


days_of_the_week = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]

It would be nice if
days_of_the_week[0]
is the same as
days_of_the_week[7] 

is the same as
days_of_the_week[14] etc

In other words use modular indexing. In other words if the index is outside the range 0 to n-1, we simply take the remainder when the index is divided by n as the index.
Because of the close relationship between finite length sequences and periodic sequences this feature might simplify scientific computing(circular convolution etc).
If circular indexing is used then we don't need the arbitrary rule that -1 is the index of the last element. Since -1 is the same as n-1 automatically in modular arithmetic.


A trivial objection:  "why not use list_name[i%n] whenever we need this feature?" By the same token we could do away with negative indices and use -1%n for example when we need to index with -1!


Its unclear why that people have an irrational preference for indices that lie to the left of 0 while strongly rejecting the idea of indices that lie to the right of n-1!

Python does not raise a "index out of bound" exception for negative indices like other programming languages. If this negative indexing is a "feature" (although it allows some fatal errors to slip) then indices above n-1 can also be considered a feature!

Are there any deep mathematical reasons for adopting  circular convention?
Circular convolution is a most important operation in a wide variety of scientific disciplines since the Discrete Fourier Transform (DFT) of the circular convolution of two signals is the product of the transforms. Because of the universal applicability of Fourier ideas in science and the close mathematical relationship between finite length and periodic sequences circular indexing is extensively used in signal processing and mathematics.

We can extend the idea of circular indexing to multidimensional arrays. A 2D array can be folded into a cylinder for indexing. Further this cylinder can be folded into a toroid to reduce a triple FOR loop to a single FOR loop. A deep mathematical justification for cylindrical indexing of 2D and in general nD arrays is offered by the fact that n-dimensional DFT reduces n-dimensional circular convolution to element-wise multiplication.

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/5TJYKFLBHB26WEFFQXMY6AGWS34XTIUR/
Code of Conduct: http://python.org/psf/codeofconduct/


--

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

cspealma@redhat.com  M: +1.336.210.5107

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/RRW5ZMPRJBOYATS7U2N7SGAWRYSNF4NY/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Antoine Rozo