Is OffsetArrays.jl a poison pill?

I agree. In the end, of this needs to go to Base, and more of the definitions need errors instead of fallbacks (one of the reasons that IMO it’s not quite ready for Base).

ismutable is a surprisingly hard case though :sweat_smile:. Most people don’t use it correctly when arrays are involved.

2 Likes

To me, this approach feels like conflating cardinal vs ordinal indexing, that is, using the value of an index vs. the ordinal rank of the index in the axis. When someone writes a[2] they may mean one of two things: (a) I want the value corresponding to the key 2 (b) I want the second value in the array, irrespective of the key. The second of these requires one to use a[begin - 1 + 2]. I have written a small package that provides a convenient syntax for this:

julia> using OrdinalIndexing, OffsetArrays

julia> a = 1:10
1:10

julia> a[3rd]
3

julia> b = OffsetArray(a, -10)
1:10 with indices -9:0

julia> b[3rd]
3

Using this, we may write the loop in the OP as

julia> x = ones(-10:-1, -10:-1);

julia> for t in axes(x, 1)[4th:end], i in axes(x, 2)[2nd:end]
           x[t, i] = 2x[t-3, i-1]
       end

julia> x
10×10 OffsetArray(::Matrix{Float64}, -10:-1, -10:-1) with eltype Float64 with indices -10:-1×-10:-1:
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0
 1.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0
 1.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0  2.0
 1.0  2.0  4.0  4.0  4.0  4.0  4.0  4.0  4.0  4.0
 1.0  2.0  4.0  4.0  4.0  4.0  4.0  4.0  4.0  4.0
 1.0  2.0  4.0  4.0  4.0  4.0  4.0  4.0  4.0  4.0
 1.0  2.0  4.0  8.0  8.0  8.0  8.0  8.0  8.0  8.0

or alternately,

julia> for t in (4:size(x,1))th, i in (2:size(x,2))th
           x[t, i] = 2x[t-3, i-1]
       end

which would lead to the same result, and this should work for both standard 1-indexed arrays and OffsetArrays.

The package is not fully composable or feature-complete as of this moment, but vanilla indexing should work.

4 Likes