K i4\dZddlmZddlmZefdZdZefdZdZd Z d Z efd Z d Z y )zP Generic Rules for SymPy This file assumes knowledge of Basic and little else. )sift)newcfd}|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 identities then we keep one Basic(0) See Also: unpack c Vtt|j}t|dk(r|St|t |k7r=|j gt |j|Dcgc] \}}|r | c}}S|j |jdScc}}w)z Remove identities r)listmapargssumlen __class__zip)expridsargxisidrs Y/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/strategies/rl.py ident_removezrm_id..ident_removes3tTYY'( s8q=K XS !t~~J+.tyy#+>HaaHJ Jt~~tyy|4 4Is / B% :B% )rrrs`` rrm_idr s& 5 cfd}|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 vt|j }|jDcic]\}}|tt |}}}|jDcgc]\}}||}}}t |t |jk7rt t|g|S|Scc}}wcc}}w)z2 Conglomerate together identical args x + x -> 2x )rr itemsr r setrtype) rgroupskr countsmatcntnewargscombinecountkeys r conglomeratezglom..conglomerateFsdii%:@,,.Iwq$!SUD)**II5;\\^Dc73$DD w<3tyy> )tDz,G, ,K JDs B/!B5r)r&r%r$r's``` rglomr(+s6 rcfd}|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) cT|jgt|jS)N)r&)r sortedr )rr&rs rsort_rlzsort..sort_rl`s"4>>?F499#$>??rr)r&rr,s`` rsortr-Ss@ Nrcfd}|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|jD]j\}}t|s|jd||j||j|dzd}}}|jDcgc]}||fz|zc}cS|Scc}w)Nr) enumerater isinstance)rirfirstbtailABs r distribute_rlz!distribute..distribute_rlus * KFAs#q!!%2A ! diiA>O$q!&&I31uv~46IJJ K Js*B r)r6r7r8s`` r distributer9es  rcfd}|S)z Replace expressions exactly c|k(rS|S)Nr)rar4s rsubs_rlzsubs..subs_rls 19HKrr)r<r4r=s`` rsubsr>~s NrcTt|jdk(r|jdS|S)z Rule to unpack singleton args >>> from sympy.strategies import unpack >>> from sympy import Basic, S >>> unpack(Basic(S(2))) 2 rr)r r rs runpackrAs' 499~yy| rc|j}g}|jD]>}|j|k(r|j|j.|j|@||jg|S)z9 Flatten T(a, b, T(c, d), T2(e)) to T(a, b, c, d, T2(e)) )r r extendappend)rrclsr rs rflattenrFsa ..C Dyy ==C  KK ! KK   t~~ % %%rc~|jr|S|jttt|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_Atomfuncrr rebuildr r@s rrJrJs1 || tyy$s7DII6788rN) __doc__sympy.utilities.iterablesrutilrrr(r-r9r>rArFrJrrrrNsK+B%P$2  & 9r