
    wg3                     \    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y)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           	      V   t        t        | j                              }t        |      dk(  r| S t        |      t	        |      k7  r= | j
                  gt        | j                  |      D cg c]
  \  }}|r	| c}} S  | j
                  | j                  d         S c c}}w )z Remove identities r   )listmapargssumlen	__class__zip)expridsargxisidr   s       X/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/strategies/rl.pyident_removezrm_id.<locals>.ident_remove   s    3tTYY'(s8q=KXS!t~~ J+.tyy#+>HaaHJ J t~~tyy|44 Is   /
B%:B% )r   r   r   s   `` r   rm_idr   
   s    &	5     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                 v   t        | j                  
      }|j                         D ci c]  \  }}|t        t	        	|             }}}|j                         D cg c]  \  }} ||       }}}t        |      t        | j                        k7  rt        t        |       g| S | S c c}}w c c}}w )z2 Conglomerate together identical args x + x -> 2x )r   r
   itemsr   r	   setr   type)r   groupskr
   countsmatcntnewargscombinecountkeys           r   conglomeratezglom.<locals>.conglomerateF   s    dii%:@,,.Iwq$!SUD)**II5;\\^Dc73$DDw<3tyy>)tDz,G,,K JDs    B/!B5r   )r&   r%   r$   r'   s   ``` r   glomr(   +   s    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                 T     | j                   gt        | j                         S )N)r&   )r   sortedr
   )r   r&   r   s    r   sort_rlzsort.<locals>.sort_rl`   s"    4>>?F499#$>??r   r   )r&   r   r,   s   `` r   sortr-   S   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 ]j  \  }}t        |      s| j                  d | | j                  |   | j                  |dz   d  }}} |j                  D cg c]  } ||fz   |z     c} c S  | S c c}w )Nr   )	enumerater
   
isinstance)r   ir   firstbtailABs         r   distribute_rlz!distribute.<locals>.distribute_rlu   s    		* 	KFAs#q!!%2A		!diiA>O$q!&&I31uv~46IJJ	K  Js   *B
r   )r6   r7   r8   s   `` r   
distributer9   e   s      r   c                       fd}|S )z Replace expressions exactly c                     | k(  rS | S )Nr   )r   ar4   s    r   subs_rlzsubs.<locals>.subs_rl   s    19HKr   r   )r<   r4   r=   s   `` r   subsr>   ~   s    
 Nr   c                 T    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   unpackrA      s'     499~yy|r   c                     | j                   }g }| j                  D ]>  }|j                   |k(  r|j                  |j                         .|j                  |       @  || j                   g| 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   flattenrF      sa    
..CDyy ==CKK!KK	
 t~~%%%r   c                 ~    | 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
   r@   s    r   rJ   rJ      s1     ||tyy$s7DII6788r   N)__doc__sympy.utilities.iterablesr   utilr   r   r(   r-   r9   r>   rA   rF   rJ   r   r   r   <module>rN      sK    +   B%P  $2  	&9r   