U
    -e#                     @   s   d dl mZ d dlmZ d dlmZ ddlmZ dd Zdd	d
ZG dd dZ	ddi fddZ
dd ZdddZdddZdS )   )Basic)orderedsympify    )iterablec              	   c   s@   | g}|D ]0}|V  z| |j W q
 tk
r8   Y q
X q
dS )a  Yield the args of a Basic object in a breadth-first traversal.
    Depth-traversal stops if `arg.args` is either empty or is not
    an iterable.

    Examples
    ========

    >>> from sympy import Integral, Function
    >>> from sympy.abc import x
    >>> f = Function('f')
    >>> from sympy.core.traversal import iterargs
    >>> list(iterargs(Integral(f(x), (f(x), 1))))
    [Integral(f(x), (f(x), 1)), f(x), (f(x), 1), x, f(x), 1, x]

    See Also
    ========
    iterfreeargs, preorder_traversal
    N)extendargs	TypeError)exprr	   i r   U/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/sympy/core/traversal.pyiterargs   s    r   Tc              	   c   s~   | g}|D ]n}|V  |rRt |drR|j }t| ddD ]}|j| s<|V  q<z||j W q
 tk
rv   Y q
X q
dS )a4  Yield the args of a Basic object in a breadth-first traversal.
    Depth-traversal stops if `arg.args` is either empty or is not
    an iterable. The bound objects of an expression will be returned
    as canonical variables.

    Examples
    ========

    >>> from sympy import Integral, Function
    >>> from sympy.abc import x
    >>> f = Function('f')
    >>> from sympy.core.traversal import iterfreeargs
    >>> list(iterfreeargs(Integral(f(x), (f(x), 1))))
    [Integral(f(x), (f(x), 1)), 1]

    See Also
    ========
    iterargs, preorder_traversal
    Zbound_symbolsF)_firstN)	hasattrZcanonical_variablesvaluesiterfreeargsZas_dummyhasr   r	   r
   )r   r   r	   r   voidr   r   r   r   $   s    

r   c                   @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )preorder_traversala  
    Do a pre-order traversal of a tree.

    This iterator recursively yields nodes that it has visited in a pre-order
    fashion. That is, it yields the current node then descends through the
    tree breadth-first to yield all of a node's children's pre-order
    traversal.


    For an expression, the order of the traversal depends on the order of
    .args, which in many cases can be arbitrary.

    Parameters
    ==========
    node : SymPy expression
        The expression to traverse.
    keys : (default None) sort key(s)
        The key(s) used to sort args of Basic objects. When None, args of Basic
        objects are processed in arbitrary order. If key is defined, it will
        be passed along to ordered() as the only key(s) to use to sort the
        arguments; if ``key`` is simply True then the default keys of ordered
        will be used.

    Yields
    ======
    subtree : SymPy expression
        All of the subtrees in the tree.

    Examples
    ========

    >>> from sympy import preorder_traversal, symbols
    >>> x, y, z = symbols('x y z')

    The nodes are returned in the order that they are encountered unless key
    is given; simply passing key=True will guarantee that the traversal is
    unique.

    >>> list(preorder_traversal((x + y)*z, keys=None)) # doctest: +SKIP
    [z*(x + y), z, x + y, y, x]
    >>> list(preorder_traversal((x + y)*z, keys=True))
    [z*(x + y), z, x + y, x, y]

    Nc                 C   s   d| _ | ||| _d S )NF)
