Abelian sandpile model: Difference between revisions

m
syntax highlighting fixup automation
(Added a Scheme implementation.)
m (syntax highlighting fixup automation)
Line 30:
</pre>
=={{header|11l}}==
<langsyntaxhighlight lang=11l>V grid = [[0] * 10] * 10
grid[5][5] = 64
 
Line 77:
L(row) grid
L(c) row
ppm.write_bytes(colors[c])</langsyntaxhighlight>
 
{{out}}
Line 109:
{{works with|as|Raspberry Pi 3B version Buster 64 bits
or android 64 bits with application Termux }}
<langsyntaxhighlight lang=AArch64 Assembly>
/* 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>
</lang>
{{Output}}
<pre>
Line 340:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<langsyntaxhighlight lang=ARM Assembly>
/* ARM assembly Raspberry PI or android 32 bits */
/* program abelian.s */
Line 532:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
{{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>
<lang C>
#include<stdlib.h>
#include<string.h>
Line 686:
return 0;
}
</syntaxhighlight>
</lang>
 
Console output :
Line 725:
<!-- c++ bindings -->
<br>
<langsyntaxhighlight lang=cpp>#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
Line 798:
 
return 0;
}</langsyntaxhighlight>
Compile with following <tt>CMakeList.txt</tt>:
<langsyntaxhighlight lang=cmake>cmake_minimum_required(VERSION 3.1)
project(abelian_sandpile)
 
Line 814:
 
target_compile_options(abelian_sandpile PRIVATE -march=native -std=c++14)
target_link_libraries(abelian_sandpile xtensor ${OIIO})</langsyntaxhighlight>
 
=={{header|Delphi}}==
{{Trans|Python}}
<langsyntaxhighlight lang=Delphi>
program Abelian_sandpile_model;
 
Line 978:
Readln;
end.
</syntaxhighlight>
</lang>
 
 
Line 984:
{{works with|gforth|0.7.3}}
<br>
<langsyntaxhighlight lang=forth>#! /usr/bin/gforth -d 20M
\ 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</langsyntaxhighlight>
 
{{out}}
Line 1,065:
{{works with|gfortran|9.2.0}}
The Abelian sandpile operations are defined here.
<langsyntaxhighlight lang=fortran>module abelian_sandpile_m
 
implicit none
Line 1,156:
end subroutine
 
end module</langsyntaxhighlight>
 
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]].
<langsyntaxhighlight lang=fortran>program main
 
use rgbimage_m
Line 1,194:
call im%write('fig.ppm')
 
end program</langsyntaxhighlight>
 
=={{header|Fōrmulæ}}==
Line 1,205:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang=fsharp>
// 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>
</lang>
{{out}}
<pre>
Line 1,260:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang=freebasic>ScreenRes 320, 200, 8
WindowTitle "Abelian sandpile model"
 
Line 1,290:
Loop Until partic < 4
Bsave "abelian_sandpile.bmp",0
Sleep</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,408:
// after the recursive algorithm has ended.
// writePile(pile)
}</langsyntaxhighlight>
 
{{out}}
Line 1,438:
<br>
Using a custom monad to make the code cleaner.
<langsyntaxhighlight lang=haskell>{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
Line 1,553:
fp = printf "sandpile_%d_%d.pgm" size height
toPGM b fp
putStrLn $ "wrote image to " ++ fp</langsyntaxhighlight>
 
{{out}}
Line 1,563:
=={{header|J}}==
 
<langsyntaxhighlight lang=J>grid=: 4 : 'x (<<.-:2$y)} (2$y)$0' NB. y by y grid with x grains in middle
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 </langsyntaxhighlight>
 
=={{header|Java}}==
This is based on the JavaScript implementation linked to in the task description.
<langsyntaxhighlight lang=java>import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Line 1,682:
};
private static final int GRID_LENGTH = 300;
}</langsyntaxhighlight>
 
{{out}}
Line 1,689:
=={{header|Julia}}==
Modified from code by Hayk Aleksanyan, viewable at github.com/hayk314/Sandpiles, license viewable there.
<langsyntaxhighlight lang=julia>module AbelSand
 
# supports output functionality for the results of the sandpile simulations
Line 1,964:
 
