U
    L?h                  
   @   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   J/var/www/html/venv/lib/python3.8/site-packages/sympy/codegen/algorithms.py<lambda>       r!   )rtoldebugitermaxcounterdelta_fncse
handle_nanboundsc                C   s  |dkrt  }t}d}ndd }|j}|| |}|	rtddlm}	 |	| g\}\}dd |D }|t||g7 }nt||g}|
dk	r|tt|t	|
t
g7 }|t||g7 }|dk	r|t|tt||d |d	 g7 }|rt||gd
|j|}||g7 }tt|||t|  }tt|ttdg}|dk	r|pLt 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   r"   z newtons_method.<locals>.<lambda>r   )r(   c                 S   s   g | ]\}}t ||qS r   )r   ).0ZdumZ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   nameZsympy.simplify.cse_mainr(   factorr   r   r   r   r   r   r	   r
   r   formatr   r   r   r   r   r   Zdeducedappendr   r   )exprwrtZatolr+   r#   r$   r%   r&   r'   r(   r)   r*   ZWrapperZname_dZ
delta_exprZcsesredZwhl_bdyZprntreqdeclarsZ	v_counterZwhlZblckr   r   r    newtons_method   sF    ;
$


r;   c                 C   s(   t | tr| jj} nt | tr$| j} | S r   )
isinstancer   variablesymbolr   )argr   r   r    
_symbol_ofs   s
    


r@   Znewton)r+   c                K   s   |dkr|f}dd |D }|dkr@t d|j }| |r@d}t| |fd|i||}t|trl|j}| j	dd |D }	|	rt
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))r<   r   r>   r   r2   r,   pr   r   r    
<dictcomp>   s    
 z+newtons_method_function.<locals>.<dictcomp>Zd_r+   c                 S   s   h | ]}t |qS r   )r@   rA   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   rA   r   r   r    	<genexpr>   s     z*newtons_method_function.<locals>.<genexpr>)attrs)r   r2   hasr;   Zxreplacer<   r   bodyZfree_symbols
difference
ValueErrorjoinmapstrtupler   r   r   r   )r6   r7   params	func_namerF   r+   kwargsZpointer_subsalgoZnot_in_paramsr:   rH   r   r   r    newtons_method_function{   s$    )

rS   )r   N)$Zsympy.core.containersr   Zsympy.core.numbersr   Zsympy.core.relationalr   r   Zsympy.core.symbolr   r   Z$sympy.functions.elementary.complexesr   Z(sympy.functions.elementary.miscellaneousr	   r
   Zsympy.logic.boolalgr   Zsympy.codegen.astr   r   r   r   r   r   r   r   r   r   r   r   r   Zsympy.codegen.cfunctionsr   r;   r@   rS   r   r   r   r    <module>   s$   <   c