# Arrays with periodic boundaries

**URL:** <https://discourse.julialang.org/t/arrays-with-periodic-boundaries/4015>\
**Category:** New to Julia\
**Created:** [May 31, 2017, 1:56pm UTC](https://discourse.julialang.org/t/arrays-with-periodic-boundaries/4015 "2017-05-31T13:56:01Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [May 31, 2017, 3:44pm UTC](https://discourse.julialang.org/t/arrays-with-periodic-boundaries/4015/3 "2017-05-31T15:44:49Z")

</div>

> [@stefanocampanella](#):
>
> So my question is: what is the proper/julian way to deal with periodic arrays?

The proper way is to do a PeriodicArray type like you said. If you can’t do it yourself yet then you just have to hope someone else does it.

In theory it should be quite simple, you just have to overload size, getindex and setindex! (c.f. [https://docs.julialang.org/en/release-0.5/manual/interfaces/#indexing](https://docs.julialang.org/en/release-0.5/manual/interfaces/#indexing)), but in practice it can get a bit complicated - like in the FFTViews case - when you do the general case and you want optimal performance.

`getindex` is the function that get called when you do `x[1]` (`x[1]` is the same as `getindex(x,1)`). `setindex` is the same thing for `x[1] = pi` (it calls `setindex!(x,pi,1)`). So all you need to do is to transform the index with a modulo to make it periodic.

I made a minimal implementation for 1D arrays, it seems to work, but it’s probably not very good:

```julia
module tmp
    
    immutable CircularArray{T} <: AbstractArray{T,1}
        data::AbstractArray{T,1}
    end
    
    circindex(i::Int,N::Int) = 1 + mod(i-1,N)
    circindex(I,N::Int) = [circindex(i,N)::Int for i in I]

    Base.size(A::CircularArray) = size(A.data)
    
    Base.getindex(A::CircularArray, i::Int) = getindex(A.data, circindex(i,length(A.data)))
    Base.getindex(A::CircularArray, I) = getindex(A.data, circindex(I,length(A.data)))
    
    Base.setindex!(A::CircularArray, v, i::Int) = setindex!(A.data, v, circindex(i,length(A.data)))
    Base.setindex!(A::CircularArray, v, I) = setindex!(A.data, v, circindex(I,length(A.data)))    
end

c = tmp.CircularArray(rand(10))

@assert c[11] == c[1] 
@assert c[-9:0] == c[1:10]

```

---

_[View the full topic](https://discourse.julialang.org/t/arrays-with-periodic-boundaries/4015)._
