K i>6ddlmZddlmZGddZdZy) combinations)GrayCodec0eZdZdZdZdZdZdZdZdZ dZ dZ dZ dZ dZd Zd Zd Zd Zed ZedZedZedZedZedZedZedZedZedZedZedZ edZ!y)Subseta Represents a basic subset object. Explanation =========== We generate subsets using essentially two techniques, binary enumeration and lexicographic enumeration. The Subset class takes two arguments, the first one describes the initial subset to consider and the second describes the superset. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.next_binary().subset ['b'] >>> a.prev_binary().subset ['c'] Nct|t|kDr td|D] }||vstdj|tj |}||_||_|S)ax Default constructor. It takes the ``subset`` and its ``superset`` as its parameters. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.subset ['c', 'd'] >>> a.superset ['a', 'b', 'c', 'd'] >>> a.size 2 zRInvalid arguments have been provided. The superset must be larger than the subset.zFThe superset provided is invalid as it does not contain the element {})len ValueErrorformatobject__new___subset _superset)clssubsetsupersetelemobjs a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/combinatorics/subsets.pyr zSubset.__new__$s$ v;X &HI I LD8# ">>DfTlLL LnnS!    ct|tstS|j|jk(xr|j|jk(S)zReturn a boolean indicating whether a == b on the basis of whether both objects are of the class Subset and if the values of the subset and superset attributes are the same. ) isinstancerNotImplementedrr)selfothers r__eq__z Subset.__eq__Bs9 %(! !{{ell*Nt}}/NNrcBtj|j|j}t dj |d|zd|j zz}t|ddj|j d}tj|j|S)a This is a helper function. It iterates over the binary subsets by ``k`` steps. This variable can be both positive or negative. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.iterate_binary(-2).subset ['d'] >>> a = Subset(['a', 'b', 'c'], ['a', 'b', 'c', 'd']) >>> a.iterate_binary(2).subset [] See Also ======== next_binary, prev_binary N0) rbitlist_from_subsetrrintjoin superset_sizebinrjustsubset_from_bitlist)rkbin_listnbitss riterate_binaryzSubset.iterate_binaryKs,--dkk4==I "A & *a1C1C.C C1vabz 2 2C8))$-->>rc$|jdS)a Generates the next binary ordered subset. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.next_binary().subset ['b'] >>> a = Subset(['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd']) >>> a.next_binary().subset [] See Also ======== prev_binary, iterate_binary r,rs r next_binaryzSubset.next_binaryfs(""1%%rc$|jdS)a Generates the previous binary ordered subset. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset([], ['a', 'b', 'c', 'd']) >>> a.prev_binary().subset ['a', 'b', 'c', 'd'] >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.prev_binary().subset ['c'] See Also ======== next_binary, iterate_binary r/r0s r prev_binaryzSubset.prev_binary|s(""2&&rc(|jdz }tj|j|j}||vru|dz |vr|j |dz n|j ||dz }|dk\r||vr|dz }|dk\r||vr|dk\rR|j ||j |dzn,||vr|dk\r|dz }||vr|dk\r|j |dzg}|j}|D]}|j ||t||S)a Generates the next lexicographically ordered subset. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.next_lexicographic().subset ['d'] >>> a = Subset(['d'], ['a', 'b', 'c', 'd']) >>> a.next_lexicographic().subset [] See Also ======== prev_lexicographic r.rr$rsubset_indicesrrremoveappendriindicesret_set super_sets rnext_lexicographiczSubset.next_lexicographics!(    "'' T]]C <1uq1u%q!E1f'!1AA1f'!16NN1%NN1Q3'7"qAvE7"qAv NN1q5 !MM  )A NN9Q< ( )gy))rc|jdz }tj|j|j}|dk\r||vr|dz }|dk\r||vr|dk(s|dz |vr|j |nH|dk\r%|j ||j |dz |j |jdz g}|j}|D]}|j ||t||S)a Generates the previous lexicographically ordered subset. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset([], ['a', 'b', 'c', 'd']) >>> a.prev_lexicographic().subset ['d'] >>> a = Subset(['c','d'], ['a', 'b', 'c', 'd']) >>> a.prev_lexicographic().subset ['c'] See Also ======== next_lexicographic r.rr6r:s rprev_lexicographiczSubset.prev_lexicographics(    "'' T]]C1f')AA1f') 6QUg% NN1 Avq!q1u% NN4--1 2MM  )A NN9Q< ( )gy))rctj|j|j|z|jz}t j |j|S)a Helper function used for prev_gray and next_gray. It performs ``k`` step overs to get the respective Gray codes. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset([1, 2, 3], [1, 2, 3, 4]) >>> a.iterate_graycode(3).subset [1, 4] >>> a.iterate_graycode(-2).subset [1, 2, 4] See Also ======== next_gray, prev_gray )runrankr$ rank_gray cardinalityrr'r)rr( unranked_codes riterate_graycodezSubset.iterate_graycodesN(!(:(:(,(:d>N>N'NP ))$--*79 9rc$|jdS)a= Generates the next Gray code ordered subset. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset([1, 2, 3], [1, 2, 3, 4]) >>> a.next_gray().subset [1, 3] See Also ======== iterate_graycode, prev_gray r.rGr0s r next_grayzSubset.next_grays"$$Q''rc$|jdS)aJ Generates the previous Gray code ordered subset. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset([2, 3, 4], [1, 2, 3, 4, 5]) >>> a.prev_gray().subset [2, 3, 4, 5] See Also ======== iterate_graycode, next_gray r3rIr0s r prev_grayzSubset.prev_grays"$$R((rc|jHtdjtj |j |j d|_|jS)a Computes the binary ordered rank. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset([], ['a','b','c','d']) >>> a.rank_binary 0 >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.rank_binary 3 See Also ======== iterate_binary, unrank_binary rr) _rank_binaryr"r#rr!rrr0s r rank_binaryzSubset.rank_binary&sT*    $ #BGG**4;;+/==:%;<=!?D    rc|jIfdtj|j|j}||d|j |_|jS)aa Computes the lexicographic ranking of the subset. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.rank_lexicographic 14 >>> a = Subset([2, 4, 5], [1, 2, 3, 4, 5, 6]) >>> a.rank_lexicographic 43 c|gk(s||kDry||vr"|j|d|||dz|zSd||z dz z|||dz|zS)Nrr.r)r8)r subset_indexr;r*_ranklexs rrSz+Subset.rank_lexicographic.._ranklexRsj2%Q $ ''*xlAE1EEE1q519~|QUA(NNNrr) _rank_lexrr7rrr$)rr<rSs @rrank_lexicographiczSubset.rank_lexicographicAsS >> ! O++DKKGG%dGQ8J8JKDN~~rc|jOtj|j|j}t t ||j|_|jS)a Computes the Gray code ranking of the subset. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset(['c','d'], ['a','b','c','d']) >>> a.rank_gray 2 >>> a = Subset([2, 4, 5], [1, 2, 3, 4, 5, 6]) >>> a.rank_gray 27 See Also ======== iterate_graycode, unrank_gray )start)_rank_graycoderr!rrrr rank)rr+s rrDzSubset.rank_gray]sO*    &--dkk4==ID"*3t9D"A"F"FD """rc|jS)aU Gets the subset represented by the current instance. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.subset ['c', 'd'] See Also ======== superset, size, superset_size, cardinality )rr0s rrz Subset.subsetws$||rc,t|jS)a4 Gets the size of the subset. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.size 2 See Also ======== subset, superset, superset_size, cardinality )r rr0s rsizez Subset.sizes$4;;rc|jS)aK Gets the superset of the subset. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.superset ['a', 'b', 'c', 'd'] See Also ======== subset, size, superset_size, cardinality )rr0s rrzSubset.supersets$~~rc,t|jS)a9 Returns the size of the superset. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.superset_size 4 See Also ======== subset, superset, size, cardinality )r rr0s rr$zSubset.superset_sizes$4==!!rc d|jzS)aD Returns the number of all possible subsets. Examples ======== >>> from sympy.combinatorics import Subset >>> a = Subset(['c', 'd'], ['a', 'b', 'c', 'd']) >>> a.cardinality 16 See Also ======== subset, superset, size, superset_size r)r$r0s rrEzSubset.cardinalitys$4%%&&rct|t|k7r tdg}tt|D]}||dk(s |j||!t ||S)a/ Gets the subset defined by the bitlist. Examples ======== >>> from sympy.combinatorics import Subset >>> Subset.subset_from_bitlist(['a', 'b', 'c', 'd'], '0011').subset ['c', 'd'] See Also ======== bitlist_from_subset z$The sizes of the lists are not equal1)r r ranger9r)rr>bitlistr=r;s rr'zSubset.subset_from_bitlistsh" y>S\ )CD Ds7|$ -AqzS y|, -gy))rcdgt|z}t|tr |j}tj ||D]}d||< dj |S)a, Gets the bitlist corresponding to a subset. Examples ======== >>> from sympy.combinatorics import Subset >>> Subset.bitlist_from_subset(['c', 'd'], ['a', 'b', 'c', 'd']) '0011' See Also ======== subset_from_bitlist r rar)r rrrr7r#)rrrrcr;s rr!zSubset.bitlist_from_subsets["%#h-' ff %]]F&&vx8 AGAJ wwwrc|t|ddjt|d}tj ||S)a5 Gets the binary ordered subset of the specified rank. Examples ======== >>> from sympy.combinatorics import Subset >>> Subset.unrank_binary(4, ['a', 'b', 'c', 'd']).subset ['b'] See Also ======== iterate_binary, rank_binary rNr )r%r&r rr')rrYrr+s r unrank_binaryzSubset.unrank_binary s7"4y}""3x=#6))(D99rcltjt||}tj ||S)a{ Gets the Gray code ordered subset of the specified rank. Examples ======== >>> from sympy.combinatorics import Subset >>> Subset.unrank_gray(4, ['a', 'b', 'c']).subset ['a', 'b'] >>> Subset.unrank_gray(0, ['a', 'b', 'c']).subset [] See Also ======== iterate_graycode, rank_gray )rrCr rr')rrYrgraycode_bitlists r unrank_grayzSubset.unrank_gray s-&$??3x=$?))(4DEErc||}}t|}i}t|D]$\}}||vs |||<|j||r$ngS|D cgc]} ||  c} Scc} w)aReturn indices of subset in superset in a list; the list is empty if all elements of ``subset`` are not in ``superset``. Examples ======== >>> from sympy.combinatorics import Subset >>> superset = [1, 3, 2, 5, 4] >>> Subset.subset_indices([3, 2, 1], superset) [1, 2, 0] >>> Subset.subset_indices([1, 6], superset) [] >>> Subset.subset_indices([], superset) [] )set enumerater8) rrrabsbdr;aibis rr7zSubset.subset_indices6sr$1 V q\ EArRx" "   I !""""""s A)"__name__ __module__ __qualname____doc__rNrTrXrrr rr,r1r4r?rArGrJrLpropertyrOrUrDrr\rr$rE classmethodr'r!rfrir7rrrrs].LINGI<O?6&,',+*Z&*P92(&)&!!46##2&  &&""&''&**0  .::&FF*##rrct||S)a Finds the subsets of size ``k`` in lexicographic order. This uses the itertools generator. Examples ======== >>> from sympy.combinatorics.subsets import ksubsets >>> list(ksubsets([1, 2, 3], 2)) [(1, 2), (1, 3), (2, 3)] >>> list(ksubsets([1, 2, 3, 4, 5], 2)) [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)] See Also ======== Subset r)rr(s rksubsetsr{Vs* ! $$rN) itertoolsrsympy.combinatorics.graycoderrr{ryrrr~s"1M #M #`%r