+
    iiQ                         ^ RI HtHtHtHt ^ RIHt ^ RIHt ^ RI	H
t
 ^ RIHt ^ RIHt ^ RIHtHt ^ RIHtHt ^ R	IHt ^ R
IHt  ! R R]4      t ! R R]4      tRR ltR tR tR tR tR# )    )BasicDictsympifyTuple)Integerdefault_sort_key)_sympifybell)zeros)	FiniteSetUnion)flattengroup)as_int)defaultdictc                      a  ] tR t^t o RtRtRtR tRR lt]	R 4       t
R tR tR tR	 t]	R
 4       t]	R 4       t]R 4       tRtV tR# )	Partitionz
This class represents an abstract partition.

A partition is a set of disjoint sets whose union equals a given set.

See Also
========

sympy.utilities.iterables.partitions,
sympy.utilities.iterables.multiset_partitions
Nc                F   . pRpV F]  p\        V\        4      '       d+   \        V4      p\        V4      \        V4      8  d   Rp M TpVP	                  \        V4      4       K_  	  \        ;QJ d    R V 4       F  '       d   K   RM	  RM! R V 4       4      '       g   \        R4      h\        V!  pV'       g!   \        V4      \        R V 4       4      8  d   \        R4      h\        P                  ! V .VO5!  p\        V4      Vn        \        V4      Vn        V# )a  
Generates a new partition object.

This method also verifies if the arguments passed are
valid and raises a ValueError if they are not.

Examples
========

Creating Partition from Python lists:

>>> from sympy.combinatorics import Partition
>>> a = Partition([1, 2], [3])
>>> a
Partition({3}, {1, 2})
>>> a.partition
[[1, 2], [3]]
>>> len(a)
2
>>> a.members
(1, 2, 3)

Creating Partition from Python sets:

>>> Partition({1, 2, 3}, {4, 5})
Partition({4, 5}, {1, 2, 3})

Creating Partition from SymPy finite sets:

>>> from sympy import FiniteSet
>>> a = FiniteSet(1, 2, 3)
>>> b = FiniteSet(4, 5)
>>> Partition(a, b)
Partition({4, 5}, {1, 2, 3})
FTc              3   B   "   T F  p\        V\        4      x  K  	  R # 5iN)
isinstancer   ).0parts   & ~/Users/tonyclaw/.openclaw/workspace/skills/math-calculator/venv/lib/python3.14/site-packages/sympy/combinatorics/partitions.py	<genexpr>$Partition.__new__.<locals>.<genexpr>N   s     @44:dI..4s   z@Each argument to Partition should be a list, set, or a FiniteSetc              3   8   "   T F  p\        V4      x  K  	  R # 5ir   )len)r   args   & r   r   r   U   s     9DSCDs   z'Partition contained duplicate elements.)r   listsetr    appendr
   all
ValueErrorr   sumr   __new__tuplememberssize)cls	partitionargsdupsr!   as_setUobjs   &*      r   r(   Partition.__new__   s    H C#t$$Sv;S)DKK&  s@4@sss@4@@@./ /
 4L3q6C9D999FGG+d+Ahq6
    c                   a Sf   V P                   pM$\        \        V P                   V3R lR7      4      p\        \        \        V P
                  W P                  34      4      # )a9  Return a canonical key that can be used for sorting.

Ordering is based on the size and sorted elements of the partition
and ties are broken with the rank.

Examples
========

>>> from sympy import default_sort_key
>>> from sympy.combinatorics import Partition
>>> from sympy.abc import x
>>> a = Partition([1, 2])
>>> b = Partition([3, 4])
>>> c = Partition([1, x])
>>> d = Partition(list(range(4)))
>>> l = [d, b, a + 1, a, c]
>>> l.sort(key=default_sort_key); l
[Partition({1, 2}), Partition({1}, {2}), Partition({1, x}), Partition({3, 4}), Partition({0, 1, 2, 3})]
c                    < \        V S4      # r   r   )worders   &r   <lambda>$Partition.sort_key.<locals>.<lambda>u   s    +;Au+Er4   key)r*   r)   sortedmapr	   r+   rank)selfr8   r*   s   &f r   sort_keyPartition.sort_key]   sO    ( =llGF4<<!EG HGS)DIIw		+JKLLr4   c           
         V P                   f:   \        V P                   Uu. uF  p\        V\        R7      NK  	  up4      V n         V P                   # u upi )zReturn partition as a sorted list of lists.

Examples
========

>>> from sympy.combinatorics import Partition
>>> Partition([1], [2, 3]).partition
[[1], [2, 3]]
r;   )
_partitionr=   r.   r	   )r@   ps   & r   r-   Partition.partitionx   sP     ??"$/3yy&:/8! '-Q4D&E/8&: ;DO&:s   Ac                    \        V4      pV P                  V,           p\        V\        V P                  4      ,          V P                  4      p\
        P                  W0P                  4      # )a  
