combinat::permutations – permutations

The library combinat::permutations provides functions for counting, generating, and manipulating permutations.

Further functions are documented theme by theme in separate help sections:    

→ Examples

Superdomain

combinat::words

Categories

Cat::CombinatorialClass

Axioms

Ax::systemRep

Related Domains:

combinat::words

Details:

Entries

"domtype"

The MuPAD domain used to represent permutations: DOM_LIST

"typeStandard"

The MuPAD type used to represent standard permutations.

isA – test if an object is a permutation

combinat::permutations::isA(any type perm)

combinat::permutations::isA(any type perm, word wrd)

combinat::permutations::isA(any type perm, nonnegative integer n)

Returns whether perm is a permutation.

Returns whether perm is a permutation of the word wrd.

Returns whether perm is a permutations of the list [1..n].

isAStandard – test if an object is a standard permutation

combinat::permutations::isAStandard(any type perm)

Returns whether perm is a standard permutation.

isStandard – test if a permutation is standard

combinat::permutations::isStandard(permutation perm)

Returns whether perm is a standard permutation.

count – number of permutations

combinat::permutations::count(list math, <Duplicate>)

combinat::permutations::count(nonnegative integer n)

Returns the number of permutations of the list l, with or without duplicates.

Returns the number of permutations of the list [1,...,n].

list – list of permutations

combinat::permutations::list(list math, <Duplicate>)

combinat::permutations::list(nonnegative integer n)

Returns the list of the permutations of the list l, with or without duplicates.

Returns the list of the permutations of the list [1,...,n].

generator – generator for permutations

combinat::permutations::generator(list math, <Duplicate>)

combinat::permutations::generator(nonnegative integer n)

Returns a generator for the permutations of the list l, with or without duplicates.

Returns a generator for the permutations of the list [1,...,n].

first – first permutation

combinat::permutations::first(list l)

combinat::permutations::first(nonnegative integer n)

Returns the first permutation of the list l in lexicographic order. This is equivalent to sort(l).

Returns the first permutation of the list [1,...,n], that is [1,...,n].

last – last permutation

combinat::permutations::last(list l)

combinat::permutations::last(nonnegative integer n)

Returns the last permutation of the list l in lexicographic order. This is equivalent to revert(sort(l)).

Returns the last permutation of the list [1,...,n], that is [n,...,1].

Next – next permutation

combinat::permutations::Next(numerical list l)

Returns the next permutation of the list l in lexicographic order.

Next cannot deal with duplications, nor with lists whose elements cannot be ordered by <.

previous – previous permutation

combinat::permutations::previous(numerical list l)

Returns the previous permutation of the list l in lexicographic order.

previous can not deal with duplications, nor with lists whose elements cannot be ordered by <.

rank – rank of a permutation

combinat::permutations::rank(standard permutation p)

Returns the rank math of a permutation p of size math, this permutation being by definition the math-th one in the list of all permutations of size math lexicographically sorted.

rank cannot deal with duplications, nor with lists whose elements cannot be ordered by <.

unrank – unranking of standard permutations

combinat::permutations::unrank(nonnegative integer r, nonnegative integer n)

Returns the permutation of rank math in the list of standard permutations of size math lexicographically sorted.

random – random permutation

combinat::permutations::random(list l)

combinat::permutations::random(nonnegative integer n)

Returns a random permutation of the list l.

Returns a random permutation of the list [1,...,n].

inverse – inverse of a standard permutation

combinat::permutations::inverse(standard permutation p)

Returns the inverse of the standard permutation p.

_mult – action and composition of permutations

combinat::permutations::_mult(word w, standard permutation p)

combinat::permutations::_mult(standard permutation p1, standard permutation p2)

Computes the action of the standard permutation p on the word w by permutation of the positions (action on the right).

If p1 is a standard permutation, the result is the composition of p1 and p2 (as math, p2 acting on the right of p1).

multInverse – Inverse action on positions

combinat::permutations::multInverse(word w, standard permutation p)

combinat::permutations::multInverse(standard permutation p1, standard permutation p2)

Computes the action of the standard permutation p on the word w by inverse permutation of the positions (inverse action on the right).

If p1 is also a standard permutation, the result is the composition of p1 and p2^(-1).

shiftedConcat2 – binary shifted concatenation

combinat::permutations::shiftedConcat2(permutation p1, permutation p2)

Returns the concatenation of p1 with a copy of p2 with all its elements shifted by nops(p1): math.

