
    wgd                     H    d Z  G d d      Zd Zd Zd Z G d d      Zd Zy	)
ar  
Algorithms and classes to support enumerative combinatorics.

Currently just multiset partitions, but more could be added.

Terminology (following Knuth, algorithm 7.1.2.5M TAOCP)
*multiset* aaabbcccc has a *partition* aaabc | bccc

The submultisets, aaabc and bccc of the partition are called
*parts*, or sometimes *vectors*.  (Knuth notes that multiset
partitions can be thought of as partitions of vectors of integers,
where the ith element of the vector gives the multiplicity of
element i.)

The values a, b and c are *components* of the multiset.  These
correspond to elements of a set, but in a multiset can be present
with a multiplicity greater than 1.

The algorithm deserves some explanation.

Think of the part aaabc from the multiset above.  If we impose an
ordering on the components of the multiset, we can represent a part
with a vector, in which the value of the first element of the vector
corresponds to the multiplicity of the first component in that
part. Thus, aaabc can be represented by the vector [3, 1, 1].  We
can also define an ordering on parts, based on the lexicographic
ordering of the vector (leftmost vector element, i.e., the element
with the smallest component number, is the most significant), so
that [3, 1, 1] > [3, 1, 0] and [3, 1, 1] > [2, 1, 4].  The ordering
on parts can be extended to an ordering on partitions: First, sort
the parts in each partition, left-to-right in decreasing order. Then
partition A is greater than partition B if A's leftmost/greatest
part is greater than B's leftmost part.  If the leftmost parts are
equal, compare the second parts, and so on.

In this ordering, the greatest partition of a given multiset has only
one part.  The least partition is the one in which the components
are spread out, one per part.

The enumeration algorithms in this file yield the partitions of the
argument multiset in decreasing order.  The main data structure is a
stack of parts, corresponding to the current partition.  An
important invariant is that the parts on the stack are themselves in
decreasing order.  This data structure is decremented to find the
next smaller partition.  Most often, decrementing the partition will
only involve adjustments to the smallest parts at the top of the
stack, much as adjacent integers *usually* differ only in their last
few digits.

Knuth's algorithm uses two main operations on parts:

Decrement - change the part so that it is smaller in the
  (vector) lexicographic order, but reduced by the smallest amount possible.
  For example, if the multiset has vector [5,
  3, 1], and the bottom/greatest part is [4, 2, 1], this part would
  decrement to [4, 2, 0], while [4, 0, 0] would decrement to [3, 3,
  1].  A singleton part is never decremented -- [1, 0, 0] is not
  decremented to [0, 3, 1].  Instead, the decrement operator needs
  to fail for this case.  In Knuth's pseudocode, the decrement
  operator is step m5.

Spread unallocated multiplicity - Once a part has been decremented,
  it cannot be the rightmost part in the partition.  There is some
  multiplicity that has not been allocated, and new parts must be
  created above it in the stack to use up this multiplicity.  To
  maintain the invariant that the parts on the stack are in
  decreasing order, these new parts must be less than or equal to
  the decremented part.
  For example, if the multiset is [5, 3, 1], and its most
  significant part has just been decremented to [5, 3, 0], the
  spread operation will add a new part so that the stack becomes
  [[5, 3, 0], [0, 0, 1]].  If the most significant part (for the
  same multiset) has been decremented to [2, 0, 0] the stack becomes
  [[2, 0, 0], [2, 0, 0], [1, 3, 1]].  In the pseudocode, the spread
  operation for one part is step m2.  The complete spread operation
  is a loop of steps m2 and m3.

In order to facilitate the spread operation, Knuth stores, for each
component of each part, not just the multiplicity of that component
in the part, but also the total multiplicity available for this
component in this part or any lesser part above it on the stack.

One added twist is that Knuth does not represent the part vectors as
arrays. Instead, he uses a sparse representation, in which a
component of a part is represented as a component number (c), plus
the multiplicity of the component in that part (v) as well as the
total multiplicity available for that component (u).  This saves
time that would be spent skipping over zeros.

