Best way to use a compiled function in findroot




On Wed, 1 Sep 2010, Sergio Callegari wrote:

< On 31/08/2010 18:31, Robert Dodier wrote:
< > On Tue, Aug 31, 2010 at 10:07 AM, Sergio Callegari
< > <sergio.callegari at gmail.com>  wrote:
< > 
< >    
< > > (%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)
< > >      
< > There is no findroot function. There is a function find_root, however.
< > 
< >    
< > > g(y) := block([x : gensym()], findroot(lambda([x], f(x) - y), x, - 1, 1))
< > > 
< > > g(0) hangs forever...
< > >      
< > Did you omit the final semicolon?\
< >    
< I wonder if I'll ever get used to the difference between wxmaxima (impossible
< to cut and paste on the ml, but nice autosemicolon with shift+enter) and the
< text mode maxima!
< 
< In any case, it still does not work
< 
< g(y) := block([x : gensym()], findroot(lambda([x], f(x) - y), x, - 1, 1));
< (%o3) g(y) := block([x : gensym()], findroot(lambda([x], f(x) - y), x, - 1,
< 1))
< (%i4) g(0);
< (%o4)            findroot(lambda([x], f(x) - y), g899, - 1, 1)
 
How about:

(%i2) g(y) := block([x : ?gensym()], find_root(lambda([x], f(x) - y), x, - 1, 1))$

(%i3) f(x) := block(mode_declare(x,float),sin(x)); compile(f);

(%o3) f(x):=block(mode_declare(x,float),sin(x))

; In: LAMBDA ($X)

;   (SIN $X)
; Note: Unable to avoid inline argument range check
; because the argument range (DOUBLE-FLOAT) was not within 2^64
;

; Compilation unit finished.
;   1 note

(%o4) [f]
(%i5) g(0.5);

(%o5) .5235987755982989
(%i6) f(%);

(%o6) 0.5

Leo

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.