How to resolve the algorithm Abelian sandpile model/Identity step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Abelian sandpile model/Identity step by step in the Lua programming language

Table of Contents

Problem Statement

Our sandpiles are based on a 3 by 3 rectangular grid giving nine areas that contain a number from 0 to 3 inclusive. (The numbers are said to represent grains of sand in each area of the sandpile). E.g. s1 = and s2 = Addition on sandpiles is done by adding numbers in corresponding grid areas, so for the above: If the addition would result in more than 3 "grains of sand" in any area then those areas cause the whole sandpile to become "unstable" and the sandpile areas are "toppled" in an "avalanche" until the "stable" result is obtained. Any unstable area (with a number >= 4), is "toppled" by loosing one grain of sand to each of its four horizontal or vertical neighbours. Grains are lost at the edge of the grid, but otherwise increase the number in neighbouring cells by one, whilst decreasing the count in the toppled cell by four in each toppling. A toppling may give an adjacent area more than four grains of sand leading to a chain of topplings called an "avalanche". E.g. The final result is the stable sandpile on the right. Note: The order in which cells are toppled does not affect the final result.

Show confirming output here, with your examples.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Abelian sandpile model/Identity step by step in the Lua programming language

Source code in the lua programming language

sandpile.__index = sandpile
sandpile.new = function(self, vals)
  local inst = setmetatable({},sandpile)
  inst.cell, inst.dim = {}, #vals
  for r = 1, inst.dim do
    inst.cell[r] = {}
    for c = 1, inst.dim do
      inst.cell[r][c] = vals[r][c]
    end
  end
  return inst
end
sandpile.add = function(self, other)
  local vals = {}
  for r = 1, self.dim do
    vals[r] = {}
    for c = 1, self.dim do
      vals[r][c] = self.cell[r][c] + other.cell[r][c]
    end
  end
  local inst = sandpile:new(vals)
  inst:iter()
  return inst
end

local s1 = sandpile:new{{1,2,0},{2,1,1},{0,1,3}}
local s2 = sandpile:new{{2,1,3},{1,0,1},{0,1,0}}
print("s1 =")  s1:draw()
print("\ns2 =")  s2:draw()
local s1ps2 = s1:add(s2)
print("\ns1 + s2 =")  s1ps2:draw()
local s2ps1 = s2:add(s1)
print("\ns2 + s1 =")  s2ps1:draw()
local topple = sandpile:new{{4,3,3},{3,1,2},{0,2,3}}
print("\ntopple, before =")  topple:draw()
topple:iter()
print("\ntopple, after =")  topple:draw()
local s3 = sandpile:new{{3,3,3},{3,3,3},{3,3,3}}
print("\ns3 =")  s3:draw()
local s3_id = sandpile:new{{2,1,2},{1,0,1},{2,1,2}}
print("\ns3_id =") s3_id:draw()
local s3ps3_id = s3:add(s3_id)
print("\ns3 + s3_id =")  s3ps3_id:draw()
local s3_idps3_id = s3_id:add(s3_id)
print("\ns3_id + s3_id =")  s3_idps3_id:draw()


  

You may also check:How to resolve the algorithm Greatest common divisor step by step in the Ursa programming language
You may also check:How to resolve the algorithm Kronecker product step by step in the AWK programming language
You may also check:How to resolve the algorithm Bernoulli numbers step by step in the REXX programming language
You may also check:How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the Nim programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the Java programming language