Return permutation whose rank is ``other`` greater than current rank,
(mod the maximum rank for the set).

Examples
========

>>> from sympy.combinatorics import Partition
>>> a = Partition([1, 2], [3])
>>> a.rank
1
>>> (a + 1).rank
2
>>> (a + 100).rank
1
)r   r?   
RGS_unrankRGS_enumr+   r   from_rgsr*   )r@   otheroffsetresults   &&  r   __add__Partition.__add__   sV    " uU"V$TYY/0 II' !!&,,77r4   c                &    V P                  V) 4      # )z
Return permutation whose rank is ``other`` less than current rank,
(mod the maximum rank for the set).

Examples
========

>>> from sympy.combinatorics import Partition
>>> a = Partition([1, 2], [3])
>>> a.rank
1
>>> (a - 1).rank
0
>>> (a - 100).rank
1
)rN   r@   rK   s   &&r   __sub__Partition.__sub__   s    " ||UF##r4   c                V    V P                  4       \        V4      P                  4       8*  # )a  
Checks if a partition is less than or equal to
the other based on rank.

Examples
========

>>> from sympy.combinatorics import Partition
>>> a = Partition([1, 2], [3, 4, 5])
>>> b = Partition([1], [2, 3], [4], [5])
>>> a.rank, b.rank
(9, 34)
>>> a <= a
True
>>> a <= b
True
rA   r   rQ   s   &&r   __le__Partition.__le__   s"    $ }}'%."9"9";;;r4   c                V    V P                  4       \        V4      P                  4       8  # )z
Checks if a partition is less than the other.

Examples
========

>>> from sympy.combinatorics import Partition
>>> a = Partition([1, 2], [3, 4, 5])
>>> b = Partition([1], [2, 3], [4], [5])
>>> a.rank, b.rank
(9, 34)
>>> a < b
True
rU   rQ   s   &&r   __lt__Partition.__lt__   s"     }}!8!8!:::r4   c                    V P                   e   V P                   # \        V P                  4      V n         V P                   # )z
Gets the rank of a partition.

Examples
========

>>> from sympy.combinatorics import Partition
>>> a = Partition([1, 2], [3], [4, 5])
>>> a.rank
13
)_rankRGS_rankRGSr@   s   &r   r?   Partition.rank   s2     ::!::dhh'
zzr4   c           
        / pV P                   p\        V4       F  w  r4V F  pW1V&   K	  	  K  	  \        \        V UUu. uF  qf F  q3NK  	  K  	  upp\        R7       Uu. uF  q1V,          NK  	  up4      # u uppi u upi )aD  
Returns the "restricted growth string" of the partition.

Explanation
===========

The RGS is returned as a list of indices, L, where L[i] indicates
the block in which element i appears. For example, in a partition
of 3 elements (a, b, c) into 2 blocks ([c], [a, b]) the RGS is
[1, 1, 0]: "a" is in block 1, "b" is in block 1 and "c" is in block 0.

Examples
========

