6 ms·
It matters when doing multidimensional index arithmetic. The formulae for ranking and unranking multi-dimensional to flat indexes are neater with 0-indexing th
by Athas 6y ago
It matters when doing multidimensional index arithmetic. The formulae for ranking and unranking multi-dimensional to flat indexes are neater with 0-indexing than with 1-indexing.
- ogogmad 6y agoFair enough. But is this a common enough occurrence in the life of a programmer that it's worth being a religious extremist about?
- leephillips 6y agoNo need for an inquisition. In both Fortran and Julia you can set your index origin to be whatever works best for the problem you are expressing.
- remram 6y agoIt's in a list of annoyances, together with 16 others. I don't think anyone is being "a religious extremist about" it...
- improbable22 6y agoJulia has really nice abstractions for dealing with this. Things for which you might do such calculations in other languages can often be done in a way generic to the number of dimensions, as well as the initial index, using CartesianIndices & friends. And this should be zero cost.
- Athas 6y agoAlmost all languages above the C level has nice abstractions for this, but you still sometimes need to do it for various reasons. Basically, whenever the indexes don't matter, then 1-indexing and 0-indexing are equivalently good, but when they do matter, then 0-indexing leads to neater calculations.
- 7thaccount 6y agoThere are plenty of matrix uses where 1-indexing is more natural. I think the main reason Julia uses it is to be more consistent with Matlab.
- fghorow 6y agoOffsetArrays.jl is your friend. I'm working on a project where indexing naturally runs from -n to n. I kludged the usual index hackery and was so plagued with off-by-one errors that I gave up on it. Then I remembered Tim Holy had a blog post about OffsetArrays.jl and started using that. Problem solved. Seriously, the most annoying thing I find about Julia is that the various packages fairly often have "creative" names, that make it difficult discover that they might actually be useful. YMMV.
- porker 6y ago> I'm working on a project where indexing naturally runs from -n to n. I kludged the usual index hackery and was so plagued with off-by-one errors that I gave up on it. Then I remembered Tim Holy had a blog post about OffsetArrays.jl and started using that. Ah, thank you. I've been tinkering around with Julia, and was trying out 2D random walks. Not being able to use a 2 dimensional array and store the position [0, 0] confused me. I ended up starting at X,X (where X > total number of moves) and then got stuck with plotting it nicely, because I wanted the plot axes to show how far it had moved, not X +- how far it had moved.
- ntoshev 6y agoAbstraction discovery is a hard problem in general. Good names help, but there's probably more we can do. Tooling finding a common pattern that the abstraction eliminates, perhaps?
- improbable22 6y agoI guess it could poke you every time you say `strides(A)`? Here's a recent example of an upgrade, from code with stride calculations to a more abstracted version, which is generic to offsets & number of dimensions: https://github.com/JuliaLang/julia/pull/37367/files#diff-29c1bc2da57123cc2bfd6741dda44e9dR105 https://github.com/JuliaLang/julia/pull/37367/files#diff-29c...
- TheRealKing 6y agoIn Fortran, you can start the index from any number you want. A(-10:-3) is a completely valid slice or full array declaration, so are A(0:7) or A(1:8). and this feature is absolutely useful in scientific computing.