8 ms·
Reading through the docs, looks like they have 1-indexed arrays..?
by typon 8y ago
Reading through the docs, looks like they have 1-indexed arrays..?
- computerphage 8y agoSo does Matlab/Octave and R, so Julia's choice is unsurprising to many people in the target demographic.
- jordigh 8y agoAnd Matlab and Octave in turn inherited it from Fortran, which inherited it from classical numerical analysis. One-based indexing has a good, long history.
- creativeembassy 8y agoTo use a silly example... can I finally realize my dream of two-based indexing?
- ChrisRackauckas 8y agoYes. And 1.5-based indexing, though I would recommend using rational numbers instead of floating point numbers for this one.
- JulianMorrison 8y agoAnd imaginary is right out.
- stabbles 8y agoMaybe you could use complex numbers to index matrices
- 3JPLW 8y agoSure can: # Magic julia> Base.to_indices(A, inds, I::Tuple{Complex, Vararg{Any}}) = (real(I[1]), imag(I[1]), to_indices(A, Base._maybetail(Base._maybetail(inds)), Base.tail(I))...) julia> A 4×5 reshape(::UnitRange{Int64}, 4, 5) with eltype Int64: 1 5 9 13 17 2 6 10 14 18 3 7 11 15 19 4 8 12 16 20 julia> A[3 + 2im] 7 ```
- ChrisRackauckas 8y agoAnd quaternions for image stacks in ML? Troll, but not too troll...
- elcritch 8y agoThat’s a right kind of wrong... and could work with FEA tensor too... ;)
- jordigh 8y agoStory time! When Octave was getting a lot of traffic from the first Coursera machine learning class, when it was just Andrew Ng doing a course and before the Coursera org existed, we were getting a lot of novice users who would do something like some_matrix(i,j) = 5 and get cryptic errors about how matrices cannot be indexed by complex numbers. This is because by default, in Matlab and Octave `i` and `j` are functions that evaluate to the imaginary unit. You have to overwrite the function names with `i=2, j=3` or whatever beforehand, which novices often forget. This was happening often enough that I pushed some patches to warn, "did you forget to assign i or j?" if someone tried to index matrices with complex numbers. Point is, people in Matlab and Octave often unintentionally try to index by complex numbers.
- currymj 8y agoApart from being close to MATLAB, a goal of Julia is to make it easy to just type mathematical formulas straight off the page and into your code. 1-based indexing makes this so much easier, even if it seems somewhat depraved to a computer scientist.
- kmm 8y agoIn my experience, 0-based indexing is way more common in mathematics. Maybe it depends on the field you're working in. Off the top of my head, dimensions are numbered from zero in relativity and particle physics, Fourier series only make sense with 0-based indexing, coefficients of polynomials are numbered from zero, Taylor series, indexing by taking the modulo ...
- simonbyrne 8y agoA slight quibble: FFTs are more naturally indexed periodically. Luckily, Julia has that covered: https://github.com/JuliaArrays/FFTViews.jl https://github.com/JuliaArrays/FFTViews.jl
- dagw 8y agoThat is correct, mainly due to coming from Fortran and Matlab background. However they also have offset arrays (like in Fortran) if you need arrays that start on a different index.
- ur-whale 8y agoDoes that work properly with multi-dimensional arrays?
- KenoFischer 8y agoOf course.
- Q6T46nT668w6i3m 8y agoYes. Julia’s mission was (and still includes) providing an alternative to 1-indexed MATLAB.
- peatmoss 8y agoFor a numerics-oriented language this is a good choice. It’s more consistent with mathematical conventions for matrices. 0-based indexes are commonplace in programming circles because of the C language, however 1-based indexes are the earlier standard set by Fortran. EDIT: If Julia becomes popular outside data and numerics circles, it will have pulled off nothing short of a miracle in getting people to adopt 1-based indexes. This feud is older than vi vs. emacs. :-)
- Accipitriform 8y agoJulia supports 0 based indexes if desired, similar to Ada and other languages.
- stabbles 8y agoIf you feel uncomfortable, note that Julia internally has some indirection to allow for custom offsets. See for instance OffsetArrays.jl [1] [1] https://github.com/JuliaArrays/OffsetArrays.jl https://github.com/JuliaArrays/OffsetArrays.jl
- eigenspace 8y ago... and?
- stellalo 8y ago1-based indexing is a non-issue
- ur-whale 8y agoIt most certainly is for the vast number of folks who code in C, C++, Java, Python, etc ... It will for sure hurt adoption.
- weatherlight 8y agoI'm pretty sure if people are willing to adopt a new language, they will be open minded enough to accept 1 indexed arrays.
- ur-whale 8y agoHistory has shown the exact opposite.
- DandyDev 8y agoExamples?
- ur-whale 8y agoYou just have to look at the most popular programming languages on 2018. They're all 0-based (with maybe R as an exception, but R is a niche language. If that's what Julia aims to remain -- their loss). The folks who designed these languages knew how not to alienate their future market.
- cultus 8y agoMatlab is 1-based. That's what the vast majority of scientific programmmers still use. Worrying about 0 vs 1 is just useless bikeshedding, really. I've done lots of index heavy operations in Julia and in Python, and it's easy either way.
- JulianMorrison 8y ago0-based feels like offsetting memory to me. In a high level language, you aren't doing that.
- ur-whale 8y agoYeah, same here. Once I hit on that, it was really hard to convince myself to read further.
- chappi42 8y agoYour loss, maybe?
- ur-whale 8y agoI didn't say I stopped reading.
- psychometry 8y agoIf 1-indexing seems strange, you're probably not doing mathematical or statistical computing, so it's probably not the language for you.
- ur-whale 8y agoI've done math and stats all my life. When I do sigmas in math, they go: i=0,i<n and very rarely i=1, i<=n unless the problem really is made simpler by the weird 1-based indices.
- dangjc 8y agoPorting existing C, C++ library code hand tailored with 0 based indexing is incredibly painful and error prone. And unfortunately, that’s what most libraries used underneath python or R wrappers use.
- timholy 8y agoFor all those worried about whether Julia's indexing is good enough, let's broaden the conversation a bit. How good is your favorite language at, say, iterating along dimension m of an n-dimensional array, where m and n are not known by the programmer, and where you can't count on the array being strided? Trivial and extremely efficient in Julia: https://julialang.org/blog/2016/02/iteration https://julialang.org/blog/2016/02/iteration.
- pkofod 8y agoI know, right? Every time I want to read the top post on hacker news I always spend 10 minutes looking for it... but then I realize that HN labels the top post "1" and not "0". So annoying.