>>> from sympy.combinatorics import Partition
>>> a = Partition([1, 2], [3], [4, 5])
>>> a.members
(1, 2, 3, 4, 5)
>>> a.RGS
(0, 0, 1, 2, 2)
>>> a + 1
Partition({3}, {4}, {5}, {1, 2})
>>> _.RGS
(0, 0, 1, 2, 3)
r;   )r-   	enumerater)   r=   r	   )r@   rgsr-   ir   jrE   s   &      r   r^   Partition.RGS   s    6 NN	 +GAA  , f!-	11aQ1Q	-3C'E F 'E!ff 'E F G 	G-Fs   A9!A?c                   \        V4      \        V4      8w  d   \        R4      h\        V4      ^,           p\        V4       Uu. uF  p. NK  	  pp^ pV F)  pWT,          P	                  W&,          4       V^,          pK+  	  \
        ;QJ d    R V 4       F  '       d   K   RM	  RM! R V 4       4      '       g   \        R4      h\        V!  # u upi )a  
Creates a set partition from a restricted growth string.

Explanation
===========

The indices given in rgs are assumed to be the index
of the element as given in elements *as provided* (the
elements are not sorted by this routine). Block numbering
starts from 0. If any block was not referenced in ``rgs``
an error will be raised.

Examples
========

>>> from sympy.combinatorics import Partition
>>> Partition.from_rgs([0, 1, 2, 0, 1], list('abcde'))
Partition({c}, {a, d}, {b, e})
>>> Partition.from_rgs([0, 1, 2, 0, 1], list('cbead'))
Partition({e}, {a, c}, {b, d})
>>> a = Partition([1, 4], [2], [3, 5])
>>> Partition.from_rgs(a.RGS, a.members)
Partition({2}, {1, 4}, {3, 5})
z#mismatch in rgs and element lengthsc              3   $   "   T F  qx  K  	  R # 5ir    )r   rE   s   & r   r   %Partition.from_rgs.<locals>.<genexpr>/  s     (i1is   FTz(some blocks of the partition were empty.)r    r&   maxranger$   r%   r   )r@   rc   elementsmax_elemrd   r-   re   s   &&&    r   rJ   Partition.from_rgs  s    4 s8s8}$BCCs8a<!&x1AR	1AL,FA  s(i(sss(i(((GHH)$$ 2s   C)rD   r\   r   )__name__
__module____qualname____firstlineno____doc__r\   rD   r(   rA   propertyr-   rN   rR   rV   rY   r?   r^   classmethodrJ   __static_attributes____classdictcell____classdict__s   @r   r   r      s     
 EJ<|M6  80$&<(;"  "  G  GD #% #%r4   r   c                   t   a  ] tR tRt o RtRtRtRR ltR tR t	R t
]R 4       tR	 tR
 tRR ltR tRtV tR# )IntegerPartitioni4  a  
This class represents an integer partition.

Explanation
===========

In number theory and combinatorics, a partition of a positive integer,
``n``, also called an integer partition, is a way of writing ``n`` as a
list of positive integers that sum to n. Two partitions that differ only
in the order of summands are considered to be the same partition; if order
matters then the partitions are referred to as compositions. For example,
4 has five partitions: [4], [3, 1], [2, 2], [2, 1, 1], and [1, 1, 1, 1];
the compositions [1, 2, 1] and [1, 1, 2] are the same as partition
[2, 1, 1].

See Also
========

sympy.utilities.iterables.partitions,
sympy.utilities.iterables.multiset_partitions

References
==========

.. [1] https://en.wikipedia.org/wiki/Partition_%28number_theory%29
Nc                   Ve   Yr\        V\        \        34      '       dk   . p\        VP	                  4       RR7       F=  w  rEV'       g   K  \        V4      \        V4      rTVP                  V.V,          4       K?  	  \        V4      pM$\        \        \        \
        V4      RR7      4      pRpVf   \        V4      pRpM\        V4      pV'       g#   \        V4      V8w  d   \        RV,          4      h\        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      '       d   \        R4      h\        P                  ! V \        V4      \        V!  4      p\!        V4      Vn        W'n        V# )aP  
Generates a new IntegerPartition object from a list or dictionary.

Explanation
===========

The partition can be given as a list of positive integers or a
dictionary of (integer, multiplicity) items. If the partition is
preceded by an integer an error will be raised if the partition
does not sum to that given integer.

Examples
========

>>> from sympy.combinatorics.partitions import IntegerPartition
>>> a = IntegerPartition([5, 4, 3, 1, 1])
>>> a
IntegerPartition(14, (5, 4, 3, 1, 1))
>>> print(a)
[5, 4, 3, 1, 1]
>>> IntegerPartition({1:3, 2:1})
IntegerPartition(5, (2, 1, 1, 1))

If the value that the partition should sum to is given first, a check
will be made to see n error will be raised if there is a discrepancy:

>>> IntegerPartition(10, [5, 4, 3, 1])
Traceback (most recent call last):
...
ValueError: The partition is not valid

TreverseFzPartition did not add to %sc              3   *   "   T F	  q^8  x  K  	  R# 5i)   Nri   )r   rd   s   & r   r   +IntegerPartition.__new__.<locals>.<genexpr>  s     (i1uis   z-All integer summands must be greater than one)r   dictr   r=   itemsr   extendr)   r>   r'   r&   anyr   r(   r   r   r"   r-   integer)r,   r-   r   _kvsum_okr2   s   &&&     r   r(   IntegerPartition.__new__S  s(   B !*Yi$..Ay0$?ay&)1!Q	 @
 aIfS%;TJKI?)nGFWoG#i.G3:WDEE3(i(333(i(((LMMmmC!15)3DEY
