
    Ed                         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d	S )    )Tuple)oo)GtLt)DummySymbol)Abs)And)
AssignmentAddAugmentedAssignment	CodeBlockDeclarationFunctionDefinitionPrintReturnScopeWhileVariablePointerreal-q=NFc                 D   |t                      }t          }d}n
d }|j        }|  |                     |          z  }	t	          ||	          t          ||          g}
|r@t          ||gd                    |j        |                    }|
d         |g|
dd         z   }
t          t          |          |          }t          t          |t          t                              g}||pt          d	          }t          j        |d          }|                    t          |                     |
                    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    8lib/python3.11/site-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_methodr:      s   \  +tyy~~%J%,,.DS%.P.PQG 3c5\#;#B#B38V#T#TUU1:t$wqrr{2
SZZ

C8EB???@@AG -0U4000$Wa00	{9--...-gq99:::#r'7++,,
Y(
)
)CcU?D79d#$$$r    c                     t          | t                    r| j        j        } nt          | t                    r| j        } | S r   )
isinstancer   variablesymbolr   )args    r   
_symbol_ofr@   V   s>    #{## l!	C	"	" jJr    newton)r   c          	      \   ||f}d |D             }|.t          d|j        z             }|                     |          rd}t          | |fd|i|                    |          }t          |t                    r|j        }| j        	                    d |D                       }	|	r8t          dd                    t          t          |	                    z            t          d |D                       }
t          |t!          |                    }t#          t$          ||
||	          S )
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

    Nc                 |    i | ]9}t          |t                    |j        t          d |j        j        z            :S )z(*%s))r<   r   r>   r   r%   .0ps     r   
<dictcomp>z+newtons_method_function.<locals>.<dictcomp>   sK     ? ? ?z!W'='=?AHfWqx}%<== ? ? ?r    d_r   c                 ,    h | ]}t          |          S r   )r@   rD   s     r   	<setcomp>z*newtons_method_function.<locals>.<setcomp>   s    1P1P1PA*Q--1P1P1Pr    zMissing symbols in params: %sz, c              3   @   K   | ]}t          |t                    V  d S r   )r   r   rD   s     r   	<genexpr>z*newtons_method_function.<locals>.<genexpr>   s,      66!HQ%%666666r    )attrs)r   r%   hasr:   xreplacer<   r   bodyfree_symbols
difference
ValueErrorjoinmapstrtupler   r   r   r   )r*   r+   params	func_namerM   r   kwargspointer_subsalgonot_in_paramsr6   rP   s               r   newtons_method_functionr^   ^   sI   R  ? ?#? ? ?L tch''88E?? 	E$;;5;F;;DD\RRD$ y%001P1P1P1P1PQQM _8499SmE\E\;];]]^^^66v66666GT6#;;''DdIwEJJJJr    )r   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   r:   r@   r^   r   r    r   <module>rf      s   ' ' ' ' ' ' ! ! ! ! ! ! * * * * * * * * - - - - - - - - 4 4 4 4 4 4 # # # # # #                           
 V<A)-E% E% E% E%P   /3heegg 9K`d 9K 9K 9K 9K 9K 9K 9Kr    