The point is that the shifted concatenation of two standard permutations is again a standard permutation.

shiftedConcat – n-ary shifted concatenation

combinat::permutations::shiftedConcat(permutation p1, permutation p2)

The shifted concatenation combinat::permutations::shiftedConcat2 is an associative operation. This function implements the corresponding n-ary operation.

rearrangement – rearrangement of a list

combinat::permutations::rearrangement(list l1, list l2)

Returns a permutation sigma such that l2[sigma[i]]=l[i] if it exists and FAIL otherwise.

toMatrix – permutation matrix of a standard permutation

combinat::permutations::toMatrix(standard permutation p)

Returns the permutation matrix of the standard permutation p seen as an endomorphism of the symmetric group algebra.

Known bug: toMatrix does not work for the trivial permutation [].

Inversions, Lehmer code and reduced words of permutations

An inversion of a permutation math is a pair of positions math such that math and math. It is represented in MuPAD as a set of two positive integers {i,j}.

The sign of a permutation math is math if math has an odd number of inversions, and math otherwise.

The Lehmer code of a permutation is the list math such that math. where math denotes the cardinal of the set. Note that math. The cocode is the list math such that math.

A standard permutation is completely determined by its Lehmer code (resp. cocode), and it follows that the Lehmer code provides a bijection between standard permutations of size math and words math such that math (resp math). If math is a non standard permutation, then decoding the Lehmer code of math yields the standardized of math.

Extending a permutation with fixed points at its end is equivalent to appending zeroes at the end of its Lehmer code. So, the code behaves well with respect to the natural embedding between symmetric groups of different order.

The functions described here only work for permutations whose elements can be ordered by <.

Let math denote the elementary transposition math. A word for the permutation math is a list math such that math is equal to the product math. A word is said to be reduced if it is of minimal length among all the words for the same permutation. The length of a reduced word for math is equal to the number of inversions of math. In MuPAD words are represented by lists of positive integers.

inversions – list of the inversions of a permutation

combinat::permutations::inversions(permutation p)

Returns the list of the inversions of the permutation p.

inversionsNumber – number of inversions of a permutation

combinat::permutations::inversionsNumber(permutation p)

Returns the number of inversions of the permutation p.

Complexity: math, could be math.

inversionsNumberMerge – number of inversions of a permutation

combinat::permutations::inversionsNumberMerge(permutation p)

Returns the number of inversions of the permutation p.

This method consists in a faster implementation of the combinat::permutations::inversionsNumber method using a modified merge sort algorithm.

Complexity: math, with bad constant time factor.

sign – sign of a standard permutation

combinat::permutations::sign(standard permutation p)

Returns the sign of the permutation p.

Complexity: math.

signFromInversionsNumber – sign of a permutation

combinat::permutations::signFromInversionsNumber(permutation p)

Returns the sign of the permutation p, using the number of inversions. The permutation need not be standard.

Complexity: math, could be math.

isEven – parity of a standard permutation

combinat::permutations::isEven(permutation p)

Tests if the permutation p is even.

code – Lehmer code of a permutation

combinat::permutations::code(permutation p)

Returns the Lehmer code of the permutation p.

fromCode – standard permutation from its code

combinat::permutations::fromCode(Lehmer code c, <non-negative integer n>)

Recovers the standard permutation of minimal size from the sequence c, appending the minimum necessary number of zeroes to it so that it becomes a Lehmer code.

The argument n, when provided, specifies the size of the returned permutation.

coCode – Lehmer cocode of a permutation

combinat::permutations::coCode(permutation p)

Returns the Lehmer cocode of the permutation p.

reducedWord – Reduced word of a standard permutation

combinat::permutations::reducedWord(permutation p)

Returns a reduced word of the permutation p.

reducedWords – Reduced words of a standard permutation

combinat::permutations::reducedWords(permutation p)

Returns the list of all reduced words of the permutation p.

reducedWordsLexMin – Lexicographically minimal reduced word of a standard permutation

combinat::permutations::reducedWordsLexMin(permutation p)

Returns the Lexicographically minimal reduced word of the permutation p.

fromReducedWord – Standard permutation from a (reduced) word

combinat::permutations::fromReducedWord(reduced word math, <non-negative integer n>)

Returns the standard permutation of minimal size that is the product of the elementary transpositions represented by w. Note that this word does not need to be reduced.

The argument n, when provided, specifies the size of the returned permutation.

Cycle decomposition of standard permutations

