Abelian sandpile model: Difference between revisions
Content added Content deleted
(add RPL) |
|||
Line 3,305: | Line 3,305: | ||
Passing in a stack size of 20000 results in: [https://github.com/thundergnat/rc/blob/master/img/Abelian-sandpile-sdl2.png Abelian-sandpile-sdl2.png] (offsite .png image) |
Passing in a stack size of 20000 results in: [https://github.com/thundergnat/rc/blob/master/img/Abelian-sandpile-sdl2.png Abelian-sandpile-sdl2.png] (offsite .png image) |
||
=={{header|RPL}}== |
|||
Using the built-in matrix data structure fulfils the requirements of the task: |
|||
[[1 2 0][2 1 1][0 1 3]] [[2 1 3][1 0 1][0 1 0]] + |
|||
'''Output:''' |
|||
1: [[3 3 3] |
|||
[3 1 2] |
|||
[0 2 3]] |
|||
{| class="wikitable" ≪ |
|||
! RPL code |
|||
! Comment |
|||
|- |
|||
| |
|||
≪ ROT OVER RE + { } + ROT ROT IM + + |
|||
≫ ‘<span style="color:blue">→IDX</span>’ STO |
|||
≪ DUP SIZE 1 GET → n |
|||
≪ '''DO''' |
|||
1 CF 1 n '''FOR''' h 1 n '''FOR''' j |
|||
'''IF''' DUP h j 2 →LIST GET 3 > '''THEN''' |
|||
1 SF DUP 0 CON |
|||
h j 2 →LIST -4 PUT |
|||
1 4 '''FOR''' a |
|||
h j (0,1) a ^ <span style="color:blue">→IDX</span> |
|||
'''IFERR''' 1 PUT '''THEN''' DROP2 '''END''' |
|||
'''NEXT''' + |
|||
'''END NEXT NEXT''' |
|||
'''UNTIL''' 1 FC? '''END''' |
|||
≫ ≫ ‘<span style="color:blue">SPILE</span>’ STO |
|||
| |
|||
<span style="color:blue">→IDX</span> ''( a b (c,d) → { a+c b+d } ) '' |
|||
<span style="color:blue">SPILE</span> ''( [[a]] → [[a]] ) '' |
|||
loop |
|||
for h, j = 1 to n |
|||
if a[h,j] > 3 then |
|||
set flag, create empty matrix b |
|||
b[h,j] = -4 |
|||
for a = 1 to 4 |
|||
(x,y) = (h,j) + i^a |
|||
b[x,y] = 1 only if x > 0 and y > 0 |
|||
a += b |
|||
end if, next h, j |
|||
until all elements <= 3 |
|||
return a |
|||
|} |
|||
It is sometimes necessary to run the program several times to reach stability: user's eye is much faster than a program to detect a remaining unstable sandpile. This is the way in RPL. |
|||
It may nevertheless make sense when working on large matrices to have to run the program only once. In this case, the addtional line below shall be inserted after the <code>END NEXT NEXT</code> line: |
|||
1 n '''FOR''' h 1 n '''FOR''' j '''IF''' DUP h j 2 →LIST GET 3 > '''THEN''' 1 SF '''END NEXT NEXT''' |
|||
{3 3} 3 CON '<span style="color:green">S3</span>' STO |
|||
[[2 1 2][1 0 1][2 1 2]] '<span style="color:green">S3ID</span>' STO |
|||
<span style="color:green">S3</span> <span style="color:green">S3ID</span> + <span style="color:blue">SPILE</span> <span style="color:blue">SPILE</span> |
|||
<span style="color:green">S3ID</span> DUP + <span style="color:blue">SPILE</span> <span style="color:blue">SPILE</span> |
|||
{{out}} |
|||
<pre> |
|||
2: [[ 3 3 3 ] |
|||
[ 3 3 3 ] |
|||
[ 3 3 3 ]] |
|||
1: [[ 2 1 2 ] |
|||
[ 1 0 1 ] |
|||
[ 2 1 2 ]] |
|||
</pre> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 3,449: | Line 3,513: | ||
</pre> |
</pre> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{works with|Chez Scheme}} |
{{works with|Chez Scheme}} |