Abelian sandpile model: Difference between revisions

(Realize in F#)
Line 998:
=={{header|J}}==
 
<lang J>NB.grid=: have4 2d: grid.'x piles(<<.-:2$y)} (x,2$y)$0' withNB. >=y 4by unitsy ofgrid sandwith x spillgrains overin tomiddle
ab=: + [: (_4&* + [: +/ ((,-)2]\i:1)|.!.0 ]) 3&< NB. abelian sand pile for grid graph
NB. neighbors.
require 'viewmat' NB. viewmat utility
 
spillviewmat ab ^: IFQT_ (1024 grid 25) NB. visual </lang>
NB. grid with x units of sand in center of grid of size y. y may be a
NB. single number in which case the grid is square or a 2-array with
NB. the dimensions y0 y1.
grid=: dyad : 'x (<<.-:2$y)} (2$y)$0'
 
NB. the current sand matrix is added with the change in sand by hook.
NB. the change is _4 at all the locations with 4&<:, plus the spilled
NB. sand from rotating (|.) by neighborhood _2 ]\ _1 0 0 _1 1 0 0 1
NB. and adding (+/).NB. this implementation has the grid wrapping around
NB. so that sand on the top may spill to the bottom. the fit conjunction
NB. |.!.0 could be used if sand should spill off the world instead.
sand=: + [: (_4&* + [: +/ (_2]\_1 0 0 _1 1 0 0 1)&|.) 4&<:
 
NB. reach a fix point (^: _) and display the sand using viewmat.
require 'viewmat'
spill=: [: viewmat sand ^: _
 
NB. if in jqt use viewmat to display an abelian sandpile.
spill ^: IFQT 1024 grid 25</lang>
 
=={{header|Java}}==
Anonymous user