A cycle  math of length  math is the permutation such that math, and math for math. Such a cycle is represented in MuPAD by a list math. This representation is unique if, for example, c1 is chosen to be minimal.

A cycle of length math is equal to the identity permutation and is called trivial.

Two disjoint cycles commute.

Any standard permutation math can be written as a product of disjoint cycles. This is called the cycle decomposition of math. It is unique up to the order of the cycles, and up to the first element of the cycles.

cycles – cycle decomposition of a standard permutation

combinat::permutations::cycles(standard permutation p)

Returns the cycle decomposition of p. This is the list of the non-trivial cycles of p. Each cycle is represented by the list [i, p[i], p[p[i]], ...] of its elements starting from the smallest one. The cycles themselves are sorted increasingly w.r.t. their first element.

cycleType – cycle type of a standard permutation

combinat::permutations::cycleType(standard permutation p)

Returns the cycle type of the permutation p, as a partition. This is the decreasing list of the lengths of the cycles of p.

fromCycles – standard permutation from its cycle decomposition

combinat::permutations::fromCycles(list of list of integers cycleList, <integer n>)

Returns the standard permutation of minimal size that has the given cycle list. Note that this minimal size corresponds to the greatest element in cycleList.

Trivial cycles of length 1 may be omitted if the greatest element of the permutation is not in a cycle of length 1.

The argument n, when provided, specifies the size of the returned permutation.

fromCycleType – standard permutations with a given cycle type

combinat::permutations::fromCycleType(partitions l)

Returns the list of standard permutations that have cycle type the descent set s. Note that this minimal size corresponds to the greatest element in l.

This is in fact a full-featured combinatorial class, with the usual methods count, generator, list and unrank.

fixedPoints – fixed points of a standard permutation

combinat::permutations::fixedPoints(standard permutation p)

Returns the list of the fixed points of p.

fixedPointsNumber – number of fixed points of a standard permutation

combinat::permutations::fixedPointsNumber(standard permutation p)

Returns the number of fixed points of p.

Descents, major indice and major code of permutations

A descent of a permutation math is an integer math such that and math.

Let math be the increasing list of the descents of a permutation math. Let math be its corresponding composition. We say that math is the ribbon shape of math, or, equivalently that math has math as ribbon shape.

descents – descents of a permutation

combinat::permutations::descents(permutation p, <FinalDescent>)

Returns the list of the descents of the permutation p, that is, the list of integers math such that p[i]>p[i+1].

With the option FinalDescent, the last position of a non empty permutation is also considered as a descent.

descentsNumber – number of descents of a permutation

combinat::permutations::descentsNumber(permutation p, <FinalDescent>)

Returns the number of descents of the permutation p.

With the option FinalDescent, the last position of a non empty permutation is also considered as a descent.

majorIndex – major index of a permutation

combinat::permutations::majorIndex(permutation p, <FinalDescent>)

Returns the major index of the permutation p, that is the sum of the descents of p.

With the option FinalDescent, the last position of a non empty permutation is also considered as a descent.

majorCode – major code of a permutation

combinat::permutations::majorCode(permutation p)

Returns the major code of the permutation p, which is defined as the list [m1-m2, m2-m3,..,mn] where mi := maj(pi) is the major indices of the permutation math obtained by erasing the letters smaller than math in p.

fromMajorCode – permutation from its major code

combinat::permutations::fromMajorCode(major code c)

Returns the permutations whose major code is p. This is the converse bijection of majorCode.

descentPolynomial – descent polynomial of a permutation

combinat::permutations::descentPolynomial(permutation p, variable z)

Returns the descent polynomial of the permutation p in the variable z. It is the product of the monomials z[p[1]]z[p[2]]...z[p[i]] for all descents i of the permutation p.

Note that the the last position of a non empty permutation is never considered as a descent.

fromDescents – standard permutations with a given descent set

combinat::permutations::fromDescents(list or set s, <integer n>)

Returns the list of standard permutations of minimal size having the descent set s. Note that this minimal size corresponds to the greatest element in s.

The argument n, when provided, specifies the size of the returned permutation.

This is in fact a full-featured combinatorial class, with the usual methods count, generator, list, first, last.

fromDescentsComposition – standard permutations by ribbon shape

combinat::permutations::fromDescentsComposition(composition s)

Returns the list of standard permutations that have ribbon shape s, in lexicographic order.

This is a full-featured combinatorial class, with the usual methods count, generator, list, first, last.

fromDescentsCompositionFiner – standard permutations by finer descents composition

