Subject: Best way to use a compiled function in findroot
From: Robert Dodier
Date: Mon, 30 Aug 2010 09:31:17 -0600
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