# Tag Info

2

In addition to @ybeltukov's excellent answer, I thought it would be worth noting the behaviour of RuntimeOptions, when compiling to either the Wolfram Virtual Machine (WVM) or to C, for these subnormal positive doubles. f = Compile[{{t, _Real}}, Exp[-9 t^2]]; Needs["GeneralUtilities"] f[1.] (* = 0.00012341 *) f[8.872] (* = 2.191*10^-308 *) Do[f[1.], ...

5

Looking at your code, the problem occurs when you're trying to return {node-1, Transpose[{xn,phi}]}. If instead you run the following code, which only returns Transpose[{xn,phi}], calU=Compile[{{x,_Real,1},{energy,_Real},{m,_Real},{a,_Real}}, Module[{i,node,xn,nn,phi,V,h,f,temp}, h=x[[3]]; xn=Range[x[[1]],x[[2]],h]; nn=Length@xn; ...

3

The integral over a spherical region is easily performed by Mathematica even analytically. Assuming f=1 and for brevity putting the center of the sphere at the origin: Timing@Integrate[1, {x, -r, +r}, {y, -Sqrt[r^2 - x^2], +Sqrt[ r^2 - x^2]}, {z, -Sqrt[r^2 - x^2 - y^2], +Sqrt[r^2 - x^2 - y^2]}] (*{0.218401, 4 Pi r^3 / 3}*) Assigning a numerical value to ...

3

For your example, I would simply do this: R = 2.3; {x0, y0, z0} = {1.2, 2.3, 3.4}; NIntegrate[1, {x, -R + x0, R + x0}, {y, y0 - Sqrt[R^2 - (x - x0)^2], y0 + Sqrt[R^2 - (x - x0)^2]}, {z, z0 - Sqrt[R^2 - (x - x0)^2 - (y - y0)^2], z0 + Sqrt[R^2 - (x - x0)^2 - (y - y0)^2]}]; For a general function func = Function[{x,y,z},body] and a set of boundaries ...

1

This seems to work. Use With to "inject" the inner definition into outer. Clear[inner]; inner = Compile[{{i, _Integer}, {j, _Integer}}, If[i >= j, 0, AppendTo[bag, list]; inner[i + 1, j]]]; Clear[outer]; outer = With[{inner = inner}, Compile[{{i, _Integer}}, Block[{list = ConstantArray[0, {i, 2}], bag}, bag = {list}; ...

5

As @rasher points out in a comment, the compensated summation form of Total can't be compiled. You can check this using CompilePrint - note the call to MainEvaluate. Needs["CompiledFunctionTools"] CompilePrint@f2 (* from your question *) There seem to be plenty of options for summing a list in Mathematica. For example, you can use Plus, which ...

0

This variant works. But it will of necessity do some evaluation outside of Compile so it may not offer much in the way of a speed gain (I do not have time to check right now). Another drawback is I have not succeeded in getting it to cooperate with compiling to C. SetAttributes[myreap, HoldFirst]; myreap[a__] := Reap[a][[2, 1]] func = Compile[{}, ...

0

When Compile is used the list returned must have a consistent structure. A version of your 2. code block that does compile without errors: func = Compile[{}, Module[{t, tmax, dt, n, eps, tbl1, tbl2, i, result}, n = 10; t = 0.0; tmax = 10.0; eps = 10.0^-2; tbl1 = Table[0.0, {n}]; tbl2 = Table[0.0, {n}]; result = {{tbl1, tbl2}}; (* <== extra {} ...

Top 50 recent answers are included