c                   ,    e Zd ZdZdZd Zd Zd Zd Zy)PartComponentaX  Internal class used in support of the multiset partitions
    enumerators and the associated visitor functions.

    Represents one component of one part of the current partition.

    A stack of these, plus an auxiliary frame array, f, represents a
    partition of the multiset.

    Knuth's pseudocode makes c, u, and v separate arrays.
    cuvc                 .    d| _         d| _        d| _        y )N    r   selfs    `/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/utilities/enumerative.py__init__zPartComponent.__init__j   s        c                 N    d| j                   | j                  | j                  fz  S )z&for debug/algorithm animation purposeszc:%d u:%d v:%dr   r
   s    r   __repr__zPartComponent.__repr__t   s     466466466":::r   c                     t        || j                        xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S )z<Define  value oriented equality, which is useful for testers)
isinstance	__class__r   r   r   r   others     r   __eq__zPartComponent.__eq__x   sV    5$..1 "%''!"%''!" %''!	#r   c                     | |k(   S )z#Defined for consistency with __eq__ r   s     r   __ne__zPartComponent.__ne__   s    5=  r   N)	__name__
__module____qualname____doc__	__slots__r   r   r   r   r   r   r   r   r   \   s"    	  I;#!r   r   c              #     K   t        |       }t        |       }t        ||z  dz         D cg c]  }t                }}dg|dz   z  }t        |      D ]"  }||   }||_        | |   |_        | |   |_        $ d|d<   d}d}	||d<   |}
	 	 |}|
}d}||
k  r||   j
                  ||   j                  z
  ||   _        ||   j
                  dk(  rd}n|sj||   j                  ||   _        t        ||   j                  ||   j
                        ||   _        ||   j
                  ||   j                  k  }|dz   }n3||   j                  ||   _        ||   j
                  ||   _        |dz   }|dz   }||
k  r||
kD  r|
}|}
|	dz   }	|
||	dz   <   nn||	|g}| 	 |
dz
  }||   j                  dk(  r|dz
  }||   j                  dk(  r||k(  r%||   j                  dk(  r|	dk(  ry|	dz
  }	|}
||	   }nF||   j                  dz
  ||   _        t        |dz   |
      D ]  }||   j
                  ||   _         nc c}w w)a  Enumerates partitions of a multiset.

    Parameters
    ==========

    multiplicities
         list of integer multiplicities of the components of the multiset.

    Yields
    ======

    state
        Internal data structure which encodes a particular partition.
        This output is then usually processed by a visitor function
        which combines the information from this data structure with
        the components themselves to produce an actual partition.

        Unless they wish to create their own visitor function, users will
        have little need to look inside this data structure.  But, for
        reference, it is a 3-element list with components:

        f
            is a frame array, which is used to divide pstack into parts.

        lpart
            points to the base of the topmost part.

        pstack
            is an array of PartComponent objects.

        The ``state`` output offers a peek into the internal data
        structures of the enumeration function.  The client should
        treat this as read-only; any modification of the data
        structure will cause unpredictable (and almost certainly
        incorrect) results.  Also, the components of ``state`` are
        modified in place at each iteration.  Hence, the visitor must
        be called at each loop iteration.  Accumulating the ``state``
        instances and processing them later will not work.

    Examples
    ========

    >>> from sympy.utilities.enumerative import list_visitor
    >>> from sympy.utilities.enumerative import multiset_partitions_taocp
    >>> # variables components and multiplicities represent the multiset 'abb'
    >>> components = 'ab'
    >>> multiplicities = [1, 2]
    >>> states = multiset_partitions_taocp(multiplicities)
    >>> list(list_visitor(state, components) for state in states)
    [[['a', 'b', 'b']],
    [['a', 'b'], ['b']],
    [['a'], ['b', 'b']],
    [['a'], ['b'], ['b']]]

    See Also
    ========

    sympy.utilities.iterables.multiset_partitions: Takes a multiset
        as input and directly yields multiset partitions.  It
        dispatches to a number of functions, including this one, for
        implementation.  Most users will find it more convenient to
        use than multiset_partitions_taocp.

       r	   TFN)lensumranger   r   r   r   min)multiplicitiesmnipstackfjpsalpartbkxstates                 r   multiset_partitions_taocpr3      s    H 	NANA (-QUQY'78!mo8F8	
q1uA 1X !AYa a 	! AaD	AEAaD	A
AAAa%$QikkF1IKK7q	!9;;!#A"()++F1IK"%fQikk6!9;;"?F1IKq	fQikk1AAA"()++F1IK"()++F1IKAAE a%" 1u	 %!) ; @ E6" !A!9;;!#E !9;;!#Av&)++*A:	eH %QikkAoq	q1ua .A"()++F1IK.! K % 9s   *IID=I;AIA8Ic                     | \  }}}g }t        |dz         D ]W  }d}|||   ||dz       D ]1  }|j                  dkD  s|||j                     |j                  z  z  }3 |j                  |       Y |S )a  Use with multiset_partitions_taocp to enumerate the ways a
    number can be expressed as a product of factors.  For this usage,
    the exponents of the prime factors of a number are arguments to
    the partition enumerator, while the corresponding prime factors
    are input here.

    Examples
    ========

    To enumerate the factorings of a number we can think of the elements of the
    partition as being the prime factors and the multiplicities as being their
    exponents.

    >>> from sympy.utilities.enumerative import factoring_visitor
    >>> from sympy.utilities.enumerative import multiset_partitions_taocp
    >>> from sympy import factorint
    >>> primes, multiplicities = zip(*factorint(24).items())
    >>> primes
    (2, 3)
    >>> multiplicities
    (3, 1)
    >>> states = multiset_partitions_taocp(multiplicities)
    >>> list(factoring_visitor(state, primes) for state in states)
    [[24], [8, 3], [12, 2], [4, 6], [4, 2, 3], [6, 2, 2], [2, 2, 2, 3]]
    r    r	   )r#   r   r   append)	r2   primesr*   r.   r)   	factoringr(   factorr,   s	            r   factoring_visitorr9   0  s    4 AufI519 !1qQx( 	/Bttax&,"$$..	/ 	 ! r   c                     | \  }}}g }t        |dz         D ]d  }g }|||   ||dz       D ]>  }|j                  dkD  s|j                  ||j                     g|j                  z         @ |j	                  |       f |S )a  Return a list of lists to represent the partition.

    Examples
    ========

    >>> from sympy.utilities.enumerative import list_visitor
    >>> from sympy.utilities.enumerative import multiset_partitions_taocp
    >>> states = multiset_partitions_taocp([1, 2, 1])
    >>> s = next(states)
    >>> list_visitor(s, 'abc')  # for multiset 'a b b c'
    [['a', 'b', 'b', 'c']]
    >>> s = next(states)
    >>> list_visitor(s, [1, 2, 3])  # for multiset '1 2 2 3
    [[1, 2, 2], [3]]
    r    r	   )r#   r   extendr   r5   )	r2   
componentsr*   r.   r)   	partitionr(   partr,   s	            r   list_visitorr?   U  s      AufI57^ 1a!f% 	7BttaxZ-.56	7 	 r   c                   j    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zy)MultisetPartitionTraversera'  
    Has methods to ``enumerate`` and ``count`` the partitions of a multiset.

    This implements a refactored and extended version of Knuth's algorithm
    7.1.2.5M [AOCP]_."

    The enumeration methods of this class are generators and return
    data structures which can be interpreted by the same visitor
    functions used for the output of ``multiset_partitions_taocp``.

    Examples
    ========

    >>> from sympy.utilities.enumerative import MultisetPartitionTraverser
    >>> m = MultisetPartitionTraverser()
    >>> m.count_partitions([4,4,4,2])
    127750
    >>> m.count_partitions([3,3,3])
    686

    See Also
    ========

    multiset_partitions_taocp
    sympy.utilities.iterables.multiset_partitions

    References
    ==========

    .. [AOCP] Algorithm 7.1.2.5M in Volume 4A, Combinatoral Algorithms,
           Part 1, of The Art of Computer Programming, by Donald Knuth.

    .. [Factorisatio] On a Problem of Oppenheim concerning
           "Factorisatio Numerorum" E. R. Canfield, Paul Erdos, Carl
           Pomerance, JOURNAL OF NUMBER THEORY, Vol. 17, No. 1. August
           1983.  See section 7 for a description of an algorithm
           similar to Knuth's.

    .. [Yorgey] Generating Multiset Partitions, Brent Yorgey, The
           Monad.Reader, Issue 8, September 2007.

    c                     d| _         d| _        d| _        d| _        d | _        d | _        d| _        d| _        g | _        t        | d      si | _
        y y )NFr	   dp_map)debugk1k2p1r)   r*   r.   	discardeddp_stackhasattrrC   r
   s    r   r   z#MultisetPartitionTraverser.__init__  s[    
 
 tX&DK 'r   c                 (    | j                   rt        y)zeUseful for understanding/debugging the algorithms.  Not
        generally activated in end-user code.N)rD   RuntimeError)r   msgs     r   db_tracez#MultisetPartitionTraverser.db_trace  s     ::  r   c                 l   t        |      }t        |      }t        ||z  dz         D cg c]  }t                c}| _        dg|dz   z  | _        t        |      D ],  }| j                  |   }||_        ||   |_        ||   |_        . d| j
                  d<   || j
                  d<   d| _	        yc c}w )zAllocates and initializes the partition stack.

        This is called from the enumeration/counting routines, so
        there is no need to call it separately.r    r	   N)
r!   r"   r#   r   r)   r*   r   r   r   r.   )r   r%   num_componentscardinalityr(   r+   r,   s          r   _initialize_enumerationz2MultisetPartitionTraverser._initialize_enumeration  s     ^,.)
 ^k9A=>@1} @a( ~& 	%AQBBD!!$BD!!$BD		% q	"q	
@s   B1c                 ,   t        |      }t        |dz
  dd      D ]v  }|dk(  r||   j                  dkD  s|dkD  s ||   j                  dkD  s3||   xj                  dz  c_        t        |dz   |      D ]  }||   j                  ||   _          y y)a*  Decrements part (a subrange of pstack), if possible, returning
        True iff the part was successfully decremented.

        If you think of the v values in the part as a multi-digit
        integer (least significant digit on the right) this is
        basically decrementing that integer, but with the extra
        constraint that the leftmost digit cannot be decremented to 0.

        Parameters
        ==========

        part
           The part, represented as a list of PartComponent objects,
           which is to be decremented.

        r    r	   TF)r!   r#   r   r   )r   r>   plenr+   r0   s        r   decrement_partz)MultisetPartitionTraverser.decrement_part  s    " 4ytaxR( 	AAv$q'))a-1q5T!WYY]Q		Q	q1ud+ *A $Q		DGI*	 r   c                    | j                   |dz
  k\  r| xj                  dz  c_        yt        |      }t        |dz
  dd      D ]E  }|dk(  rI|d   j                  dz
  || j                   z
  z  |d   j
                  k  r| xj                  dz  c_         y|dk(  r||   j                  dkD  s|dkD  so||   j                  dkD  s||   xj                  dz  c_        t        |dz   |      D ]  }||   j
                  ||   _         |dkD  r||d   j                  dk(  rj|d   j
                  |d   j                  z
  || j                   z
  dz
  |d   j                  z  k(  r(| xj                  dz  c_        | j                  d        y y y)a  Decrements part (a subrange of pstack), if possible, returning
        True iff the part was successfully decremented.

        Parameters
        ==========

        part
            part to be decremented (topmost part on the stack)

        ub
            the maximum number of parts allowed in a partition
            returned by the calling traversal.

        Notes
        =====

        The goal of this modification of the ordinary decrement method
        is to fail (meaning that the subtree rooted at this part is to
        be skipped) when it can be proved that this part can only have
        child partitions which are larger than allowed by ``ub``. If a
        decision is made to fail, it must be accurate, otherwise the
        enumeration will miss some partitions.  But, it is OK not to
        capture all the possible failures -- if a part is passed that
        should not be, the resulting too-large partitions are filtered
        by the enumeration one level up.  However, as is usual in
        constrained enumerations, failing early is advantageous.

        The tests used by this method catch the most common cases,
        although this implementation is by no means the last word on
        this problem.  The tests include:

        1) ``lpart`` must be less than ``ub`` by at least 2.  This is because
           once a part has been decremented, the partition
           will gain at least one child in the spread step.

        2) If the leading component of the part is about to be
           decremented, check for how many parts will be added in
           order to use up the unallocated multiplicity in that
           leading component, and fail if this number is greater than
           allowed by ``ub``.  (See code for the exact expression.)  This
           test is given in the answer to Knuth's problem 7.2.1.5.69.

        3) If there is *exactly* enough room to expand the leading
           component by the above test, check the next component (if
           it exists) once decrementing has finished.  If this has
           ``v == 0``, this next component will push the expansion over the
           limit by 1, so fail.
        r    FrT   r	   zDecrement fails test 3T)	r.   rG   r!   r#   r   r   rE   rF   rN   )r   r>   ubrU   r+   r0   s         r   decrement_part_smallz/MultisetPartitionTraverser.decrement_part_small  sf   b ::aGGqLG4ytaxR( 	AAv4799q=2

?;d1giiG1Av$q'))a-1q5T!WYY]Q		Q	q1ud+ *A $Q		DGI* 1Haa!WYYa*tzz/A-a:<GGqLGMM":; 1	2 r   c                    |dk(  r| j                  |      sy|| j                  z
  }|dk  ryt        d |D              }t        d |D              }||k  ry|||z
  z
  }|dk  ryt        t	        |      dz
  dd      D ]}  }|dk(  r.|d   j
                  |kD  r|d   xj
                  |z  c_         y y||   j
                  |k\  r||   xj
                  |z  c_         y|||   j
                  z  }d||   _         y)	a  Decrements part, while respecting size constraint.

        A part can have no children which are of sufficient size (as
        indicated by ``lb``) unless that part has sufficient
        unallocated multiplicity.  When enforcing the size constraint,
        this method will decrement the part (if necessary) by an
        amount needed to ensure sufficient unallocated multiplicity.

        Returns True iff the part was successfully decremented.

        Parameters
        ==========

        part
            part to be decremented (topmost part on the stack)

        amt
            Can only take values 0 or 1.  A value of 1 means that the
            part must be decremented, and then the size constraint is
            enforced.  A value of 0 means just to enforce the ``lb``
            size constraint.

        lb
            The partitions produced by the calling enumeration must
            have more parts than this value.

        r    Fr	   Tc              3   4   K   | ]  }|j                     y wN)r   .0pcs     r   	<genexpr>zBMultisetPartitionTraverser.decrement_part_large.<locals>.<genexpr>  s     -"-   c              3   4   K   | ]  }|j                     y wr\   )r   r]   s     r   r`   zBMultisetPartitionTraverser.decrement_part_large.<locals>.<genexpr>  s     .2"$$.ra   rT   N)rV   r.   r"   r#   r!   r   )	r   r>   amtlbmin_unalloc
