+
    i4                     d    R t ^ RIHt ^RIHt ]3R ltR t]3R ltR tR t	R t
]3R	 ltR
 tR# )zOGeneric Rules for SymPy

This file assumes knowledge of Basic and little else.
)sift)newc                   a a V V3R lpV# )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           	     x  < \        \        SV P                  4      4      p\        V4      ^ 8X  d   V # \        V4      \	        V4      8w  dG   S! V P
                  .\        V P                  V4       UUu. uF  w  r#V'       d   K  VNK  	  uppO5!  # S! V P
                  V P                  ^ ,          4      # u uppi )zRemove identities )listmapargssumlen	__class__zip)expridsargxisidr   s   &   s/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/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   3B6B6 )r   r   r   s   ff r   rm_idr   
   s    &	5     c                   a aa VVV 3R lpV# )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                ~  < \        V P                  S
4      pVP                  4        UUu/ uF  w  r#V\        \	        S	V4      4      bK  	  pppVP                  4        UUu. uF  w  rVS! We4      NK  	  ppp\        V4      \        V P                  4      8w  d   \        \        V 4      .VO5!  # V # u uppi u uppi )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   fff r   glomr(   +   s    6 r   c                   a a V V3R lpV# )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                 V   < S! V P                   .\        V P                  SR 7      O5!  # ))r%   )r   sortedr   )r   r%   r   s   &r   sort_rlsort.<locals>.sort_rl`   s"    4>>?F499#$>??r   r   )r%   r   r,   s   ff r   sortr.   S   s    @Nr   c                   a a V V3R lpV# )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           
      H  < \        V P                  4       F  w  r\        VS4      '       g   K  V P                  R V V P                  V,          V P                  V^,           R  rTpS! VP                   Uu. uF  pS! W23,           V,           !  NK  	  up!  u # 	  V # u upi N)	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   7B
r   )r8   r9   r:   s   ff r   
distributer<   e   s      r   c                   a a V V3R lpV# )zReplace expressions exactly c                    < V S8X  d   S# V # r1   r   )r   ar6   s   &r   subs_rlsubs.<locals>.subs_rl   s    19HKr   r   )r?   r6   r@   s   ff r   subsrB   ~   s    
 Nr   c                `    \        V P                  4      ^8X  d   V P                  ^ ,          # V # )zRule to unpack singleton args

>>> from sympy.strategies import unpack
>>> from sympy import Basic, S
>>> unpack(Basic(S(2)))
2
)r
   r   r   s   &r   unpackrE      s'     499~yy|r   c                    V P                   p. pV P                   FB  pVP                   V8X  d   VP                  VP                  4       K1  VP                  V4       KD  	  V! V P                   .VO5!  # )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   flattenrJ      s\    
..CDyy==CKK!KK	 
 t~~%%%r   c                    V P                   '       d   V # V P                  ! \        \        \        V P
                  4      4      !  # )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   rD   s   &r   rN   rN      s1     |||yy$s7DII6788r   N)__doc__sympy.utilities.iterablesr   utilr   r   r(   r.   r<   rB   rE   rJ   rN   r   r   r   <module>rR      sK    +   B%P  $2  	&9r   