K inddlZddlmZddlmZmZmZddlmZdZ dZ dZ dZ d Z d Zd Zd Zd Zy)N) zip_longest) list_visitorMultisetPartitionTraversermultiset_partitions_taocp)_set_partitionsc#FK|D]}|\}}}||k\s||ks|yw)a Filters (on the number of parts) a multiset partition enumeration Arguments ========= lb, and ub are a range (in the Python slice sense) on the lpart variable returned from a multiset partition enumeration. Recall that lpart is 0-based (it points to the topmost part on the part stack), so if you want to return parts of sizes 2,3,4,5 you would use lb=1 and ub=5. N)partition_iteratorlbubstateflpartpstacks l/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/utilities/tests/test_enumerative.pypart_range_filterrs5$ 5& B;52:Ks !!!c g}t||D]\}}|j|g|zt}t|}t |D]\}}t |D cgc]} g} } t |D]} | || j || tt| D cgc] } t| c} } |j| |Scc} wcc} w)aEnumerates partitions of a multiset Parameters ========== multiplicities list of integer multiplicities of the components of the multiset. components the components (elements) themselves Returns ======= Set of partitions. Each partition is tuple of parts, and each part is a tuple of components (with repeats to indicate multiplicity) Notes ===== Multiset partitions can be created as equivalence classes of set partitions, and this function does just that. This approach is slow and memory intensive compared to the more advanced algorithms available, but the code is simple and easy to understand. Hence this routine is strictly for testing -- to provide a straightforward baseline against which to regress the production versions. (This code is a simplified version of an earlier production implementation.) ) zipextendsetlenrrangeappendtuplesortedadd) multiplicities componentscanonctelemcachenncqirvp canonicals rmultiset_partitions_baseliner*"s@ E 3 D dVBY  EE E A #A) $Qb $ $q &A qtHOOE!H % & b)E!H) *,  )  L %*s  C"Cc tj}t||}t}t |D]H}t t t||Dcgc] }t |c}}|j|J||k(sJycc}w)z~ Enumerates the partitions of multiset with AOCP algorithm and baseline implementation, and compare the results. N) stringascii_lowercaser*rrrrrr)rletters bl_partitionsaocp_partitionsr r(p1s rcompare_multiset_w_baseliner2Ss $$G0IM eO*>:  6#/w#?@aq@BCB O ++ +As Bcx|\}}}|\}}}||k(r)|d|dz|d|dzk(r|d||dz|d||dzk(ryy)zcompare for equality two instances of multiset partition states This is useful for comparing different versions of the algorithm to verify correctness.rTFr )s1s2f1lpart1pstack1f2lpart2pstack2s rcompare_multiset_statesr=hskBB&r!F1H~AfQh? 1Rq\ "ga6!8 &= = c@ddg}t|gd}t|y)zjCompares the output of multiset_partitions_taocp with a baseline (set partition based) implementation.)r4N)r2)rs rtest_multiset_partitions_taocprCws" UN/N/r>cgd}t}t|j|t|D]\}}t ||rJy)z4Compares Knuth-based versions of multiset_partitions)r@r@r4N)rrenum_allrr=)rmr5r6s r!test_multiset_partitions_versionsrHsKN"$Aajj87GI/B&r2.../r>ct}|j||j|k(sJt}t}t}|j|||}t t |||}t |j |||t|} t |j||d|} t||| | D]1\} } } }t| | sJt| | sJt| |r1Jy)zCompare filter-based and more optimized subrange implementations Helper for tests, called with both small and larger multisets. rN) rcount_partitionscount_partitions_slow enum_rangerr enum_smallsum enum_largerr=)multr r rGmamcmda_itb_itc_itd_itsasbscsds rsubrange_exerciser\s #$A  d # % && & $ %B # %B # %B ==r2 &D 6tc.gd}d}d}t|||y)N)rArAr@r4r4r@r\rPr r s r test_subranger` D B BdB#r>c.gd}d}d}t|||y)N)rBr@r4rAr^r_s rtest_subrange_largererar>)r, itertoolsrsympy.utilities.enumerativerrrsympy.utilities.iterablesrrr*r2r=rCrHr\r`rer r>rrisH ! 6$.b,*  0//6$$r>