r4   c                   \        \        4      pVP                  V P                  4       4       V P                  pV^.8X  d   \        V P                  ^/4      # VR,          ^8w  dH   WR,          ;;,          ^,          uu&   VR,          ^8X  d   ^V^&   M^;WR,          ^,
          &   V^&   MWR,          ;;,          ^,          uu&   V^,          VR,          ,           pVR,          p^ V^&   V'       dK   V^,          pW4,
          ^ 8  g   K   W;;,          W4,          ,          uu&   W1V,          V,          ,          pKR  \        V P                  V4      # )a:  Return the previous partition of the integer, n, in lexical order,
wrapping around to [1, ..., 1] if the partition is [n].

Examples
========

>>> from sympy.combinatorics.partitions import IntegerPartition
>>> p = IntegerPartition([4])
>>> print(p.prev_lex())
[3, 1]
>>> p.partition > p.prev_lex().partition
True
)r   intupdateas_dict_keysr|   r   )r@   dkeysleftnews   &    r   prev_lexIntegerPartition.prev_lex  s    	 zzA3;#T\\1$5668q=2hK1KBx1}!)**r(Q,!A$2hK1KQ4$r(?Dr(CAaDq:?Fdi'FcF3J&Da00r4   c                   \        \        4      pVP                  V P                  4       4       V P                  pVR,          pW0P
                  8X  d"   VP                  4        V P
                  V^&   EMtV^8X  d   W,          ^8  d2   W^,           ;;,          ^,          uu&   W;;,          ^,          uu&   EM/VR,          pW^,           ;;,          ^,          uu&   W,          ^,
          V,          V^&   ^ W&   MW,          ^8  d   \        V4      ^8X  d:   VP                  4        ^W^,           &   V P
                  V,
          ^,
          V^&   MV^,           pW;;,          ^,          uu&   W,          V,          V,
          V^&   ^ W&   M\VR,          pV^,           pW;;,          ^,          uu&   W,          V,          W,          V,          ,           V,
          p^ ;W&   W&   Wq^&   \        V P
                  V4      # )a6  Return the next partition of the integer, n, in lexical order,
wrapping around to [n] if the partition is [1, ..., 1].

Examples
========

>>> from sympy.combinatorics.partitions import IntegerPartition
>>> p = IntegerPartition([3, 1])
>>> print(p.next_lex())
[4]
>>> p.partition < p.next_lex().partition
True
r   r   )	r   r   r   r   r   r   clearr    r|   )r@   r   r<   aba1b1needs   &       r   next_lexIntegerPartition.next_lex  sw    	 jjGGGI<<AaD!Vtaxa%A	Ga%Aq!|!taxs8q=GGI A!eH<<!+a/AaDQBEQJE46B;AaDADGU
tAvQ+qt!a00r4   c                    V P                   fE   \        V P                  RR7      pV Uu. uF  q"^ ,          NK  	  upV n        \	        V4      V n         V P                   # u upi )a  Return the partition as a dictionary whose keys are the
partition integers and the values are the multiplicity of that
integer.

