
    g              
           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Z!d Z"dd e            f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                 4    |  |                      |          z  S N)diff)exs     T/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/codegen/algorithms.py<lambda>r"      s    aRq		\     )rtoldebugitermaxcounterdelta_fncse
handle_nanboundsc                <   |t                      }t          }d}n
d }|j        } || |          }|	rJddlm}	  |	|                                g          \  }\  }d |D             }|t          ||          gz  }nt          ||          g}|
4|t          t          |          t          |
t                              gz  }|t          ||          gz  }|<|t          |t          t          ||d                   |d                             gz  }|r1t          ||gd                    |j        |                    }||gz  }t!          t#          |          ||t#          |          z  z             }t%          t'          |t(          t*          	                    g}||pt          d
          }t'          j        |d          }|                    t%          |                     |                    t          |d                     t1          |t3          ||                    }t          |t          |           }|}|r<|                    t          |gd                    |j                                       ||gz  } |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 r    )r    s    r!   r"   z newtons_method.<locals>.<lambda>P   s    A r#   r   )r)   c                 4    g | ]\  }}t          ||          S r/   )r   ).0dumsub_es      r!   
<listcomp>z"newtons_method.<locals>.<listcomp>W   s&    AAAjc5:c5))AAAr#      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blcks                           r!   newtons_methodrN      s   v }+$$$J
 2//////sJ--//011fsAADAAAJuc**++eZ001E%,,	*f(E(EFFGG&sE2233GJsCC(;(;VAY$G$GHHII c5\#;#B#B38V#T#TUUD6
SZZSXX-
.
.C8EB???@@AG0U4000$Wa00	{9--...-gq99:::#r'7++,,
Y(
)
)CD CE3%!6!6sx!@!@AABBBSEMD79d#$$$r#   c                     t          | t                    r| j        j        } nt          | t                    r| j        } | S r   )
isinstancer   variablesymbolr   )args    r!   
_symbol_ofrT   s   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))rP   r   rR   r   r9   r1   ps     r!   
<dictcomp>z+newtons_method_function.<locals>.<dictcomp>   sK     ? ? ?z!W'='=?AHfWqx}%<== ? ? ?r#   d_r-   c                 ,    h | ]}t          |          S r/   )rT   rX   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   rX   s     r!   	<genexpr>z*newtons_method_function.<locals>.<genexpr>   s,      66!HQ%%666666r#   )attrs)r   r9   hasrN   xreplacerP   r   bodyfree_symbols
difference
ValueErrorjoinmapstrtupler   r   r   r   )r?   r@   params	func_namer`   r-   kwargspointer_subsalgonot_in_paramsrJ   rc   s               r!   newtons_method_functionrq   {   sC   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   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   rN   rT   rq   r/   r#   r!   <module>r{      s   ' ' ' ' ' ' ! ! ! ! ! ! * * * * * * * * - - - - - - - - 4 4 4 4 4 4 = = = = = = = = # # # # # #                              + * * * * * U`%e58Q8Q`% `% `% `% `%F   /3heegg 9K`d 9K 9K 9K 9K 9K 9K 9Kr#   