total_multtotal_allocdeficitr(   s	            r   decrement_part_largez/MultisetPartitionTraverser.decrement_part_largeU  s   : !8
 &&t,
 4::o!---
...$k!9:a<s4y1}b"- 	"AAv799w&GII(I 799'GII(ItAwyy(G !DGI	"r   c                 P    | j                  ||      xr | j                  |d|      S )a  Decrements part (a subrange of pstack), if possible, returning
        True iff the part was successfully decremented.

        Parameters
        ==========

        part
            part to be decremented (topmost part on the stack)

        ub
            the maximum number of parts allowed in a partition
            returned by the calling traversal.

        lb
            The partitions produced by the calling enumeration must
            have more parts than this value.

        Notes
        =====

        Combines the constraints of _small and _large decrement
        methods.  If returns success, part has been decremented at
        least once, but perhaps by quite a bit more if needed to meet
        the lb constraint.
        r	   )rY   ri   )r   r>   rd   rX   s       r   decrement_part_rangez/MultisetPartitionTraverser.decrement_part_range  s1    B ((r2 3%%dAr2	3r   c                    | j                   | j                     }| j                   | j                  dz      }|}d}t        | j                   | j                     | j                   | j                  dz            D ]Q  }| j                  |   j                  | j                  |   j
                  z
  | j                  |   _        | j                  |   j                  dk(  rd}h| j                  |   j                  | j                  |   _        |r,| j                  |   j                  | j                  |   _        n| j                  |   j                  | j                  |   j
                  k  r.| j                  |   j                  | j                  |   _        d}n+| j                  |   j
                  | j                  |   _        |dz   }T ||kD  r1| j                  dz   | _        || j                   | j                  dz   <   yy)a  Returns True if a new part has been created, and
        adjusts pstack, f and lpart as needed.

        Notes
        =====

        Spreads unallocated multiplicity from the current top part
        into a new part created above the current on the stack.  This
        new part is constrained to be less than or equal to the old in
        terms of the part ordering.

        This call does nothing (and returns False) if the current top
        part has no unallocated multiplicity.

        r    Fr	   T)r*   r.   r#   r)   r   r   r   )r   r+   r0   basechangeds        r   spread_part_multiplicityz3MultisetPartitionTraverser.spread_part_multiplicity  s     FF4::FF4::>" tvvdjj)466$**q.+AB 	A#{{1~//$++a.2B2BBDKKN{{1~1$#';;q>#3#3A '+{{1~'7'7DKKN${{1~''$++a.*:*::+/;;q>+;+;A("&+/;;q>+;+;A(E	 t8aDJ%&DFF4::>"r   c                     | j                   | j                  | j                     | j                  | j                  dz       S )zEReturn current top part on the stack, as a slice of pstack.

        r    )r)   r*   r.   r
   s    r   top_partz#MultisetPartitionTraverser.top_part  s3     {{466$**-dffTZZ!^.DEEr   c              #     K   | j                  |       	 | j                         r	 | j                         r| j                  | j                  | j                  g}| | j                  | j                               sE| j                  dk(  ry| xj                  dz  c_        | j                  | j                               sEw)a  Enumerate the partitions of a multiset.

        Examples
        ========

        >>> from sympy.utilities.enumerative import list_visitor
        >>> from sympy.utilities.enumerative import MultisetPartitionTraverser
        >>> m = MultisetPartitionTraverser()
        >>> states = m.enum_all([2,2])
        >>> list(list_visitor(state, 'ab') for state in states)
        [[['a', 'a', 'b', 'b']],
        [['a', 'a', 'b'], ['b']],
        [['a', 'a'], ['b', 'b']],
        [['a', 'a'], ['b'], ['b']],
        [['a', 'b', 'b'], ['a']],
        [['a', 'b'], ['a', 'b']],
        [['a', 'b'], ['a'], ['b']],
        [['a'], ['a'], ['b', 'b']],
        [['a'], ['a'], ['b'], ['b']]]

        See Also
        ========

        multiset_partitions_taocp:
            which provides the same result as this method, but is
            about twice as fast.  Hence, enum_all is primarily useful
            for testing.  Also see the function for a discussion of
            states and visitors.

        r	   Nr    )rR   ro   r*   r.   r)   rV   rq   )r   r%   r2   s      r   enum_allz#MultisetPartitionTraverser.enum_all  s     > 	$$^4//1 //1 VVTZZ5EK ))$--/:::?

