It seems like an absurd question, but is the logical conclusion I got to (for Vector
s) through, yes, a contrived usage of OffsetArrays, explained here: Add an abstract type signaling e.g. OffsetArray safe to use (also add badges to packages) · Issue #284 · JuliaArrays/OffsetArrays.jl · GitHub
It’s not unheard of disallowing indexing, i.e. for changing an array (see StaticArray
s and immutability is often a good thing), but to also have indexing disallowed for looking up in an array seems a bit extreme. It seems like the most basic property you want to have for an array/vector.
I might be wrong about my “logical conclusion” (in math or if Julia where different, but seems so for the status quo), but lets first pretend I’m right.
For a diagonal of an array to neither be logically 1-based nor 0-based (always) doesn’t mean it doesn’t exists, only problematic how you index it.
What do you want to do with the diagonal? The trace comes to mind:
tr(A) = sum(diag(A))
You can define it without referring to any specific index of diag(A).
But as soon as you try to add up, you want something like:
sum(A) = sum = 0; for i in eachindex(A) ...
At this lower level you DO want to index into the array. I suppose the internals of diag(A) could define a 1-based Vector (what it does now), and that sum would have access to it, by piracy, but otherwise you have no (public) access to A[i]. It does however seem to me well-defined to index into the first and last index, and all between, by A[begin+1]
etc. but just not referring to a specific Int
index (if only this wouldn’t call getindex
).
I’m not sure what you do usefully with the diagonal of a matrix, besides trace. And note, Diagonal(A)
is (still) always definable, just not diag(A) which gives a Vector
The trend in math is to explore more generality, and it’s not unusual to lose some properties, e.g. complex numbers are more general than reals but can no longer ordered.
I hesitate to put this into the “Internals & Design”-category but though people might still be intrigued.
Note, while in linear algebra and math in general 1-based is the norm sometimes 0-based indexing is used for e.g. matrices. I’m not sure what happens currently in Julia if you try to multiply matrices of those two types, otherwise identical. In math I guess it is well-defined, these are just arbitrary labels, just unstated what the end-result should be 1-based or 0-based. I even thought of unifying regular Julia arrays and OffsetArray
s to allow BOTH 1-based and 0-based indexing. It’s not totally outlandish, could work (and there’s a precedent in e.g. PHP allowing two kinds of indexing for arrays, 0-based and by text key). It might solve my diag(A) dilemma.