o
    jg                     @   sx   d dl mZ d dlmZmZmZ d dlmZ dd Zdd Z	dd	 Z
d
d Zdd Zdd Zdd Zdd Zdd ZdS )    )zip_longest)list_visitorMultisetPartitionTraversermultiset_partitions_taocp)_set_partitionsc                 c   s0    | D ]}|\}}}||kr||k r|V  qdS )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_iteratorlbubstateflpartpstackr   r   ^/var/www/html/zoom/venv/lib/python3.10/site-packages/sympy/utilities/tests/test_enumerative.pypart_range_filter   s   
r   c                 C   s   g }t | |D ]\}}||g|  qt }t|}t|D ]/\}}dd t|D }	t|D ]}
|	||
  ||
  q0ttdd |	D }|	| q|S )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.)
    c                 S   s   g | ]}g qS r   r   ).0ir   r   r   
<listcomp>I   s    z0multiset_partitions_baseline.<locals>.<listcomp>c                 S      g | ]}t |qS r   tupler   pr   r   r   r   M       )
zipextendsetlenr   rangeappendr   sortedadd)multiplicities
componentscanonctelemcachenncqrvr   	canonicalr   r   r   multiset_partitions_baseline!   s    r-   c                 C   sX   d}t | |}t }t| D ]}ttdd t||D }|| q||ks*J dS )z~
    Enumerates the partitions of multiset with AOCP algorithm and
    baseline implementation, and compare the results.

    abcdefghijklmnopqrstuvwxyzc                 S   r   r   r   r   r   r   r   r   b   r   z/compare_multiset_w_baseline.<locals>.<listcomp>N)r-   r   r   r   r    r   r!   )r"   lettersbl_partitionsaocp_partitionsr   p1r   r   r   compare_multiset_w_baselineR   s   
r3   c                 C   sl   | \}}}|\}}}||kr4|d|d  |d|d  kr4|d||d   |d||d   kr4dS dS )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pstack2r   r   r   compare_multiset_statesg   s   

((r=   c                  C   s$   ddg} t |  g d} t |  dS )zjCompares the output of multiset_partitions_taocp with a baseline
    (set partition based) implementation.   )      r4   N)r3   )r"   r   r   r   test_multiset_partitions_taocpv   s   rA   c                  C   s>   g d} t  }t|| t| D ]\}}t||sJ qdS )z4Compares Knuth-based versions of multiset_partitions)   r>   r>   r4   N)r   r   enum_allr   r=   )r"   mr5   r6   r   r   r   !test_multiset_partitions_versions   s   
rE   c                 C   s   t  }|| || ksJ t  }t  }t  }|| ||}tt| ||}t|| ||t| }	t|| |d|}
t	|||	|
D ]\}}}}t
||sQJ t
||sXJ t
||s_J qDdS )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
   rD   mamcmda_itb_itc_itd_itsasbscsdr   r   r   subrange_exercise   s    rX   c                  C       g d} d}d}t | || d S )N)r?   r?   r>   r4   r4   r>   rX   rL   r	   r
   r   r   r   test_subrange      r\   c                  C   rY   )N)   r@   r>   r4   r?      rZ   r[   r   r   r   test_subrange_large   r]   r`   N)	itertoolsr   sympy.utilities.enumerativer   r   r   sympy.utilities.iterablesr   r   r-   r3   r=   rA   rE   rX   r\   r`   r   r   r   r   <module>   s    1