a
	 ))$--/: s   4CB
CCc              #     K   d| _         |dk  ry| j                  |       	 | j                         rb| j                  d       | j                  |k\  r1| xj                   dz  c_         | j                  d       |dz
  | _        n8| j                         rb| j
                  | j                  | j                  g}| | j                  | j                         |      sh| j                  d       | j                  dk(  ry| xj                  dz  c_        | j                  d       | j                  | j                         |      sh| j                  d	       4w)
a  Enumerate multiset partitions with no more than ``ub`` parts.

        Equivalent to enum_range(multiplicities, 0, ub)

        Parameters
        ==========

        multiplicities
             list of multiplicities of the components of the multiset.

        ub
            Maximum number of parts

        Examples
        ========

        >>> from sympy.utilities.enumerative import list_visitor
        >>> from sympy.utilities.enumerative import MultisetPartitionTraverser
        >>> m = MultisetPartitionTraverser()
        >>> states = m.enum_small([2,2], 2)
        >>> list(list_visitor(state, 'ab') for state in states)
        [[['a', 'a', 'b', 'b']],
        [['a', 'a', 'b'], ['b']],
        [['a', 'a'], ['b', 'b']],
        [['a', 'b', 'b'], ['a']],
        [['a', 'b'], ['a', 'b']]]

        The implementation is based, in part, on the answer given to
        exercise 69, in Knuth [AOCP]_.

        See Also
        ========

        enum_all, enum_large, enum_range

        r	   Nspread 1r    z  Discarding   $Failed decrement, going to backtrackBacktracked todecrement ok, about to expand)	rH   rR   ro   rN   r.   r*   r)   rY   rq   )r   r%   rX   r2   s       r   