Examples
========

>>> from sympy.combinatorics.partitions import IntegerPartition
>>> IntegerPartition([1]*3 + [2] + [3]*4).as_dict()
{1: 3, 2: 1, 3: 4}
F)multiple)_dictr   r-   r   r   )r@   groupsgs   &  r   r   IntegerPartition.as_dict  sR     ::4>>E:F(./1A$$/DJfDJzz 0s   Ac                    ^p\        V P                  4      ^ .,           pV^ ,          p^ .V,          pV^ 8  d/   W2V,          8  d   WV^,
          &   V^,          pK#  V^,          pK5  V# )z
Computes the conjugate partition of itself.

Examples
========

>>> from sympy.combinatorics.partitions import IntegerPartition
>>> a = IntegerPartition([6, 3, 3, 2, 1])
>>> a.conjugate
[5, 4, 3, 1, 1, 1]
)r"   r-   )r@   re   temp_arrr   r   s   &    r   	conjugateIntegerPartition.conjugate  sd     '1#-QKCE!eqk/!a%QFAr4   c                |    \        \        V P                  4      4      \        \        VP                  4      4      8  # )a  Return True if self is less than other when the partition
is listed from smallest to biggest.

Examples
========

>>> from sympy.combinatorics.partitions import IntegerPartition
>>> a = IntegerPartition([3, 1])
>>> a < a
False
>>> b = a.next_lex()
>>> a < b
True
>>> a == b
False
r"   reversedr-   rQ   s   &&r   rY   IntegerPartition.__lt__  s+    " HT^^,-Xeoo5N0OOOr4   c                |    \        \        V P                  4      4      \        \        VP                  4      4      8*  # )zReturn True if self is less than other when the partition
is listed from smallest to biggest.

Examples
========

>>> from sympy.combinatorics.partitions import IntegerPartition
>>> a = IntegerPartition([4])
>>> a <= a
True
r   rQ   s   &&r   rV   IntegerPartition.__le__%  s+     HT^^,-hu6O1PPPr4   c                n    RP                  V P                   Uu. uF  q!V,          NK  	  up4      # u upi )z
Prints the ferrer diagram of a partition.

Examples
========

>>> from sympy.combinatorics.partitions import IntegerPartition
>>> print(IntegerPartition([1, 1, 5]).as_ferrers())
#####
#
#

)joinr-   )r@   charrd   s   && r   
as_ferrersIntegerPartition.as_ferrers3  s-     yy$..9.Qq&&.9::9s   2c                >    \        \        V P                  4      4      # r   )strr"   r-   r_   s   &r   __str__IntegerPartition.__str__B  s    4'((r4   )r   r   r   )#)rp   rq   rr   rs   rt   r   r   r(   r   r   r   ru   r   rY   rV   r   r   rw   rx   ry   s   @r   r|   r|   4  s\     6 EE<|#1J01d$  .P&Q;) )r4   r|   Nc                b   ^ RI Hp \        V 4      p V ^8  d   \        R4      hV! V4      p. pV ^ 8  d<   V! ^V 4      pV! ^W,          4      pVP	                  WV34       WV,          ,          p KB  VP                  RR7       \        V UUu. uF  w  rWV.V,          NK  	  upp4      pV# u uppi )a  
Generates a random integer partition summing to ``n`` as a list
of reverse-sorted integers.

Examples
========

>>> from sympy.combinatorics.partitions import random_integer_partition

For the following, a seed is given so a known value can be shown; in
practice, the seed would not be given.

