# Creating looping matrixes (Torus-like) with type inheritance

**URL:** <https://discourse.julialang.org/t/creating-looping-matrixes-torus-like-with-type-inheritance/135475>\
**Category:** General Usage\
**Tags:** type, inheritance, matrix\
**Created:** [February 5, 2026, 1:02pm UTC](https://discourse.julialang.org/t/creating-looping-matrixes-torus-like-with-type-inheritance/135475 "2026-02-05T13:02:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daeltam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daeltam/32/220698_2.png) [@Daeltam](https://discourse.julialang.org/u/Daeltam)\
**Post date:** [February 5, 2026, 1:02pm UTC](https://discourse.julialang.org/t/creating-looping-matrixes-torus-like-with-type-inheritance/135475/1 "2026-02-05T13:02:32Z")

</div>

## Context :

I am trying to create a cellular automata and want its behaviour on edges to be looping (ex : neighbour of the cell at `[0,0]` are the cells at `[0,end]` and `[end, 0]` (as well as `[0,1]` and `[1,0]` )  
Some kind of torus-representing matrix (when 2d).

## My hypothesis

Maybe a deformation from python OOP, but I thought that creating a subclass of Matrix that would have special properties such as Matrix[0] = Matrix[end] etc… would be the easiest approach

This way, if I ever intended to generalise it to a `3x3` or `nxn` Matrix, it would not change so much in the code because i would just be able to say `Matrix[... , coord-1]` to loop back  
And similarly if i wanted to extend my reach, I could do `Matrix[coord - k]` and go as far as I want from the end, looping.

I’ve looked at several posts on Inheritance etc but it seems like Julia isn’t built for this type of programming, so here are my questions :

- Is it worth trying to find a way to create this struct ?
- What’s the better way to code this behaviour and make the code reusable as much as possible

I don’t mind installing Packages but I’d like to keep my code as simple as possible, I’ve seen the `Class.jl` package that might offer what I’m looking for but I’m not sure about it.

Note : I know that, in my case, I could do a borders check and filter values to make them modulo the matrix size but that doesn’t seem neither aesthetic nor very functional. Tell me if I’m wrong

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [February 5, 2026, 1:14pm UTC](https://discourse.julialang.org/t/creating-looping-matrixes-torus-like-with-type-inheritance/135475/2 "2026-02-05T13:14:14Z")

</div>

Sure, this is something that is very natural to write in julia. There’s a few implementations of it kicking around, but it might be informative for you to try implementing it yourself.

Here’s some documentation on julia’s Abstract Array interface: [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array)

You just need to make a struct that wraps another `Array`, and then intercepts `getindex` and `setindex!` calls, and transforms the indices to match your desired boundary conditions.

---

<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:** [February 5, 2026, 4:02pm UTC](https://discourse.julialang.org/t/creating-looping-matrixes-torus-like-with-type-inheritance/135475/3 "2026-02-05T16:02:23Z")

</div>

This type of thing has been discussed before, but I usually advocate “ghost cells” (periodic padding) rather than paying the price of wrapping indices on every array access. See e.g. [Arrays with periodic boundaries - #4 by stevengj](https://discourse.julialang.org/t/arrays-with-periodic-boundaries/4015/4)

---

<div class="post-metadata">

**Author:** ![Daeltam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daeltam/32/220698_2.png) [@Daeltam](https://discourse.julialang.org/u/Daeltam)\
**Post date:** [February 17, 2026, 7:34pm UTC](https://discourse.julialang.org/t/creating-looping-matrixes-torus-like-with-type-inheritance/135475/4 "2026-02-17T19:34:37Z")

</div>

Sorry for the delay in the answer, I’ve been busy.

I can’t find out how to rewrite `getindex` (first one I’ve tried for now)

I’ve created my own struct :

```julia
struct LoopArray <: AbstractArray
 #what do I do here ?
end

```

And then I’ve looked at the code for get index in essentials.jl and in abstractarray.jl but they’re far too complicated to recode because they’re built first with an int index first and then generalized to vector indices.

So I’ve thought about saying something like

```julia-auto
Function getindex(LoopArray, index:: AbstractArray)
  index = index modulo array size
 getindex(LoopArray, index) # calling base function now that it's corrected

```

But won’t my second call just call the first one and loop ?  
I’ve searched similar implémentations but couldn’t find one with my needs

Edit : just found this : [Arrays with periodic boundaries - #3 by jonathanBieler](https://discourse.julialang.org/t/arrays-with-periodic-boundaries/4015/3) , I’ll try based on this for now, haven’t finished yet

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [February 17, 2026, 7:55pm UTC](https://discourse.julialang.org/t/creating-looping-matrixes-torus-like-with-type-inheritance/135475/5 "2026-02-17T19:55:56Z")

</div>

I’ve not used them myself, but these two packages seemingly implement the functionality you want:

PeriodicArrays.jl  
CircularArrays.jl

If nothing else, perhaps they can be helpful in writing your own type.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [February 18, 2026, 2:30pm UTC](https://discourse.julialang.org/t/creating-looping-matrixes-torus-like-with-type-inheritance/135475/6 "2026-02-18T14:30:08Z")

</div>

The [array interfaces manual page](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array) (also linked above) is useful here. In particular, it says that (by default) vector indexing is implemented in terms of scalar indexing and that it is sufficient to implement either `getindex(A, i::Int)` (for `IndexLinear` arrays – not your case) or `getindex(A, I::Vararg{Int, N})` (for `IndexCartesian` arrays – your case). But because you want to be able to index “outside” of the array’s size, we’ll need to implement the generic indexing function as well (since otherwise it will check bounds). _There might be a more elegant way around this, but it’s not coming to me at this moment._

Here’s an example implementation:

```julia
struct LoopArray{T, N, A<:AbstractArray{T, N}} <: AbstractArray{T, N}
  arr::A
end

# not needed since it's the default
# Base.IndexStyle(::Type{<:LoopArray}) = IndexCartesian()

Base.size(x::LoopArray) = size(x.arr)

# scalar indexing
function Base.getindex(x::LoopArray{<:Any, N, <:Any}, inds::Vararg{Int, N}) where N
  looped_inds = mod1.(inds, size(x))
  return getindex(x.arr, looped_inds...)
end

# general indexing
# here, we simply use a comprehension to call the scalar version a bunch of times
# there is probably a better way to do this, but it's beyond this example
Base.getindex(x::LoopArray{<:Any, N, <:Any}, inds::Vararg{Any, N}) where N = [x[i...] for i in Iterators.product(inds...)]

```

then

```julia-repl
julia> A = LoopArray(reshape(1:8, 2, 4))
2×4 LoopArray{Int64, 2, Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}}:
 1 3 5 7
 2 4 6 8

julia> A[0:3, 2:6]
4×5 Matrix{Int64}:
 4 6 8 2 4
 3 5 7 1 3
 4 6 8 2 4
 3 5 7 1 3

```

While I’ve provided this example for educational purposes, you should probably prefer one of the above-suggested packages if they meet your needs.

Note that unlike with a class, the functionality is not described within the `struct` definition. Instead, in Julia we add specialized methods to the interface functions (such as `Base.getindex`). Note that it’s necessary to write `Base.getindex` rather than just `getindex` because otherwise we’ll simply create a new `getindex` function that is not what’s actually called by `A[inds...]`.

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [February 18, 2026, 6:14pm UTC](https://discourse.julialang.org/t/creating-looping-matrixes-torus-like-with-type-inheritance/135475/7 "2026-02-18T18:14:04Z")

</div>

> [@stevengj](#):
>
> This type of thing has been discussed before, but I usually advocate “ghost cells” (periodic padding) rather than paying the price of wrapping indices on every array access. See e.g. [Arrays with periodic boundaries - #4 by stevengj](https://discourse.julialang.org/t/arrays-with-periodic-boundaries/4015/4)

I’d assume smart compiler to avoid checking each access given it knows the shape of the array (Like in [FixedSizeArray.jl](https://github.com/JuliaArrays/FixedSizeArrays.jl)).  
For instance in the context of convolution I’d expect to create head, middle and tail loops.

I am not sure, but could it be that [StaticKernels.jl](https://github.com/stev47/StaticKernels.jl) avoids that overhead?

---

<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:** [February 18, 2026, 11:36pm UTC](https://discourse.julialang.org/t/creating-looping-matrixes-torus-like-with-type-inheritance/135475/8 "2026-02-18T23:36:13Z")

</div>

> [@RoyiAvital](#):
>
> I’d assume smart compiler to avoid checking each access given it knows the shape of the array (Like in [FixedSizeArray.jl](https://github.com/JuliaArrays/FixedSizeArrays.jl)).

For typical `Array`s the compiler doesn’t know the size of the array — runtime sizes are typical of most applications. Even for statically sized arrays (but large enough that the loops are not unrolled), I’d be skeptical about assuming that this optimization occurs without checking.

Besides, ghost cells are much more flexible, because they separate the boundary conditions from the code working with the array, and it is easy to have a mix of boundary conditions (e.g. Dirichlet in one direction and periodic in the other). They also work well with distributed-memory parallelism, where the boundary elements are communicated from another process.
