1
$\begingroup$

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?

$\endgroup$
6
  • 7
    $\begingroup$ use the third argument of Mod, e.g, i = 1; data[[Mod[i - 1, 4, 1]]] data[[Mod[i, 4, 1]]]? $\endgroup$ – kglr Dec 27 '20 at 16:31
  • $\begingroup$ 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]] $\endgroup$ – Unbelievable Dec 27 '20 at 16:55
  • $\begingroup$ @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? $\endgroup$ – MarcoB Dec 27 '20 at 17:00
  • $\begingroup$ You are right ...But what is the mathematical interpretation? I just know Mod[2,5]=2, $\endgroup$ – Unbelievable Dec 27 '20 at 17:10
  • 2
    $\begingroup$ Would the identity Mod[p, q, h] == h + Mod[p - h, q] help you understand what's going on, then? $\endgroup$ – J. M.'s torpor Dec 27 '20 at 17:32
2
$\begingroup$

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}
$\endgroup$
1
  • $\begingroup$ Then Mod[0,4,1]=4. You have LIKE. Even Over Like, Even Over Over Over.....Over Like!!! $\endgroup$ – Unbelievable Dec 27 '20 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.