>>> random_integer_partition(100, seed=[1, 1, 12, 1, 2, 1, 85, 1])
[85, 12, 2, 1]
>>> random_integer_partition(10, seed=[1, 2, 3, 1, 5, 1])
[5, 3, 1, 1]
>>> random_integer_partition(1)
[1]
)_randintzn must be a positive integerTr~   )sympy.core.randomr   r   r&   r$   sortr   )nseedr   randintr-   r   multms   &&      r   random_integer_partitionr   F  s    ( +q	A1u788tnGIq5AqMq!$!#	tVNN4N 95941!Q956I 6s   B+
c                ^   \        V ^,           4      p\        V ^,           4       F
  p^V^ V3&   K  	  \        ^V ^,           4       Fc  p\        V 4       FQ  pW0V,
          8:  d<   W1V^,
          V3,          ,          W^,
          V^,           3,          ,           WV3&   KK  ^ WV3&   KS  	  Ke  	  V# )a  
Computes the m + 1 generalized unrestricted growth strings
and returns them as rows in matrix.

Examples
========

>>> from sympy.combinatorics.partitions import RGS_generalized
>>> RGS_generalized(6)
Matrix([
[  1,   1,   1,  1,  1, 1, 1],
[  1,   2,   3,  4,  5, 6, 0],
[  2,   5,  10, 17, 26, 0, 0],
[  5,  15,  37, 77,  0, 0, 0],
[ 15,  52, 151,  0,  0, 0, 0],
[ 52, 203,   0,  0,  0, 0, 0],
[203,   0,   0,  0,  0, 0, 0]])
)r   rl   )r   r   rd   re   s   &   r   RGS_generalizedr   m  s    & 	a!eA1q5\!Q$  1a!e_qAEzAqk/A!eQUlO;Q$Q$	   Hr4   c                <    V ^8  d   ^ # V ^8X  d   ^# \        V 4      # )a%  
RGS_enum computes the total number of restricted growth strings
possible for a superset of size m.

Examples
========

>>> from sympy.combinatorics.partitions import RGS_enum
>>> from sympy.combinatorics import Partition
>>> RGS_enum(4)
15
>>> RGS_enum(5)
52
>>> RGS_enum(6)
203

We can check that the enumeration is correct by actually generating
the partitions. Here, the 15 partitions of 4 items are generated:

>>> a = Partition(list(range(4)))
>>> s = set()
>>> for i in range(20):
...     s.add(a)
...     a += 1
...
>>> assert len(s) == 15

r   )r   s   &r   rI   rI     s!    : 	
A
q&Awr4   c                   V^8  d   \        R4      hV ^ 8  g   \        V4      V 8:  d   \        R4      h^.V^,           ,          p^p\        V4      p\        ^V^,           4       Fb  pWAV,
          V3,          pW6,          pWp8:  d   V^,           W%&   W,          p V^,          pK@  \	        W,          ^,           4      W%&   W,          p Kd  	  VR,           Uu. uF  q^,
          NK  	  up# u upi )z
Gives the unranked restricted growth string for a given
superset size.

Examples
========

>>> from sympy.combinatorics.partitions import RGS_unrank
>>> RGS_unrank(14, 4)
[0, 1, 2, 3]
>>> RGS_unrank(0, 4)
[0, 0, 0, 0]
zThe superset size must be >= 1zInvalid arguments:r   NN)r&   rI   r   rl   r   )	r?   r   Lre   Drd   r   crxs	   &&       r   rH   rH     s     	1u9::ax8A;$&,--	
q1uA	AA1a!e_!eQhKS:q5ADJDFAtx!|$ADID  R5!5aEE5!!!s   C+c                    \        V 4      p^ p\        V4      p\        ^V4       FI  p\        W^,           R 4      p\        V ^ V 4      pW#WV^,           3,          W,          ,          ,          pKK  	  V# )z
Computes the rank of a restricted growth string.

Examples
========

>>> from sympy.combinatorics.partitions import RGS_rank, RGS_unrank
>>> RGS_rank([0, 1, 2, 1, 3])
42
>>> RGS_rank(RGS_unrank(4, 7))
4
N)r    r   rl   rk   )rc   rgs_sizer?   r   rd   r   r   s   &      r   r]   r]     sn     3xHD!A1hUHAaM!U(cf$$   Kr4   r   ) 
sympy.corer   r   r   r   sympy.core.numbersr   sympy.core.sortingr	   sympy.core.sympifyr
   %sympy.functions.combinatorial.numbersr   sympy.matricesr   sympy.sets.setsr   r   sympy.utilities.iterablesr   r   sympy.utilities.miscr   collectionsr   r   r|   r   r   rI   rH   r]   ri   r4   r   <module>r      sc    2 2 & / ' 6   , 4 ' $b%	 b%J	O)u O)d$N@"J "Fr4   