enum_smallz%MultisetPartitionTraverser.enum_small   s    P 7$$^4//1j)::#NNa'NMM.1!#aDJ //1 T[[9 //DDE::?

a
./ //D MM9:+ s   BEB.EEc              #   x  K   d| _         |t        |      k\  ry| j                  |       | j                  | j	                         d|       	 d}| j                         rJ| j                  | j	                         d|      s| xj                   dz  c_         d}n| j                         rJ|r'| j                  | j                  | j                  g}| | j                  | j	                         d|      sG| j                  dk(  ry| xj                  dz  c_        | j                  | j	                         d|      sGw)a  Enumerate the partitions of a multiset with lb < num(parts)

        Equivalent to enum_range(multiplicities, lb, sum(multiplicities))

        Parameters
        ==========

        multiplicities
            list of multiplicities of the components of the multiset.

        lb
            Number of parts in the partition must be greater than
            this lower bound.


        Examples
        ========

        >>> from sympy.utilities.enumerative import list_visitor
        >>> from sympy.utilities.enumerative import MultisetPartitionTraverser
        >>> m = MultisetPartitionTraverser()
        >>> states = m.enum_large([2,2], 2)
        >>> list(list_visitor(state, 'ab') for state in states)
        [[['a', 'a'], ['b'], ['b']],
        [['a', 'b'], ['a'], ['b']],
        [['a'], ['a'], ['b', 'b']],
        [['a'], ['a'], ['b'], ['b']]]

        See Also
        ========

        enum_all, enum_small, enum_range

        r	   NTr    F)	rH   r"   rR   ri   rq   ro   r*   r.   r)   )r   r%   rd   good_partitionr2   s        r   
