Manipulating Some Lists in Compile

Consider a list of lists, such as:

Z={{1,4,3},{2,1,3},{3,1,4}}

Z is an $n \times n$ matrix (here $3 \times 3$) which represent sites. Each site has a random number. Here we have 9 sites that indexed with: 1,2,3,4,5,6,7,8,9.

Neighbors={{1, 2}, {2, 1, 3, 5, 4}, {3, 2}, {4, 7, 2}, {5, 6, 2, 8, 7, 9}, {6, 5, 9}, {7, 4, 5}, {8, 9, 5}, {9, 8, 6, 5}}

Each site have some random neighbors. In Neighbors's list first element of each member represent index of the site, and others represent neighbors of that site, for example here neighbors of site 2 are 1,3,5,4.

t={{1,0,1},{1,1,0},{0,0,0}}

Here $t$ is a list same as Z but just have 0 at first. In every step we choose some sites and adding 1 to the neighbors of that sites (here we choosing site with index 2).

I want to write a Compile function (func) that create list t. also I write two function ijTok, and kToij to transform index of sites in neighbors list to position of that site in Z or t.

At last we need to use FixedPoint:

FixedPoint[func,Z] or fixedPoint[func,z,neighbors]

I spent many hours on it and learn that Compile have some limitation. For example we not allow to use True,False value in it. this is what I write without compile I need to write same thing in Compile: Pseudo Code:

func[z_] := Module[{t= z /. x_Integer -> 0},
POSij = Position of chosen sites from  Z ;
POSk = Map[ijTok, POSij];
Neighborsij = Position of neighbors of chosen sites in {i, j} form;
t += 1 just for sites that are neighbors of chosen sites ;
Return[t];
]

Mathematica form of above pseudo code:

Func[Z_] := Module[{list = Z/.x_Integer->0},
POSij =Position[Z, #& (* choosing some special sites*)];
POSk = Map[ijTok, POSij];
NeiNodeij =
Map[Map[kTOij,
Drop[Neighbors[[#]], 1]] &, POSk];
t=Map[Map[list[[Sequence @@ #]]++ &, #] &, Neighborsij], 0]
]

Here is my previous question.

• I tried several version of this algorithm in compile, but each one have a special difficulty and don't work correctly. – jack cilba Jan 13 '16 at 21:44
• "return another list that created with t and Z", how is the "another list" created? I think you'd better describe your problem more clearly. – xzczd Jan 14 '16 at 3:55
• this part is not difficult and I did it in compile my self, some special sites in z should be minus and then adding each corresponding site's value (in z and t) together.@xzczd – jack cilba Jan 14 '16 at 5:23
• Well, to be honest, I think you're in the wrong way. Compile is the toy of experienced Mathematica user and one should gain a good enough understanding for "coding in Mathematica" before trying it. I think you'd better describe what you're trying to do in a more clear way so we can help figuring out if there's a better way to code. – xzczd Jan 14 '16 at 5:54
• If I understand the pseudo code correctly, it's just creating t described above? Then how about func[z_, nei_] := Module[{t = ConstantArray[0, Length@Flatten@z]}, t[[Rest@nei]] = 1; Partition[t, Length@z]]; func[Z, Neighbors[]] – xzczd Jan 14 '16 at 9:14

OK, let me kill the unanswered question. Your func can be simplified to:

func[z_, nei_] := Module[{t = ConstantArray[0, Length@Flatten@z]},
t[[Rest@nei]] = 1; Partition[t, Length@z]];
func[Z, Neighbors[]]

If you still want to compile it, you just need a little modification:

cfunc = Compile[{{z, _Integer, 2}, {nei, _Integer, 1}},
Module[{t = Table[0, {Length@Flatten@z}]}, (t[[#]] = 1) & /@ Rest@nei;
Partition[t, Length@z]]]
func[Z, Neighbors[]]

But according to my personal experience, pure list manipulation code (by pure I mean code that doesn't include algebraic operation) usually won't be sped up much (if any) by Compile, functions for list manipulation in Mathematica have already been highly optimized.