Compiled Post Tag System
Compiled Post Tag System
In[]:=
$postRules = { (* {phase, value} {newPhase, newValues} *) {0, 0} {1, {0}}, {1, 0} {2, {0}}, {2, 0} {0, {}}, {0, 1} {2, {1, 1}}, {1, 1} {0, {1}}, {2, 1} {1, {0}}};
In[]:=
TagSystemNextGeneration = FunctionCompile[Function[{ Typed[initPhase, "MachineInteger"], Typed[initState, TypeSpecifier["PackedArray"]["MachineInteger", 1]]}, Module[{ emptyNumericList, newState, phase, newPhase, i}, emptyNumericList = Drop[{0}, 1]; newState = emptyNumericList; phase = initPhase; For[i = 1, i ≤ Length[initState], ++i, newPhase = -1; If[initState[[i]] 0 && phase 0, AppendTo[newState, 0]; newPhase = 1; ]; If[initState[[i]] 0 && phase 1, AppendTo[newState, 0]; newPhase = 2; ]; If[initState[[i]] 0 && phase 2, newPhase = 0; ]; If[initState[[i]] 1 && phase 0, AppendTo[newState, 1]; AppendTo[newState, 1]; newPhase = 2; ]; If[initState[[i]] 1 && phase 1, AppendTo[newState, 1]; newPhase = 0; ]; If[initState[[i]] 1 && phase 2, AppendTo[newState, 0]; newPhase = 1; ]; phase = newPhase; ]; AppendTo[newState, phase]]]];
In[]:=
Through[{Last,Most}@TagSystemNextGeneration[0,{1,0,1,1,0,0,0,0,1,1,1}]]
Out[]=
{2,{1,1,1,1,0,0,0,0,0,1,1,1}}
In[]:=
TagSystemGenerationsCompiled = FunctionCompile[Function[{ Typed[initPhase, "MachineInteger"], Typed[initState, TypeSpecifier["PackedArray"]["MachineInteger", 1]]}, Module[{ TagSystemNextGeneration, j, states}, TagSystemNextGeneration = Function[{initStateAndPhase}, Module[{newState, phase, newPhase, i}, newState = CreateDataStructure["DynamicArray"]; phase = initStateAndPhase["Part", -1]; For[i = 1, i < initStateAndPhase["Length"], ++i, newPhase = -1; If[initStateAndPhase[[i]] 0 && phase 0, newState["Append", 0]; newPhase = 1; ]; If[initStateAndPhase[[i]] 0 && phase 1, newState["Append", 0]; newPhase = 2; ]; If[initStateAndPhase[[i]] 0 && phase 2, newPhase = 0; ]; If[initStateAndPhase[[i]] 1 && phase 0, newState["Append", 1]; newState["Append", 1]; newPhase = 2; ]; If[initStateAndPhase[[i]] 1 && phase 1, newState["Append", 1]; newPhase = 0; ]; If[initStateAndPhase[[i]] 1 && phase 2, newState["Append", 0]; newPhase = 1; ]; phase = newPhase; ]; newState["Append", phase] ]]; states = CreateDataStructure["DynamicArray"]; states["Append", CreateDataStructure["DynamicArray"]]; For[j = 1, j ≤ Length[initState], ++j, states["Part", 1]["Append", initState[[j]]]; ]; states["Part", 1]["Append", initPhase]; While[states["Length"] < 2 || Not[states["Part", -1] === states["Part", -2]], Print["Part ", states["Length"], ": ", states["Part", -1]]; states["Append", TagSystemNextGeneration[states["Part", -1]]]; ]; states]]];
In[]:=
TagSystemGenerationsCompiled[0,{1,0,1,1,0,0,0,0,1,1,1}]
Part 1: DynamicArray[1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0]
Part 2: DynamicArray[1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 2]
Part 3: DynamicArray[0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0]
Part 4: DynamicArray[0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0]
Part 5: DynamicArray[0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 2]
Part 6: DynamicArray[1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0]
Part 7: DynamicArray[1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1]
Part 8: DynamicArray[1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1]
Out[]=
TypeProduct
In[]:=
TagSystemGenerationsCompiled[2,{0,0,1,1,1,0,0}]
Out[]=
{0,1,1,1,0,0,0}
In[]:=
TagSystemGenerationsCompiled[0,{0,1,1,1,0,0}]
Out[]=
{0,1,1,1,0,0,0}
In[]:=
TagSystemGenerations[generations_Integer][initPhase_Integer, initState : {___Integer}] := TagSystemGenerationsCompiled[initPhase, initState, generations]
In[]:=
AllInits[count_] := Tuples[{Range[0, 2], IntegerDigits[#, 2, count] & /@ Range[0, 2^count - 1]}]
In[]:=
AllInits[4]
Out[]=
{{0,{0,0,0,0}},{0,{0,0,0,1}},{0,{0,0,1,0}},{0,{0,0,1,1}},{0,{0,1,0,0}},{0,{0,1,0,1}},{0,{0,1,1,0}},{0,{0,1,1,1}},{0,{1,0,0,0}},{0,{1,0,0,1}},{0,{1,0,1,0}},{0,{1,0,1,1}},{0,{1,1,0,0}},{0,{1,1,0,1}},{0,{1,1,1,0}},{0,{1,1,1,1}},{1,{0,0,0,0}},{1,{0,0,0,1}},{1,{0,0,1,0}},{1,{0,0,1,1}},{1,{0,1,0,0}},{1,{0,1,0,1}},{1,{0,1,1,0}},{1,{0,1,1,1}},{1,{1,0,0,0}},{1,{1,0,0,1}},{1,{1,0,1,0}},{1,{1,0,1,1}},{1,{1,1,0,0}},{1,{1,1,0,1}},{1,{1,1,1,0}},{1,{1,1,1,1}},{2,{0,0,0,0}},{2,{0,0,0,1}},{2,{0,0,1,0}},{2,{0,0,1,1}},{2,{0,1,0,0}},{2,{0,1,0,1}},{2,{0,1,1,0}},{2,{0,1,1,1}},{2,{1,0,0,0}},{2,{1,0,0,1}},{2,{1,0,1,0}},{2,{1,0,1,1}},{2,{1,1,0,0}},{2,{1,1,0,1}},{2,{1,1,1,0}},{2,{1,1,1,1}}}