Indeed if you do this:

mpmath.findroot(lambda xi: p.subs(x, Expr._from_mpmath(xi, mpmath.mp.prec)), 1.0)

[That is, you manually construct sympy objects which reflect the precision required by mpmath.]

On 27.05.2011 05:57, Mateusz Paprocki wrote:
Hi,

On 27 May 2011 06:31, Aaron S. Meurer <[email protected]
<mailto:[email protected]>> wrote:

    Well, it still fails there with the subs.  Like I said, it's a bug.
      Can you open an issue for it?


It's not necessarily a bug, because you expect subs() (sympy) and
findroot() (mpmath) to work together, but you are skipping the glue
(core/evalf.py or wherever this is). I'm not sure how exactly evalf code
in SymPy works, but I think that manipulation of mp.dps (or .prec)
doesn't affect evalf (the reason to think this way is that each instance
of mpf can have its own dps/prec setting). Poly.eval (also nroots())
work because it uses mpf directly, skipping the glue.


    Aaron Meurer

    On May 26, 2011, at 9:09 PM, Matthew Emmett wrote:

     > Thanks for the quick reply Aaron.  I will get the latest git version
     > and run with it.  BTW, I was running the sample on 0.6.7.
     >
     > Thanks again,
     > Matt
     >
     > On Thu, May 26, 2011 at 08:28:51PM -0600, Aaron S. Meurer wrote:
     >> If I try this with mpmath 0.16 (the version we currently have in
    sympy in the latest git master), I get:
     >>
     >> In [22]: print 'subs: ', mpmath.findroot(lambda xi: p.subs(x,
    xi), 1.0)
     >> subs:
      
---------------------------------------------------------------------------
     >> ValueError                                Traceback (most recent
    call last)
     >>
     >> /Users/aaronmeurer/Documents/python/sympy/sympy/<ipython
    console> in <module>()
     >>
     >>
    
/Users/aaronmeurer/Documents/python/sympy/sympy/sympy/mpmath/calculus/optimization.pyc
    in findroot(ctx, f, x0, solver, tol, verbose, verify, **kwargs)
     >>    968 '(%g > %g)\n'
     >>    969 'Try another starting point or tweak arguments.'
     >> --> 970                              % (norm(f(*xl))**2, tol))
     >>    971         return x
     >>    972     finally:
     >>
     >> ValueError: Could not find root within given tolerance.
    (6.67917e-33 > 1.71057e-49)
     >> Try another starting point or tweak arguments.
     >>
     >> Another option for Poly is eval.  I get this:
     >>
     >> In [37]: print 'eval: ', mpmath.findroot(lambda xi: p.eval(x,
    xi), 1.0)
     >> eval:   0.774596669241483377035853079956479922166584341
     >>
     >> In [38]: print 'exact: ',
    mpmath.sqrt(mpmath.mpf('3.0')/mpmath.mpf('5.0'))
     >> exact:  0.774596669241483377035853079956479922166584341
     >>
     >> In [39]: print 'lambda:', mpmath.findroot(lambda x: 5.0*x**3/2.0
    - 3.0*x/2.0, 1.0)
     >> lambda: 0.774596669241483377035853079956479922166584341
     >>
     >> So I'd say there is some kind of bug with subs.
     >>
     >> Aaron Meurer
     >>
     >> On May 26, 2011, at 8:21 PM, Matthew Emmett wrote:
     >>
     >>> Hi everyone,
     >>>
     >>> I am having trouble combining mpmath's findroot function with
    sympy.
     >>> Here is a short example of what I am trying to accomplish:
     >>>
     >>> import sympy
     >>> import mpmath
     >>>
     >>> mpmath.mp.dps = 45
     >>>
     >>> x = sympy.var('x')
     >>> p = (5.0/2.0*x**3 - 3.0/2.0*x).as_poly()
     >>>
     >>> print 'subs: ', mpmath.findroot(lambda xi: p.subs(x, xi), 1.0)
     >>> print 'lambda:', mpmath.findroot(lambda x: 5.0*x**3/2.0 -
    3.0*x/2.0,
     >>> 1.0)
     >>> print 'exact: ', mpmath.sqrt(mpmath.mpf('3.0')/mpmath.mpf('5.0'))
     >>>
     >>>
     >>> The output is:
     >>>
     >>> subs:   0.77459666924148342341315597798728447146706553
     >>> lambda: 0.774596669241483377035853079956479922166584341
     >>> exact:  0.774596669241483377035853079956479922166584341
     >>>
     >>>
     >>> That is, when using mpmath.findroot, substituting into a SymPy
     >>> polynomial does not produce the same result as using a lambda form
     >>> with a handwritten version of the polynomial.  Is there a way
    to the
     >>> SymPy subs method to work as expected?
     >>>
     >>> Thanks,
     >>> Matt
     >>>
     >>> --
     >>> You received this message because you are subscribed to the
    Google Groups "sympy" group.
     >>> To post to this group, send email to [email protected]
    <mailto:[email protected]>.
     >>> To unsubscribe from this group, send email to
    [email protected]
    <mailto:sympy%[email protected]>.
     >>> For more options, visit this group at
    http://groups.google.com/group/sympy?hl=en.
     >>>
     >>
     >> --
     >> You received this message because you are subscribed to the
    Google Groups "sympy" group.
     >> To post to this group, send email to [email protected]
    <mailto:[email protected]>.
     >> To unsubscribe from this group, send email to
    [email protected]
    <mailto:sympy%[email protected]>.
     >> For more options, visit this group at
    http://groups.google.com/group/sympy?hl=en.
     >>
     >
     > --
     > You received this message because you are subscribed to the
    Google Groups "sympy" group.
     > To post to this group, send email to [email protected]
    <mailto:[email protected]>.
     > To unsubscribe from this group, send email to
    [email protected]
    <mailto:sympy%[email protected]>.
     > For more options, visit this group at
    http://groups.google.com/group/sympy?hl=en.
     >

    --
    You received this message because you are subscribed to the Google
    Groups "sympy" group.
    To post to this group, send email to [email protected]
    <mailto:[email protected]>.
    To unsubscribe from this group, send email to
    [email protected]
    <mailto:sympy%[email protected]>.
    For more options, visit this group at
    http://groups.google.com/group/sympy?hl=en.


Mateusz

--
You received this message because you are subscribed to the Google
Groups "sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.

--
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to