How do I tell Julia in which order to iterate in a

2019-08-27 16:33发布

问题:

This answser led me to an other question:

When defining a new structure like this one:

struct ReversedRowMajor{T,A} <: AbstractMatrix{T}
    data::A
end
ReversedRowMajor(data::AbstractMatrix{T}) where {T} = ReversedRowMajor{T, typeof(data)}(data)
Base.size(R::ReversedRowMajor) = reverse(size(R.data))
Base.getindex(R::ReversedRowMajor, i::Int, j::Int) = R.data[end-j+1, end-i+1]

If R is a ReversedRowMajor array, when accessing R[:,:] Julia will iterate through the CartesianIndices in the order which should be the fastest for the array i.e. the memory order (see array performance tips) but in this case it's not the expected one, since we are permutating the indices: (i,j) → (end-j+1, end-i+1).

So the question is: given an array, is there a way to tell Julia which axis is the fastest one?

(see also Multidimensional algorithms and iteration)