Best way to use a compiled function in findroot



On 30/08/2010 17:31, Robert Dodier wrote:
> On 8/30/10, Sergio Callegari<sergio.callegari at gmail.com>  wrote:
>
>    
>> (%i1) f(x):=(mode_declare(x,float),sin(x));
>> (%o1)              f(x) := (mode_declare(x, float), sin(x))
>> (%i2) compile(f);
>> (%o2)                                 [f]
>> (%i3) g(y):=block([x:gensym()],findroot(f(x)-y,x,-1,1));
>> (%o3)    g(y) := block([x : gensym()], findroot(f(x) - y, x, - 1, 1))
>>      
> Um, the function of interest here is find_root.
>
> Variations in which f(x) is evaluated for symbolic argument
> barf out an error because of the mode_declare(x, float).
> I guess that's all well and good. But it is easy enough to
> devise an expression that postpones evaluation appropriately.
> Try lambda([x], f(x) - y) or 'f(x) - y as the find_root argument.
>
> I don't know if it's important to compile f to begin with.
> You could probably save yourself some headaches if you
> don't compile f.
>
> FWIW
>
> Robert Dodier
>    
Hi,

unfortunately, simple quoting does not seem to work (see my previous 
message)

with

(%i5) g(y):=block([x:gensym()],findroot('f(x)-y,x,-1,1));
(%o5)    g(y) := block([x : gensym()], findroot('f(x) - y, x, - 1, 1))


I get:

(%i6) g(0);
(%o6) findroot(f(g900), g900, - 1, 1)


which is not what is expected...

with lambda, namely

g(y) := block([x : gensym()], findroot(lambda([x], f(x) - y), x, - 1,
1))


g(0) hangs forever...

I agree I can save myself headaches by not compiling the function... 
however...

- It is nice first to try things uncompiled and then to make them fast.
If I could make things fast in maxima without having to recode them in C 
with the gsl that would be great!

- In the specific case, the function I am trying to invert is recomputed 
many many times by find_root, so I guess that compiling it could make a 
difference.

- find_root is anyway a numeric algorithm

so it would be nice to be able to use also compiled functions with it.

Thanks,

Sergio