enum_largez%MultisetPartitionTraverser.enum_largec  s    F ^$$$$^4!!$--/1b9!N//100!RHNNa'N%*N //1 T[[9 //BG::?

a
	 //BG s   B%D:(BD:9D:c              #     K   d| _         |dk  s|t        |      k\  ry| j                  |       | j                  | j	                         d|       	 d}| j                         r| j                  d       | j                  | j	                         d|      s)| j                  d       | xj                   dz  c_         d}nS| j                  |k\  r3| xj                   dz  c_         d}| j                  d       |d	z
  | _        n| j                         r|r'| j                  | j                  | j                  g}| | j                  | j	                         ||      si| j                  d
       | j                  dk(  ry| xj                  dz  c_        | j                  d       | j                  | j	                         ||      si| j                  d       w)a  Enumerate the partitions of a multiset with
        ``lb < num(parts) <= ub``.

        In particular, if partitions with exactly ``k`` parts are
        desired, call with ``(multiplicities, k - 1, k)``.  This
        method generalizes enum_all, enum_small, and enum_large.

        Examples
        ========

        >>> from sympy.utilities.enumerative import list_visitor
        >>> from sympy.utilities.enumerative import MultisetPartitionTraverser
        >>> m = MultisetPartitionTraverser()
        >>> states = m.enum_range([2,2], 1, 2)
        >>> list(list_visitor(state, 'ab') for state in states)
        [[['a', 'a', 'b'], ['b']],
        [['a', 'a'], ['b', 'b']],
        [['a', 'b', 'b'], ['a']],
        [['a', 'b'], ['a', 'b']]]

        r	   NTru   z  Discarding (large cons)r    Fz  Discarding small consrv   rw   rx   ry   )rH   r"   rR   ri   rq   ro   rN   r.   r*   r)   rk   )r   r%   rd   rX   r|   r2   s         r   
