11 ms·
Like Matlab and Lua, 1-based indexing :(
by erwincoumans 6y ago
Like Matlab and Lua, 1-based indexing :(
- ogogmad 6y agoThis small detail always comes up for some reason. Anyway, I recently tried implementing some numerical linear algebra algorithms based on descriptions from papers and books. The books and papers all used 1-based indexing. This created some problems for me when I translated the pseudo-code to Python (which is 0-based).
- amelius 6y agoPython has other quirks, such as a[-1] being the last element of the array.
- st1x7 6y agoIt's really a strength, not a quirk. Negative indexing and array slicing in general are great in Python. Really easy to pick up and way more convenient than any other language that I've come across.
- ogogmad 6y ago> array slicing in general) are great in Python Without checking, I'm not sure whether a[-1:0:-1] reverses a list. I'm not sure if it includes the first element of the list or not [edit: It doesn't]. I'm not sure why in contrast a[::-1] does reverse a list. I found array slicing (and ranges) to be a source of confusion when programming the aforementioned linear algebra algorithms. IMO, the Julia approach is better: 3:-1:1 produces [3,2,1]. Both the starting and ending points of the range are included.
- hansvm 6y agoThe example a[-1:0:-1] should be pretty understandable once you've spent enough time in the language -- you're inclusive at -1 and exclusive at 0, so the list is reversed and is missing its formerly first element (should one exist). That logic is pretty consistent. The start is always inclusive, so it needs to be `len(a)-`, `-`, or `None`. The end is always exclusive, so you need to choose `None`. As a result, counting all the syntactic sugar available to you, you have 8 slices that can reverse a list. To name a few you have a[::-1], a[None:None:-1], and a[-1::-1]. IMO a much more interesting example for the strengths of inclusive indexes is a[len(a)-1:-1:-1]. The result is always empty, but it wouldn't be too much of a stretch to think you included len(a)-1, you decremented to -1 exclusively (thus including 0), and hence reversed the entire list. The problem is that -1 is a valid index, and unlike the a[0:len(a)] case you don't have any values "before" the beginning of the list to include 0 in an _exclusive_ expression. It's all a bit of a moot point though. I know Python especially chose its [inclusive,exclusive) convention largely because it wanted expressions like range(len(a)) to not require additional arithmetic for common use cases given that it had zero-based indexing. Julia has one-based indexing, so for common use cases an [inclusive,inclusive] pattern falls out as the most natural choice. I have no idea if Julia actually cared about that sort of thing or if such a convention came about by accident, but it seems like a clean choice for a one-based language.
- shele 6y agoOhm, a lot of thought has gone into indexing in Julia. Julia allows indexing by position `A[1,1]`, slicing `A[1:5, :]`, linear indexing `A[1]`, and logical indexing `A[a.==1]`, relative indexing `A[end-1]` and cartesian indexing `A[CartesianIndex(1,1)]`. The way Julia combines these in a mostly non-conflicting and non-confusing manner is a major engineering feat. For example the following rule was found to be the just the right balance between permissive and strict behaviour: * You are permitted to index into arrays with more indices than dimensions, but all trailing indices must be 1. * You are permitted to index into arrays with fewer indices than dimensions, but the length of all the omitted dimensions must be 1.
- amelius 6y agoDo you really think so? I'd personally feel more reassured if a[-1] caused an assertion to fail. Python should have used a different notation, imho. Like: a[<0] for the last element.
- hansvm 6y agoI don't know; the grammar is already pretty complicated without introducing additional indexing schemes. Now that you mention it though, I don't know that I've ever written an algorithm mixing and matching negative/positive indices in a way that couldn't be trivially re-written with that kind of syntax. I'm sure such cases exist for somebody... Looking for solutions, if you're stuck with Python and hate that behavior: - If you don't need errors raised then a[~i] is already equal to a[-i-1]. - In your own code (or at its boundaries when wrapping external lists) it's nearly free and not much code to subclass list, override __getitem__, and raise errors for negative indices while responding to some syntax like a[0,] or a.R[0].
- StefanKarpinski 6y agoNegative indexing in Python is a dangerous design that hides bugs and causes incorrect programs to return garbage instead of erroring. If you have an incorrect index computation, instead of getting a bounds error, you get different indexing behavior. This makes it dangerously easy to write code that appears to work but computes nonsense.
- tester756 6y agoSometimes I do wonder... maybe 1 based indexing is better and would cause less off by 1 bugs for non-US devs?
- johnmyleswhite 6y agoNot the other way around? My experience is that the US uses 1-based indexing for floors in buildings and that Western Europe uses 0-based indexing for floors in buildings.
- ninjin 6y agoIndeed: https://en.wikipedia.org/wiki/Storey#Numbering https://en.wikipedia.org/wiki/Storey#Numbering
- QuesnayJr 6y agoNorway uses 1-based, which confused me once I was used to 0-based.
- Someone 6y agoCorrections welcome, but I think Europeans don’t count floors, they count additional storeys, 1-based. I think that’s influenced from French, which has parterre (“on the ground”) for the ground floor, and étage, derived from Latin stare, “to stand”, for higher (and lower) storeys (in the end, both may come from Latin) If Europeans use 0-based counting for floors, I would expect at least some language to say “zeroth floor”. I’m not aware of any.
- dunefox 6y agoAs a German, it's Erdgeschoß for ground floor, then first, second, third, etc. for the floors above ground.
- hibbelig 6y ago> If Europeans use 0-based counting for floors, I would expect at least some language to say “zeroth floor”. I’m not aware of any. I feel that argument does not hold water. Nobody says “second month” or “month two” in English, everyone says “February”. So it sometimes happens that a number is assigned to something, but nobody uses the number in regular speech. I feel that the ground floor is similar: it's got a number assigned to it, but everybody uses a different word, instead (parterre in French, Erdgeschoss in German, ...). In Germany at least, it is common to find negative floor numbers, denoting floors that are below ground. For example, an elevator might have these buttons (top to bottom): 6, 5, 4, 3, 2, 1, 0, -1, -2 But it is also common to find some other letters on the buttons, instead of the numbers. E.g.: P2, P1, 4, 3, 2, 1, E, U1, U2 P2 and P1 would be the car park, E would be Erdgeschoss = parterre = ground floor, U1 and U2 would be “underground” (below ground).
- rscho 6y agoMost languages designed for scientific (vs. business) use cases are 1-based. Mathematicians prefer it to zero.
- lahvak 6y agoA mathematician here. The problem is that both 0 and 1 are useful in different situations: when dealing with sequences and series, especially ones arising from discrete models where the important thing to consider is going from one term to the next one (as is always the case when dealing with series), or when sequences are used as discrete approximations of a continuous process, it usually makes much more sense to start indexing with zero. On the other hand, using zero indexing with matrices and vectors leads to madness.
- Jtsummers 6y agoI've seen this in other Julia discussions. Can someone show me a use case other than trying to cycle over the same data (EDIT: or range) repeatedly where 0-based is fundamentally better than 1-based? I mean, that's the only case I can think of where 0-based is actually going to be easier and more sensible. But I'd argue that languages should stop having fixed offsets, 1-based and 0-based are both too limiting. Ada is around 40 years old (and it's not unique in this) and it provides arbitrary index ranges and the option to use any discrete type as the index so that you can use whatever index is most natural for your particular problem.
- warlog 6y agoIf I'm not mistaken, Lua let's you assign any key (index) you want.
- Jtsummers 6y agoLua's arrays/tables are more like hash tables, with an ability (still true? been a while since I've done anything with Lua) to optimize if you use a contiguous range starting at 1 for the key. But yes, you can use an arbitrary key.
- hobby-coder-guy 6y agoStill true.
- ogogmad 6y ago(lets not let's)
- patagurbon 6y agoI believe Julia lets you do this, although I'm not sure about the ease or generality of it.
- Jtsummers 6y agoApparently you can [0]. However, it seems (in my first reading) to be something I'd avoid because it sounds like the "natural" way to code in Julia is to assume 1-based and you're likely to run into issues with libraries if you try and do anything else. [0] https://docs.julialang.org/en/v1/devdocs/offset-arrays/ https://docs.julialang.org/en/v1/devdocs/offset-arrays/
- warlog 6y agoAnd R! 1-based works for folks that count things (Scientists).
- cambalache 6y agoAs it should be.0-based indexing is at best another equally valid convention. It is like the other eternal debate about what number is ground floor.
- st1x7 6y agoI need to switch between zero- and one-indexed languages often. It really doesn't make a difference.
- michaelcampbell 6y agoAll this discussion and Stan Kelly-Bootle's quote hasn't come up yet? Shameful. (For the uninitiated: > Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. -- Stan Kelly-Bootle )
- QuesnayJr 6y agoI have never heard that quote before. That's great.
- ModernMech 6y agoIn languages like C it's useful to have 0-based indexing because it's not really an index at all but a pointer offset. You don't do pointer math in languages like Julia and Matlab (and Fortran), but instead actual indexing, so 1 is a better place to start. Also, 1-based indexing is easier to teach those new to programming, especially children. 0-based indexing is a significant stumbling block for people, since they are used to counting from 1, which leads to all kinds of off-by-one errors.
- ur-whale 6y ago> Like Matlab and Lua, 1-based indexing :( This should be the first line of the document.
- Mauricebranagh 6y agoAnd Fortran before them, it makes sense for technical programming