Abelian sandpile model: Difference between revisions
m
syntax highlighting fixup automation
(Added a Scheme implementation.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 30:
</pre>
=={{header|11l}}==
<
grid[5][5] = 64
Line 77:
L(row) grid
L(c) row
ppm.write_bytes(colors[c])</
{{out}}
Line 109:
{{works with|as|Raspberry Pi 3B version Buster 64 bits
or android 64 bits with application Termux }}
<
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program abelian64.s */
Line 306:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{Output}}
<pre>
Line 340:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<
/* ARM assembly Raspberry PI or android 32 bits */
/* program abelian.s */
Line 532:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
{{output}}
<pre>
Line 565:
=={{header|C}}==
Writes out the initial and final sand piles to the console and the final sand pile to a PPM file.
<syntaxhighlight lang=C>
#include<stdlib.h>
#include<string.h>
Line 686:
return 0;
}
</syntaxhighlight>
Console output :
Line 725:
<!-- c++ bindings -->
<br>
<
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
Line 798:
return 0;
}</
Compile with following <tt>CMakeList.txt</tt>:
<
project(abelian_sandpile)
Line 814:
target_compile_options(abelian_sandpile PRIVATE -march=native -std=c++14)
target_link_libraries(abelian_sandpile xtensor ${OIIO})</
=={{header|Delphi}}==
{{Trans|Python}}
<
program Abelian_sandpile_model;
Line 978:
Readln;
end.
</syntaxhighlight>
Line 984:
{{works with|gforth|0.7.3}}
<br>
<
\ Abelian Sandpile Model
Line 1,049:
: simulate prepare begin stack-full? while 2dup 2>r reduce 2r> inc-all repeat drop to-pgm ." written to " filename type cr ;
simulate bye</
{{out}}
Line 1,065:
{{works with|gfortran|9.2.0}}
The Abelian sandpile operations are defined here.
<
implicit none
Line 1,156:
end subroutine
end module</
The <code>main</code> program calls the <code>abelian_sandpile_m</code> and creates an ppm bitmap file by loading <code>rgbimage_m</code> module, which is defined [[Basic bitmap storage#Fortran|here]].
<
use rgbimage_m
Line 1,194:
call im%write('fig.ppm')
end program</
=={{header|Fōrmulæ}}==
Line 1,205:
=={{header|F_Sharp|F#}}==
<
// Abelian sandpile model. Nigel Galloway: July 20th., 2020
type Sandpile(x,y,N:int[])=
Line 1,232:
let e1=Array.zeroCreate<int> 25 in e1.[12]<-6; printfn "%s\n" (Sandpile(5,5,e1)).toS
let e1=Array.zeroCreate<int> 25 in e1.[12]<-16; printfn "%s\n" (Sandpile(5,5,e1)).toS
</syntaxhighlight>
{{out}}
<pre>
Line 1,260:
=={{header|FreeBASIC}}==
<
WindowTitle "Abelian sandpile model"
Line 1,290:
Loop Until partic < 4
Bsave "abelian_sandpile.bmp",0
Sleep</
{{out}}
<pre>[https://www.dropbox.com/s/lky2ji9n15gn5u6/abelian_sandpile.bmp?dl=0 Abelian sandpile model FreeBasic image]
Line 1,300:
<br>
Stack management in Go is automatic, starting very small (2KB) for each goroutine and expanding as necessary until the maximum allowed size is reached.
<
import (
Line 1,408:
// after the recursive algorithm has ended.
// writePile(pile)
}</
{{out}}
Line 1,438:
<br>
Using a custom monad to make the code cleaner.
<
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
Line 1,553:
fp = printf "sandpile_%d_%d.pgm" size height
toPGM b fp
putStrLn $ "wrote image to " ++ fp</
{{out}}
Line 1,563:
=={{header|J}}==
<
ab=: - [: +/@(-"2 ((,-)=/~i.2)|.!.0]) 3&< NB. abelian sand pile for grid graph
require 'viewmat' NB. viewmat utility
viewmat ab ^: _ (1024 grid 25) NB. visual </
=={{header|Java}}==
This is based on the JavaScript implementation linked to in the task description.
<
import java.awt.event.*;
import javax.swing.*;
Line 1,682:
};
private static final int GRID_LENGTH = 300;
}</
{{out}}
Line 1,689:
=={{header|Julia}}==
Modified from code by Hayk Aleksanyan, viewable at github.com/hayk314/Sandpiles, license viewable there.
<
# supports output functionality for the results of the sandpile simulations
Line 1,964:
Z_lat, Odometer = AbelSand.move(100000)
</
[http://alahonua.com/temp/Abel_Z_color_100000.png Link to PNG output file for N=100000 ie. AbelSand.move(100000)] <br />
[http://alahonua.com/temp/Abel_Z_color_1000000.png Link to PNG output file (run time >90 min) for N=1000000 (move(1000000))]
=={{header|Lua}}==
<
init = function(self, dim, val)
self.cell, self.dim = {}, dim
Line 2,006:
sandpile:init(15, 256)
sandpile:iter()
sandpile:draw()</
{{out}}
<pre>0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Line 2,025:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
sp[s_List] + sp[n_Integer] ^:= sp[s] + sp[ConstantArray[n, Dimensions[s]]]
sp[s_List] + sp[t_List] ^:= Module[{dim, r, tmp, neighbours}, dim = Dimensions[s];
Line 2,039:
u = sp[CenterArray[250, {15, 15}]];
u += sp[0];
StringRiffle[StringJoin /@ Map[ToString, u[[1]], {2}], "\n"]</
{{out}}
<pre>000000000000000
Line 2,061:
Our program uses Rust algorithm (and also its colors 🙂) and the formula to compute grid size from number of particles comes from Pascal algorithm.
Number of particles is an input from user. The program displays the values on the terminal if there are not too many and produce a PNG image. Code to produce a PPM image is also provided but not used.
<
# Abelian sandpile.
Line 2,208:
#grid.writePpmFile(fmt"grid_{initVal}.ppm")
grid.writePngFile(fmt"grid_{initVal}.png")
</syntaxhighlight>
{{out}}
Line 2,235:
Memorizing the used colums of the rows has little effect when choosing the right size of the grid.Only 11 secs for abelian(1e6) -> 1min 9sec<BR>
[http://rosettacode.org/wiki/Abelian_sandpile_model#Python Python] shows 64 too.
<
program Abelian2;
{$IFDEF FPC}
Line 2,423:
OneTurn(100000);
END.
</syntaxhighlight>
{{out}}
<pre>
Line 2,487:
{{works with|OCaml|4.11}}
In Sandpile module (sandpile.ml)
<
module Make =
functor (M : sig val m : int val n : int end)
Line 2,567:
; S.avalanche ()
; S.print ()
</syntaxhighlight>
=={{header|Perl}}==
<
use strict; # http://www.rosettacode.org/wiki/Abelian_sandpile_model
Line 2,593:
}
select undef, undef, undef, 0.1; # comment out for full speed
}</
=={{header|Phix}}==
Line 2,599:
Generates moving images similar to the julia output.
The distributed version also has variable speed, additional display modes, and a random dropping toggle.
<!--<
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Abelian_sandpile_model.exw</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 2,692:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
=={{header|Python}}==
===Python: Original, with output===
<
import matplotlib.pyplot as plt
Line 2,734:
plt.figure()
plt.gray()
plt.imshow(final_grid)</
{{Out}}
Before:
<
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
Line 2,746:
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]</
After:
<
[0. 0. 0. 1. 2. 1. 0. 0. 0. 0.]
[0. 0. 2. 2. 2. 2. 2. 0. 0. 0.]
Line 2,757:
[0. 0. 0. 1. 2. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]</
An interactive variant to the above solution:
<
from time import sleep
Line 2,825:
run(area)
print('\nAfter:')
show_area(area)</
{{Out}}
<
Before:
0 0 0 0 0 0 0 0 0 0
Line 2,851:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
</syntaxhighlight>
===Python: using tkinter===
<
''' Python 3.6.5 code using Tkinter graphical user
interface (Canvas widget) to display final results.'''
Line 2,970:
## 0 0 0 0 0 0 0 0 0
## 0 0 0 0 0 0 0 0 0
</syntaxhighlight>
=={{header|Raku}}==
Line 2,978:
Defaults to a stack of 1000 and showing progress. Pass in a custom stack size if desired and -hide-progress to run without displaying progress (much faster.)
<
signal(SIGINT).tap: { cleanup(); exit(0) }
Line 3,024:
print "\e[1;1H", @buffer.map( { @color[$_ min 4] }).join;
cleanup;</
Passing in 2048 as a stack size results in: [https://github.com/thundergnat/rc/blob/master/img/Abelian-sandpile-model-perl6.png Abelian-sandpile-model-perl6.png] (offsite .png image)
===SDL2 Animation===
<
use SDL2::Raw;
Line 3,125:
}
$fps
}</
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|Rust}}==
<
//
// It loops over the current state of the sandpile and updates it on-the-fly.
Line 3,251:
display(&playfield);
//write_pile(&playfield);
}</
'''Output:'''
Line 3,274:
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<
; Create an empty (all cells 0) grid of the specified size.
Line 3,377:
(asm-stabilize asm)
(printf "After:~%")
(grid-display asm digcnt)))</
{{out}}
<pre>
Line 3,403:
=={{header|VBA}}==
<
Application.ScreenUpdating = False
For i = 1 To a
Line 3,549:
Debug.Print "End:" & Now()
End Sub</
'''Output:'''
<pre>
Line 3,558:
{{trans|Go}}
<
import strings
Line 3,653:
// after the recursive algorithm has ended.
// write_pile(pile)
}</
{{out}}
Line 3,678:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
class Sandpile {
Line 3,747:
var str2 = s.toString
printAcross.call(str1, str2)
}</
{{out}}
Line 3,782:
=={{header|XPL0}}==
<
char Pile1(Size*Size), Pile2(Size*Size);
int Spigot, I, X, Y;
Line 3,808:
I:= Pile1; Pile1:= Pile2; Pile2:= I;
until Spigot < 4;
]</
{{out}}
|