Re: [sage-trac] Re: [Sage] #4457: [with patch, needs review] tutorial: add find_root() to "2.4.1 Solving Equations"

已查看 17 次
跳至第一个未读帖子

David Joyner

未读,
2009年1月24日 08:29:322009/1/24
收件人 Mike Hansen、sage-devel
On Sat, Jan 24, 2009 at 5:18 AM, Sage <tr...@math.washington.edu> wrote:
> #4457: [with patch, needs review] tutorial: add find_root() to "2.4.1 Solving
> Equations"
> ---------------------------+------------------------------------------------
> Reporter: dhbradshaw | Owner: mhansen
> Type: enhancement | Status: assigned
> Priority: major | Milestone: sage-3.4.1
> Component: documentation | Resolution:
> Keywords: |
> ---------------------------+------------------------------------------------
> Changes (by mhansen):
>
> * owner: tba => mhansen
> * status: new => assigned
> * summary: tutorial: add find_root() to "2.4.1 Solving Equations" =>
> [with patch, needs review] tutorial: add
> find_root() to "2.4.1 Solving Equations"
>
> Comment:
>
> I've made a patch for this against the Sphinx version of the reference
> manual.
>
> The output can be found at
> http://sage.math.washington.edu/home/mhansen/sage/devel/sage/doc/output/html/en/tutorial/tour_algebra.html
>


Separate issue:
FYI, the displayed equation following "Given an initial value problem
of the form..." on the above url
is missing a comma and a space.



> --
> Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/4457#comment:2>
> Sage <http://sagemath.org/>
> Sage - Open Source Mathematical Software: Building the Car Instead of Reinventing the Wheel
> >
>

Luiz Felipe Martins

未读,
2009年1月24日 11:46:352009/1/24
收件人 sage-...@googlegroups.com
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.

I wondered if find_root is a wraper of brentq, so I tried:

from scipy import optimize
optimize.brentq(lambda x:cos(x)-sin(x),0,pi)

This is still faster than the second option above.

There is also the problem that for functions that are not defined in
Sage, even the find_root method requires a Python function to work.
For example, if we want the first positive zero of the Bessel function
of order 1, the following does not work:

from scipy import special
find_root(special.jn(1r,x),3,4)

One must use:
find_root(lambda x: special.jn(1r,x),3,4)

or

optimize.brentq(lambda x: special.jn(1r,x),3,4)

(This is just an example, scipy.special has routines for zeros of
Bessel functions.)

So, I would suggest that examples in this vein are included in the
documentation. I would try to do that, if people think it is a good
idea.

I published a worksheet with my experiments in sagenb.org:
http://sagenb.org:8000/home/pub/171/
--
"The main things which seem to me important on their own account, and
not merely as means to other things, are knowledge, art, instinctive
happiness, and relations of friendship or affection."
-Bertrand Russell

L. Felipe Martins
Department of Mathematics
Cleveland State University
luizfelip...@gmail.com

David Joyner

未读,
2009年1月24日 13:11:372009/1/24
收件人 sage-...@googlegroups.com
I vote +1.

Robert Bradshaw

未读,
2009年1月24日 16:57:252009/1/24
收件人 sage-...@googlegroups.com
On Jan 24, 2009, at 8:46 AM, Luiz Felipe Martins wrote:

> 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

Robert Bradshaw

未读,
2009年1月24日 17:01:392009/1/24
收件人 sage-...@googlegroups.com

William Stein

未读,
2009年1月24日 18:01:402009/1/24
收件人 sage-...@googlegroups.com

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

Robert Bradshaw

未读,
2009年1月24日 18:17:392009/1/24
收件人 sage-...@googlegroups.com

Good point. It's still curious that fast_float takes so long to call...

- Robert


回复全部
回复作者
转发
0 个新帖子