combinat::permutations::fromDescentsCompositionFiner(composition s)

Returns the list of standard permutations whose descent composition is finer than s, in lexicographic order.

This is a full-featured combinatorial class, with the usual methods count, generator, list, first, last.

fromDescentsCompositionFatter – standard permutations by fatter descents composition

combinat::permutations::fromDescentsCompositionFatter(composition s)

Returns the list of standard permutations whose descent composition is finer than s, in lexicographic order.

This is a full-featured combinatorial class, with the usual methods count, generator, list, first, last.

fromRecoilsComposition – standard permutations by recoils composition

combinat::permutations::fromRecoilsComposition(composition s)

Returns the list of standard permutations that have recoil composition s, in lexicographic order.

This is a full-featured combinatorial class, with the usual methods count, generator, list, first, last.

fromRecoilsCompositionFiner – standard permutations by finer recoils composition

combinat::permutations::fromRecoilsCompositionFiner(composition s)

Returns the list of standard permutations whose recoil composition is finer than s, in lexicographic order.

This is a full-featured combinatorial class, with the usual methods count, generator, list, first, last.

fromRecoilsCompositionFatter – standard permutations by fatter recoils composition

combinat::permutations::fromRecoilsCompositionFatter(composition s)

Returns the list of standard permutations whose recoil composition is finer than s, in lexicographic order.

This is a full-featured combinatorial class, with the usual methods count, generator, list, first, last.

descentsToMin – smallest permutation having a given descent set

combinat::permutations::descentsToMin(list or set s, <integer n>)

Returns the standard permutation of minimal size and smallest one with respect to the number of inversions that has the given descent set. Note that this minimal size corresponds to the greatest element in s.

The argument n, when provided, specifies the size of the returned permutation.

This method is deprecated. Please use "fromDescents::first".

descentsToMax – greatest permutation having a given descent set

combinat::permutations::descentsToMax(list or set s, <integer n>)

Returns the standard permutation of minimal size and greatest one with respect to the number of inversions that has the given descent set. Note that this minimal size corresponds to the greatest element in s.

The argument n, when provided, specifies the size of the returned permutation.

This method is deprecated. Please use "fromDescents::last".

descentsCompositionToMin – smallest permutation having a given ribbon shape

combinat::permutations::descentsCompositionToMin(composition s)

Returns the standard permutation of minimal size and smallest one with respect to the number of inversions that has the ribbon shape s. Note that this minimal size corresponds to the sum of the entries of s.

This method is deprecated. Please use "fromDescentsComposition::first".

descentsCompositionToMax – greatest permutation having a given ribbon shape

combinat::permutations::descentsCompositionToMax(composition s)

Returns the standard permutation of minimal size and greatest one with respect to the number of inversions that has the ribbon shape s. Note that this minimal size corresponds to the sum of the entries of s.

This method is deprecated. Please use "fromDescentsComposition::last".

youngSubgroup – Young subgroup

combinat::permutations::youngSubgroup(composition c)

Returns the list of the standard permutations in the Young subgroup (also called parabolic subgroup) math of the symmetric group math; otherwise said the standard permutations which stabilize the consecutive intervals math whose length are given by math.

Note: combinat::permutations::youngSubgroup is actually a combinatorial class, with the usual list, count and generator methods.

modYoungSubgroup – permutation modulo a Young subgroup

combinat::permutations::modYoungSubgroup(permutation p, composition c)

This returns a canonical representative of p modulo the right action of the Young subgroup math (see above) on the positions.

This boils down to sorting math in the consecutive intervals whose lengths are given by math.

fromSetPartition – permutations stabilizing a standard set partition

combinat::permutations::fromSetPartition(standard set partition setPartition)

setPartition is a set partition of math, represented by a list of lists or list of sets.

Returns the list of the standard permutations of math stabilizing each component of setPartition.

Note: combinat::permutations::fromSetPartition is actually a combinatorial class, with the usual list, count and generator methods.

Peaks of permutations

A peak of a permutation math is an integer math such that math and math.

peaks – peaks of a permutation

combinat::permutations::peaks(permutation p)

Returns the list of the peaks of the permutation p, that is the list of integers math such that math and math.

peaksNumber – number of peaks of a permutation

combinat::permutations::peaksNumber(permutation p)

Returns the number of peaks of the permutation p.

Saillances of permutations

A saillance of a permutation math is an integer math such that math, for all math.

saillances – saillances of a permutation

combinat::permutations::saillances(permutation p)

