I want to automatically Compile an expression in between a routine of certain analytical manipulations. The number of arguments for the function may change for every run, so I would like to avoid hardcoding the arguments into my call of Compile. Consider this MWE:
vars = {x, y};
expr = Sin[x + y];
fun1 = With[{e = expr}, Compile[{{x, _Real}, {y, _Real}}, e]];
The disadvantage here is that I need to provide {{x, _Real}, {y, _Real}} as variables explicitly. I would rather like to be able to do something like
fun2 = With[{e = expr, v = Transpose[{vars, ConstantArray[_Real, Length@vars]}]},
Compile[v, e]]
where knowing vars in advance allows me to avoid manually computing v, print it and then copy-paste into Compile. All variants of fun2 I could come up with boil down to the issue that Compile only recognizes a single variable as input. Is there any way to achieve the desired functionality?


HoldCompletethan aList– b3m2a1 Mar 29 '18 at 05:02Hold[]orHoldComplete[]for the second version, because the part that assembles the sequence of arguments won't fire inside aHold[], unless youMapAt[]anEvaluate[]. – J. M.'s missing motivation Mar 29 '18 at 05:04With[{vars=varList}, Compile@@Hold[vars, expr]]. Allows you to keepexprheld. – b3m2a1 Mar 29 '18 at 05:28Holda lot more. Nice and compact solution. Side question: Is there a specific reason why you are using thePadRightbased way to generate the list of variables instead of my way? Is it just personal taste or is there more behind it? – Lukas Mar 29 '18 at 06:32PadRight[]in casevarshas a lot of variables in it, and I don't want to bother counting. Your method withConstantArray[]is fine and equivalent. – J. M.'s missing motivation Mar 29 '18 at 07:28