# Using views with CartesianIndices: Is this a bug?

**URL:** <https://discourse.julialang.org/t/using-views-with-cartesianindices-is-this-a-bug/19823>\
**Category:** General Usage\
**Tags:** question, indexing\
**Created:** [January 19, 2019, 5:21pm UTC](https://discourse.julialang.org/t/using-views-with-cartesianindices-is-this-a-bug/19823 "2019-01-19T17:21:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_McBane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sean_mcbane/32/4580_2.png) [@Sean\_McBane](https://discourse.julialang.org/u/Sean_McBane)\
**Post date:** [January 19, 2019, 5:21pm UTC](https://discourse.julialang.org/t/using-views-with-cartesianindices-is-this-a-bug/19823/1 "2019-01-19T17:21:58Z")

</div>

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
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](https://discourse.julialang.org/t/error-writing-into-a-view-of-an-array-using-cartesianindices/16738). It appears that the problem is views with `CartesianIndices`. I’m reposting for visibility as there were no replies to the original.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [October 25, 2021, 11:23am UTC](https://discourse.julialang.org/t/using-views-with-cartesianindices-is-this-a-bug/19823/2 "2021-10-25T11:23:32Z")

</div>

In 1.7.0-rc2 it seems to work:

```julia
julia> reverse_first_dim(reshape([1, 2, 3, 4], (2, 2)))
2×2 Matrix{Int64}:
 2 4
 1 3

```

Edit: damn I was tricked into replying to a 3 year old topic… @mbauman it seems someone changing the tags is enough to put the thread in the “latest” list?

---

<div class="post-metadata">

**Author:** ![Rex\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rex_wang/32/35108_2.png) [@Rex\_Wang](https://discourse.julialang.org/u/Rex_Wang)\
**Post date:** [November 20, 2022, 2:06am UTC](https://discourse.julialang.org/t/using-views-with-cartesianindices-is-this-a-bug/19823/3 "2022-11-20T02:06:50Z")

</div>

I came across the same problem when dealing with discrete indexes.

```julia
using Test
a = rand(5)
b = similar(a)
I = CartesianIndex.([1,2])
a[I] = @views b[I] # work well
@test_throws MethodError a[I] .= @views b[I]
I = CartesianIndices(1:2)
a[I] .= @views b[I] # work well again

```

In some cases, we need the `.=` method, like

```julia
a[I] .+= @view(b[I]) # prefer
a[I] = @views a[I] + b[I] # rather than

```

Version info

```julia
Julia Version 1.8.0
Commit 5544a0fab76 (2022-08-17 13:38 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, icelake-client)
  Threads: 1 on 8 virtual cores

```

I wonder if it is a bug for `@view` since both `a[I]` and `b[I]` are vectors of equal length.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [November 20, 2022, 8:01am UTC](https://discourse.julialang.org/t/using-views-with-cartesianindices-is-this-a-bug/19823/4 "2022-11-20T08:01:56Z")

</div>

It looks like a missing method:

```julia
julia> CartesianIndex{1}(1)
CartesianIndex(1,)

julia> convert(CartesianIndex{1}, 1)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type CartesianIndex{1}
Closest candidates are:
  convert(::Type{T}, ::T) where T at Base.jl:61
  CartesianIndex{N}(::Integer...) where N at multidimensional.jl:71
  CartesianIndex{N}(::Integer...) where N at multidimensional.jl:74
Stacktrace:
 [1] top-level scope
   @ REPL[40]:1

```

Perhaps the conversion should work as well.
