
    \h4                     b    S r SSKJr  SSKJr  \4S jrS r\4S jrS rS	 r	S
 r
\4S jrS rg)zOGeneric Rules for SymPy

This file assumes knowledge of Basic and little else.
    )sift   )newc                    ^ ^ U U4S jnU$ )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 identities then we keep one
Basic(0)

See Also:
    unpack
c           	      j  > [        [        TU R                  5      5      n[        U5      S:X  a  U $ [        U5      [	        U5      :w  aE  T" U R
                  /[        U R                  U5       VVs/ s H  u  p#U(       a  M  UPM     snnQ76 $ T" U R
                  U R                  S   5      $ s  snnf )zRemove identities r   )listmapargssumlen	__class__zip)expridsargxisidr   s       K/var/www/auris/envauris/lib/python3.13/site-packages/sympy/strategies/rl.pyident_removerm_id.<locals>.ident_remove   s    3tTYY'(s8q=KXS!t~~ J+.tyy#+>H+>a+>HJ J t~~tyy|44 Is   1B/B/ )r   r   r   s   `` r   rm_idr   
   s    &	5     c                    ^ ^^ UUU 4S jnU$ )a  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                 ~  > [        U R                  T
5      nUR                  5        VVs0 s H  u  p#U[        [	        T	U5      5      _M     nnnUR                  5        VVs/ s H  u  pVT" Xe5      PM     nnn[        U5      [        U R                  5      :w  a  [        [        U 5      /UQ76 $ U $ s  snnf s  snnf )z1Conglomerate together identical args x + x -> 2x )r   r
   itemsr   r	   setr   type)r   groupskr
   countsmatcntnewargscombinecountkeys           r   conglomerateglom.<locals>.conglomerateF   s    dii%:@,,.I.wq!SUD)**.I5;\\^D^73$^Dw<3tyy>)tDz,G,,K JDs   "B3#B9r   )r'   r&   r%   r(   s   ``` r   glomr*   +   s    6 r   c                    ^ ^ U U4S jnU$ )zCreate 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                 N   > T" U R                   /[        U R                  TS9Q76 $ )N)r'   )r   sortedr
   )r   r'   r   s    r   sort_rlsort.<locals>.sort_rl`   s"    4>>?F499#$>??r   r   )r'   r   r.   s   `` r   sortr0   S   s    @Nr   c                    ^ ^ U U4S jnU$ )a*  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           
      $  > [        U R                  5       Hp  u  p[        UT5      (       d  M  U R                  S U U R                  U   U R                  US-   S  pTnT" UR                   Vs/ s H  nT" X24-   U-   6 PM     sn6 s  $    U $ s  snf )Nr   )	enumerater
   
isinstance)r   ir   firstbtailABs         r   distribute_rl!distribute.<locals>.distribute_rlu   s    		*FA#q!!!%2A		!diiA>O$!&&I&31uv~46&IJJ +  Js   .B
r   )r9   r:   r;   s   `` r   
distributer=   e   s      r   c                    ^ ^ U U4S jnU$ )zReplace expressions exactly c                    > U T:X  a  T$ U $ )Nr   )r   ar7   s    r   subs_rlsubs.<locals>.subs_rl   s    19HKr   r   )r@   r7   rA   s   `` r   subsrC   ~   s    
 Nr   c                 V    [        U R                  5      S:X  a  U R                  S   $ U $ )zRule 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   unpackrF      s'     499~yy|r   c                     U R                   n/ nU R                   HA  nUR                   U:X  a  UR                  UR                  5        M0  UR                  U5        MC     U" U R                   /UQ76 $ )z8Flatten 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   flattenrK      s\    
..CDyy==CKK!KK	 
 t~~%%%r   c                     U R                   (       a  U $ U R                  " [        [        [        U R
                  5      5      6 $ )zRebuild 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
   rE   s    r   rO   rO      s/     ||yy$s7DII6788r   N)__doc__sympy.utilities.iterablesr   utilr   r   r*   r0   r=   rC   rF   rK   rO   r   r   r   <module>rS      sK    +   B%P  $2  	&9r   