# Periodic Boundary Condition for a List

Please take attention to a list as:

data={6,12,5,8};


It may be interesting in Python if i=0, then data[(i-1)%4]=8 (% applies as Mod). Because in this language the index of a list's elements initiates from 0 i.e., data[0]=6.

Therefore, when I want to access 8*6, I can write data[(i-1)%4]*data[i%4] provided that i=0. If i=1, then we access 6*12.

But it is mysteries for Mathematica. Because, the indices initiate from 1 instead of 0.

Of course, one can say we use data[[(i-2)%5]] in Mathematica for showing 8 with i=1. But it is problematic when we show 6*12. Because, data[[(i-2)%5]]*data[[i%5]] is wrong for i=2.

Really how we can define a periodic boundary condition similar in Python?

• use the third argument of Mod, e.g, i = 1; data[[Mod[i - 1, 4, 1]]] data[[Mod[i, 4, 1]]]? – kglr Dec 27 '20 at 16:31
• O My God!!! Interesting!!! But I saw the help about Mod. But I cannot understand the role of the last unity in [[i-1,4,1]] – Unbelievable Dec 27 '20 at 16:55
• @Unbelievable that last $1$ is the third argument of Mod, and it is not related to Part ([[ ... ]). It causes the answer to be offset by one. Pretty much exactly what you were looking for, I would think? – MarcoB Dec 27 '20 at 17:00
• You are right ...But what is the mathematical interpretation? I just know Mod[2,5]=2, – Unbelievable Dec 27 '20 at 17:10
• Would the identity Mod[p, q, h] == h + Mod[p - h, q] help you understand what's going on, then? – J. M.'s torpor Dec 27 '20 at 17:32

You can use the three-argument form of Mod:

i = 1;
{data[[Mod[i, 4, 1]]] , data[[Mod[i - 1, 4, 1]]]}

{6, 8}

{data[[Mod[i, 4, 1]]] , data[[Mod[i + 1, 4, 1]]]}

{6, 12}


Compare:

Mod[Range[0, 10], 4]

{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2}

Mod[Range[0, 10], 4, 1]

{4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2}

• Then Mod[0,4,1]=4. You have LIKE. Even Over Like, Even Over Over Over.....Over Like!!! – Unbelievable Dec 27 '20 at 17:22