
    MZd                         d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	 d dl
mZ d dl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	Zd
 Zdd e       fdddZy)    )Tuple)oo)GtLt)DummySymbol)Abs)And)
AssignmentAddAugmentedAssignment	CodeBlockDeclarationFunctionDefinitionPrintReturnScopeWhileVariablePointerrealNc                    |t               }t        }d}nd }|j                  }|  | j                  |      z  }	t	        ||	      t        ||      g}
|r5t        ||gdj                  |j                  |            }|
d   |g|
dd z   }
t        t        |      |      }t        t        |t        t                    g}|q|xs t        d	      }t        j                  |d      }|j                  t        |             |
j                  t        |d             t!        |t#        ||            }t%        |t'        |
       }||gz   } |t'        |       S )
ay   Generates an AST for Newton-Raphson method (a root-finding algorithm).

    Explanation
    ===========

    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

    Ndeltac                     | S N )xs    :/usr/lib/python3/dist-packages/sympy/codegen/algorithms.py<lambda>z newtons_method.<locals>.<lambda>A   s    A     z{}=%12.5g {}=%12.5g\nr      )typevalueT)integer)r   r   namediffr   r   r   formatr   r	   r   r   r   r   deducedappendr
   r   r   r   )exprwrtatolr   debugitermaxcounterWrappername_d
delta_exprwhl_bdyprntreqdeclars	v_counterwhlblcks                    r   newtons_methodr9      sE   \ }tyy~%J%,.DS%.PQGc5\#;#B#B388V#TU1:t$wqr{2
SZ
C8EB?@AG0U40$$Wa0	{9-.-gq9:#r'7+,
Y(
)CcU?D9d#$$r   c                     t        | t              r| j                  j                  } | S t        | t              r| j                  } | S r   )
isinstancer   variablesymbolr   )args    r   
_symbol_ofr?   V   s<    #{#ll!! J 
C	"jjJr   newton)r   c          	         ||f}|D ci c]?  }t        |t              r-|j                  t        d|j                  j                  z        A }}|+t        d|j                  z         }| j                  |      rd}t        | |fd|i|j                  |      }	t        |	t              r|	j                  }	| j                  j                  |D ch c]  }t        |       c}      }
|
r+t        ddj                  t        t         |
            z        t#        d |D              }t%        |	t'        |            }t)        t*        ||||      S c c}w c c}w )	a   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
    >>> 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.algorithms.newtons_method

    Nz(*%s)d_r   zMissing symbols in params: %sz, c              3   <   K   | ]  }t        |t                y wr   )r   r   ).0ps     r   	<genexpr>z*newtons_method_function.<locals>.<genexpr>   s     6!HQ%6s   )attrs)r;   r   r=   r   r$   hasr9   xreplacer   bodyfree_symbols
differencer?   
ValueErrorjoinmapstrtupler   r   r   r   )r)   r*   params	func_namerG   r   kwargsrE   pointer_subsalgonot_in_paramsr5   rJ   s                r   newtons_method_functionrX   ^   s4   R ~#?z!W'= HHfWqxx}}%<== ?L ?}tchh'88E?E$;5;F;DD\RD$yy%%001PA*Q-1PQM8499SmE\;]]^^6v66GT6#;'DdIwEJJ? 2Qs   AEE)g-q=NFNN)sympy.core.containersr   sympy.core.numbersr   sympy.core.relationalr   r   sympy.core.symbolr   r   $sympy.functions.elementary.complexesr	   sympy.logic.boolalgr
   sympy.codegen.astr   r   r   r   r   r   r   r   r   r   r   r   r9   r?   rX   r   r   r   <module>r`      s]    ' ! * - 4 #   
 V<A)-E%P /3heg 9K`d 9Kr   