o
    ¦ƒðjï  ã                
   @   sð   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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mZ d d	lm Z  	 ddddddd„ ddddœdd„Z!dd„ Z"ddeƒ fddœdd„Z#dS )é    )ÚTuple)Úoo)ÚGtÚLt)ÚDummyÚSymbol)ÚAbs)ÚMinÚMax)ÚAnd)Ú
AssignmentÚAddAugmentedAssignmentÚbreak_Ú	CodeBlockÚDeclarationÚFunctionDefinitionÚPrintÚReturnÚScopeÚWhileÚVariableÚPointerÚreal)Úisnançê-�™—q=Ng¼‰Ø—²Ò¼<Fc                 C   s   |  |   |¡ S ©N)Údiff)ÚeÚx© r   úš/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/codegen/algorithms.pyÚ<lambda>   s    r!   )ÚrtolÚdebugÚitermaxÚcounterÚdelta_fnÚcseÚ
handle_nanÚboundsc                C   sÈ  |du rt ƒ }t}d}ndd„ }|j}|| |ƒ}|	r:ddlm}	 |	| ¡ gƒ\}\}dd„ |D ƒ}|t||ƒg7 }nt||ƒg}|
durQ|tt|ƒt	|
t
ƒƒg7 }|t||ƒg7 }|duro|t|tt||d ƒ|d	 ƒƒg7 }|r‚t||gd
 |j|¡ƒ}||g7 }tt|ƒ||t|ƒ  ƒ}tt|ttd�ƒg}|durÁ|p£t dd�}t |d¡}| t|ƒ¡ | t|d	ƒ¡ t|t||ƒƒ}t|t	|Ž ƒ}|}|rÙ| t|gd |j¡ƒ¡ ||g7 }|t	|Ž ƒS )až   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 expression
        Absolute tolerance (stopping criterion)
    rtol : number or expression
        Relative 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``.
    delta_fn: Callable[[Expr, Symbol], Expr]
        computes the step, default is newtons method. For e.g. Halley's method
        use delta_fn=lambda e, x: -2*e*e.diff(x)/(2*e.diff(x)**2 - e*e.diff(x, 2))
    cse: bool
        Perform common sub-expression elimination on delta expression
    handle_nan: Token
        How to handle occurrence of not-a-number (NaN).
    bounds: Optional[tuple[Expr, Expr]]
        Perform optimization within bounds

    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=atol, delta=dx)
    >>> algo.has(Assignment(dx, -expr/expr.diff(x)))
    True

    References
    ==========

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

    NÚdeltac                 S   s   | S r   r   )r   r   r   r    r!   P   s    z newtons_method.<locals>.<lambda>r   )r'   c                 S   s   g | ]	\}}t ||ƒ‘qS r   )r   )Ú.0ÚdumÚsub_er   r   r    Ú
<listcomp>W   s    z"newtons_method.<locals>.<listcomp>é   z{}=%12.5g {}=%12.5g\n)ÚtypeÚvalueT)Úintegerz{}=%12.5g\n)r   r   ÚnameÚsympy.simplify.cse_mainr'   Úfactorr   r   r   r   r   r   r	   r
   r   Úformatr   r   r   r   r   r   ÚdeducedÚappendr   r   )ÚexprÚwrtÚatolr*   r"   r#   r$   r%   r&   r'   r(   r)   ÚWrapperÚname_dÚ
delta_exprÚcsesÚredÚwhl_bdyÚprntÚreqÚdeclarsÚ	v_counterÚwhlÚblckr   r   r    Únewtons_method   sF   ;
$

rH   c                 C   s*   t | tƒr| jj} | S t | tƒr| j} | S r   )Ú
isinstancer   ÚvariableÚsymbolr   )Úargr   r   r    Ú
_symbol_ofs   s   

þrM   Únewton)r*   c                K   sÐ   |du r|f}dd„ |D ƒ}|du r t d|j ƒ}|  |¡r d}t| |fd|i|¤Ž |¡}t|tƒr6|j}| j 	dd„ |D ƒ¡}	|	rOt
dd	 tt|	ƒ¡ ƒ‚td
d„ |D ƒƒ}
t|t|ƒƒ}tt||
||d�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                 S   s*   i | ]}t |tƒr|jtd |jj ƒ“qS )z(*%s))rI   r   rK   r   r3   ©r+   Úpr   r   r    Ú
<dictcomp>¦   s    
ÿz+newtons_method_function.<locals>.<dictcomp>Úd_r*   c                 S   s   h | ]}t |ƒ’qS r   )rM   rO   r   r   r    Ú	<setcomp>¯   s    z*newtons_method_function.<locals>.<setcomp>zMissing symbols in params: %sz, c                 s   s   � | ]}t |tƒV  qd S r   )r   r   rO   r   r   r    Ú	<genexpr>²   s   € z*newtons_method_function.<locals>.<genexpr>)Úattrs)r   r3   ÚhasrH   ÚxreplacerI   r   ÚbodyÚfree_symbolsÚ
differenceÚ
ValueErrorÚjoinÚmapÚstrÚtupler   r   r   r   )r9   r:   ÚparamsÚ	func_namerU   r*   ÚkwargsÚpointer_subsÚalgoÚnot_in_paramsrD   rX   r   r   r    Únewtons_method_function{   s$   )ÿ

rf   )r   N)$Úsympy.core.containersr   Úsympy.core.numbersr   Úsympy.core.relationalr   r   Úsympy.core.symbolr   r   Ú$sympy.functions.elementary.complexesr   Ú(sympy.functions.elementary.miscellaneousr	   r
   Úsympy.logic.boolalgr   Úsympy.codegen.astr   r   r   r   r   r   r   r   r   r   r   r   r   Úsympy.codegen.cfunctionsr   rH   rM   rf   r   r   r   r    Ú<module>   s"    <
ýc