# How to return the rhs of a transformation rule from within Compile[]

I have defined the following function:

g = Compile[{{x0, _Real}},
sol = NSolve[
Rationalize[CDF[ExponentialDistribution[1], x] == x0, 10^-10],
{x}, Reals];
sol]


Running g[0.5] it fails with:

In[460]:= g[0.5]

During evaluation of In[460]:= CompiledFunction::cfse: Compiled expression {{x->0.693147}} should be a machine-size integer. >>

During evaluation of In[460]:= CompiledFunction::cfex: Could not complete external evaluation at instruction 2; proceeding with uncompiled evaluation. >>

Out[460]= {{x -> 0.693147}}

If I try to extract the rhs from the transformation rule with sol[[1,1,2]] I get the following error:

Compile::part: Part specification sol[[1,1,2]] cannot be compiled since the argument is not a tensor of sufficient rank. Evaluation will use the uncompiled function. >>

Compile::part: Part specification {{x->0.693147}}[[1,1,2]] cannot be compiled since the argument is not a tensor of sufficient rank. Evaluation will use the uncompiled function. >>

How could I let Compile[] know that I intend to return {{a -> b}} or how could I extract the rhs of the transformation rule and return that only?

EDIT: It appears that none of my functions (Rationalize[], NSolve[], ExponentialDistribution[]) is "compilable" based on this link by @MichaelE2.

-
See this question and perhaps these, too. I don't think you can compile your expression, and you might want to read up on how Compile works. –  Michael E2 Oct 25 '13 at 3:29
Here's another good one: mathematica.stackexchange.com/questions/1803/… –  Michael E2 Oct 25 '13 at 3:31

Just some observations (which may be counter the intention): 1. You could use in-built function: Quantile:

qf[x_] := Quantile[ExponentialDistribution[1], x]


qf[0.5] yields 0.693147

The uncompiled function could be done:

func[x0_] :=
First[x /.
Quiet@NSolve[CDF[ExponentialDistribution[1], x] == x0, x, Reals]]


In the preceding I have not used Rationalize. I was uncertain what the aim of its use was. If the aim is to express the quantile as a raitonal approximation than it can be applied post. If it was to aid calculation the compiled version seems problematic to me.

-
Thanks for the Quantile[] hint @ubpdqn! I will try it and it see if it speeds things up, because otherwise my function is veeery slow :( It takes ~2 secs for 200 samples. –  Zet Oct 25 '13 at 18:36
@Zet There is also InverseCDF[ExponentialDistribution[1], x0], which probably has the same underlying function as Quantile` for a distribution. –  Michael E2 Oct 25 '13 at 19:30