# Custom indices range for arrays

**URL:** <https://discourse.julialang.org/t/custom-indices-range-for-arrays/21203>\
**Category:** Performance\
**Created:** [February 26, 2019, 8:28am UTC](https://discourse.julialang.org/t/custom-indices-range-for-arrays/21203 "2019-02-26T08:28:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrRobot](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@MrRobot](https://discourse.julialang.org/u/MrRobot)\
**Post date:** [February 26, 2019, 8:28am UTC](https://discourse.julialang.org/t/custom-indices-range-for-arrays/21203/1 "2019-02-26T08:28:40Z")

</div>

The Julia manual talks about being able to define custom unit ranges [Arrays with custom indices · The Julia Language](https://docs.julialang.org/en/v1/devdocs/offset-arrays/index.html).

There are at least 3 packages that deal with this. 1) [GitHub - JuliaArrays/CustomUnitRanges.jl: Package-specific AbstractUnitRange types for julia](https://github.com/JuliaArrays/CustomUnitRanges.jl) 2) [GitHub - JuliaArrays/OffsetArrays.jl: Fortran-like arrays with arbitrary, zero or negative starting indices.](https://github.com/JuliaArrays/OffsetArrays.jl) 3) [GitHub - JuliaArrays/EndpointRanges.jl: Julia package for doing arithmetic on endpoints in array indexing](https://github.com/JuliaArrays/EndpointRanges.jl)

I am not having much luck figuring it out. I am trying to duplicate fortran code, this piece specifically

```julia
COMPLEX, DIMENSION(n, 0:nk) :: eikx
COMPLEX, DIMENSION(n,-nk:nk) :: eiky, eikz

```

In this particular case it is incredibly helpful to have negative indices (and 0 too).

One issue I have run into with `OffsetArrays` is that I can’t use `setindex!` or `setindex`. Unfortunately I must update the elements in the 2 dimension array constantly.

Is there a way to make a 2 dimensional complex array that can have negative indices and is mutable?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 26, 2019, 8:42am UTC](https://discourse.julialang.org/t/custom-indices-range-for-arrays/21203/2 "2019-02-26T08:42:33Z")

</div>

Following and slightly adapting the first example in the readme of OffsetArrays I get:

```julia
julia> nk = 1
1

julia> y = OffsetArray{Complex{Float64}}(undef, -nk:nk, -nk:nk)
OffsetArray(::Array{Complex{Float64},2}, -1:1, -1:1) with eltype Complex{Float64} with indices -1:1×-1:1:
 6.94641e-310+6.94641e-310im 6.94641e-310+6.94641e-310im 6.94641e-310+6.94641e-310im
 6.94641e-310+6.94641e-310im 6.94641e-310+6.94641e-310im 6.94641e-310+6.94641e-310im
 6.94641e-310+6.94641e-310im 6.94641e-310+6.94641e-310im 6.94641e-310+6.94641e-310im

julia> y[-nk,-nk] = 1
1

julia> y
OffsetArray(::Array{Complex{Float64},2}, -1:1, -1:1) with eltype Complex{Float64} with indices -1:1×-1:1:
          1.0+0.0im 6.94641e-310+6.94641e-310im 6.94641e-310+6.94641e-310im
 6.94641e-310+6.94641e-310im 6.94641e-310+6.94641e-310im 6.94641e-310+6.94641e-310im
 6.94641e-310+6.94641e-310im 6.94641e-310+6.94641e-310im 6.94641e-310+6.94641e-310im

```

---

<div class="post-metadata">

**Author:** ![MrRobot](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@MrRobot](https://discourse.julialang.org/u/MrRobot)\
**Post date:** [February 26, 2019, 8:49am UTC](https://discourse.julialang.org/t/custom-indices-range-for-arrays/21203/3 "2019-02-26T08:49:09Z")

</div>

Yes this works! I was playing around with a simpler example from a forum and it would not allow me to mutate :S. This however does, so I am happy with that.

I was playing with `test = OffsetArray(1:5,-2:2)`, and I could not update the values after… this is probably due to the use of a range.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [February 26, 2019, 8:59am UTC](https://discourse.julialang.org/t/custom-indices-range-for-arrays/21203/4 "2019-02-26T08:59:54Z")

</div>

Yes, it uses the range as underlying storage:

```julia
julia> typeof(OffsetArray(1:5,-2:2))
OffsetArray{Int64,1,UnitRange{Int64}}

julia> typeof(OffsetArray([1:5...],-2:2)) # this you can mutate
OffsetArray{Int64,1,Array{Int64,1}}

```

The last parameter is the underlying storage.
