> A few comments about numerical solutions. Users will want to know the
> numerical method that is being used. Also, there are two ways to
> define the equation bein solved:
>
> #Sage expression
> find_root(sin(x)-cos(x), 0, pi)
>
> #Python function
> find_root(lambda x: sin(x)-cos(x), 0, pi)
>
> The second is substantially faster.
Even faster
sage: f(x) = sin(x)-cos(x)
sage: g = f._fast_float_()
sage: timeit("find_root(g, 0, pi)")
625 loops, best of 3: 24.7 µs per loop
Compared to
sage: timeit("find_root(lambda x: sin(x)-cos(x), 0, pi)")
625 loops, best of 3: 101 µs per loop
This means that find_root should be calling fast_float on its input,
and so the first (most obvious) solution would be the best.
- Robert
WARNING: There may be a gotcha.
sage: timeit('(sin(x)-cos(x))._fast_float_()')
5 loops, best of 3: 85.1 ms per loop
but
sage: timeit('find_root(sin(x)-cos(x), 0, pi)')
25 loops, best of 3: 46.1 ms per loop
So you think you're making this way faster by putting in _fast_float_,
but you're not because in this example because the call to
_fast_float_ itself takes twice as long as just not doing it at all.
For more complicated functions things would likely be different.
William
Good point. It's still curious that fast_float takes so long to call...
- Robert