# Tag Info

2

By default, in compiled functions, underflow is not caught and becomes zero automatically. cf = Compile[x, Module[{z = x, y = x}, While[y > 0, z = y; y = y/2]; {y, z}]]; cf[1.] (* {0., 5.*10^-324} *) Such a number as the number 5.*10^-324 achieved just before underflow is a subnormal machine number. The minimum machine number with full machine ...

4

As @episanty says in a comment, try applying Chop to your data before you return the value. It's compilable, according to List of compilable functions, which means it is appropriate for you here. From the documentation, Chop[expr] replaces approximate real numbers in expr that are close to zero by the exact integer 0. The default tolerance is ...

1

Something like this: m4 = Quiet[Table[a[[i, j]], {i, 4}, {j, 4}]]; soln = With[{det = Simplify[Det[m4]]}, Compile[{{a, _Real, 2}}, det]]; soln[RandomReal[1., {4, 4}]] -0.036117495644621564 But Det is quite efficient as it is, I would think.

0

In principle 63 variable is not a problem. Lets define them var = Table[ToExpression["x" <> ToString[i]], {i, 64}]; and a Quadratic function Q = var.var; This defines the function fun = Compile[Sequence@Map[{{#, _Real}} &, var] // Evaluate, Q, CompilationOptions -> {"InlineExternalDefinitions" -> True}]; and the minimization ...

4

Update What if we use instead of Sin an expression like a+b? I'll try a simple example, namely minimizing $(a + 3)^2 + (b - 3)^2$. Making use of CompilationOptions, I'll define a function with two variables, then nest that inside another compiled function prior to minimization. Needs["CompiledFunctionTools"] myfunction = Compile[{{a}, {b}}, (a + ...

5

You could also use CompilationOptions fun = Compile[{x}, variable, CompilationOptions -> {"InlineExternalDefinitions" -> True}]; This will automatically inline the definition of variable, which is unknown to the compiler otherwise.

4

You have a slight syntax error and a deeper problem binding the x in your "variable" to the x you have in the Compile function. Both can be fixed... variable = Sin[x]; fun = Compile[{{x, _Real}}, Evaluate[variable]]; fun[1] You must tell Compile that x is Real, hence the _Real (it only has zero dimensions so this is assumed). The x in Compile[{{x etc) ...

4

Here is a quick (and dirty?) implementation of the Laplace distribution. Relationship to the uniform distribution as given on Wikipedia: randomLaplace = With[{$MachineEpsilon =$MachineEpsilon}, Compile[{{µ, _Real, 0}, {b, _Real, 0}, {n, _Integer, 0}}, With[{u = RandomReal[{-1/2, 1/2} (1 - \$MachineEpsilon), n]}, µ - b Sign[u] Log[1 - 2 Abs[u]] ] ...

8

Another option to the answer posted by @Andy Ross cropped up in a recent question of mine about corrupting an image with Poisson noise. In my own answer, I made use of LibraryLink to utilise the distributions built into C++, which you can find here: http://www.cplusplus.com/reference/random/ This was especially useful in my case because Poisson noise in an ...

6

In this instance, compiling your triply nested For loops is unlikely to be the most effective way to speed up your code. I may have misunderstood what you are trying to do, being allergic to nested loops. However, two obvious speedups occur to me: Define your starting value in a more efficient way Use functional programming (specifically NestList) to build ...

6

You can fix the error by setting max = 0. (Note the .). But this doesn't really Compile completely and you can check that by inspecting the 6th Part of the compiled function: FreeQ[cf1[[6]], _Function, {0, Infinity}] False Whenever your compiled function has a Function definition in Part 6, your function did not compile properly. On the other hand, ...

Top 50 recent answers are included