_skip_flag_preorder_traversal_pt)selfnodekeysr   r   r   __init__s   s    zpreorder_traversal.__init__c                 c   s   |V  | j rd| _ d S t|tr~|s6t|dr6|j}n|j}|r`|dkrXt||dd}nt|}|D ]}| ||E d H  qdn$t|r|D ]}| ||E d H  qd S )NF_argsetTdefault)	r   
isinstancer   r   r   r	   r   r   r   )r   r   r   r	   argitemr   r   r   r   w   s"    
z&preorder_traversal._preorder_traversalc                 C   s
   d| _ dS )a  
        Skip yielding current node's (last yielded node's) subtrees.

        Examples
        ========

        >>> from sympy import preorder_traversal, symbols
        >>> x, y, z = symbols('x y z')
        >>> pt = preorder_traversal((x + y*z)*z)
        >>> for i in pt:
        ...     print(i)
        ...     if i == x + y*z:
        ...             pt.skip()
        z*(x + y*z)
        z
        x + y*z
        TN)r   r   r   r   r   skip   s    zpreorder_traversal.skipc                 C   s
   t | jS N)nextr   r$   r   r   r   __next__   s    zpreorder_traversal.__next__c                 C   s   | S r&   r   r$   r   r   r   __iter__   s    zpreorder_traversal.__iter__)N)	__name__
__module____qualname____doc__r   r   r%   r(   r)   r   r   r   r   r   F   s   ,
r   r   c                    s     fdd  t | |S )a8  
    Use ``func`` to transform ``expr`` at the given level.

    Examples
    ========

    >>> from sympy import use, expand
    >>> from sympy.abc import x, y

    >>> f = (x + y)**2*x + 1

    >>> use(f, expand, level=2)
    x*(x**2 + 2*x*y + y**2) + 1
    >>> expand(f)
    x**3 + 2*x**2*y + x*y**2 + 1

    c                    sJ    s| fS | j r| S  d8   fdd| jD }| j| S d S )Nr   c                    s   g | ]} |qS r   r   ).0r"   )_uselevelr   r   
<listcomp>   s     z%use.<locals>._use.<locals>.<listcomp>)Zis_Atomr	   	__class__)r   r0   _argsr/   r	   funckwargs)r0   r   r/      s    zuse.<locals>._user   )r   r5   r0   r	   r6   r   r4   r   use   s    r7   c                 g   s4   t | |r0| V  | jD ]}t|f| E dH  qdS )aA  Iterate through the args that are the given types (target) and
    return a list of the args that were traversed; arguments
    that are not of the specified types are not traversed.

    Examples
    ========

    >>> from sympy.core.traversal import walk
    >>> from sympy import Min, Max
    >>> from sympy.abc import x, y, z
    >>> list(walk(Min(x, Max(y, Min(1, z))), Min))
    [Min(x, Max(y, Min(1, z)))]
    >>> list(walk(Min(x, Max(y, Min(1, z))), Min, Max))
    [Min(x, Max(y, Min(1, z))), Max(y, Min(1, z)), Min(1, z)]

    See Also
    ========

    bottom_up
    N)r!   r	   walk)etargetr   r   r   r   r8      s    

r8   Fc                    s   t | dd}|dk	r^|rPt fdd|D }|| jkrF| j| }  | } qr | } n&rz | } W n tk
r   Y nX | S )zApply ``F`` to all expressions in an expression tree from the
    bottom up. If ``atoms`` is True, apply ``F`` even if there are no args;
    if ``nonbasic`` is True, try to apply ``F`` to non-Basic objects.
    r	   Nc                    s   g | ]}t | qS r   )	bottom_up)r.   aFatomsnonbasicr   r   r1      s     zbottom_up.<locals>.<listcomp>)getattrtupler	   r5   r
   )rvr>   r?   r@   r	   r   r=   r   r;      s    



r;   Nc                 c   s|   t | trP| j}|r4|dkr,t||dd}nt|}|D ]}t||E dH  q8n"t| rr| D ]}t||E dH  q\| V  dS )am  
    Do a postorder traversal of a tree.

    This generator recursively yields nodes that it has visited in a postorder
    fashion. That is, it descends through the tree depth-first to yield all of
    a node's children's postorder traversal before yielding the node itself.

    Parameters
    ==========

    node : SymPy expression
        The expression to traverse.
    keys : (default None) sort key(s)
        The key(s) used to sort args of Basic objects. When None, args of Basic
        objects are processed in arbitrary order. If key is defined, it will
        be passed along to ordered() as the only key(s) to use to sort the
        arguments; if ``key`` is simply True then the default keys of
        ``ordered`` will be used (node count and default_sort_key).

    Yields
    ======
    subtree : SymPy expression
        All of the subtrees in the tree.

    Examples
    ========

    >>> from sympy import postorder_traversal
    >>> from sympy.abc import w, x, y, z

    The nodes are returned in the order that they are encountered unless key
    is given; simply passing key=True will guarantee that the traversal is
    unique.

    >>> list(postorder_traversal(w + (x + y)*z)) # doctest: +SKIP
    [z, y, x, x + y, z*(x + y), w, w + z*(x + y)]
    >>> list(postorder_traversal(w + (x + y)*z, keys=True))
    [w, z, x, y, x + y, z*(x + y), w + z*(x + y)]


    TFr   N)r!   r   r	   r   postorder_traversalr   )r   r   r	   r"   r#   r   r   r   rD      s    *
rD   )T)FF)N)basicr   Zsortingr   r   Zsympy.utilities.iterablesr   r   r   r   r7   r8   r;   rD   r   r   r   r   <module>   s   
"c 
