# Tag Info

3

Another way: comp = With[{x = Unevaluated@Array[#^2 &, n]}, Compile[{{n, _Integer}}, x]] By the way, Trace or TracePrint will show the order of evaluation, when there is a question about what is happening. When the code is short, like in this case, it can clarify what is going on. TracePrint@With[{x = Array[#^2 &, n]}, Compile[{{n, _Integer}}, x]...

3

As explained by Henrik, the problem is that Array is being evaluated before it gets inserted into Compile. The solution here is to use SetDelayed to prevent its evaluation. comp = With[{x := Array[#^2 &, n]}, Compile[{{n, _Integer}}, x]]

3

The error is thrown by Array, not by compile. With has to evaluate the code for x first and this is why Array throws the error.

3

3

The slowness is due to several instances of MainEvaluate. I replaced the Product factorials with Gamma, which turns out to be compilable. clist = Compile[{{a1, _Real}, {b1, _Real}, {a2, _Real}, {b2, _Real}, {c, _Real}, {upper, _Integer}}, Module[{ lambda = Exp[a1 - b2 + c], mu = Exp[a2 - b1], i, j}, {Sum[(Exp[(-lambda - mu)]*lambda^i*mu^...

1

The easiest solution is using NotebookEvaluate. I will provide a minimal example, assuming all files are saved in the same directory. I have a file notebook1.nb which depends on the value of some variable a, e.g. (* notebook1.nb *) Export["output.dat", a^2]; The notebook notebook0.nb can set the variable a and evaluate notebook1.nb (* notebook0.nb ...

4

This should work better: It generates a rectangular array (filled with zeroes) first and than fills in the entries: deltaX = 1./128; W = 256; Mmax = 40; lPoly = DeveloperToPackedArray[ Table[ LegendreP[order, -1. + 0.5 deltaX + (index - 1.) deltaX], {order, 0, Mmax}, {index, 1, W}] ]; XPoly = Compile[{{index, _Integer}, {lPoly, _Real, 2}}, ...

1

Indexed isn't bad. You can also use CompileGetElement: Hold[symbolicLHS = {{Derivative[1, 0][a][x], b}, {c, d}}; symbolicRHS = {Derivative[1, 0][a][y], e}; LHSwitharguments = symbolicLHS /. {Derivative[1, 0][a][___] -> arg1[], b -> arg1[], c -> arg1[], d -> arg1[], e -> arg1[]}; RHSwitharguments = ...

2

How about having n separate compiled functions?: cfunclst = MapThread[Compile[{{arg1, _Real, 1}}, LinearSolve@##] &, {LHSarray, RHSarray}] Then just use e.g. cFunclst[][{1., 2.}]. If you insist on using a single compiled function, then a possible solution is toseq = Flatten[#, 1] &@Transpose@{Range@Length@#, #} &; cfunc = Hold@ Compile[{...

1

I found Indexed, which is like Part without the warning message. In the compiled code, Indexed turns into Part. Leaving this up in case anyone knows a smoother way of going about this. symbolicLHS = {{Derivative[1, 0][a][x], b}, {c, d}}; symbolicRHS = {Derivative[1, 0][a][y], e}; LHSwitharguments = symbolicLHS /. {Derivative[1, 0][a][___] -> Indexed[...

Top 50 recent answers are included