# Circular array in Julia

**URL:** <https://discourse.julialang.org/t/circular-array-in-julia/25588>\
**Category:** Geo\
**Created:** [June 24, 2019, 12:51am UTC](https://discourse.julialang.org/t/circular-array-in-julia/25588 "2019-06-24T00:51:59Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 20, 2021, 2:07pm UTC](https://discourse.julialang.org/t/circular-array-in-julia/25588/21 "2021-01-20T14:07:50Z")

</div>

It is still not clear to me, perhaps a simple example of this sphere use case?

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [January 20, 2021, 2:34pm UTC](https://discourse.julialang.org/t/circular-array-in-julia/25588/22 "2021-01-20T14:34:52Z")

</div>

I don’t promise this is right, but it looks like the simplest things you can do are these:

```julia
julia> torus = CyclicArray(
         [1 2 3; 4 5 6; 7 8 9], 
         hcat(
           reshape([1 1 2 0; 1 1 1 0],1,1,2,4), 
           reshape([1 2 2 0; 1 2 1 0],1,1,2,4)
       ))
3×3 CyclicArray{Int64, 2}:
 1 2 3
 4 5 6
 7 8 9

julia> [torus[i,j] for i in -2:6, j in -2:6]
9×9 Matrix{Int64}:
 1 2 3 1 2 3 1 2 3
 4 5 6 4 5 6 4 5 6
 7 8 9 7 8 9 7 8 9
 1 2 3 1 2 3 1 2 3
 4 5 6 4 5 6 4 5 6
 7 8 9 7 8 9 7 8 9
 1 2 3 1 2 3 1 2 3
 4 5 6 4 5 6 4 5 6
 7 8 9 7 8 9 7 8 9

julia> klein = CyclicArray(
         [1 2 3; 4 5 6; 7 8 9], 
         hcat(
           reshape([1 1 2 0; 1 1 1 0], 1,1,2,4), 
           reshape([1 2 2 1; 1 2 1 1], 1,1,2,4)
       ))
3×3 CyclicArray{Int64, 2}:
 1 2 3
 4 5 6
 7 8 9

julia> [klein[i,j] for i in -2:6, j in -2:6]
9×9 Matrix{Int64}:
 3 2 1 3 2 1 3 2 1
 6 5 4 6 5 4 6 5 4
 9 8 7 9 8 7 9 8 7
 1 2 3 1 2 3 1 2 3
 4 5 6 4 5 6 4 5 6
 7 8 9 7 8 9 7 8 9
 3 2 1 3 2 1 3 2 1
 6 5 4 6 5 4 6 5 4
 9 8 7 9 8 7 9 8 7

```

---

<div class="post-metadata">

**Author:** ![udistr](https://avatars.discourse-cdn.com/v4/letter/u/73ab20/32.png) [@udistr](https://discourse.julialang.org/u/udistr)\
**Post date:** [January 20, 2021, 2:37pm UTC](https://discourse.julialang.org/t/circular-array-in-julia/25588/23 "2021-01-20T14:37:50Z")

</div>

The idea is similar to the xgcm python package:

[https://xgcm.readthedocs.io/en/latest/grid\_topology.html#](https://xgcm.readthedocs.io/en/latest/grid_topology.html#)

---

<div class="post-metadata">

**Author:** ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)\
**Post date:** [January 20, 2021, 2:50pm UTC](https://discourse.julialang.org/t/circular-array-in-julia/25588/24 "2021-01-20T14:50:56Z")

</div>

Just a note, `CyclicArrays` define a [container with an abstract type](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-abstract-container). Which may lead to some performance issues. A way around this would be mimic `CircularArrays` and define an extra type parameter `struct CyclicArray{T,N,A}`…  
This may not be important for some application cases, but defining `Base.sum(ca::CyclicArray) = Base.sum(ca.data)` should have some inference trouble in `@code_warntype`.

---

<div class="post-metadata">

**Author:** ![udistr](https://avatars.discourse-cdn.com/v4/letter/u/73ab20/32.png) [@udistr](https://discourse.julialang.org/u/udistr)\
**Post date:** [January 20, 2021, 4:57pm UTC](https://discourse.julialang.org/t/circular-array-in-julia/25588/25 "2021-01-20T16:57:40Z")

</div>

Here is a quick example to play with:

x0=permutedims(reshape((1:1:96)',(4,4,6,1)),(3,4,2,1))  
x1=CyclicArray(x0,“cubed”)

This example includes 6 faces interconnected in this order:  
5  
1 2 3 4. # → x direction  
6

julia\> x1[1,1,1,0]==x1[4,1,1,4]  
true  
julia\> x1[3,1,1,0]==x1[2,1,1,4]  
true

The package is still under development. The named grid topology is still not documented but it is active.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [November 9, 2021, 11:11pm UTC](https://discourse.julialang.org/t/circular-array-in-julia/25588/26 "2021-11-09T23:11:30Z")

</div>

for searching purposes, there is now a registered Package that does this:  
[https://github.com/Vexatos/CircularArrays.jl](https://github.com/Vexatos/CircularArrays.jl)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 10, 2021, 1:23am UTC](https://discourse.julialang.org/t/circular-array-in-julia/25588/27 "2021-11-10T01:23:33Z")

</div>

Note that with this type of approach (as in CircularArrays.jl and CyclicArrays.jl), you pay a computational price for _every_ `getindex` operations (for a `mod` in CircularArrays and a much more expensive calculation in CyclicArrays).

In many circumstances, you don’t need to access the array at _arbitrary_ indices, but only at indices in the interior and _just_ outside the boundary, and in such cases you can get both greater efficiency and greater flexibility (in topology etcetera) [by using ghost cells](https://discourse.julialang.org/t/arrays-with-periodic-boundaries/4015/4).

---

<div class="post-metadata">

**Author:** ![jakewilliami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakewilliami/32/18945_2.png) [@jakewilliami](https://discourse.julialang.org/u/jakewilliami)\
**Post date:** [November 16, 2021, 12:28pm UTC](https://discourse.julialang.org/t/circular-array-in-julia/25588/28 "2021-11-16T12:28:02Z")

</div>

If you only need one-dimensional arrays, you can use [CircularList.jl](https://github.com/tk3369/CircularList.jl). It looks to have a really nice interface, where you can have a head pointing to a certain point in the list. (And the developer is great!)

[Previous page](https://discourse.julialang.org/t/circular-array-in-julia/25588.md?page=1)