enum_rangez%MultisetPartitionTraverser.enum_range  s    2 7bC//$$^4!!$--/1b9!N//1j)00!RHMM"=>NNa'N%*NZZ2%NNa'N%*NMM";<!#aDJ //1  T[[9 //RHDE::?

a
./ //RH MM9:= s   DGB2GGc                    d| _         | j                  |       	 | j                         r	 | j                         r| xj                   dz  c_         | j                  | j	                               sP| j
                  dk(  r| j                   S | xj
                  dz  c_        | j                  | j	                               sP)a  Returns the number of partitions of a multiset whose elements
        have the multiplicities given in ``multiplicities``.

        Primarily for comparison purposes.  It follows the same path as
        enumerate, and counts, rather than generates, the partitions.

        See Also
        ========

        count_partitions
            Has the same calling interface, but is much faster.

        r	   r    )pcountrR   ro   rV   rq   r.   )r   r%   s     r   count_partitions_slowz0MultisetPartitionTraverser.count_partitions_slow  s     $$^4//1 //1 KK1K ))$--/:::?;;&

a
	 ))$--/: r   c                    d| _         g | _        | j                  |       t        | j	                               }| j                  j                  |dfg       	 | j                         rt        | j	                               }|| j                  v r;| xj                   | j                  |   dz
  z  c_         | xj                  dz  c_        n9| j                  j                  || j                   fg       | j                         r| xj                   dz  c_         | j                  | j	                               s| j                  j                         D ]!  \  }}| j                   |z
  | j                  |<   # | j                  dk(  r| j                   S | xj                  dz  c_        | j                  | j	                               st        | j	                               }| j                  d   j                  || j                   f       )a  Returns the number of partitions of a multiset whose components
        have the multiplicities given in ``multiplicities``.

        For larger counts, this method is much faster than calling one
        of the enumerators and counting the result.  Uses dynamic
        programming to cut down on the number of nodes actually
        explored.  The dictionary used in order to accelerate the
        counting process is stored in the ``MultisetPartitionTraverser``
        object and persists across calls.  If the user does not
        expect to call ``count_partitions`` for any additional
        multisets, the object should be cleared to save memory.  On
        the other hand, the cache built up from one count run can
        significantly speed up subsequent calls to ``count_partitions``,
        so it may be advantageous not to clear the object.

        Examples
        ========

        >>> from sympy.utilities.enumerative import MultisetPartitionTraverser
        >>> m = MultisetPartitionTraverser()
        >>> m.count_partitions([9,8,2])
        288716
        >>> m.count_partitions([2,2])
        9
        >>> del m

        Notes
        =====

        If one looks at the workings of Knuth's algorithm M [AOCP]_, it
        can be viewed as a traversal of a binary tree of parts.  A
        part has (up to) two children, the left child resulting from
        the spread operation, and the right child from the decrement
        operation.  The ordinary enumeration of multiset partitions is
        an in-order traversal of this tree, and with the partitions
        corresponding to paths from the root to the leaves. The
        mapping from paths to partitions is a little complicated,
        since the partition would contain only those parts which are
        leaves or the parents of a spread link, not those which are
        parents of a decrement link.

        For counting purposes, it is sufficient to count leaves, and
        this can be done with a recursive in-order traversal.  The
        number of leaves of a subtree rooted at a particular part is a
        function only of that part itself, so memoizing has the
        potential to speed up the counting dramatically.

        This method follows a computational approach which is similar
        to the hypothetical memoized recursive function, but with two
        differences:

        1) This method is iterative, borrowing its structure from the
           other enumerations and maintaining an explicit stack of
           parts which are in the process of being counted.  (There
           may be multisets which can be counted reasonably quickly by
           this implementation, but which would overflow the default
           Python recursion limit with a recursive implementation.)

        2) Instead of using the part data structure directly, a more
           compact key is constructed.  This saves space, but more
           importantly coalesces some parts which would remain
           separate with physical keys.

        Unlike the enumeration functions, there is currently no _range
        version of count_partitions.  If someone wants to stretch
        their brain, it should be possible to construct one by
        memoizing with a histogram of counts rather than a single
        count, and combining the histograms.
        r	   r    rT   )r   rI   rR   part_keyrq   r5   ro   rC   r.   rV   pop)r   r%   pkeykeyoldcounts        r   count_partitionsz+MultisetPartitionTraverser.count_partitions  s   N  $$^4(tQi]+//104;;& KKDKK$5$9:KJJ!OJMM((4*=)@A //1" KK1K ))$--/:%)]]%6%6%8 >MC'+{{X'=DKK$>::?;;&

a
 ))$--/: DMMO,DMM"$$dDKK%8:C r   N)r   r   r   r   r   rN   rR   rV   rY   ri   rk   ro   rq   rs   rz   r}   r   r   r   r   r   r   rA   rA   r  s`    )V4 ><N`A"F"3H+ZF- ^A;F; z<;| >p;r   rA   c                     g }| D ]8  }|j                  |j                         |j                  |j                         : t        |      S )a  Helper for MultisetPartitionTraverser.count_partitions that
    creates a key for ``part``, that only includes information which can
    affect the count for that part.  (Any irrelevant information just
    reduces the effectiveness of dynamic programming.)

    Notes
    =====

    This member function is a candidate for future exploration. There
    are likely symmetries that can be exploited to coalesce some
    ``part_key`` values, and thereby save space and improve
    performance.

    )r5   r   r   tuple)r>   rvalr,   s      r   r   r   p  sD    " D BDDBDD ;r   N)r   r   r3   r9   r?   rA   r   r   r   r   <module>r      s;   Yv%! %!lV|"J:{; {;|r   