I want to do something analogous to the following snippet:
function reverse_first_dim(A::AbstractArray)
B = similar(A)
trailing = size(A)[2:end] |> CartesianIndices
N = size(A, 1)
@views B[N:-1:1, trailing] .= A[:, trailing]
B
end
But, attempting to call this function results in the following error:
julia> reverse_first_dimension(reshape([1, 2, 3, 4], (2, 2)))
ERROR: ArgumentError: an array of type `CartesianIndices` shares memory with another argument and must
make a preventative copy of itself in order to maintain consistent semantics,
but `copy(A)` returns a new array of type `Array{CartesianIndex{1},1}`. To fix, implement:
`Base.unaliascopy(A::CartesianIndices)::typeof(A)`
Stacktrace:
[1] _unaliascopy(::CartesianIndices{1,Tuple{Base.OneTo{Int64}}}, ::Array{CartesianIndex{1},1}) at ./abstractarray.jl:1120
[2] unaliascopy(::CartesianIndices{1,Tuple{Base.OneTo{Int64}}}) at ./abstractarray.jl:1118
[3] map(::typeof(Base.unaliascopy), ::Tuple{Base.Slice{Base.OneTo{Int64}},CartesianIndices{1,Tuple{Base.OneTo{Int64}}}}) at ./tuple.jl:166
[4] unaliascopy(::SubArray{Int64,2,Array{Int64,2},Tuple{Base.Slice{Base.OneTo{Int64}},CartesianIndices{1,Tuple{Base.OneTo{Int64}}}},false}) at ./subarray.jl:98
[5] unalias at ./abstractarray.jl:1101 [inlined]
[6] copyto!(::SubArray{Int64,2,Array{Int64,2},Tuple{StepRange{Int64,Int64},CartesianIndices{1,Tuple{Base.OneTo{Int64}}}},false}, ::SubArray{Int64,2,Array{Int64,2},Tuple{Base.Slice{Base.OneTo{Int64}},CartesianIndices{1,Tuple{Base.OneTo{Int64}}}},false}) at ./multidimensional.jl:875
[7] copyto! at ./broadcast.jl:838 [inlined]
[8] copyto! at ./broadcast.jl:797 [inlined]
[9] materialize!(::SubArray{Int64,2,Array{Int64,2},Tuple{StepRange{Int64,Int64},CartesianIndices{1,Tuple{Base.OneTo{Int64}}}},false}, ::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{2},Nothing,typeof(identity),Tuple{SubArray{Int64,2,Array{Int64,2},Tuple{Base.Slice{Base.OneTo{Int64}},CartesianIndices{1,Tuple{Base.OneTo{Int64}}}},false}}}) at ./broadcast.jl:756
[10] reverse_first_dimension(::Array{Int64,2}) at ./REPL[7]:5
[11] top-level scope at none:0
This is a bit hard to parse and, it seems, not an issue with the user facing code. Should this be classed as a bug? I can write the same algorithm in another way, but the code is not as nice.
Here is an older topic with the issue: Error writing into a view of an array using CartesianIndices. It appears that the problem is views with CartesianIndices
. I’m reposting for visibility as there were no replies to the original.