Home > Mobile >  Particles movement with spin -1, 1 in a square in R
Particles movement with spin -1, 1 in a square in R

Time:01-03

I have a particle that moves in a square with spin -1 and 1. I want to create a matrix in R in order to register the following directions:

The matrix must have number of columns L = 4, row N = L*L, and must go for i in range N:

right: ((i %/% L) * L (i 1) %% L,
down : (i L) %% N,
left: (i %/% L) * L (i - 1) %% L,
up: (i - L) %% N)

where %% is the modulo operator and %/% the floor division ( %/% is the integer division but I do not know if there is an operator in R for floor division)

 (1, 4, 3, 12),
 (2, 5, 0, 13),
 (3, 6, 1, 14),
 (0, 7, 2, 15),
 (5, 8, 7, 0),
 (6, 9, 4, 1),
 (7, 10, 5, 2),
 (4, 11, 6, 3),
 (9, 12, 11, 4),
 (10, 13, 8, 5),
 (11, 14, 9, 6),
 (8, 15, 10, 7),
 (13, 0, 15, 8),
 (14, 1, 12, 9),
 (15, 2, 13, 10),
 (12, 3, 14, 11)

How can I do it in R ? Any help ?

In python this is done with:

L = 4
N = L*L
neighbors = {i : ((i // L) * L   (i   1) % L,   # RIGHT
                 (i   L) % N,                   # DOWN
                 (i // L) * L   (i - 1) % L,    # LEFT
                 (i - L) % N)                   # UP
                 for i in range(N)}
neighbors

But how this can be implemented in R?

CodePudding user response:

At first the variables are defined:

  • c = L = no. of columns
  • r = N = no of rows

Then a Null-matrix is defined and the matrix is filled according to your formulas.

c <- 4; r <- N <- 16; L <- 4
AA = matrix (nr= r, nc=c, 0)
for (i in 0:15){
    AA[i 1,1] = (i %/% L) * L   (i   1) %% L
    AA[i 1,2] = (i   L) %% N
    AA[i 1,3] = (i %/% L) * L   (i - 1) %% L
    AA[i 1,4] = (i - L) %% N
}
AA
#>       [,1] [,2] [,3] [,4]
#>  [1,]    1    4    3   12
#>  [2,]    2    5    0   13
#>  [3,]    3    6    1   14
#>  [4,]    0    7    2   15
#>  [5,]    5    8    7    0
#>  [6,]    6    9    4    1
#>  [7,]    7   10    5    2
#>  [8,]    4   11    6    3
#>  [9,]    9   12   11    4
#> [10,]   10   13    8    5
#> [11,]   11   14    9    6
#> [12,]    8   15   10    7
#> [13,]   13    0   15    8
#> [14,]   14    1   12    9
#> [15,]   15    2   13   10
#> [16,]   12    3   14   11
  • Related