Returns the saillances of the permutation p.

This function may be renamed in the future.

saillancesNumber – saillances number of a permutation

combinat::permutations::saillancesNumber(permutation p)

Returns the number of saillances of the permutation p.

This function may be renamed in the future.

Bruhat order on standard permutations

The Bruhat order is a partial order defined on the elements of a given symmetric group, that is, on the permutations of a given size.

A permutation math is smaller than a permutation math if and only if one of the reduced words of math is a subsequence (the chosen letters do not have to be consecutive) of one of the reduced word of math.

The Bruhat order is the transitive closure of the following relation: math is smaller than math if math is obtained by multiplying math to the left or to the right by a transposition in such a way that it increases its number of inversions.

leequalBruhat – comparison of two permutations

combinat::permutations::leequalBruhat(standard permutations p1, p2)

Returns the boolean answering the question “Is p1 smaller than p2 in the Bruhat order?”

bruhatInversions – inversions in the Bruhat order

combinat::permutations::bruhatInversions(standard permutation p)

Returns the list of inversions of p such that the application of this inversion to p decrements its number of inversions.

Equivalently, it returns the list of pairs math, math such that math and such that there exists no math between math and math satisfying math.

predBruhat – predecessors of a permutation

combinat::permutations::predBruhat(standard permutation p)

Returns the list of permutations strictly smaller than p in the Bruhat order, such that there is no permutation between one of those and p.

succBruhat – successors of a permutation

combinat::permutations::succBruhat(standard permutation p)

Returns the list of permutations strictly greater than p in the Bruhat order, such that there is no permutation between p and one of those.

smallerBruhat – permutations smaller than a permutation

combinat::permutations::smallerBruhat(standard permutation p)

Returns the list of permutations smaller than or equal to p in the Bruhat order.

greaterBruhat – permutations greater than a permutation

combinat::permutations::greaterBruhat(standard permutation p)

Returns the list of permutations greater than or equal to p in the Bruhat order.

Permutohedron order on standard permutations

The permutohedron order is a partial order defined on the elements of a given symmetric group, that is, on the permutations of a given size. It is also known under the name of “weak Bruhat order”.

There are two different orders whether one make the permutations act to the left or to the right on the symmetric group. By default, computations are done in the right permutohedron.

A permutation math is smaller than a permutation math in the left (resp. right) permutohedron if and only if any reduced word of math is a suffix (resp. prefix) of one of the reduced word of math.

Equivalently, math is smaller than math in the left (resp. right) permutohedron if and only if one can go from math to math by multiplying math on the left (resp. right) by a sequence of elementary transpositions that increment at each step its number of inversions.

leequalPermutohedron – comparison of two permutations

combinat::permutations::leequalPermutohedron(standard permutations p1, p2, <Left | Right>)

Returns the boolean answering the question “Is p1 smaller than p2 in the permutohedron order?”

With no option given or with the option Right, computations are done in the right permutohedron. With the option Left, computations are done in the left permutohedron.

predPermutohedron – predecessors of a permutation

combinat::permutations::predPermutohedron(standard permutation p, <Left | Right>)

Returns the list of permutations strictly smaller than p in the permutohedron order, such that there is no permutation between one of those and p.

With no option given or with the option Right, computations are done in the right permutohedron. With the option Left, computations are done in the left permutohedron.

succPermutohedron – successors of a permutation

combinat::permutations::succPermutohedron(standard permutation p, <Left | Right>)

Returns the list of permutations strictly greater than p in the permutohedron order, such that there is no permutation between p and one of those.

With no option given or with the option Right, computations are done in the right permutohedron. With the option Left, computations are done in the left permutohedron.

smallerPermutohedron – permutations smaller than a permutation

combinat::permutations::smallerPermutohedron(standard permutation p, <Left | Right>)

Returns the list of permutations smaller than or equal to p in the permutohedron order.

With no option given or with the option Right, computations are done in the right permutohedron. With the option Left, computations are done in the left permutohedron.

greaterPermutohedron – permutations greater than a permutation

combinat::permutations::greaterPermutohedron(standard permutation p, <Left | Right>)

Returns the list of permutations greater than or equal to p in the permutohedron order.

With no option given or with the option Right, computations are done in the right permutohedron. With the option Left, computations are done in the left permutohedron.

Patterns of standard permutations

A permutation math of size math  avoids the pattern  math of size math if and only if there is no subsequence math in math such that math.

hasPattern – test of the existence of a pattern in a standard permutation

