
    Ed+                     ^    d Z ddlmZ ddlmZ efdZd ZefdZd Zd	 Z	d
 Z
efdZd ZdS )zP Generic Rules for SymPy

This file assumes knowledge of Basic and little else.
    )sift   )newc                       fd}|S )a   Create a rule to remove identities.

    isid - fn :: x -> Bool  --- whether or not this element is an identity.

    Examples
    ========

    >>> from sympy.strategies import rm_id
    >>> from sympy import Basic, S
    >>> remove_zeros = rm_id(lambda x: x==0)
    >>> remove_zeros(Basic(S(1), S(0), S(2)))
    Basic(1, 2)
    >>> remove_zeros(Basic(S(0), S(0))) # If only identites then we keep one
    Basic(0)

    See Also:
        unpack
    c                 @   t          t          | j                            }t          |          dk    r| S t          |          t	          |          k    r+ | j        gd t          | j        |          D             R  S  | j        | j        d                   S )z Remove identities r   c                     g | ]	\  }}||
S  r	   ).0argxs      3lib/python3.11/site-packages/sympy/strategies/rl.py
<listcomp>z/rm_id.<locals>.ident_remove.<locals>.<listcomp>$   s!    HHHaaHHHH    )listmapargssumlen	__class__zip)expridsisidr   s     r   ident_removezrm_id.<locals>.ident_remove   s    3tTY''((s88q= 	5KXXS! 	53t~ JHH3ty#+>+>HHHJ J J J 3t~ty|444r   r	   )r   r   r   s   `` r   rm_idr   
   s*    &	5 	5 	5 	5 	5 	5 r   c                       fd}|S )a6   Create a rule to conglomerate identical args.

    Examples
    ========

    >>> from sympy.strategies import glom
    >>> from sympy import Add
    >>> from sympy.abc import x

    >>> key     = lambda x: x.as_coeff_Mul()[1]
    >>> count   = lambda x: x.as_coeff_Mul()[0]
    >>> combine = lambda cnt, arg: cnt * arg
    >>> rl = glom(key, count, combine)

    >>> rl(Add(x, -x, 3*x, 2, 3, evaluate=False))
    3*x + 5

    Wait, how are key, count and combine supposed to work?

    >>> key(2*x)
    x
    >>> count(2*x)
    2
    >>> combine(2, x)
    2*x
    c                 0   t          | j                  }fd|                                D             }fd|                                D             }t          |          t          | j                  k    rt	          t          |           g|R  S | S )z2 Conglomerate together identical args x + x -> 2x c           	      R    i | ]#\  }}|t          t          |                    $S r	   )r   r   )r
   kr   counts      r   
<dictcomp>z.glom.<locals>.conglomerate.<locals>.<dictcomp>H   s1    IIIwq$!SUD))**IIIr   c                 .    g | ]\  }} ||          S r	   r	   )r
   matcntcombines      r   r   z.glom.<locals>.conglomerate.<locals>.<listcomp>I   s)    DDDc773$$DDDr   )r   r   itemssetr   type)r   groupscountsnewargsr%   r    keys       r   conglomeratezglom.<locals>.conglomerateE   s    di%%IIII&,,..IIIDDDDV\\^^DDDw<<3ty>>) 	tDzz,G,,,,Kr   r	   )r,   r    r%   r-   s   ``` r   glomr.   *   s0    6       r   c                       fd}|S )z Create a rule to sort by a key function.

    Examples
    ========

    >>> from sympy.strategies import sort
    >>> from sympy import Basic, S
    >>> sort_rl = sort(str)
    >>> sort_rl(Basic(S(3), S(1), S(2)))
    Basic(1, 2, 3)
    c                 H     | j         gt          | j                  R  S )N)r,   )r   sortedr   )r   r,   r   s    r   sort_rlzsort.<locals>.sort_rl^   s,    s4>?F49#$>$>$>????r   r	   )r,   r   r2   s   `` r   sortr3   Q   s-    @ @ @ @ @ @Nr   c                       fd}|S )aW   Turns an A containing Bs into a B of As

    where A, B are container types

    >>> from sympy.strategies import distribute
    >>> from sympy import Add, Mul, symbols
    >>> x, y = symbols('x,y')
    >>> dist = distribute(Mul, Add)
    >>> expr = Mul(2, x+y, evaluate=False)
    >>> expr
    2*(x + y)
    >>> dist(expr)
    2*x + 2*y
    c                     t          | j                  D ]^\  }}t          |          rI| j        d |         | j        |         | j        |dz   d          c} fd|j        D              c S _| S )Nr   c                 (    g | ]} |fz   z    S r	   r	   )r
   r   Afirsttails     r   r   z5distribute.<locals>.distribute_rl.<locals>.<listcomp>v   s+    III311uv~46IIIr   )	enumerater   
isinstance)r   ir   br8   r9   r7   Bs       @@r   distribute_rlz!distribute.<locals>.distribute_rlr   s    	** 	K 	KFAs#q!! K!%2A2	!di!oq$qIIIIII!&IIIJJJJK r   r	   )r7   r>   r?   s   `` r   
distributer@   b   s*           r   c                       fd}|S )z Replace expressions exactly c                     | k    rS | S )Nr	   )r   ar=   s    r   subs_rlzsubs.<locals>.subs_rl|   s    19 	HKr   r	   )rC   r=   rD   s   `` r   subsrE   z   s)         
 Nr   c                 P    t          | j                  dk    r| j        d         S | S )z Rule to unpack singleton args

    >>> from sympy.strategies import unpack
    >>> from sympy import Basic, S
    >>> unpack(Basic(S(2)))
    2
    r   r   )r   r   r   s    r   unpackrH      s*     49~~ y|r   c                     | j         }g }| j        D ]=}|j         |k    r|                    |j                   (|                    |           > || j         g|R  S )z9 Flatten T(a, b, T(c, d), T2(e)) to T(a, b, c, d, T2(e)) )r   r   extendappend)r   r   clsr   r   s        r   flattenrM      st    
.CDy  =C 	KK!!!!KK3t~%%%%%r   c                 r    | j         r| S  | j        t          t          t          | j                             S )z Rebuild a SymPy tree.

    Explanation
    ===========

    This function recursively calls constructors in the expression tree.
    This forces canonicalization and removes ugliness introduced by the use of
    Basic.__new__
    )is_Atomfuncr   r   rebuildr   rG   s    r   rQ   rQ      s6     | 9ty$s7DI667788r   N)__doc__sympy.utilities.iterablesr   utilr   r   r.   r3   r@   rE   rH   rM   rQ   r	   r   r   <module>rU      s     + * * * * *           @% % %N     "  0      	& 	& 	& 	&9 9 9 9 9r   