K iVddlZddlmZddlmZddlmZddlmZddl m Z ddl m Z ddl mZdd l mZdd lmZdd lmZdd lmZdd lmZddlmZmZmZmZmZmZddlm Z ddl!m"Z"ddl#m$Z$dZ%dZ&dZ'dZ(dZ)dZ*Gdde+Z,Gdde Z-dZ.e-Z/e/j`Z0Gdde Z1e$e-e-dZ2y) N) defaultdict)Iterable)reduce)global_parameters)Atom)Expr) int_valued)Integer)_sympify)zeros)lcmsrepr)flatten has_varietyminlexhas_dupsruns is_sequenceas_int)ifac)dispatchc2|Dcgc]}|| c}Scc}w)aM Return the product b*a; input and output are array forms. The ith value is a[b[i]]. Examples ======== >>> from sympy.combinatorics.permutations import _af_rmul, Permutation >>> a, b = [1, 0, 2], [0, 2, 1] >>> _af_rmul(a, b) [1, 2, 0] >>> [a[b[i]] for i in range(3)] [1, 2, 0] This handles the operands in reverse order compared to the ``*`` operator: >>> a = Permutation(a) >>> b = Permutation(b) >>> list(a*b) [2, 0, 1] >>> [b(a(i)) for i in range(3)] [2, 0, 1] See Also ======== rmul, _af_rmuln )abis f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/combinatorics/permutations.py_af_rmulr s< QAaD  s c B|}t|}|dk(r|\}}}|Dcgc] }||| c}S|dk(r |\}}}}|Dcgc] }||||c}S|dk(r$|\}}}}}|Dcgc]}|||||c}S|dk(r(|\}}}}}} | Dcgc]}||||||c}S|dk(r,|\}}}}}} } | Dcgc]}|||||| |c}S|dk(r0|\}}}}}} } } | Dcgc]}|||||| | |c}S|dk(r|dd d S|d k(r|\}} | Dcgc]}|| c}S|dk(r td t|d |d z}t||d zd }|Dcgc]}|| c}Scc}wcc}wcc}wcc}wcc}wcc}wcc}wcc}w) aV Given [a, b, c, ...] return the product of ...*c*b*a using array forms. The ith value is a[b[c[i]]]. Examples ======== >>> from sympy.combinatorics.permutations import _af_rmul, Permutation >>> a, b = [1, 0, 2], [0, 2, 1] >>> _af_rmul(a, b) [1, 2, 0] >>> [a[b[i]] for i in range(3)] [1, 2, 0] This handles the operands in reverse order compared to the ``*`` operator: >>> a = Permutation(a); b = Permutation(b) >>> list(a*b) [2, 0, 1] >>> [b(a(i)) for i in range(3)] [2, 0, 1] See Also ======== rmul, _af_rmul rNzString must not be empty)len ValueError _af_rmuln) abcrmp0p1p2rp3p4p5p6p7rs rr,r,6s-: A AAAv B#%&a2a5 &&AvBB')*!2be9 **AvBB+-.a2bAi=!..Av!"BBB/12!2bBqEm$%22Av%&"BBB356a2bBr!uI'()66Av)*&BBBB79:!2bBr"Q%yM*+,-::AvtAwAv1 !  Av344 Aeq!tH B AadeH B aBqE 3'+/37; ! s.E9E>%FF=F /F+ F* Fct|}dg|z}d}t|D]3}||dk(s |dz }d||<|}|||k7s!||}d||<|||k7r5||z dzS)a Computes the parity of a permutation in array form. Explanation =========== The parity of a permutation reflects the parity of the number of inversions in the permutation, i.e., the number of pairs of x and y such that x > y but p[x] < p[y]. Examples ======== >>> from sympy.combinatorics.permutations import _af_parity >>> _af_parity([0, 1, 2, 3]) 0 >>> _af_parity([3, 2, 0, 1]) 1 See Also ======== Permutation rr(r))r*range)pinrcjrs r _af_parityr=ss2 BA aA A 1X Q419 FAAaDAQ%1*qE!Q%1*  EQ;cTdgt|z}t|D] \}}|||< |S)aP Finds the inverse, ~A, of a permutation, A, given in array form. Examples ======== >>> from sympy.combinatorics.permutations import _af_invert, _af_rmul >>> A = [1, 2, 0, 3] >>> _af_invert(A) [2, 0, 1, 3] >>> _af_rmul(_, A) [0, 1, 2, 3] See Also ======== Permutation, __invert__ r)r* enumerate)rinv_formrais r _af_invertrCs8&sSV|H12  Or>c~|dk(rttt|S|dkrtt || S|dk(r|ddS|dk(r|Dcgc]}|| }}|S|dk(r|Dcgc] }||| }}|S|dk(r|Dcgc] }||||}}|Sttt|} |dzr|Dcgc]}|| }}|dz}|s |S|dzdk(r|Dcgc] }||||}}|dz}n|dzdk(r|Dcgc]}|| }}|dz}gcc}wcc}wcc}wcc}wcc}wcc}w)a4 Routine for finding powers of a permutation. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy.combinatorics.permutations import _af_pow >>> p = Permutation([2, 0, 3, 1]) >>> p.order() 4 >>> _af_pow(p._array_form, 4) [0, 1, 2, 3] rr(Nr)r"r#)listr8r*_af_powrC)rr:rrs rrFrFs AvE#a&M""1uz!}qb))Avt a aQqT  ( H' a QqtW  $ H# a!" #AQq1wZ # # H s1v 1u#$%aQqT%%Q H 1uz)*+AQq1wZ++FQ!#$%aQqT%%F  # & ,&s$ D!*D&D+ D0'D5 D:c\tfdttdz D S)a7 Checks if the two permutations with array forms given by ``a`` and ``b`` commute. Examples ======== >>> from sympy.combinatorics.permutations import _af_commutes_with >>> _af_commutes_with([1, 2, 0], [0, 2, 1]) False See Also ======== Permutation, commutes_with c3@K|]}||k7ywNr.0rrrs r z$_af_commutes_with..s&A!1QqT7a!g%Asr()anyr8r*)rrs``r_af_commutes_withrNs&"AuSVaZ/@AA AAr>cReZdZdZdZdZdZd dZdZdZ d Z e d Z d Z y) Cyclea7 Wrapper around dict which provides the functionality of a disjoint cycle. Explanation =========== A cycle shows the rule to use to move subsets of elements to obtain a permutation. The Cycle class is more flexible than Permutation in that 1) all elements need not be present in order to investigate how multiple cycles act in sequence and 2) it can contain singletons: >>> from sympy.combinatorics.permutations import Perm, Cycle A Cycle will automatically parse a cycle given as a tuple on the rhs: >>> Cycle(1, 2)(2, 3) (1 3 2) The identity cycle, Cycle(), can be used to start a product: >>> Cycle()(1, 2)(2, 3) (1 3 2) The array form of a Cycle can be obtained by calling the list method (or passing it to the list function) and all elements from 0 will be shown: >>> a = Cycle(1, 2) >>> a.list() [0, 2, 1] >>> list(a) [0, 2, 1] If a larger (or smaller) range is desired use the list method and provide the desired size -- but the Cycle cannot be truncated to a size smaller than the largest element that is out of place: >>> b = Cycle(2, 4)(1, 2)(3, 1, 4)(1, 3) >>> b.list() [0, 2, 1, 3, 4] >>> b.list(b.size + 1) [0, 2, 1, 3, 4, 5] >>> b.list(-1) [0, 2, 1] Singletons are not shown when printing with one exception: the largest element is always shown -- as a singleton if necessary: >>> Cycle(1, 4, 10)(4, 5) (1 5 4 10) >>> Cycle(1, 2)(4)(5)(10) (1 2)(10) The array form can be used to instantiate a Permutation so other properties of the permutation can be investigated: >>> Perm(Cycle(1, 2)(3, 4).list()).transpositions() [(1, 2), (3, 4)] Notes ===== The underlying structure of the Cycle is a dictionary and although the __iter__ method has been redefined to give the array form of the cycle, the underlying dictionary items are still available with the such methods as items(): >>> list(Cycle(1, 2).items()) [(1, 2), (2, 1)] See Also ======== Permutation ct|S)z)Enter arg into dictionary and return arg.r)selfargs r __missing__zCycle.__missing__@s c{r>c#@K|jEd{y7wrI)rErRs r__iter__zCycle.__iter__Ds99;s c t|}tt|j|jDcgc] }||| c}D] \}}|||< |Scc}w)a Return product of cycles processed from R to L. Examples ======== >>> from sympy.combinatorics import Cycle >>> Cycle(1, 2)(2, 3) (1 3 2) An instance of a Cycle will automatically parse list-like objects and Permutations that are on the right. It is more flexible than the Permutation in that all elements need not be present: >>> a = Cycle(1, 2) >>> a(2, 3) (1 3 2) >>> a(2, 3)(4, 5) (1 3 2)(4 5) )rPziprEkeys)rRotherrvkvs r__call__zCycle.__call__Gs_,E]TYY[)+MABtAwK+MN DAqBqE  ,NsA Nc|s | td|Dt|jDcgc] }|||k7s |c}dgz}t||dz}n |j}t |Dcgc]}|| c}Scc}wcc}w)aReturn the cycles as an explicit list starting from 0 up to the greater of the largest value in the cycles and size. Truncation of trailing unmoved items will occur when size is less than the maximum element in the cycle; if this is desired, setting ``size=-1`` will guarantee such trimming. Examples ======== >>> from sympy.combinatorics import Cycle >>> p = Cycle(2, 3)(4, 5) >>> p.list() [0, 1, 3, 2, 5, 4] >>> p.list(10) [0, 1, 3, 2, 5, 4, 6, 7, 8, 9] Passing a length too small will trim trailing, unchanged elements in the permutation: >>> Cycle(2, 4)(1, 2, 4).list(-1) [0, 2, 1] must give size for empty Cyclerr()r+maxrZsizer8)rRrcrbigs rrEz Cycle.listbs0 => >  $))+>QaAq>!DECtS1W%D99D!&t-AQ-- ?.s A>A>/ Bc|syt|j}djd|D}|jdz t fd|Ds|dzz }d|zS)aWe want it to print as a Cycle, not as a dict. Examples ======== >>> from sympy.combinatorics import Cycle >>> Cycle(1, 2) (1 2) >>> print(_) (1 2) >>> list(Cycle(1, 2).items()) [(1, 2), (2, 1)] zCycle()c3DK|]}tt|ywrIstrtuplerKr;s rrLz!Cycle.__repr__..2aCaM2 r(c36K|]}|D] }|k( ywrIrrKr;rrds rrLz!Cycle.__repr__..!7Q718787(%s)zCycle%s) Permutation cyclic_formjoinrcrMrRcyclessrds @r__repr__zCycle.__repr__sbT".. GG262 2ii!m7V77 # A1}r>c|syt|j}djd|D}|jdz t fd|Ds|dzz }|j dd}|S)a We want it to be printed in a Cycle notation with no comma in-between. Examples ======== >>> from sympy.combinatorics import Cycle >>> Cycle(1, 2) (1 2) >>> Cycle(1, 2, 4)(5, 6) (1 2 4)(5 6) z()rfc3DK|]}tt|ywrIrhrks rrLz Cycle.__str__..rlrmr(c36K|]}|D] }|k( ywrIrros rrLz Cycle.__str__..rprqrr,)rsrtrurcrMreplacervs @r__str__z Cycle.__str__smT".. GG262 2ii!m7V77 # A IIc2 r>c|syt|dk(rpt|dtr)|djD]}|j ||yt|dt r!|dj D] \}}|||< y|Dcgc] }t|}}td|Dr tdt|r tdtt| dD]}||dz|||<ycc}w)zLoad up a Cycle instance with the values for the cycle. Examples ======== >>> from sympy.combinatorics import Cycle >>> Cycle(1, 2, 6) (1 2 6) Nr(rc3&K|] }|dk yw)rNr)rKrs rrLz!Cycle.__init__..s#q1u#sz-negative integers are not allowed in a cycle.z'All elements must be unique in a cycle.) r* isinstancersrtupdaterPitemsrrMr+rr8)rRargsr;r]r^rrs r__init__zCycle.__init__s  t9>$q';/a,,*AKKa)*DGU+ GMMO DAqDG #'(aq (( #d# #LM M D>FG GD z1% (A QKDaM ( )sC7c@|syt|jdzSNrr()rbrZrVs rrcz Cycle.sizes499;!##r>ct|SrI)rPrVs rcopyz Cycle.copy T{r>rI)__name__ __module__ __qualname____doc__rTrWr_rEryrrpropertyrcrrr>rrPrPsFJV6.B..(<$$ r>rPceZdZdZdZdZdZdZdZdZ dddZ e fdZ dZ dZd Zed ZdLd Zed Zed ZedZdZdZdZedZe dZdZdZdZdZdZ dZ!dZ"dZ#e dLdZ$dZ%dZ&dZ'd Z(d!Z)d"Z*d#Z+e d$Z,dLd%Z-d&Z.d'Z/ed(Z0d)Z1ed*Z2ed+Z3ed,Z4ed-Z5ed.Z6ed/Z7d0Z8d1Z9d2e:fd3Z;d2e:fd4Zd7Z?d8Z@d9ZAed:ZBed;ZCd<ZDd=ZEd>ZFd?ZGe d@ZHdAZIdBZJdCZKdDZLdEZMdFZNe dMdGZOe dHZPe dIZQe dJZRdKZSdZTxZUS)Nrsa6 A permutation, alternatively known as an 'arrangement number' or 'ordering' is an arrangement of the elements of an ordered list into a one-to-one mapping with itself. The permutation of a given arrangement is given by indicating the positions of the elements after re-arrangement [2]_. For example, if one started with elements ``[x, y, a, b]`` (in that order) and they were reordered as ``[x, y, b, a]`` then the permutation would be ``[0, 1, 3, 2]``. Notice that (in SymPy) the first element is always referred to as 0 and the permutation uses the indices of the elements in the original ordering, not the elements ``(a, b, ...)`` themselves. >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) Permutations Notation ===================== Permutations are commonly represented in disjoint cycle or array forms. Array Notation and 2-line Form ------------------------------------ In the 2-line form, the elements and their final positions are shown as a matrix with 2 rows: [0 1 2 ... n-1] [p(0) p(1) p(2) ... p(n-1)] Since the first line is always ``range(n)``, where n is the size of p, it is sufficient to represent the permutation by the second line, referred to as the "array form" of the permutation. This is entered in brackets as the argument to the Permutation class: >>> p = Permutation([0, 2, 1]); p Permutation([0, 2, 1]) Given i in range(p.size), the permutation maps i to i^p >>> [i^p for i in range(p.size)] [0, 2, 1] The composite of two permutations p*q means first apply p, then q, so i^(p*q) = (i^p)^q which is i^p^q according to Python precedence rules: >>> q = Permutation([2, 1, 0]) >>> [i^p^q for i in range(3)] [2, 0, 1] >>> [i^(p*q) for i in range(3)] [2, 0, 1] One can use also the notation p(i) = i^p, but then the composition rule is (p*q)(i) = q(p(i)), not p(q(i)): >>> [(p*q)(i) for i in range(p.size)] [2, 0, 1] >>> [q(p(i)) for i in range(p.size)] [2, 0, 1] >>> [p(q(i)) for i in range(p.size)] [1, 2, 0] Disjoint Cycle Notation ----------------------- In disjoint cycle notation, only the elements that have shifted are indicated. For example, [1, 3, 2, 0] can be represented as (0, 1, 3)(2). This can be understood from the 2 line format of the given permutation. In the 2-line form, [0 1 2 3] [1 3 2 0] The element in the 0th position is 1, so 0 -> 1. The element in the 1st position is three, so 1 -> 3. And the element in the third position is again 0, so 3 -> 0. Thus, 0 -> 1 -> 3 -> 0, and 2 -> 2. Thus, this can be represented as 2 cycles: (0, 1, 3)(2). In common notation, singular cycles are not explicitly written as they can be inferred implicitly. Only the relative ordering of elements in a cycle matter: >>> Permutation(1,2,3) == Permutation(2,3,1) == Permutation(3,1,2) True The disjoint cycle notation is convenient when representing permutations that have several cycles in them: >>> Permutation(1, 2)(3, 5) == Permutation([[1, 2], [3, 5]]) True It also provides some economy in entry when computing products of permutations that are written in disjoint cycle notation: >>> Permutation(1, 2)(1, 3)(2, 3) Permutation([0, 3, 2, 1]) >>> _ == Permutation([[1, 2]])*Permutation([[1, 3]])*Permutation([[2, 3]]) True Caution: when the cycles have common elements between them then the order in which the permutations are applied matters. This module applies the permutations from *left to right*. >>> Permutation(1, 2)(2, 3) == Permutation([(1, 2), (2, 3)]) True >>> Permutation(1, 2)(2, 3).list() [0, 3, 1, 2] In the above case, (1,2) is computed before (2,3). As 0 -> 0, 0 -> 0, element in position 0 is 0. As 1 -> 2, 2 -> 3, element in position 1 is 3. As 2 -> 1, 1 -> 1, element in position 2 is 1. As 3 -> 3, 3 -> 2, element in position 3 is 2. If the first and second elements had been swapped first, followed by the swapping of the second and third, the result would have been [0, 2, 3, 1]. If, you want to apply the cycles in the conventional right to left order, call the function with arguments in reverse order as demonstrated below: >>> Permutation([(1, 2), (2, 3)][::-1]).list() [0, 2, 3, 1] Entering a singleton in a permutation is a way to indicate the size of the permutation. The ``size`` keyword can also be used. Array-form entry: >>> Permutation([[1, 2], [9]]) Permutation([0, 2, 1], size=10) >>> Permutation([[1, 2]], size=10) Permutation([0, 2, 1], size=10) Cyclic-form entry: >>> Permutation(1, 2, size=10) Permutation([0, 2, 1], size=10) >>> Permutation(9)(1, 2) Permutation([0, 2, 1], size=10) Caution: no singleton containing an element larger than the largest in any previous cycle can be entered. This is an important difference in how Permutation and Cycle handle the ``__call__`` syntax. A singleton argument at the start of a Permutation performs instantiation of the Permutation and is permitted: >>> Permutation(5) Permutation([], size=6) A singleton entered after instantiation is a call to the permutation -- a function call -- and if the argument is out of range it will trigger an error. For this reason, it is better to start the cycle with the singleton: The following fails because there is no element 3: >>> Permutation(1, 2)(3) Traceback (most recent call last): ... IndexError: list index out of range This is ok: only the call to an out of range singleton is prohibited; otherwise the permutation autosizes: >>> Permutation(3)(1, 2) Permutation([0, 2, 1, 3]) >>> Permutation(1, 2)(3, 4) == Permutation(3, 4)(1, 2) True Equality testing ---------------- The array forms must be the same in order for permutations to be equal: >>> Permutation([1, 0, 2, 3]) == Permutation([1, 0]) False Identity Permutation -------------------- The identity permutation is a permutation in which no element is out of place. It can be entered in a variety of ways. All the following create an identity permutation of size 4: >>> I = Permutation([0, 1, 2, 3]) >>> all(p == I for p in [ ... Permutation(3), ... Permutation(range(4)), ... Permutation([], size=4), ... Permutation(size=4)]) True Watch out for entering the range *inside* a set of brackets (which is cycle notation): >>> I == Permutation([range(4)]) False Permutation Printing ==================== There are a few things to note about how Permutations are printed. .. deprecated:: 1.6 Configuring Permutation printing by setting ``Permutation.print_cyclic`` is deprecated. Users should use the ``perm_cyclic`` flag to the printers, as described below. 1) If you prefer one form (array or cycle) over another, you can set ``init_printing`` with the ``perm_cyclic`` flag. >>> from sympy import init_printing >>> p = Permutation(1, 2)(4, 5)(3, 4) >>> p Permutation([0, 2, 1, 4, 5, 3]) >>> init_printing(perm_cyclic=True, pretty_print=False) >>> p (1 2)(3 4 5) 2) Regardless of the setting, a list of elements in the array for cyclic form can be obtained and either of those can be copied and supplied as the argument to Permutation: >>> p.array_form [0, 2, 1, 4, 5, 3] >>> p.cyclic_form [[1, 2], [3, 4, 5]] >>> Permutation(_) == p True 3) Printing is economical in that as little as possible is printed while retaining all information about the size of the permutation: >>> init_printing(perm_cyclic=False, pretty_print=False) >>> Permutation([1, 0, 2, 3]) Permutation([1, 0, 2, 3]) >>> Permutation([1, 0, 2, 3], size=20) Permutation([1, 0], size=20) >>> Permutation([1, 0, 2, 4, 3, 5, 6], size=20) Permutation([1, 0, 2, 4, 3], size=20) >>> p = Permutation([1, 0, 2, 3]) >>> init_printing(perm_cyclic=True, pretty_print=False) >>> p (3)(0 1) >>> init_printing(perm_cyclic=False, pretty_print=False) The 2 was not printed but it is still there as can be seen with the array_form and size methods: >>> p.array_form [1, 0, 2, 3] >>> p.size 4 Short introduction to other methods =================================== The permutation can act as a bijective function, telling what element is located at a given position >>> q = Permutation([5, 2, 3, 4, 1, 0]) >>> q.array_form[1] # the hard way 2 >>> q(1) # the easy way 2 >>> {i: q(i) for i in range(q.size)} # showing the bijection {0: 5, 1: 2, 2: 3, 3: 4, 4: 1, 5: 0} The full cyclic form (including singletons) can be obtained: >>> p.full_cyclic_form [[0, 1], [2], [3]] Any permutation can be factored into transpositions of pairs of elements: >>> Permutation([[1, 2], [3, 4, 5]]).transpositions() [(1, 2), (3, 5), (3, 4)] >>> Permutation.rmul(*[Permutation([ti], size=6) for ti in _]).cyclic_form [[1, 2], [3, 4, 5]] The number of permutations on a set of n elements is given by n! and is called the cardinality. >>> p.size 4 >>> p.cardinality 24 A given permutation has a rank among all the possible permutations of the same elements, but what that rank is depends on how the permutations are enumerated. (There are a number of different methods of doing so.) The lexicographic rank is given by the rank method and this rank is used to increment a permutation with addition/subtraction: >>> p.rank() 6 >>> p + 1 Permutation([1, 0, 3, 2]) >>> p.next_lex() Permutation([1, 0, 3, 2]) >>> _.rank() 7 >>> p.unrank_lex(p.size, rank=7) Permutation([1, 0, 3, 2]) The product of two permutations p and q is defined as their composition as functions, (p*q)(i) = q(p(i)) [6]_. >>> p = Permutation([1, 0, 2, 3]) >>> q = Permutation([2, 3, 1, 0]) >>> list(q*p) [2, 3, 0, 1] >>> list(p*q) [3, 2, 1, 0] >>> [q(p(i)) for i in range(p.size)] [3, 2, 1, 0] The permutation can be 'applied' to any list-like object, not only Permutations: >>> p(['zero', 'one', 'four', 'two']) ['one', 'zero', 'four', 'two'] >>> p('zo42') ['o', 'z', '4', '2'] If you have a list of arbitrary elements, the corresponding permutation can be found with the from_sequence method: >>> Permutation.from_sequence('SymPy') Permutation([1, 3, 2, 0, 4]) Checking if a Permutation is contained in a Group ================================================= Generally if you have a group of permutations G on n symbols, and you're checking if a permutation on less than n symbols is part of that group, the check will fail. Here is an example for n=5 and we check if the cycle (1,2,3) is in G: >>> from sympy import init_printing >>> init_printing(perm_cyclic=True, pretty_print=False) >>> from sympy.combinatorics import Cycle, Permutation >>> from sympy.combinatorics.perm_groups import PermutationGroup >>> G = PermutationGroup(Cycle(2, 3)(4, 5), Cycle(1, 2, 3, 4, 5)) >>> p1 = Permutation(Cycle(2, 5, 3)) >>> p2 = Permutation(Cycle(1, 2, 3)) >>> a1 = Permutation(Cycle(1, 2, 3).list(6)) >>> a2 = Permutation(Cycle(1, 2, 3)(5)) >>> a3 = Permutation(Cycle(1, 2, 3),size=6) >>> for p in [p1,p2,a1,a2,a3]: p, G.contains(p) ((2 5 3), True) ((1 2 3), False) ((5)(1 2 3), True) ((5)(1 2 3), True) ((5)(1 2 3), True) The check for p2 above will fail. Checking if p1 is in G works because SymPy knows G is a group on 5 symbols, and p1 is also on 5 symbols (its largest element is 5). For ``a1``, the ``.list(6)`` call will extend the permutation to 5 symbols, so the test will work as well. In the case of ``a2`` the permutation is being extended to 5 symbols by using a singleton, and in the case of ``a3`` it's extended through the constructor argument ``size=6``. There is another way to do this, which is to tell the ``contains`` method that the number of symbols the group is on does not need to match perfectly the number of symbols for the permutation: >>> G.contains(p2,strict=False) True This can be via the ``strict`` argument to the ``contains`` method, and SymPy will try to extend the permutation on its own and then perform the containment check. See Also ======== Cycle References ========== .. [1] Skiena, S. 'Permutations.' 1.1 in Implementing Discrete Mathematics Combinatorics and Graph Theory with Mathematica. Reading, MA: Addison-Wesley, pp. 3-16, 1990. .. [2] Knuth, D. E. The Art of Computer Programming, Vol. 4: Combinatorial Algorithms, 1st ed. Reading, MA: Addison-Wesley, 2011. .. [3] Wendy Myrvold and Frank Ruskey. 2001. Ranking and unranking permutations in linear time. Inf. Process. Lett. 79, 6 (September 2001), 281-284. DOI=10.1016/S0020-0190(01)00141-7 .. [4] D. L. Kreher, D. R. Stinson 'Combinatorial Algorithms' CRC Press, 1999 .. [5] Graham, R. L.; Knuth, D. E.; and Patashnik, O. Concrete Mathematics: A Foundation for Computer Science, 2nd ed. Reading, MA: Addison-Wesley, 1994. .. [6] https://en.wikipedia.org/w/index.php?oldid=499948155#Product_and_inverse .. [7] https://en.wikipedia.org/wiki/Lehmer_code TNrcc | t|}d}|s'|jtt|xsdSt |dkDr&|jt |j|St |dk(r|d}t ||r'|||jk(r|S||j|St |t r |j|j|St|s>||dz|kDrtd|z|jtt|dzStd|Drd}nd}|s tdt|d}|xrt|d}|r)|Dcgc]}|Dcgc] }t|c}}}}n|Dcgc] }t|}}t|} t| r |s td t| } |s]| ttt | k7rtd t| z|$| r"t| dz|kDrtd |dz z|r't }|D]} || } |j} n t|} |r;|t | kDr-| j!ttt | ||j| Scc}wcc}}wcc}w) a Constructor for the Permutation object from a list or a list of lists in which all elements of the permutation may appear only once. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) Permutations entered in array-form are left unaltered: >>> Permutation([0, 2, 1]) Permutation([0, 2, 1]) Permutations entered in cyclic form are converted to array form; singletons need not be entered, but can be entered to indicate the largest element: >>> Permutation([[4, 5, 6], [0, 1]]) Permutation([1, 0, 2, 3, 5, 6, 4]) >>> Permutation([[4, 5, 6], [0, 1], [19]]) Permutation([1, 0, 2, 3, 5, 6, 4], size=20) All manipulation of permutations assumes that the smallest element is 0 (in keeping with 0-based indexing in Python) so if the 0 is missing when entering a permutation in array form, an error will be raised: >>> Permutation([2, 1]) Traceback (most recent call last): ... ValueError: Integers 0 through 2 must be present. If a permutation is entered in cyclic form, it can be entered without singletons and the ``size`` specified so those values can be filled in, otherwise the array form will only extend to the maximum value in the cycles: >>> Permutation([[1, 4], [3, 5, 2]], size=10) Permutation([0, 4, 3, 5, 1, 2], size=10) >>> _.array_form [0, 4, 3, 5, 1, 2, 6, 7, 8, 9] Trr(rz size is too small when max is %sc32K|]}t|ywrI)r)rKrBs rrLz&Permutation.__new__..s7r;r?7sFzSPermutation argument must be a list of ints, a list of lists, Permutation or Cycle.zthere were repeated elements.z&Integers 0 through %s must be present.z max element should not exceed %s)int_af_newrEr8r*rPrrc array_formrr+rrrsetrbextend) clsrcrkwargsokris_cycler;rtempciaforms r__new__zPermutation.__new__s^  t9D;;tE$)!$456 6 Y];;ud|0067 7 t9>QA!S!<4166>H1<#A $%G!%KLL{{4a!e #5667Q77BFG G DG}0KQ0 156AQ'SV'6D6$()qCF)D)t} D>(<= =4ys5T+,, !ID "DSY]T-A !Ctax!PQQ A rF FFHEJE D3u:% LLeCJ56 7{{5!!I(6)s7 KK KK KcTt||}||_t||_|S)aA method to produce a Permutation object from a list; the list is bound to the _array_form attribute, so it must not be modified; this method is meant for internal use only; the list ``a`` is supposed to be generated as a temporary value in a method, so p = Perm._af_new(a) is the only object to hold a reference to ``a``:: Examples ======== >>> from sympy.combinatorics.permutations import Perm >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> a = [2, 1, 3, 0] >>> p = Perm._af_new(a) >>> p Permutation([2, 1, 3, 0]) )superr _array_formr*_size)rpermp __class__s rrzPermutation._af_news** GOC  d)r>c8|j|jSrI)rrrVs rrzPermutation.copys~~doo..r>c|jfSrIrrVs r__getnewargs__zPermutation.__getnewargs__!s!!r>c,t|jSrI)rjrrVs r_hashable_contentzPermutation._hashable_content$sT__%%r>c |jddS)a Return a copy of the attribute _array_form Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([[2, 0], [3, 1]]) >>> p.array_form [2, 3, 0, 1] >>> Permutation([[2, 0, 3, 1]]).array_form [3, 2, 0, 1] >>> Permutation([2, 0, 3, 1]).array_form [2, 0, 3, 1] >>> Permutation([[1, 2], [4, 5]]).array_form [0, 2, 1, 3, 5, 4] N)rrVs rrzPermutation.array_form)s$""r>c&|s | td|j}|s||jkDr0|jt t |j||S|jdz }|r#|d|k7r |S|j |dz}|r#|S)aReturn the permutation as an explicit list, possibly trimming unmoved elements if size is less than the maximum element in the permutation; if this is desired, setting ``size=-1`` will guarantee such trimming. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation(2, 3)(4, 5) >>> p.list() [0, 1, 3, 2, 5, 4] >>> p.list(10) [0, 1, 3, 2, 5, 4, 6, 7, 8, 9] Passing a length too small will trim trailing, unchanged elements in the permutation: >>> Permutation(2, 4)(1, 2, 4).list(-1) [0, 2, 1] >>> Permutation(3).list(-1) [] rar()r+rrcrrEr8pop)rRrcr\rs rrEzPermutation.list=s0 => > __  dii $uTYY567 IIM"v{ FFHFA   r>c|jt|jS|j}dgt|z}g}t t|D]}||s g}|j |d||<|}|||r$||}|j |d||<|||r$t|dkDs^|j ||tt |k(rJ|j|j|_|S)a This is used to convert to the cyclic notation from the canonical notation. Singletons are omitted. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 3, 1, 2]) >>> p.cyclic_form [[1, 3, 2]] >>> Permutation([1, 0, 2, 4, 3, 5]).cyclic_form [[0, 1], [3, 4]] See Also ======== array_form, full_cyclic_form TFr() _cyclic_formrErr*r8appendrsortr)rRr uncheckedrtrcycler<s rrtzPermutation.cyclic_formes *    ())* *__ FS_,  s:' 8A| Q$ !  1 ."1 ALLO#(IaL 1 .u:>&&u- D$7777 8 ',,.r>ctt|jtt|jz }|j|Dcgc]}|gc}z}|j |Scc}w)zReturn permutation in cyclic form including singletons. Examples ======== >>> from sympy.combinatorics import Permutation >>> Permutation([0, 2, 1]).full_cyclic_form [[0], [1, 2]] )rr8rcrrtr)rRneedrr\s rfull_cyclic_formzPermutation.full_cyclic_forms\5#$s743C3C+D'EE   d 3! 3 3   !4s A.c|jS)a& Returns the number of elements in the permutation. Examples ======== >>> from sympy.combinatorics import Permutation >>> Permutation([[3, 2], [0, 1]]).size 4 See Also ======== cardinality, length, order, rank )rrVs rrczPermutation.sizes"zzr>cn|j}t|Dcgc] \}}||k7s |c}}Scc}}w)a1Return the elements in permutation, P, for which P[i] != i. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([[3, 2], [0, 1], [4]]) >>> p.array_form [1, 0, 3, 2, 4] >>> p.support() [0, 1, 2, 3] )rr@)rRrres rsupportzPermutation.supports/ OO'l5daa1f555s 11c|j|z|jz}|j|j|}||_|S)aReturn permutation that is other higher in rank than self. The rank is the lexicographical rank, with the identity permutation having rank of 0. Examples ======== >>> from sympy.combinatorics import Permutation >>> I = Permutation([0, 1, 2, 3]) >>> a = Permutation([2, 1, 3, 0]) >>> I + a.rank() == a True See Also ======== __sub__, inversion_vector )rank cardinality unrank_lexrc_rank)rRr[rr\s r__add__zPermutation.__add__s?* e#t'7'77 __TYY - r>c&|j| S)zzReturn the permutation that is other lower in rank than self. See Also ======== __add__ )r)rRr[s r__sub__zPermutation.__sub__s||UF##r>cT|d}tdt|D] }|||z} |S)a Return product of Permutations [a, b, c, ...] as the Permutation whose ith value is a(b(c(i))). a, b, c, ... can be Permutation objects or tuples. Examples ======== >>> from sympy.combinatorics import Permutation >>> a, b = [1, 0, 2], [0, 2, 1] >>> a = Permutation(a); b = Permutation(b) >>> list(Permutation.rmul(a, b)) [1, 2, 0] >>> [a(b(i)) for i in range(3)] [1, 2, 0] This handles the operands in reverse order compared to the ``*`` operator: >>> a = Permutation(a); b = Permutation(b) >>> list(a*b) [2, 0, 1] >>> [b(a(i)) for i in range(3)] [2, 0, 1] Notes ===== All items in the sequence will be parsed by Permutation as necessary as long as the first item is a Permutation: >>> Permutation.rmul(a, [0, 2, 1]) == Permutation.rmul(a, b) True The reverse order of arguments will raise a TypeError. rr()r8r*)rr\rs rrmulzPermutation.rmuls:P!Wq#d)$ AaB  r>cp|Dcgc]}|j}}|jt|}|Scc}w)zo same as rmul, but the elements of args are Permutation objects which have _array_form )rrr,)rrxrr\s r rmul_with_afzPermutation.rmul_with_afs7 %) )qQ]] ) ) [[A '  *s3czt|j}|j}|jt||S)z> other*~self, self and other have _array_form )rCrrr rRr[rrs rmul_invzPermutation.mul_invs4 t'' (   ||HQN++r>c.t|}|||zS)z6This is needed to coerce other to Permutation in rmul.)type)rRr[rs r__rmul__zPermutation.__rmul__&s4j5z$r>c Vddlm}m}t||r |||dS|j}|j}|s|}nX|j t tt|t||Dcgc]}|| c}|t|dz}|j|Scc}w)a Return the product a*b as a Permutation; the ith value is b(a(i)). Examples ======== >>> from sympy.combinatorics.permutations import _af_rmul, Permutation >>> a, b = [1, 0, 2], [0, 2, 1] >>> a = Permutation(a); b = Permutation(b) >>> list(a*b) [2, 0, 1] >>> [b(a(i)) for i in range(3)] [2, 0, 1] This handles operands in reverse order compared to _af_rmul and rmul: >>> al = list(a); bl = list(b) >>> _af_rmul(al, bl) [1, 2, 0] >>> [al[bl[i]] for i in range(3)] [1, 2, 0] It is acceptable for the arrays to have different lengths; the shorter one will be padded to match the longer one: >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> b*Permutation([1, 0]) Permutation([1, 2, 0]) >>> Permutation([1, 0])*b Permutation([2, 0, 1]) It is also acceptable to allow coercion to handle conversion of a single list to the left of a Permutation: >>> [0, 1]*a # no change: 2-element identity Permutation([1, 0, 2]) >>> [[0, 1]]*a # exchange first two elements Permutation([0, 1, 2]) You cannot use more than 1 cycle notation in a product of cycles since coercion can only handle one argument to the left. To handle multiple cycles it is convenient to use Cycle instead of Permutation: >>> [[1, 2]]*[[2, 3]]*Permutation([]) # doctest: +SKIP >>> from sympy.combinatorics.permutations import Cycle >>> Cycle(1, 2)(2, 3) (1 3 2) r)PermutationGroupCoset-)dirN) sympy.combinatorics.perm_groupsrrrrrrEr8r*r)rRr[rrrrrrs r__mul__zPermutation.__mul__+sh L e- .u#. . OO   D HHT%AA/0 1"#$QAaD$qQz1D||D!!%s7 B&cJ|j}|j}t||S)a| Checks if the elements are commuting. Examples ======== >>> from sympy.combinatorics import Permutation >>> a = Permutation([1, 4, 3, 0, 2, 5]) >>> b = Permutation([0, 1, 2, 3, 4, 5]) >>> a.commutes_with(b) True >>> b = Permutation([2, 3, 5, 4, 1, 0]) >>> a.commutes_with(b) False )rrNrs r commutes_withzPermutation.commutes_withls% OO    A&&r>ct|tr tdt|}|j t |j |S)a Routine for finding powers of a permutation. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> p = Permutation([2, 0, 3, 1]) >>> p.order() 4 >>> p**4 Permutation([0, 1, 2, 3]) z1p**p is not defined; do you mean p^p (conjugate)?)rrsNotImplementedErrorrrrFr)rRr:s r__pow__zPermutation.__pow__sC a %%CE E F||GDOOQ788r>cDt|r||Std|z)zReturn self(i) when ``i`` is an int. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation(1, 2, 9) >>> 2^p == p(2) == 9 True z(i^p = p(i) when i is an integer, not %s.)r r)rRrs r__rxor__zPermutation.__rxor__s+ a=7N%:Q>@ @r>c |j|jk7r tddg|jz}|j}|j}t|jD]}||||||<|j |S)aReturn the conjugate permutation ``~h*self*h` `. Explanation =========== If ``a`` and ``b`` are conjugates, ``a = h*b*~h`` and ``b = ~h*a*h`` and both have the same cycle structure. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation(1, 2, 9) >>> q = Permutation(6, 9, 8) >>> p*q != q*p True Calculate and check properties of the conjugate: >>> c = p^q >>> c == ~q*p*q and p == q*c*~q True The expression q^p^r is equivalent to q^(p*r): >>> r = Permutation(9)(4, 6, 8) >>> q^p^r == q^(p*r) True If the term to the left of the conjugate operator, i, is an integer then this is interpreted as selecting the ith element from the permutation to the right: >>> all(i^p == p(i) for i in range(p.size)) True Note that the * operator as higher precedence than the ^ operator: >>> q^r*p^r == q^(r*p)^r == Permutation(9)(1, 6, 4) True Notes ===== In Python the precedence rule is p^q^r = (p^q)^r which differs in general from p^(q^r) >>> q^p^r (9)(1 4 8) >>> q^(p^r) (9)(1 8 6) For a given r and p, both of the following are conjugates of p: ~r*p*r and r*p*~r. But these are not necessarily the same: >>> ~r*p*r == r*p*~r True >>> p = Permutation(1, 2, 9)(5, 6) >>> ~r*p*r == r*p*~r False The conjugate ~r*p*r was chosen so that ``p^q^r`` would be equivalent to ``p^(q*r)`` rather than ``p^(r*q)``. To obtain r*p*~r, pass ~r to this method: >>> p^~r == r*p*~r True 'The permutations must be of equal size.N)rcr+rr8r)rRhrrrs r__xor__zPermutation.__xor__sN 99 FG G F499  MM   tyy! A!gAadG ||Ar>c|j}g}|D][}t|}|dk(r|jt|.|dkDs4|d|j fd||dz ddD]|S)a Return the permutation decomposed into a list of transpositions. Explanation =========== It is always possible to express a permutation as the product of transpositions, see [1] Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([[1, 2, 3], [0, 4, 5, 6, 7]]) >>> t = p.transpositions() >>> t [(0, 7), (0, 6), (0, 5), (0, 4), (1, 3), (1, 2)] >>> print(''.join(str(c) for c in t)) (0, 7)(0, 6)(0, 5)(0, 4)(1, 3)(1, 2) >>> Permutation.rmul(*[Permutation([ti], size=p.size) for ti in t]) == p True References ========== .. [1] https://en.wikipedia.org/wiki/Transposition_%28mathematics%29#Properties r)rc3&K|]}|f ywrIr)rKyfirsts rrLz-Permutation.transpositions..s>!E1:>sr(r)rtr*rrjr)rRrresrnxrs @rtranspositionszPermutation.transpositionss{:    ?AQBQw 58$a! >qa"~>>  ? r>c tt|ttt|}r|j fdn|j t |Dcgc]}|d c}Scc}w)aReturn the permutation needed to obtain ``i`` from the sorted elements of ``i``. If custom sorting is desired, a key can be given. Examples ======== >>> from sympy.combinatorics import Permutation >>> Permutation.from_sequence('SymPy') (4)(0 1 3) >>> _(sorted("SymPy")) ['S', 'y', 'm', 'P', 'y'] >>> Permutation.from_sequence('SymPy', key=lambda x: x.lower()) (4)(0 2)(1 3) c|dS)Nrr)rkeys rz+Permutation.from_sequence..2s#ad)r>)rr()rErYr8r*rrs)rRrrics ` r from_sequencezPermutation.from_sequences^"#aeCFm,- .  GG+G , GGI2.aQqT.///.s# A7cJ|jt|jS)a Return the inverse of the permutation. A permutation multiplied by its inverse is the identity permutation. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> p = Permutation([[2, 0], [3, 1]]) >>> ~p Permutation([2, 3, 0, 1]) >>> _ == p**-1 True >>> p*~p == ~p*p == Permutation([0, 1, 2, 3]) True )rrCrrVs r __invert__zPermutation.__invert__7s(||Jt'7'7899r>c#8K|jEd{y7w)zYield elements from array form. Examples ======== >>> from sympy.combinatorics import Permutation >>> list(Permutation(range(3))) [0, 1, 2] NrrVs rrWzPermutation.__iter__Ms??""s ct|SrIrrVs rryzPermutation.__repr__Yrr>ct|dk(r|d}t|tsVt|}|dks||jkDr(t dj ||jdz |j|St||jk7r%t dj ||j|jDcgc]}|| c}S|tt||jzScc}w)a+ Allows applying a permutation instance as a bijective function. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([[2, 0], [3, 1]]) >>> p.array_form [2, 3, 0, 1] >>> [p(i) for i in range(4)] [2, 3, 0, 1] If an array is given then the permutation selects the items from the array (i.e. the permutation is applied to the array): >>> from sympy.abc import x >>> p([x, 1, 0, x**2]) [0, x**2, x, 1] r(r({} should be an integer between 0 and {}z{} should have the length {}.r) r*rrrrc TypeErrorformatrrsrP)rRrr<s rr_zPermutation.__call__\s0 q6Q;!Aa*1Iq5A M#B499Q;/11''**1v"3::1diiHJJ"&"2"23QAaD3 3Kq  :::4s C4c,t|jS)a; Returns all the elements of a permutation Examples ======== >>> from sympy.combinatorics import Permutation >>> Permutation([0, 1, 2, 3, 4, 5]).atoms() {0, 1, 2, 3, 4, 5} >>> Permutation([[0, 1], [2, 3], [4, 5]]).atoms() {0, 1, 2, 3, 4, 5} )rrrVs ratomszPermutation.atomss4??##r>c<t|}|jdurtdj||j}|dkdk(s||k\dk(rtdj||dz |j rt |j|St||S)a:Apply the permutation to an expression. Parameters ========== i : Expr It should be an integer between $0$ and $n-1$ where $n$ is the size of the permutation. If it is a symbol or a symbolic expression that can have integer values, an ``AppliedPermutation`` object will be returned which can represent an unevaluated function. Notes ===== Any permutation can be defined as a bijective function $\sigma : \{ 0, 1, \dots, n-1 \} \rightarrow \{ 0, 1, \dots, n-1 \}$ where $n$ denotes the size of the permutation. The definition may even be extended for any set with distinctive elements, such that the permutation can even be applied for real numbers or such, however, it is not implemented for now for computational reasons and the integrity with the group theory module. This function is similar to the ``__call__`` magic, however, ``__call__`` magic already has some other applications like permuting an array or attaching new cycles, which would not always be mathematically consistent. This also guarantees that the return type is a SymPy integer, which guarantees the safety to use assumptions. Fz{} should be an integer.rTrr() r is_integerrrrc is_Integerr rAppliedPermutation)rRrr:s rapplyzPermutation.applysH QK <<5 %&@&G&G&JK K II Ed?qAv$.%:AA!QqSIK K <<4++A./ /!$**r>cv|jdd}t|}|dz }||dz||kr|dz}||dz||kr|dk(ry|dz }||||kr|dz}||||kr||||c||<||<|dz }|dz }||kr!||||c||<||<|dz }|dz}||kr!|j|S)a Returns the next permutation in lexicographical order. If self is the last permutation in lexicographical order it returns None. See [4] section 2.4. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([2, 3, 1, 0]) >>> p = Permutation([2, 3, 1, 0]); p.rank() 17 >>> p = p.next_lex(); p.rank() 18 See Also ======== rank, unrank_lex Nr)r(r)rr*r)rRrr:rr<s rnext_lexzPermutation.next_lexs.q! I E1q5kDG# FA1q5kDG# 7AAq'DG#Qq'DG##AwQ DGT!W FAAAa%#'7DG QaQQa%||D!!r>cfdtt|}t|}|t|z}||||j |S)a This is a linear time unranking algorithm that does not respect lexicographic order [3]. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> Permutation.unrank_nonlex(4, 5) Permutation([2, 0, 3, 1]) >>> Permutation.unrank_nonlex(4, -1) Permutation([0, 1, 2, 3]) See Also ======== next_nonlex, rank_nonlex cl|dkDr.|||z||dz c||dz <|||z<|dz ||z|yyrr)r:rr_unrank1s rrz+Permutation.unrank_nonlex.._unrank1sO1u%&q1uXqQx"!a%!AE(Q1a(r>)rEr8rrr)rRr:rid_permrs @r unrank_nonlexzPermutation.unrank_nonlexsH, ) uQx. F QKAw||G$$r>czfd| |j}|sy|jdd}t|||}|S)ae This is a linear time ranking algorithm that does not enforce lexicographic order [3]. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.rank_nonlex() 23 See Also ======== next_nonlex, unrank_nonlex c|dk(ry||dz }||dz }|||c||dz <||<|||c||dz <||<|||dz ||zzS)Nr(rr)r:rinv_permrxt_rank1s rr z'Permutation.rank_nonlex.._rank1$s|AvQU AQA#'7A DQKa+3A; (HQUOXa[qAtX666 6r>Nr)rr*)rRr rrr s @r rank_nonlexzPermutation.rank_nonlexsH& 7  ))Hq! 3t9dH -r>c|j}|t|jdz k(ry|j|j|dzS)aJ Returns the next permutation in nonlex order [3]. If self is the last permutation in this order it returns None. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> p = Permutation([2, 0, 3, 1]); p.rank_nonlex() 5 >>> p = p.next_nonlex(); p Permutation([3, 0, 1, 2]) >>> p.rank_nonlex() 6 See Also ======== rank_nonlex, unrank_nonlex r(N)rrrcr)rRrs r next_nonlexzPermutation.next_nonlex5sE.     TYY!# #!!$))QU33r>c`|j |jSd}|jdd}|jdz }|dz}tt |}t |dz D]D}||||zz }t |dz|D]}||||kDs||xxdzcc<||z}|dz}F||_|S)a Returns the lexicographic rank of the permutation. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.rank() 0 >>> p = Permutation([3, 2, 1, 0]) >>> p.rank() 23 See Also ======== next_lex, unrank_lex, cardinality, length, order, size Nrr()rrrcrrr8)rRrrhor:rcpsizer<rs rrzPermutation.rankQs( :: !:: ooa  IIM1uDG tax A CF5L D1q5$' q6CF?FaKF  aKE FA    r>c>tt|jS)a3 Returns the number of all possible permutations. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.cardinality 24 See Also ======== length, order, rank, size )rrrcrVs rrzPermutation.cardinalityvs$4 ?##r>c||j|j|jz dzSt|jS)aP Computes the parity of a permutation. Explanation =========== The parity of a permutation reflects the parity of the number of inversions in the permutation, i.e., the number of pairs of x and y such that ``x > y`` but ``p[x] < p[y]``. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.parity() 0 >>> p = Permutation([3, 2, 0, 1]) >>> p.parity() 1 See Also ======== _af_parity r))rrcrwr=rrVs rparityzPermutation.paritys76    (II +q0 0$//**r>c|j S)a[ Checks if a permutation is even. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.is_even True >>> p = Permutation([3, 2, 1, 0]) >>> p.is_even True See Also ======== is_odd )is_oddrVs ris_evenzPermutation.is_evens*;;r>c:t|jdzS)aZ Checks if a permutation is odd. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.is_odd False >>> p = Permutation([3, 2, 0, 1]) >>> p.is_odd True See Also ======== is_even r))boolrrVs rrzPermutation.is_odds*DKKMA%&&r>c |jdk(S)a Checks to see if the permutation contains only one number and is thus the only possible permutation of this set of numbers Examples ======== >>> from sympy.combinatorics import Permutation >>> Permutation([0]).is_Singleton True >>> Permutation([0, 1]).is_Singleton False See Also ======== is_Empty r(rrVs r is_SingletonzPermutation.is_Singletons(yyA~r>c |jdk(S)aI Checks to see if the permutation is a set with zero elements Examples ======== >>> from sympy.combinatorics import Permutation >>> Permutation([]).is_Empty True >>> Permutation([0]).is_Empty False See Also ======== is_Singleton rrrVs ris_EmptyzPermutation.is_Emptys&yyA~r>c|jSrI) is_IdentityrVs r is_identityzPermutation.is_identitysr>ct|j xs'tfdt|jDS)a Returns True if the Permutation is an identity permutation. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([]) >>> p.is_Identity True >>> p = Permutation([[0], [1], [2]]) >>> p.is_Identity True >>> p = Permutation([0, 1, 2]) >>> p.is_Identity True >>> p = Permutation([0, 2, 1]) >>> p.is_Identity False See Also ======== order c3.K|] }||k(ywrIr)rKrafs rrLz*Permutation.is_Identity..#sBAQ"Q%ZBs)rallr8rc)rRr%s @rr!zPermutation.is_Identitys/6__vBBtyy1ABBBr>c|j}tt|dz Dcgc]}||||dzks|}}|Scc}w)al Returns the positions of ascents in a permutation, ie, the location where p[i] < p[i+1] Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([4, 0, 1, 3, 2]) >>> p.ascents() [1, 2] See Also ======== descents, inversions, min, max r(rr8r*rRrrposs rascentszPermutation.ascents%J$ OOA +?QqtaAhq?? @ AAc|j}tt|dz Dcgc]}||||dzkDs|}}|Scc}w)am Returns the positions of descents in a permutation, ie, the location where p[i] > p[i+1] Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([4, 0, 1, 3, 2]) >>> p.descents() [0, 3] See Also ======== ascents, inversions, min, max r(r(r)s rdescentszPermutation.descents;r,r-returncV|j}|sytdt|DS)a5 The maximum element moved by the permutation. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([1, 0, 2, 3, 4]) >>> p.max() 1 See Also ======== min, descents, ascents, inversions rc32K|]\}}||k7s |ywrIrrKr_as rrLz"Permutation.max..e;%!R272; )rrbr@rRrs rrbzPermutation.maxQ(" OO;9Q<;;;r>cV|j}|sytdt|DS)a5 The minimum element moved by the permutation. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 4, 3, 2]) >>> p.min() 2 See Also ======== max, descents, ascents, inversions rc32K|]\}}||k7s |ywrIrr3s rrLz"Permutation.min..{r5r6)rminr@r7s rr;zPermutation.mingr8r>cld}|j}t|}|dkr2t|dz D]}||}||dzdD] }||kDs |dz }!|Sd}d}|dd} |dd} ||krPd}||z|kr;||dzzdz }||k\r|dz }|t| | |||z|z }||dzz}||z|kr;|dz}||krP|S)aE Computes the number of inversions of a permutation. Explanation =========== An inversion is where i > j but p[i] < p[j]. For small length of p, it iterates over all i and j values and calculates the number of inversions. For large length of p, it uses a variation of merge sort to calculate the number of inversions. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3, 4, 5]) >>> p.inversions() 0 >>> Permutation([3, 2, 1, 0]).inversions() 6 See Also ======== descents, ascents, min, max References ========== .. [1] https://www.cp.eng.chula.ac.th/~prabhas//teaching/algo/algo2008/count-inv.htm rr(Nr))rr*r8_merge) rR inversionsrr:rrr;r]rightarrrs rr?zPermutation.inversions}sF OO F s71q5\ (aD1q56(A1u"a ( (&AEA$CQ4Da%!eaiAIMEz !A&dAq1ue"DDJAE A !eai Ea%r>c L|j}|j}t|}t||k7r tddg|z}t|D] }||||< dg|z}t|D] }||||< |j |Dcgc] }||||c}Scc}w)a%Return the commutator of ``self`` and ``x``: ``~x*~self*x*self`` If f and g are part of a group, G, then the commutator of f and g is the group identity iff f and g commute, i.e. fg == gf. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> p = Permutation([0, 2, 3, 1]) >>> x = Permutation([2, 0, 3, 1]) >>> c = p.commutator(x); c Permutation([2, 1, 3, 0]) >>> c == ~x*~p*x*p True >>> I = Permutation(3) >>> p = [I + i for i in range(6)] >>> for i in range(len(p)): ... for j in range(len(p)): ... c = p[i].commutator(p[j]) ... if p[i]*p[j] == p[j]*p[i]: ... assert c == I ... else: ... assert c != I ... References ========== .. [1] https://en.wikipedia.org/wiki/Commutator rN)rr*r+r8r)rRrrrr:invarinvbs r commutatorzPermutation.commutatorsH OO LL F q6Q;FG Gvaxq AD1J vaxq AD1J ||D9qQqaz]9::9sB!c|jryy)a: Gives the signature of the permutation needed to place the elements of the permutation in canonical order. The signature is calculated as (-1)^ Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2]) >>> p.inversions() 0 >>> p.signature() 1 >>> q = Permutation([0,2,1]) >>> q.inversions() 1 >>> q.signature() -1 See Also ======== inversions r(r)rrVs r signaturezPermutation.signatures6 <<r>c ptt|jDcgc] }t|c}dScc}w)aP Computes the order of a permutation. When the permutation is raised to the power of its order it equals the identity permutation. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> p = Permutation([3, 1, 5, 2, 4, 0]) >>> p.order() 4 >>> (p**(p.order())) Permutation([], size=6) See Also ======== identity, cardinality, length, rank, size r()rr rtr*)rRrs rorderzPermutation.order s+2cD4D4DE5CJEqIIEs3 c4t|jS)ax Returns the number of integers moved by a permutation. Examples ======== >>> from sympy.combinatorics import Permutation >>> Permutation([0, 3, 2, 1]).length() 2 >>> Permutation([[0, 1], [2, 3]]).length() 4 See Also ======== min, max, support, cardinality, order, rank, size )r*rrVs rlengthzPermutation.length$ s&4<<>""r>c|jr|j}t |Stt}|j}|jD]&}|t |xxdz cc<|t |z}(|r||d<||_t |S)ahReturn the cycle structure of the permutation as a dictionary indicating the multiplicity of each cycle length. Examples ======== >>> from sympy.combinatorics import Permutation >>> Permutation(3).cycle_structure {1: 4} >>> Permutation(0, 4, 3)(1, 2)(5, 6).cycle_structure {2: 2, 3: 1} r()_cycle_structurerrrcrtr*dict)rRr\ singletonsr;s rcycle_structurezPermutation.cycle_structure9 s  &&BBxS!BJ%% %3q6 a c!f$  %"1$&D !Bxr>c,t|jS)a Returns the number of cycles contained in the permutation (including singletons). Examples ======== >>> from sympy.combinatorics import Permutation >>> Permutation([0, 1, 2]).cycles 3 >>> Permutation([0, 1, 2]).full_cyclic_form [[0], [1], [2]] >>> Permutation(0, 1)(2, 3).cycles 2 See Also ======== sympy.functions.combinatorial.numbers.stirling )r*rrVs rrwzPermutation.cyclesT s*4(())r>cn|jtfdttdz DS)aP Returns the index of a permutation. The index of a permutation is the sum of all subscripts j such that p[j] is greater than p[j+1]. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([3, 0, 2, 1, 4]) >>> p.index() 2 c3@K|]}||dzkDs|yw)r(Nr)rKr<rs rrLz$Permutation.index..| s#C1Q4!AE(?1Csr()rsumr8r*r7s @rindexzPermutation.indexk s, OOCeCFQJ/CCCr>c,t|jS)a Returns the runs of a permutation. An ascending sequence in a permutation is called a run [5]. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([2, 5, 7, 3, 6, 0, 1, 4, 8]) >>> p.runs() [[2, 5, 7], [3, 6], [0, 1, 4, 8]] >>> q = Permutation([1,3,2,0]) >>> q.runs() [[1, 3], [2], [0]] )rrrVs rrzPermutation.runs~ s$DOO$$r>c|j}t|}dg|dz z}t|dz D].}d}t|dz|D]}||||ks|dz }|||<0|S)a|Return the inversion vector of the permutation. The inversion vector consists of elements whose value indicates the number of elements in the permutation that are lesser than it and lie on its right hand side. The inversion vector is the same as the Lehmer encoding of a permutation. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([4, 8, 0, 7, 1, 5, 3, 6, 2]) >>> p.inversion_vector() [4, 7, 0, 5, 0, 2, 1, 1] >>> p = Permutation([3, 2, 1, 0]) >>> p.inversion_vector() [3, 2, 1] The inversion vector increases lexicographically with the rank of the permutation, the -ith element cycling through 0..i. >>> p = Permutation(2) >>> while p: ... print('%s %s %s' % (p, p.inversion_vector(), p.rank())) ... p = p.next_lex() (2) [0, 0] 0 (1 2) [0, 1] 1 (2)(0 1) [1, 0] 2 (0 1 2) [1, 1] 3 (0 2 1) [2, 0] 4 (0 2) [2, 1] 5 See Also ======== from_inversion_vector rr()rr*r8)rRself_array_formr:inversion_vectorrvalr<s rrYzPermutation.inversion_vector sP//  3!a%=q1u &AC1q5!_ "1%(::1HC #& Q   &  r>cN|jgk(s |jry|jddgk(ry|j}|j}d}td|D]M}d}d}|||k7r|||kr|dz }|dz }|||k7r|dz}|dzdk(r ||z|z|z }C||z|zdz }O|S)a Returns the Trotter Johnson rank, which we get from the minimal change algorithm. See [4] section 2.4. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 1, 2, 3]) >>> p.rank_trotterjohnson() 0 >>> p = Permutation([0, 2, 1, 3]) >>> p.rank_trotterjohnson() 7 See Also ======== unrank_trotterjohnson, next_trotterjohnson rr(r))rr!rcr8)rRrr:rr<r]rj1s rrank_trotterjohnsonzPermutation.rank_trotterjohnson s* ??b D$4$4 ??q!f $ IIq! 'AAAq'Q,7Q;FAQq'Q,QBax1}$w|a'$w{Q ' r>cndg|z}d}t|}d}td|dzD]~}||z}||z|z}|||zz } |dzdk(r5t|dz || z dz dD] } || dz || <|dz ||| z dz <n(t|dz | dD] } || dz || <|dz || <|}|j|S)a Trotter Johnson permutation unranking. See [4] section 2.4. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> Permutation.unrank_trotterjohnson(5, 10) Permutation([0, 3, 1, 2, 4]) See Also ======== rank_trotterjohnson, next_trotterjohnson rr(r)r)rr8r) rrcrrr2r:pjr<r1r]rs runrank_trotterjohnsonz!Permutation.unrank_trotterjohnson s&s4x  J q$(# A !GB)!BQrT AAv{q1ua!eai4*A"1q5kDG*"#a%QUQYq1ua,*A"1q5kDG*a%QB {{4  r>c|jdd}t|}d}|dd}d}|dz }|dkDr|s|j|}t||D] }||dz||<t |d|} | dk(r1||k(r|dz}n[|||zdz|||zc|||z<|||zdz<d}n5|dk(r |dz}|dz }n%|||zdz |||zc|||z<|||zdz <d}|dkDr|s|dk(ry|j |S)aO Returns the next permutation in Trotter-Johnson order. If self is the last permutation it returns None. See [4] section 2.4. If it is desired to generate all such permutations, they can be generated in order more quickly with the ``generate_bell`` function. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> p = Permutation([3, 0, 2, 1]) >>> p.rank_trotterjohnson() 4 >>> p = p.next_trotterjohnson(); p Permutation([0, 3, 2, 1]) >>> p.rank_trotterjohnson() 5 See Also ======== rank_trotterjohnson, unrank_trotterjohnson, sympy.utilities.iterables.generate_bell NrFr(T)rr*rUr8r=r) rRr9r:strdoner.drpars rnext_trotterjohnsonzPermutation.next_trotterjohnson sK6__Q  G e aC!eD ! A1a[ $QUA $S!W%Cax6FA13BFQJBF.BrAvJ26A:D6FA!GB13BFQJBF.BrAvJ26A:D#!eD$ 6||Br>ct|j}|j}t|jD]-}t|dz|j D]}d|||||f</|S)a Gets the precedence matrix. This is used for computing the distance between two permutations. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> p = Permutation.josephus(3, 6, 1) >>> p Permutation([2, 5, 3, 1, 4, 0]) >>> p.get_precedence_matrix() Matrix([ [0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 1, 0], [1, 1, 0, 1, 1, 1], [1, 1, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0], [1, 1, 0, 1, 1, 0]]) See Also ======== get_precedence_distance, get_adjacency_matrix, get_adjacency_distance r()r rcrr8rowscols)rRr.rrr<s rget_precedence_matrixz!Permutation.get_precedence_matrixM sn8 $)) qvv (A1q5!&&) (&'$q'47"# ( (r>cz|j|jk7r td|j}|j}d}t|jD]:}t|jD] }||k(r |||f|||fzdk(s|dz }"<|j|jdz zdz|z }|S)a Computes the precedence distance between two permutations. Explanation =========== Suppose p and p' represent n jobs. The precedence metric counts the number of times a job j is preceded by job i in both p and p'. This metric is commutative. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([2, 0, 4, 3, 1]) >>> q = Permutation([3, 1, 2, 4, 0]) >>> p.get_precedence_distance(q) 7 >>> q.get_precedence_distance(p) 7 See Also ======== get_precedence_matrix, get_adjacency_matrix, get_adjacency_distance rrr(r))rcr+rlr8)rRr[ self_prec_matother_prec_matn_precrr<rfs rget_precedence_distancez#Permutation.get_precedence_distancep s6 99 "FG G224 446tyy! A499% 6 A&1)==BaKF   IIQ ' *V 3r>ct|j}|j}t|jdz D]}d|||||dzf<|S)a Computes the adjacency matrix of a permutation. Explanation =========== If job i is adjacent to job j in a permutation p then we set m[i, j] = 1 where m is the adjacency matrix of p. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation.josephus(3, 6, 1) >>> p.get_adjacency_matrix() Matrix([ [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 1], [0, 1, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0]]) >>> q = Permutation([0, 1, 2, 3]) >>> q.get_adjacency_matrix() Matrix([ [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]]) See Also ======== get_precedence_matrix, get_precedence_distance, get_adjacency_distance r()r rcrr8)rRr.rrs rget_adjacency_matrixz Permutation.get_adjacency_matrix sXJ $)) tyy1}% (A&'Ad1gtAE{" # (r>cZ|j|jk7r td|j}|j}d}t|jD]:}t|jD] }||k(r |||f|||fzdk(s|dz }"<|j|z dz }|S)as Computes the adjacency distance between two permutations. Explanation =========== This metric counts the number of times a pair i,j of jobs is adjacent in both p and p'. If n_adj is this quantity then the adjacency distance is n - n_adj - 1 [1] [1] Reeves, Colin R. Landscapes, Operators and Heuristic search, Annals of Operational Research, 86, pp 473-490. (1999) Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 3, 1, 2, 4]) >>> q = Permutation.josephus(4, 5, 2) >>> p.get_adjacency_distance(q) 3 >>> r = Permutation([0, 2, 1, 4, 3]) >>> p.get_adjacency_distance(r) 4 See Also ======== get_precedence_matrix, get_precedence_distance, get_adjacency_matrix *The permutations must be of the same size.rr()rcr+rsr8)rRr[ self_adj_mat other_adj_matn_adjrr<rfs rget_adjacency_distancez"Permutation.get_adjacency_distance s@ 99 "IJ J002 224 tyy! A499% 61% ad(;;q@QJE    II  !r>c|j|jttk7r tdtfdt tDS)a Computes the positional distance between two permutations. Examples ======== >>> from sympy.combinatorics import Permutation >>> p = Permutation([0, 3, 1, 2, 4]) >>> q = Permutation.josephus(4, 5, 2) >>> r = Permutation([3, 1, 4, 0, 2]) >>> p.get_positional_distance(q) 12 >>> p.get_positional_distance(r) 12 See Also ======== get_precedence_distance, get_adjacency_distance ruc3FK|]}t||z ywrI)absrJs rrLz6Permutation.get_positional_distance.. s";3qtad{#;s!)rr*r+rTr8rs @@rget_positional_distancez#Permutation.get_positional_distance sN* OO    q6SV IJ J;U3q6];;;r>cddlm}|dz}|tt|}g}t |t |dkDrgt|D]!}|j |j#|j |jt |t |dkDrg|jt|||S)aReturn as a permutation the shuffling of range(n) using the Josephus scheme in which every m-th item is selected until all have been chosen. The returned permutation has elements listed by the order in which they were selected. The parameter ``s`` stops the selection process when there are ``s`` items remaining and these are selected by continuing the selection, counting by 1 rather than by ``m``. Consider selecting every 3rd item from 6 until only 2 remain:: choices chosen ======== ====== 012345 01 345 2 01 34 25 01 4 253 0 4 2531 0 25314 253140 Examples ======== >>> from sympy.combinatorics import Permutation >>> Permutation.josephus(3, 6, 2).array_form [2, 5, 3, 1, 4, 0] References ========== .. [1] https://en.wikipedia.org/wiki/Flavius_Josephus .. [2] https://en.wikipedia.org/wiki/Josephus_problem .. [3] https://web.archive.org/web/20171008094331/http://www.wou.edu/~burtonl/josephus.html r)dequer() collectionsrrEr8r*rbrpopleftr)rr.r:rxrQrdps rjosephuszPermutation.josephus sL & Q $uQx. !!fs1ay Ah &% & KK $!fs1ay  DG4yr>c6t|}tt|dz}g} t|D],}|||}|j||j |. |j||j|S#t $r t dwxYw)ax Calculates the permutation from the inversion vector. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> Permutation.from_inversion_vector([3, 2, 1, 0, 0]) Permutation([3, 2, 1, 0, 4, 5]) r(z"The inversion vector is not valid.) r*rEr8rremove IndexErrorr+rr)r inversionrcNrr]rZs rfrom_inversion_vectorz!Permutation.from_inversion_vector> s9~ tax ! C4[  ! o C    A{{4   CAB B Cs :BBcvtt|}tj||j |S)a? Generates a random permutation of length ``n``. Uses the underlying Python pseudo-random number generator. Examples ======== >>> from sympy.combinatorics import Permutation >>> Permutation.random(2) in (Permutation([1, 0]), Permutation([0, 1])) True )rEr8randomshuffler)rr: perm_arrays rrzPermutation.randomZ s,%(^ z"{{:&&r>cdg|z}d}t|D]T}||dzz}||z|z}|||zz}||||z dz <t||z |D]}|||dz kDs||xxdz cc<|}V|j|S)a Lexicographic permutation unranking. Examples ======== >>> from sympy.combinatorics import Permutation >>> from sympy import init_printing >>> init_printing(perm_cyclic=False, pretty_print=False) >>> a = Permutation.unrank_lex(5, 10) >>> a.rank() 10 >>> a Permutation([0, 2, 4, 1, 3]) See Also ======== rank, next_lex rr()r8r) rrcrrrr new_psizerfr<s rrzPermutation.unrank_lexm s,S4Z t Aq1u I !e+A AeGOD'(Jtax!| $4!8T* 'a=1q5(qMQ&M 'E {{:&&r>c |j}t|}||kDr-|tt||z }tj |S||kr||j }g}|D]^}t|}t|}||dz ks"||dz kDr$tdj|t||j|`t |S|S)aResize the permutation to the new size ``n``. Parameters ========== n : int The new size of the permutation. Raises ====== ValueError If the permutation cannot be resized to the given size. This may only happen when resized to a smaller size than the original. Examples ======== >>> from sympy.combinatorics import Permutation Increasing the size of a permutation: >>> p = Permutation(0, 1, 2) >>> p = p.resize(5) >>> p (4)(0 1 2) Decreasing the size of the permutation: >>> p = p.resize(4) >>> p (3)(0 1 2) If resizing to the specific size breaks the cycles: >>> p.resize(2) Traceback (most recent call last): ... ValueError: The permutation cannot be resized to 2 because the cycle (0, 1, 2) may break. r(zGThe permutation cannot be resized to {} because the cycle {} may break.) rr*rEr8rsrrr;rbr+rrjr) rRr:rlrtnew_cyclic_formr cycle_min cycle_maxs rresizezPermutation.resize sV J q5 T%1+& &E&&u- - U//K O$ 2J J !# 1Q3(>#VAuU|466 $**51 2/ / r>rI)r()Vrrrris_PermutationrrrMrrr classmethodrrrrrrrErtrrcrrr staticmethodrrrrrrrrrrrrrWryr_rrrrrrrrrrrrrr"r!r+r/rrbr;r?rErGrIrKrPrwrUrrYr]rbrhrlrqrsryr}rrrrr print_cyclic __classcell__rs@rrsrssbH NKL E E!%|"|2/"& ##&&P((T  $6 4$**X, ?"B'(9,@"N`&P00.:, #';R $/+b)"V%%@"H48#J$$&+@,'',*(  CC:,,J6#*4**,D&%(2 h(T#!#!J5 n!F'R)V,\<6..`!!6''$ ' 'DAHLr>rsc\|x}}|}d}||krH||krC||||kr||||<|dz }|dz }n||||<|dz }|dz }|||z z }||kr||krC||kr||||<|dz }|dz }||kr||kr&|||z dzz }|||z dzz }|||dz|||dz|S|||dz|||dz|S)z Merges two sorted arrays and calculates the inversion count. Helper function for calculating inversions. This method is for internal use only. rr(r) rArleftmidr@rr]r< inv_counts rr>r> s1 LA AI c'a5j q6CF?!fDG FA FA!fDG FA FA #q& !I c'a5j c'a&Q Q Q c' Ez UQY] UQY]tAE*DQ #4 2D r>c$eZdZdZdfd ZxZS)raA permutation applied to a symbolic variable. Parameters ========== perm : Permutation x : Expr Examples ======== >>> from sympy import Symbol >>> from sympy.combinatorics import Permutation Creating a symbolic permutation function application: >>> x = Symbol('x') >>> p = Permutation(0, 1, 2) >>> p.apply(x) AppliedPermutation((0 1 2), x) >>> _.subs(x, 1) 2 c |tj}t|}t|}t|tst dj ||r|jr|j|St|)|||}|S)Nz"{} must be a Permutation instance.) revaluater rrsr+rrrrr)rrrrobjrs rrzAppliedPermutation.__new__ sz  (11H~ QK$ ,A  ||zz!}$goc4+ r>rI)rrrrrrrs@rrr s.r>rch|j|jk7ry|j|jk(SrI)rr)lhsrhss r _eval_is_eqr& s( yyCII ??coo --r>)3rrrcollections.abcr functoolsrsympy.core.parametersrsympy.core.basicrsympy.core.exprrsympy.core.numbersr r sympy.core.sympifyr sympy.matricesr sympy.polys.polytoolsr sympy.printing.reprrsympy.utilities.iterablesrrrrrrsympy.utilities.miscrmpmath.libmp.libintmathrsympy.multipledispatchrr r,r=rCrFrNrNrPrsr>Permrrrrr>rrs #$3! )&' %%!!'(+B:z$N2* ZB(bDbJ{'${'|O@ ,,((V +{#.$.r>