combinat::permutations::hasPattern(standard permutations perm, patt)

Returns the boolean answering the question “Is patt a pattern appearing in perm?”

patternPositions – positions of a pattern in a permutation

combinat::permutations::patternPositions(standard permutations perm, patt)

Returns the list of positions where the pattern patt appears in the standard permutations perm.

Example 1:

There are math permutations of the list [a,b,c]:

combinat::permutations::count([a, b, c])

math

Here is the list:

combinat::permutations::list([a, b, c])

math

For the permutations of [1,2,3], it is shorter to write:

combinat::permutations::count(3);

combinat::permutations::list(3);

math

math

You can use the function combinat::permutations::first to get the “first” permutation of a list or the first standard permutation of a given size:

combinat::permutations::first([c, b, a, d]);

combinat::permutations::first(4)

math

math

Using combinat::permutations::Next, you can calculate the “next” permutation. This function takes as argument the permutation relative to which you want the next one:

combinat::permutations::Next([1, 3, 2, 4])

math

combinat::permutations::Next returns FAIL when the input is the last permutation:

combinat::permutations::Next([4, 3, 2, 1])

math

You can also go backward with combinat::permutations::last and combinat::permutations::previous

combinat::permutations::last([1, 2, 3, 4]);   

combinat::permutations::previous([1, 3, 2, 4]);

math

math

When you want to run through the permutations of a list, you can generate them one by one to save memory:

g := combinat::permutations::generator(3):

g();

g(), g(), g(), g(), g(), g();

h := combinat::permutations::generator([c, a, b]):

h();

h(), h(), h(), h(), h(), h();

math

math

math

math

Typically, this would be used as follows:

g := combinat::permutations::generator(3):

while (p := g()) <> FAIL do print(p): end:

[1, 2, 3]

 

[1, 3, 2]

 

[2, 1, 3]

 

[2, 3, 1]

 

[3, 1, 2]

 

[3, 2, 1]

 

Example 2:

By default, the permutations of a given list are all distinct lists obtained by permuting its elements:

combinat::permutations::count([a, a, b]);

combinat::permutations::list([a, a, b]);

math

math

To consider reorderings with multiplicities, one can use the Duplicate option:

combinat::permutations::count([a, a, b], Duplicate);

combinat::permutations::list([a, a, b], Duplicate);

math

math

Note that with the option Duplicate as above, the permutations are not guaranted to be listed in lexicographic order.

Example 3:

You can get the rank of a permutation using:

combinat::permutations::rank([3, 6, 5, 4, 2, 1]);

math

Conversely, you can recover the permutation of rank math and size math using:

combinat::permutations::unrank(360,6);

math

Example 4:

Here is the way to get a random permutation:

combinat::permutations::random([a,a,b,c,d,d,e,f]);

combinat::permutations::random(10);

math

math

Example 5:

In this example, we show how to use some natural associative operations on permutations.

The shifted concatenation is a variant on the usual concatenation of words which preserves standard permutations by shifting appropriately the elements of the second operand:

    combinat::permutations::shiftedConcat2([1,2,3], [1,2])

math

    combinat::permutations::shiftedConcat2([3,2,1], [2,1])

math

This binary operation is in fact associative; here is the n-ary version of it:

    combinat::permutations::shiftedConcat([3,2,1], [2,1], [1,2,3])

math

and the unit of this operation is the emtpy permutation:

    combinat::permutations::shiftedConcat()

math

Example 6:

It is sometimes useful to represent a standard permutation by its matrix:

combinat::permutations::toMatrix([2, 4, 1, 5, 3]);

math

The inverse permutation is computed by:

combinat::permutations::inverse([2, 4, 1, 5, 3])

math

It corresponds to the transpose matrix:

combinat::permutations::toMatrix(

   combinat::permutations::inverse([2,4,1, 5, 3])),

linalg::transpose(

   combinat::permutations::toMatrix([2,4, 1, 5, 3]))

math

Example 7:

The number of inversions of a permutation, also called its length, can be computed with:

combinat::permutations::inversionsNumber([1, 2, 6, 4, 7, 3, 5])

math

Here is the list of the math inversions of the previous permutation:

combinat::permutations::inversions([1, 2, 6, 4, 7, 3, 5])

math

Note that this also works for non-standard permutations, as long as the elements can be ordered with <:

combinat::permutations::inversionsNumber([1, 3, 2, 1, 2, 1]);

combinat::permutations::inversions([1, 3, 2, 1, 2, 1]);

