Tag Info

New answers tagged

5

1. Optimising the code given First of all I set up your exact example, with functions a, b, c, d and main defined by you, and ran the following code. Do[main[10], {1000}]; // AbsoluteTiming (* 10.22 seconds *) Next, I tried the code below. Here I've removed the Listable and Parallelization options, and instead I have inlined the functions. a2 = ...


5

You can get a significant boost by compiling ysaf: ysafc = Compile[{x, y}, Evaluate @ N @ ysaf[λ ArcSin[Sqrt[x^2 + y^2]], ArcTan[x, y]]]; func[x_?NumericQ, y_?NumericQ] := ysafc[x, y] gr = ContourPlot[{func[x, y] == 0, Sqrt[x^2 + y^2] == rmx}, {x, -rmx, rmx}, {y, -rmx, rmx}, RegionFunction -> Function[{x, y, z}, Sqrt[x^2 + y^2] <= rmx], Axes ...


4

One can gain a considerable speed gain by omitting RegionFunction and setting PlotPoints to 10. The following only needs 8 seconds (it uses some ideas of @PhilChang): A = { (8 Sqrt[1035741])/15625, 0, 0, 0, 0, Sqrt[6597877/15]/6250, 0, 0, 0, 0, -(Sqrt[(6338501/10)]/3125), 0, 0, 0, 0, (29 Sqrt[27347/30])/3125, 0, 0, 0, 0, ...


4

Some codes are from the answer of PhilChang Remove["Global`*"]; A = {(8 Sqrt[1035741])/15625, 0, 0, 0, 0, Sqrt[6597877/15]/6250, 0, 0, 0, 0, -(Sqrt[(6338501/10)]/3125), 0, 0, 0, 0, (29 Sqrt[27347/30])/3125, 0, 0, 0, 0, -(Sqrt[(4358874/5)]/3125), 0, 0, 0, 0, -(Sqrt[(352454081/3)]/31250), 0, 0, 0} // N; s[m_, l_] := If[Abs[m] > l, 0, ...


4

The orginal program takes 78s on my computer. First, Make vector A a real vector will make the program a little faster. A = {(8 Sqrt[1035741])/15625, 0, 0, 0, 0, Sqrt[6597877/15]/6250, 0, 0, 0, 0, -(Sqrt[(6338501/10)]/3125), 0, 0, 0, 0, (29 Sqrt[27347/30])/3125, 0, 0, 0, 0, -(Sqrt[(4358874/5)]/3125), 0, 0, 0, 0, ...


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 ...



Top 50 recent answers are included