Z_lat, Odometer = AbelSand.move(100000)
</langsyntaxhighlight>{{out}}
[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}}==
<langsyntaxhighlight lang=lua>local sandpile = {
init = function(self, dim, val)
self.cell, self.dim = {}, dim
Line 2,006:
sandpile:init(15, 256)
sandpile:iter()
sandpile:draw()</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang=Mathematica>ClearAll[sp]
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"]</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang=Nim>
# Abelian sandpile.
 
Line 2,208:
#grid.writePpmFile(fmt"grid_{initVal}.ppm")
grid.writePngFile(fmt"grid_{initVal}.png")
</syntaxhighlight>
</lang>
 
{{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.
<langsyntaxhighlight lang=pascal>
program Abelian2;
{$IFDEF FPC}
Line 2,423:
OneTurn(100000);
END.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,487:
{{works with|OCaml|4.11}}
In Sandpile module (sandpile.ml)
<langsyntaxhighlight lang=OCaml>
module Make =
functor (M : sig val m : int val n : int end)
Line 2,567:
; S.avalanche ()
; S.print ()
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
<langsyntaxhighlight lang=Perl>#!/usr/bin/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
}</langsyntaxhighlight>
 
=={{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.
<!--<langsyntaxhighlight lang=Phix>-->
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|Python}}==
===Python: Original, with output===
<langsyntaxhighlight lang=Python>import numpy as np
import matplotlib.pyplot as plt
 
Line 2,734:
plt.figure()
plt.gray()
plt.imshow(final_grid)</langsyntaxhighlight>
{{Out}}
Before:
<langsyntaxhighlight lang=Python>[[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.]
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.]]</langsyntaxhighlight>
After:
<langsyntaxhighlight lang=Python>[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[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.]]</langsyntaxhighlight>
 
An interactive variant to the above solution:
<langsyntaxhighlight lang=Python>from os import system, name
from time import sleep
 
Line 2,825:
run(area)
print('\nAfter:')
show_area(area)</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight lang=Python>
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>
</lang>
 
===Python: using tkinter===
<langsyntaxhighlight lang=Python>
''' 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>
</lang>
 
=={{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.)
 
<langsyntaxhighlight lang=perl6>sub cleanup { print "\e[0m\e[?25h\n"; exit(0) }
 
signal(SIGINT).tap: { cleanup(); exit(0) }
Line 3,024:
print "\e[1;1H", @buffer.map( { @color[$_ min 4] }).join;
 
cleanup;</langsyntaxhighlight>
 
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===
<langsyntaxhighlight lang=perl6>use NativeCall;
use SDL2::Raw;
 
Line 3,125:
}
$fps
}</langsyntaxhighlight>
 
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}}==
<langsyntaxhighlight lang=rust>// This is the main algorithm.
//
// It loops over the current state of the sandpile and updates it on-the-fly.
Line 3,251:
display(&playfield);
//write_pile(&playfield);
}</langsyntaxhighlight>
 
'''Output:'''
Line 3,274:
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<langsyntaxhighlight lang=scheme>; A two-dimensional grid of values...
 
; Create an empty (all cells 0) grid of the specified size.
Line 3,377:
(asm-stabilize asm)
(printf "After:~%")
(grid-display asm digcnt)))</langsyntaxhighlight>
{{out}}
<pre>
Line 3,403:
 
=={{header|VBA}}==
<langsyntaxhighlight lang=VBA>Sub SetupPile(a As Integer, b As Integer)
Application.ScreenUpdating = False
For i = 1 To a
Line 3,549:
Debug.Print "End:" & Now()
 
End Sub</langsyntaxhighlight>
'''Output:'''
<pre>
Line 3,558:
{{trans|Go}}
 
<langsyntaxhighlight lang=vlang>import os
import strings
 
Line 3,653:
// after the recursive algorithm has ended.
// write_pile(pile)
}</langsyntaxhighlight>
 
{{out}}
Line 3,678:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang=ecmascript>import "/fmt" for Fmt
 
class Sandpile {
Line 3,747:
var str2 = s.toString
printAcross.call(str1, str2)
}</langsyntaxhighlight>
 
{{out}}
Line 3,782:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=XPL0>def Size = 200;
char Pile1(Size*Size), Pile2(Size*Size);
int Spigot, I, X, Y;
Line 3,808:
I:= Pile1; Pile1:= Pile2; Pile2:= I;
until Spigot < 4;
]</langsyntaxhighlight>
 
{{out}}
10,333

edits