# List of compiled functions and then plot them 1 by 1.

I would like to create a list of compiled functions and then plot them 1 by 1. To illustrate the problem (In reality the functions I want to compile are of course not x^j but some complicated matrices):

cf = Table[Compile[{{x, _Real}}, x^j], {j, 1, 3}]


as output I get a list of compiled functions. Then I would like to plot one of them, for example the second:

Plot[cf[[2]][x], {x, -1, 1}]

During evaluation of In[107]:= CompiledFunction::cfse: Compiled expression j should be a machine-size integer. >>

During evaluation of In[107]:= CompiledFunction::cfex: Could not complete external evaluation at instruction 1; proceeding with uncompiled evaluation. >>


This is a bit weird since if I compile all those functions independently (without the use of a table around Compile) I can plot them nicely.

I hope this is not just a stupid syntax error, I am quite new to mathematica so please excuse me my probably stupid question :)

Luke

You need to use Evaluate inside the Compile call,
cf = Table[

• Nevermind. Didn't remove your Evaluate after copying and pasting. But Table[With[{j = j}, Compile[{{x, _Real}}, x^j]], {j, 1, 3}] still works. Feb 18 '16 at 17:19
• @march, that's what I would've gone with, too; With[] is handy for injecting externally-produced stuff into a compiled function. Feb 18 '16 at 17:41
• @J.M. I agree that With is great, but the fact is that I use Evaluate every single time I use Compile. I can't think of a situation where I wouldn't do do it. If it were bash I would make an alias Feb 18 '16 at 17:55