I tend to disagree with the OA on this – I don’t want to blame the entire language/ecosystem because of silent bugs involving OffsetArrays and @inbounds.
I still think OffsetArrays are a bad idea, though, and I don’t see the point for them.
In my own code, I usually need to iterate over a matrix, and I normally do things like:
for i ∈ 1:size(A,1), j ∈ 1:size(A,2)
A[i,j] = sin(x[i]) * cos(y[j])
end
which is very succinct and easy to understand… and if I want to traverse the diagonal of A
, this is simply:
for i ∈ 1:size(A,1)
A[i,i] = A[i,i] + 1
end
To support OffsetArrays, I should do:
for I ∈ CartesianIndices(A)
A[I[1],I[2]] = sin(x[I[1]]) * cos(y[I[2]])
end
Which is ok. But right now, I don’t know how to traverse the diagonal of a matrix in a way that supports OffsetArrays.
There seems to be an explanation in the OffsetArrays.jl home page about how to use Diagonal
from the LinearAlgebra package (which looks like it’s converting the OffsetArray to a standard array), but what really caught my eyes was:
Certain libraries, such as LinearAlgebra, require arrays to be indexed from 1
ERROR: ArgumentError: offset arrays are not supported but got an array with index other than 1
I believe that would be the quickest/wisest solution, just make more base libraries incompatible with OffsetArrays.
Unless, of course, somebody kindly illustrates what is the real need for OffsetArrays, I think that should be the best solution for most packages.