The list of compilable functions (given by Compile`CompilerFunctions[] // Sort) shows one of the more interesting- and core-sounding functions: Compile`SetIterate. It does not appear in the Mathematica Documentation Center. Moreover, except for references to merely the list of compilable functions, a search for Compile`SetIterate on the Mathematica Stack Exchange comes up with nothing and a Google search for Compile`SetIterate reveals nothing I can see relating to Mathematica.

What does Compile`SetIterate do?


1 Answer 1


It sets an iterator:

cfun = Compile[{{n, _Integer}},
  Table[If[x == 2, Compile`SetIterate[x, 4]]; x, {x, n}]

Note that 2 and 3 are skipped and the entries beyond the x = n one are junk:

(*  {1, 4, 5, -5, 0}  *)

(*  {1, 4, 5, 6, 7, 8, 5244412656, 5244438248}  *)

You need to truncate the table to avoid the uninitialized entries. [A table of size n is preallocated, and essentially a for-loop intializes the table entries. Here Compile`SetIterate short-circuits the for-loop and the initialization.]

  • $\begingroup$ I would think that decreasing the iterate would cause the loop to overrun the end of the table, but no error is given and you get a table of size n. E.g. Compile[{{n, _Integer}}, Block[{go = True}, Table[If[go && x == 4, go = False; Compile`SetIterate[x, 2]]; x, {x, n}] ] ][8] $\endgroup$
    – Michael E2
    Commented Sep 16, 2021 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.