# What exactly prevents this code from compiling?

I am trying to compile a function that operates on an initial array (doing some sequential operation) to produce a matrix (which consists of N iterations of the initial array).

Below is a minimal working example:

NT = 10^1; NM = 2;
minitial = 2 RandomInteger[{}, NM] - 1.;
mzero = ConstantArray[0, {NT, NM}];

networkStep =
Compile[{{minitial, _Integer,
1}, {NT, _Integer}, {NM, _Integer}, {mzero, _Real, 2}},
Block[{md = minitial, moo = mzero},
Rescale@Table[
Do[md[[j]] = Sign[RandomReal[{-1, 1}]], {j, Range@NM}];
moo[[i, ;;]] = md, {i, 1, NT}]]];

mm = networkStep[minitial, NT, NM, mzero]


What prevents this function from compiling? Everything seems localized and appropriately defined to me.

• Rescale isn't compilable: mathematica.stackexchange.com/a/1101/1871 ;; (whose FullForm is 1;;All) cannot be compiled, either: mathematica.stackexchange.com/a/61651/1871 Commented Dec 24, 2020 at 7:57
• Thank you - replacing ;; with All fixed the problem. Rescale seems like it can't be compiled but it does compile and speeds things up. Commented Dec 24, 2020 at 10:10
• Rescale results in a MainEvaluate in the output of CompilePrint, but given it's called only once, it doesn't cause significant slowing down. Commented Dec 24, 2020 at 10:25

Based on xzczd's comment, replacing ";;" with "All" in the code fixed the compilation problem (Rescale does not pose a problem):

NT = 10^5; NM = 2;
minitial = 2 RandomInteger[{}, NM] - 1.;
mzero = ConstantArray[0, {NT, NM}];

networkStep =
Compile[{{minitial, _Integer,
1}, {NT, _Integer}, {NM, _Integer}, {mzero, _Real, 2}},
Block[{md = minitial, moo = mzero},
Rescale@Table[
Do[md[[j]] = Sign[RandomReal[{-1, 1}]], {j, Range@NM}];
moo[[i, ;;]] = md, {i, 1, NT}]], CompilationTarget -> "C"];

mm = AbsoluteTiming[networkStep[minitial, NT, NM, mzero]]; mm[[1]]


gives

1.11684 seconds

while

NT = 10^5; NM = 2;
minitial = 2 RandomInteger[{}, NM] - 1.;
mzero = ConstantArray[0, {NT, NM}];

networkStep =
Compile[{{minitial, _Integer,
1}, {NT, _Integer}, {NM, _Integer}, {mzero, _Real, 2}},
Block[{md = minitial, moo = mzero},
Rescale@Table[
Do[md[[j]] = Sign[RandomReal[{-1, 1}]], {j, Range@NM}];
moo[[i, All]] = md, {i, 1, NT}]], CompilationTarget -> "C"];

mm = AbsoluteTiming[networkStep[minitial, NT, NM, mzero]]; mm[[1]]


gives 0.025396 seconds.