ó
¡¼™\c           @` sâ   d  d l  m Z m Z m Z d  d l m Z m Z m Z m Z m	 Z	 m
 Z
 m Z m Z d  d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d d	 e d	 d	 d „ Z d „  Z d	 d e ƒ  d „ Z d	 S(
   i    (   t   absolute_importt   divisiont   print_function(   t   Andt   Gtt   Ltt   Abst   Dummyt   oot   Tuplet   Symbol(   t
   Assignmentt   AddAugmentedAssignmentt	   CodeBlockt   Declarationt   FunctionDefinitiont   Printt   Returnt   Scopet   Whilet   Variablet   Pointert   realgê-™—q=c         C` sŒ  | d	 k r$ t ƒ  } t } d } n d „  } | j } |  |  j | ƒ }	 t | |	 ƒ t | | ƒ g }
 | r­ t | | g d j | j | ƒ ƒ } |
 d | g |
 d }
 n  t	 t
 | ƒ | ƒ } t t | d t d t ƒƒ g } | d	 k	 rZ| pt d t ƒ } t j | d ƒ } | j t | ƒ ƒ |
 j t | d ƒ ƒ t | t | | ƒ ƒ } n  t | t |
 Œ  ƒ } | | g } | t | Œ  ƒ S(
   sX   Generates an AST for Newton-Raphson method (a root-finding algorithm).

    Returns an abstract syntax tree (AST) based on ``sympy.codegen.ast`` for Netwon's
    method of root-finding.

    Parameters
    ==========

    expr : expression
    wrt : Symbol
        With respect to, i.e. what is the variable.
    atol : number or expr
        Absolute tolerance (stopping criterion)
    delta : Symbol
        Will be a ``Dummy`` if ``None``.
    debug : bool
        Whether to print convergence information during iterations
    itermax : number or expr
        Maximum number of iterations.
    counter : Symbol
        Will be a ``Dummy`` if ``None``.

    Examples
    ========

    >>> from sympy import symbols, cos
    >>> from sympy.codegen.ast import Assignment
    >>> from sympy.codegen.algorithms import newtons_method
    >>> x, dx, atol = symbols('x dx atol')
    >>> expr = cos(x) - x**3
    >>> algo = newtons_method(expr, x, atol, dx)
    >>> algo.has(Assignment(dx, -expr/expr.diff(x)))
    True

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Newton%27s_method

    t   deltac         S` s   |  S(   N(    (   t   x(    (    s7   lib/python2.7/site-packages/sympy/codegen/algorithms.pyt   <lambda>;   t    s   {0}=%12.5g {1}=%12.5g\ni    i   t   typet   valuet   integerN(   t   NoneR   R   t   namet   diffR   R   R   t   formatR   R   R   R   R   R   t   Truet   deducedt   appendR   R   R   R   (   t   exprt   wrtt   atolR   t   debugt   itermaxt   countert   Wrappert   name_dt
   delta_exprt   whl_bdyt   prntt   reqt   declarst	   v_countert   whlt   blck(    (    s7   lib/python2.7/site-packages/sympy/codegen/algorithms.pyt   newtons_method   s,    +				$!c         C` s=   t  |  t ƒ r |  j j }  n t  |  t ƒ r9 |  j }  n  |  S(   N(   t
   isinstanceR   t   variablet   symbolR   (   t   arg(    (    s7   lib/python2.7/site-packages/sympy/codegen/algorithms.pyt
   _symbol_ofP   s
    t   newtonc         K` sA  | d	 k r | f } n  d „  | Dƒ } | j d d	 ƒ } | d	 k rt t d | j ƒ } |  j | ƒ rt d	 } qt n  t |  | d | | j | ƒ } t | t ƒ r° | j	 } n  |  j
 j t d „  | Dƒ ƒ ƒ }	 |	 rı t d d j t t |	 ƒ ƒ ƒ ‚ n  t d „  | Dƒ ƒ }
 t | t | ƒ ƒ } t t | |
 | d | ƒS(
   s   Generates an AST for a function implementing the Newton-Raphson method.

    Parameters
    ==========

    expr : expression
    wrt : Symbol
        With respect to, i.e. what is the variable
    params : iterable of symbols
        Symbols appearing in expr that are taken as constants during the iterations
        (these will be accepted as parameters to the generated function).
    func_name : str
        Name of the generated function.
    attrs : Tuple
        Attribute instances passed as ``attrs`` to ``FunctionDefinition``.
    \*\*kwargs :
        Keyword arguments passed to :func:`sympy.codegen.algorithms.newtons_method`.

    Examples
    ========

    >>> from sympy import symbols, cos
    >>> from sympy.codegen.algorithms import newtons_method_function
    >>> from sympy.codegen.pyutils import render_as_module
    >>> from sympy.core.compatibility import exec_
    >>> x = symbols('x')
    >>> expr = cos(x) - x**3
    >>> func = newtons_method_function(expr, x)
    >>> py_mod = render_as_module(func)  # source code as string
    >>> namespace = {}
    >>> exec_(py_mod, namespace, namespace)
    >>> res = eval('newton(0.5)', namespace)
    >>> abs(res - 0.865474033102) < 1e-12
    True

    See Also
    ========

    sympy.codegen.ast.newtons_method

    c         S` s;   i  |  ]1 } t  | t ƒ r t d  | j j ƒ | j “ q S(   s   (*%s)(   R6   R   R
   R8   R   (   t   .0t   p(    (    s7   lib/python2.7/site-packages/sympy/codegen/algorithms.pys
   <dictcomp>„   s   	R   t   d_c         s` s   |  ] } t  | ƒ Vq d  S(   N(   R:   (   R<   R=   (    (    s7   lib/python2.7/site-packages/sympy/codegen/algorithms.pys	   <genexpr>   s    s   Missing symbols in params: %ss   , c         s` s   |  ] } t  | t ƒ Vq d  S(   N(   R   R   (   R<   R=   (    (    s7   lib/python2.7/site-packages/sympy/codegen/algorithms.pys	   <genexpr>‘   s    t   attrsN(   R   t   popR
   R   t   hasR5   t   xreplaceR6   R   t   bodyt   free_symbolst
   differencet   sett
   ValueErrort   joint   mapt   strt   tupleR   R   R   R   (   R%   R&   t   paramst	   func_nameR?   t   kwargst   pointer_subsR   t   algot   not_in_paramsR1   RC   (    (    s7   lib/python2.7/site-packages/sympy/codegen/algorithms.pyt   newtons_method_functionX   s$    *
!"%N(   t
   __future__R    R   R   t   sympyR   R   R   R   R   R   R	   R
   t   sympy.codegen.astR   R   R   R   R   R   R   R   R   R   R   R   R   t   FalseR5   R:   RR   (    (    (    s7   lib/python2.7/site-packages/sympy/codegen/algorithms.pyt   <module>   s   :R	D	