
    \h              
           S SK Jr  S SKJr  S SKJrJr  S SKJrJ	r	  S SK
Jr  S SKJrJr  S SKJr  S SKJrJrJrJrJrJrJrJrJrJrJrJrJr  S S	KJ r    SSSS
S
S SS
S
S.S jjr!S r"S
S\" 5       4S
S.S jjr#g
)    )Tuple)oo)GtLt)DummySymbol)Abs)MinMax)And)
AssignmentAddAugmentedAssignmentbreak_	CodeBlockDeclarationFunctionDefinitionPrintReturnScopeWhileVariablePointerreal)isnanNgؗҼ<Fc                 ,    U * U R                  U5      -  $ N)diff)exs     P/var/www/auris/envauris/lib/python3.13/site-packages/sympy/codegen/algorithms.py<lambda>r!      s    aRq	\    )rtoldebugitermaxcounterdelta_fncse
handle_nanboundsc                   Uc  [        5       n[        nSnOS nUR                  nU" X5      nU	(       aT  SSKJn	  U	" UR                  5       /5      u  nu  nU VVs/ s H  u  nn[        UU5      PM     nnnU[        UU5      /-  nO[        X>5      /nU
b'  U[        [        U5      [        U
[        5      5      /-  nU[        X5      /-  nUb)  U[        U[        [        XS   5      US   5      5      /-  nU(       a-  [        X/SR                  UR                  U5      5      nUU/-  n[!        [#        U5      X$[#        U5      -  -   5      n[%        ['        U[(        [*        S95      /nUbs  U=(       d	    [        SS	9n[&        R,                  " US5      nUR/                  [%        U5      5        UR/                  [        US5      5        [1        U[3        Xv5      5      n[        U[        U6 5      nUnU(       a5  UR/                  [        U/S
R                  UR                  5      5      5        UU/-  nU" [        U6 5      $ s  snnf )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

deltac                     U $ r    )r   s    r    r!    newtons_method.<locals>.<lambda>P   s    Ar"   r   )r(      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dumsub_ewhl_bdyprntreqdeclars	v_counterwhlblcks                             r    newtons_methodrK      s   v }$$J
/J--/01fs<@ADjc5:c5)DAJuc*++e01E%,	*f(EFGG&s233GJsCC(;VAY$GHIIc\#;#B#B388V#TUD6
SZSX-
.C8EB?@AG0U40$$Wa0	{9-.-gq9:#r'+,
Y(
)CDE3%!6!6sxx!@ABSEMD9d#$$3 Bs   Ic                     [        U [        5      (       a  U R                  R                  n U $ [        U [        5      (       a  U R                  n U $ r   )
isinstancer   variablesymbolr   )args    r    
_symbol_ofrQ   s   sB    #{##ll!! J 
C	"	"jjJr"   newton)r,   c          	         Uc  U4nU Vs0 s HG  n[        U[        5      (       d  M  UR                  [        SUR                  R                  -  5      _MI     nnUc0  [        SUR                  -   5      nU R                  U5      (       a  Sn[        X4SU0UD6R                  U5      n	[        U	[        5      (       a  U	R                  n	U R                  R                  U Vs1 s H  n[        U5      iM     sn5      n
U
(       a+  [        SSR                  [        [         U
5      5      -  5      e[#        S U 5       5      n[%        U	['        U5      5      n[)        [*        X;XS9$ s  snf s  snf )	aY  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   B   #    U  H  n[        U[        5      v   M     g 7fr   )r   r   ).0ps     r    	<genexpr>*newtons_method_function.<locals>.<genexpr>   s     6v!HQ%%vs   )attrs)rM   r   rO   r   r4   hasrK   xreplacer   bodyfree_symbols
differencerQ   
ValueErrorjoinmapstrtupler   r   r   r   )r:   r;   params	func_namerZ   r,   kwargsrW   pointer_subsalgonot_in_paramsrG   r]   s                r    newtons_method_functionrk   {   s<   R ~#?#z!W'= >AHHfWqxx}}%<==#  ?}tchh'88E??E$;5;F;DD\RD$yy%%001PA*Q-1PQM8499SmE\;]]^^6v66GT6#;'DdIJJ? 2Qs   E)1E)%E.)g-q=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   rK   rQ   rk   r.   r"   r    <module>ru      sv    ' ! * - 4 = #    + U`%e58Q`%F /3heg 9K`d 9Kr"   