Abelian sandpile model: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 30:
</pre>
=={{header|11l}}==
<syntaxhighlight lang="11l">V grid = [[0] * 10] * 10
grid[5][5] = 64
 
Line 109:
{{works with|as|Raspberry Pi 3B version Buster 64 bits
or android 64 bits with application Termux }}
<syntaxhighlight lang=AArch64"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program abelian64.s */
Line 340:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI or android 32 bits */
/* program abelian.s */
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"c">
#include<stdlib.h>
#include<string.h>
Line 725:
<!-- c++ bindings -->
<br>
<syntaxhighlight lang="cpp">#include <iostream>
#include "xtensor/xarray.hpp"
#include "xtensor/xio.hpp"
Line 800:
}</syntaxhighlight>
Compile with following <tt>CMakeList.txt</tt>:
<syntaxhighlight lang="cmake">cmake_minimum_required(VERSION 3.1)
project(abelian_sandpile)
 
Line 818:
=={{header|Delphi}}==
{{Trans|Python}}
<syntaxhighlight lang=Delphi"delphi">
program Abelian_sandpile_model;
 
Line 984:
{{works with|gforth|0.7.3}}
<br>
<syntaxhighlight lang="forth">#! /usr/bin/gforth -d 20M
\ Abelian Sandpile Model
 
Line 1,065:
{{works with|gfortran|9.2.0}}
The Abelian sandpile operations are defined here.
<syntaxhighlight lang="fortran">module abelian_sandpile_m
 
implicit none
Line 1,159:
 
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]].
<syntaxhighlight lang="fortran">program main
 
use rgbimage_m
Line 1,205:
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Abelian sandpile model. Nigel Galloway: July 20th., 2020
type Sandpile(x,y,N:int[])=
Line 1,260:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">ScreenRes 320, 200, 8
WindowTitle "Abelian sandpile model"
 
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.
<syntaxhighlight lang="go">package main
 
import (
Line 1,438:
<br>
Using a custom monad to make the code cleaner.
<syntaxhighlight lang="haskell">{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
Line 1,563:
=={{header|J}}==
 
<syntaxhighlight lang=J"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
Line 1,570:
=={{header|Java}}==
This is based on the JavaScript implementation linked to in the task description.
<syntaxhighlight lang="java">import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Line 1,689:
=={{header|Julia}}==
Modified from code by Hayk Aleksanyan, viewable at github.com/hayk314/Sandpiles, license viewable there.
<syntaxhighlight lang="julia">module AbelSand
 
# supports output functionality for the results of the sandpile simulations
Line 1,969:
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">local sandpile = {
init = function(self, dim, val)
self.cell, self.dim = {}, dim
Line 2,025:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"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,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.
<syntaxhighlight lang=Nim"nim">
# Abelian sandpile.
 
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.
<syntaxhighlight lang="pascal">
program Abelian2;
{$IFDEF FPC}
Line 2,487:
{{works with|OCaml|4.11}}
In Sandpile module (sandpile.ml)
<syntaxhighlight lang=OCaml"ocaml">
module Make =
functor (M : sig val m : int val n : int end)
Line 2,570:
 
=={{header|Perl}}==
<syntaxhighlight lang=Perl"perl">#!/usr/bin/perl
 
use strict; # http://www.rosettacode.org/wiki/Abelian_sandpile_model
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.
<!--<syntaxhighlight lang=Phix"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,696:
=={{header|Python}}==
===Python: Original, with output===
<syntaxhighlight lang=Python"python">import numpy as np
import matplotlib.pyplot as plt
 
Line 2,737:
{{Out}}
Before:
<syntaxhighlight lang=Python"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,748:
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]</syntaxhighlight>
After:
<syntaxhighlight lang=Python"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,760:
 
An interactive variant to the above solution:
<syntaxhighlight lang=Python"python">from os import system, name
from time import sleep
 
Line 2,827:
show_area(area)</syntaxhighlight>
{{Out}}
<syntaxhighlight lang=Python"python">
Before:
0 0 0 0 0 0 0 0 0 0
Line 2,854:
 
===Python: using tkinter===
<syntaxhighlight lang=Python"python">
''' Python 3.6.5 code using Tkinter graphical user
interface (Canvas widget) to display final results.'''
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.)
 
<syntaxhighlight lang=perl6"raku" line>sub cleanup { print "\e[0m\e[?25h\n"; exit(0) }
 
signal(SIGINT).tap: { cleanup(); exit(0) }
Line 3,029:
 
===SDL2 Animation===
<syntaxhighlight lang=perl6"raku" line>use NativeCall;
use SDL2::Raw;
 
Line 3,130:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// This is the main algorithm.
//
// It loops over the current state of the sandpile and updates it on-the-fly.
Line 3,274:
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<syntaxhighlight lang="scheme">; A two-dimensional grid of values...
 
; Create an empty (all cells 0) grid of the specified size.
Line 3,403:
 
=={{header|VBA}}==
<syntaxhighlight lang=VBA"vba">Sub SetupPile(a As Integer, b As Integer)
Application.ScreenUpdating = False
For i = 1 To a
Line 3,558:
{{trans|Go}}
 
<syntaxhighlight lang="vlang">import os
import strings
 
Line 3,678:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
class Sandpile {
Line 3,782:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">def Size = 200;
char Pile1(Size*Size), Pile2(Size*Size);
int Spigot, I, X, Y;
10,327

edits