# Tag Info

6

Indeed, the newt function as you wrote it freezes Mathematica at least for a minute or so (I aborted it afterwards without waiting to see if it would complete). Instead, you can prevent any attempts at symbolically evaluating the newt function by DensityPlot by restricting it to numerical arguments only: Clear[newt] newt[n_?NumericQ, z_?NumericQ] := ...

4

f::zero = "x is zero."; f = Compile[{{x, _Complex}}, If[x == 0, Throw[Message[f::zero]; $Failed]; Return[1/x], x^2]]; Catch[f[0]] Or f = With[{cf = Compile[{{x, _Complex}}, If[x == 0, Throw[Message[f::zero];$Failed], x^2]]}, Catch[cf[#]] &]; f[0] In the case of x == 0 there will be one (thanks to the comment by Oleksandr) ...

4

ClearAll[f, fc] f::zero = "x is zero."; (* compiled part *) fc = Compile[{{x, _Complex}}, (* First part of the code; can be anything . . *) If[x == 0, (* Error test*) {0, 0}, (* Error response, first part is status, second part is dummy *) {1, x^2} (* Normal response, first part is status, second part actual result *) ] ]; ...

6

Here is a compiled implementation of the Voigt profile function, based on an approximation derived by Chiarella and Reichel and improved by Abrarov, Quine and Jagpal: voigt = With[{n = 24, τ = 12}, With[{d = N[Range[n] π/τ], b = N[Exp[-(Range[n] π/τ)^2]], s = N[PadRight[{}, n, {-1, 1}]], sq = N[Sqrt[2]], sp ...

5

As a proof of concept, I present here a straightforward implementation of Don Knuth's "Algorithm L" for generating permutations in lexicographic order: cPermutations = Compile[{{list, _Integer, 1}}, Module[{n = Length[list], p = Sort[list], pbag, j, k}, pbag = Internal`Bag[Most[{1}]]; ...

4

For completeness, here is a way to extend the compiled or LibraryLink approaches to arbitrarily large integers. Since it comes so long after the original answer, I post it separately. As explained in this answer, we can bridge the gap between arbitrary and machine precision at least somewhat efficiently by using IntegerDigits to express a large integer as a ...

2

I suggest altering the definition of test as follows: test = Compile[{{x, _Real}}, If[Sin[x] > 0.1, Tan[x], 0.], "RuntimeOptions" -> "EvaluateSymbolically" -> False ]; Note that the 0 third argument of If is changed to 0.. While it seems to compile correctly anyway for this particular example, my experience has been that Compile can ...

9

Original answer J.M.'s answer in About multi-root search in Mathematica for transcendental equations shows how to approximate an analytic function in a finite interval by its Chebyshev series. We can do the same here, if we avoid the singularity at zero. Here is a utility function for evaluating a Chebyshev expansion whose coefficients are stored in the ...

Top 50 recent answers are included