combinat::permutations::inversions(["c", "b", "a"])

math

math

math

The sign of any transposition is math:

combinat::permutations::sign([4, 2, 3, 1, 5])

math

Furthermore, the sign defines a multiplicative morphism from the symmetric group into math. We check this for the symmetric group of order math:

bool(_and(combinat::permutations::sign(combinat::permutations::_mult(p,q))

        = combinat::permutations::sign(p)*combinat::permutations::sign(q)

          $ p in combinat::permutations(4)

          $ q in combinat::permutations(4)))

math

Example 8:

The code provides a bijection between permutations of [$1..n] and lists c of length n such that c[i]<=n-i:

for p in combinat::permutations(3) do

    code := combinat::permutations::code(p);

    p2   := combinat::permutations::fromCode(code);

    print(p, code, p2);

end_for

[1, 2, 3], [0, 0, 0], [1, 2, 3]

 

[1, 3, 2], [0, 1, 0], [1, 3, 2]

 

[2, 1, 3], [1, 0, 0], [2, 1, 3]

 

[2, 3, 1], [1, 1, 0], [2, 3, 1]

 

[3, 1, 2], [2, 0, 0], [3, 1, 2]

 

[3, 2, 1], [2, 1, 0], [3, 2, 1]

 

If math is a non standard permutation, then decoding the code of math yields the standardized of math:

w := [2, 3, 1, 2, 2, 4, 5, 1]:

code := combinat::permutations::code(w);

combinat::words::standard(w), combinat::permutations::fromCode(code)

math

math

Example 9:

The permutation corresponding to a given product of elementary transpositions can be computed with:

combinat::permutations::fromReducedWord([3,2,3,1,2,3,1]);

math

The word we entered previously is not reduced. Indeed, all reduced words of a given permutation have the same length, equal to the number of inversions of [3, 4, 2, 1]. It is not the case of the product we studied before.

map(combinat::permutations::reducedWords([3,4,2,1]),nops);

nops([3,2,3,1,2,3,1]);

math

math

If you have an action of the symmetric group on a set, defined by the action of the elementary transpositions, you can compute a reduced decomposition of a given permutation with:

combinat::permutations::reducedWord([3,5,4,6,2,1]);

math

Example 10:

The cycles of a permutation can be computed with:

combinat::permutations::cycles([3,5,1,2,4]);

math

You can recover the initial permutation by two different ways, forgetting or not the cycle of length math:

combinat::permutations::fromCycles([[1, 3], [2, 5], [4]]),

combinat::permutations::fromCycles([[1, 3], [2, 5]]);

math

You can generate the list of permutations with a given cycle type, which is a conjugacy class of the symmetric group, and then check that their number corresponds to the usual formula:

select(combinat::permutations::list(4),

  z-> combinat::permutations::cycleType(z)=[2,2]);

nops(%), 4!/(2^2*2!);

math

math

Example 11:

To compute the list or the number of fixed points of a permutation, you can use:

combinat::permutations::fixedPoints([6, 7, 3, 8, 5, 9, 2, 1, 4]);

combinat::permutations::fixedPointsNumber([6, 7, 3, 8, 5, 9, 2, 1, 4]);

math

math

It can be used to quickly get an estimation of the number of permutations without fixed points, using the following main block inside a loop, and check the hats paradox:

combinat::permutations::fixedPoints(combinat::permutations::random(20));

                             [13]

 

Example 12:

The descents of a permutation are computed as:

combinat::permutations::descents([2, 1, 6, 4, 7, 3, 5])

math

The sum of the descents is called the major index:

combinat::permutations::majorIndex([2, 1, 6, 4, 7, 3, 5])

math

It is the total degree of the descent polynomial, that is always a monomial on permutations:

combinat::permutations::descentPolynomial([2, 1, 6, 4, 7, 3, 5],z)

math

Example 13:

We compute the major code of a permutation:

combinat::permutations::majorCode([2, 1, 6, 4, 7, 3, 5])

math

The sum of the major code is the major index:

combinat::permutations::majorIndex([2, 1, 6, 4, 7, 3, 5])

math

One can retrieve a permutations knowing its major code:

combinat::permutations::fromMajorCode([3, 2, 0, 2, 2, 0, 0])

math

Example 14:

To compute the smallest element of a descent class, you can use:

combinat::permutations::fromDescents::first([2, 1, 5, 9], 12)

math

To get the same result using ribbon shapes, one can write:

