
    \h                     n    S SK r S SKJr  S SKJrJrJr  S SKJr  S r	S r
S rS rS	 rS
 rS rS rS rg)    N)zip_longest)list_visitorMultisetPartitionTraversermultiset_partitions_taocp)_set_partitionsc              #   L   #    U  H  nUu  pEnXQ:  d  M  XR:  d  M  Uv   M     g7f)ap  
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_iteratorlbubstateflpartpstacks          ^/var/www/auris/envauris/lib/python3.13/site-packages/sympy/utilities/tests/test_enumerative.pypart_range_filterr      s)      $ &;5:K $s   $$	$c                    / n[        X5       H  u  p4UR                  U/U-  5        M     [        5       n[        U5      n[	        U5       H  u  px[        U5       V	s/ s H  n	/ PM     n
n	[        U5       H  n	XU	      R                  X)   5        M     [        [        U
 Vs/ s H  n[        U5      PM     sn5      5      nUR                  U5        M     U$ s  sn	f s  snf )a@  Enumerates 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.)
)
zipextendsetlenr   rangeappendtuplesortedadd)multiplicities
componentscanonctelemcachenncqirvp	canonicals                r   multiset_partitions_baseliner*   "   s    @ E3dVBY 4 EEE
A #)$)Qb)$qAtHOOEH% b)bE!Hb)*,			) $ L % *s   C$C c                    [         R                  n[        X5      n[        5       n[	        U 5       HK  n[        [        [        XA5       Vs/ s H  n[        U5      PM     sn5      5      nUR                  U5        MM     X#:X  d   egs  snf )zr
Enumerates the partitions of multiset with AOCP algorithm and
baseline implementation, and compare the results.

N)	stringascii_lowercaser*   r   r   r   r   r   r   )r   lettersbl_partitionsaocp_partitionsr   r(   p1s          r   compare_multiset_w_baseliner2   S   s     $$G0IM eO*>:6#/#?@#?aq#?@B CB ;
 +++ As   Bc                 t    U u  p#nUu  pVnX6:X  a)  USUS-    USUS-    :X  a  USX#S-       USXVS-       :X  a  gg)zcompare for equality two instances of multiset partition states

This is useful for comparing different versions of the algorithm
to verify correctness.r      TFr	   )s1s2f1lpart1pstack1f2lpart2pstack2s           r   compare_multiset_statesr=   h   sa     BBr!F1H~AfQh?1Rq\"ga!8&==    c                  @    SS/n [        U 5        / SQn [        U 5        g)zfCompares the output of multiset_partitions_taocp with a baseline
(set partition based) implementation.   )      r4   N)r2   )r   s    r   test_multiset_partitions_taocprC   w   s"     UN/N/r>   c                      / SQn [        5       n[        UR                  U 5      [        U 5      5       H  u  p#[	        X#5      (       a  M   e   g)z4Compares Knuth-based versions of multiset_partitions)   r@   r@   r4   N)r   r   enum_allr   r=   )r   mr5   r6   s       r   !test_multiset_partitions_versionsrH      sF    N"$Aajj87GI&r....Ir>   c                    [        5       nUR                  U 5      UR                  U 5      :X  d   e[        5       n[        5       n[        5       nUR                  XU5      n[	        [        U 5      X5      n[	        UR                  X5      U[        U 5      5      n	[	        UR                  X5      SU5      n
[        XxX5       H<  u  pp[        X5      (       d   e[        X5      (       d   e[        X5      (       a  M<   e   g)zCompare filter-based and more optimized subrange implementations

Helper for tests, called with both small and larger multisets.
r   N)r   count_partitionscount_partitions_slow
enum_ranger   r   
enum_smallsum
enum_larger   r=   )multr   r   rG   mamcmda_itb_itc_itd_itsasbscsds                  r   subrange_exerciser\      s    
 	#$Ad#	%& & & 
$	%B	#	%B	#	%B ==2&D6t<bEDR]]44b#d)DDR]]44a<D%d$=&r....&r....&r.... >r>   c                  ,    / SQn SnSn[        XU5        g )N)rA   rA   r@   r4   r4   r@   r\   rP   r   r   s      r   test_subranger`          D	
B	
Bd#r>   c                  ,    / SQn SnSn[        XU5        g )N)   rB   r@   r4   rA      r^   r_   s      r   test_subrange_largere      ra   r>   )r,   	itertoolsr   sympy.utilities.enumerativer   r   r   sympy.utilities.iterablesr   r   r*   r2   r=   rC   rH   r\   r`   re   r	   r>   r   <module>ri      sH     ! 
 6$.b,*
0//6$$r>   