Abelian sandpile model/Identity: Difference between revisions

Content added Content deleted
Line 657: Line 657:


=={{header|J}}==
=={{header|J}}==
<lang J>
While=:2 :'u^:(0-.@:-:v)^:_'
index_of_maximum=: $ #: (i. >./)@:,

frame=: ({.~ -@:>:@:$)@:({.~ >:@:$) :. ([;.0~ (1,:_2+$))
NEIGHBORS=: _2]\_1 0 0 _1 0 0 0 1 1 0
AVALANCHE =: 1 1 _4 1 1

avalanche=: (AVALANCHE + {)`[`]}~ ([: <"1 NEIGHBORS +"1 index_of_maximum)
erode=: avalanche&.:frame While(3 < [: >./ ,)
</lang>
<pre>
NB. common ways to construct a matrix in j from directly entered vectors
s3_id=: >2 1 2;1 0 1;2 1 2 NB. 3 3$2 1 2 1 0 1 2 1 2 NB. _3]\2 1 2 1 0 1 2 1 2 NB. 2 1 2,1 0 1,:2 1 2
s3=: 3 3 $ 3 NB. ($~,~)3 NB. 3"0 i.3 3

matches =: -:
Commutes=: adverb def '(u matches u~)~'

NB. demonstrate Commutes adbverb
4 - Commutes 3
0
4 + Commutes 3
1

NB. confirmation
<"2 A , ] avalanche&.:frame@:([ 3 :'A=:A,y') While(3 < [: >./ ,) 10#.inv 433 312 023 [ A=:0 3 3$0
┌─────┬─────┬─────┬─────┬─────┐
│4 3 3│0 4 3│1 0 4│1 1 0│2 1 0│
│3 1 2│4 1 2│4 2 2│4 2 3│0 3 3│
│0 2 3│0 2 3│0 2 3│0 2 3│1 2 3│
└─────┴─────┴─────┴─────┴─────┘

NB. matrix addition commutes
's1 s2'=: 120 211 013 ;&:(10&#.inv) 213 101 010
s1 + Commutes s2
1
erode s1 + s2
3 3 3
3 1 2
0 2 3

NB. use: IDENTITY verify_identity MATRIX
verify_identity=: (erode@:+ matches ]) erode

raku_id verify_identity raku
1

(; erode) raku
┌─────────┬─────────┐
│4 1 0 5 1│1 3 2 1 0│
│9 3 6 1 0│2 2 3 3 1│
│8 1 2 5 3│1 1 2 0 3│
│3 0 1 7 5│2 0 3 2 0│
│4 2 2 4 0│3 2 3 2 1│
└─────────┴─────────┘
</pre>


=={{header|Julia}}==
=={{header|Julia}}==