combinat::permutations::fromDescentsComposition::first([1, 1, 3, 4, 3])

math

To compute the greatest element of a descent class, you can use:

combinat::permutations::fromDescents::last([2, 1, 5, 9], 12)

math

To get the same result using ribbon shapes, one can write:

combinat::permutations::fromDescentsComposition::last([1, 1, 3, 4, 3])

math

Example 15:

To compute the list of permutations that have a given descent set, you can use:

combinat::permutations::fromDescents([3, 5, 1])

math

To get the same result using ribbon shapes, one can write:

combinat::permutations::fromDescentsComposition([1, 2, 2])

math

Here are all the permutations in the Young subgroup math:

combinat::permutations::youngSubgroup([2, 3])

math

Taking canonical representative of the orbit of words w.r.t. the action of this Young subgroup on positions is trivial; it suffices to sort the two first and the last three letters:

combinat::permutations::modYoungSubgroup([c, b, e, a, d], [2, 3])

math

Example 16:

The peaks of a permutation are computed as:

combinat::permutations::peaks([2, 1, 6, 4, 7, 3, 5])

math

The number of peaks can be directly computed using:

combinat::permutations::peaksNumber([2, 1, 6, 4, 7, 3, 5])

math

Example 17:

The saillances of a permutation are computed as:

combinat::permutations::saillances([7, 1, 4, 6, 2, 5, 3]);

math

The number of saillances can be directly computed using:

combinat::permutations::saillancesNumber([7, 1, 4, 6, 2, 5, 3]);

math

Example 18:

To check whether a permutation is smaller than another thanks to the Bruhat order, you can call:

combinat::permutations::leequalBruhat([2,4,3,1],[3,4,2,1]);

math

You can ask for the inversions of a given permutation by:

combinat::permutations::bruhatInversions([6, 1, 4, 5, 2, 3])

math

Applying those inversions to the previous permutation, you exactly get the permutations preceding it as you can check with

combinat::permutations::predBruhat([6, 1, 4, 5, 2, 3])

math

Now, you can compute the transitive ideal generated by the predecessor relation and get (on the previous example, the answer is one page long)

combinat::permutations::smallerBruhat([4, 1, 2, 3])

math

What was done with the permutations preceding a given one or smaller than it, can also be performed by the permutations succeeding a given one or greater than it by:

combinat::permutations::succBruhat([6, 1, 4, 5, 2, 3]);

combinat::permutations::greaterBruhat([4, 1, 2, 3])

math

math

Example 19:

All the computations in this example are done in the right permutohedron. To check whether a permutation is smaller than another thanks to the permutohedron order, you can use:

combinat::permutations::leequalPermutohedron([3,2,1,4],[4,2,1,3]);

math

To compute what permutations precede a given permutation, you can use:

combinat::permutations::predPermutohedron([4, 2, 1, 3]);

math

Now, you can compute the transitive ideal generated by the predecessor relation and get:

combinat::permutations::smallerPermutohedron([4, 2, 1, 3]);

math

What was done with the permutations preceding a given one or smaller than it, can also be performed by the permutations succeeding a given one or greater than it by:

combinat::permutations::succPermutohedron([4, 2, 1, 3]);

combinat::permutations::greaterPermutohedron([4, 2, 1, 3]);

math

math

Example 20:

All the computations in this example are done in the left permutohedron. All the result can be compared with the results of the previous example. To check whether a permutation is smaller than another thanks to the permutohedron order, you can use:

combinat::permutations::leequalPermutohedron([3,2,1,4],[4,2,1,3],Left);

math

To compute what permutations precede a given permutation in the left permutohedron, you can use:

combinat::permutations::predPermutohedron([4, 2, 1, 3], Left);

math

Now, you can compute the transitive ideal generated by the predecessor relation in the left permutohedron and get:

combinat::permutations::smallerPermutohedron([4, 2, 1, 3], Left);

math

What was done with the permutations preceding a given one or smaller than it, can also be performed by the permutations succeeding a given one or greater than it by:

combinat::permutations::succPermutohedron([4, 2, 1, 3], Left);

combinat::permutations::greaterPermutohedron([4, 2, 1, 3], Left);

math

math

Example 21:

To check whether a permutation contains a given pattern, you can use:

combinat::permutations::hasPattern([3, 5, 1, 4, 6, 2], [1, 3, 2])

math

To look at the positions where the pattern appears, you can ask:

combinat::permutations::patternPositions([3, 5, 1, 4, 6, 2], [1, 3, 2])

math