Вы находитесь на странице: 1из 10

Printed from: Wolfram Mathematica - Complete Documentation

Permutations
Representation of a Permutation

Product of Permutations

Permutation Action and Support

Equality and Sorting of Permutations

Permute Parts of an Expression

Special Types of Permutations

Permutations are basic elements in algebra. They have a natural non-commutative product (as matrices do as well), and
hence can encode highly nontrivial structures in a compact way. Permutations provide a way of representing any finite
group, which makes them key tools in many applications in mathematics, science, engineering, or even art. In particular,
permutations play a central role in the description of discrete symmetries.
Permutations are, roughly speaking, reorderings of a set of elements, or more precisely, bijections from the set onto
itself. Only sets with a finite number of elements will be considered. The number of possible permutations of a set of n
elements is n !, and therefore for a moderate number n 100 there are already 100 ! permutations, which is almost 10158 .
This tutorial discusses how to manipulate permutations in cyclic notation in Mathematica, and "Permutation Lists"
describes the relation to permutation list notation. Other tutorials, "Permutation Groups" and "Named Groups", describe
how to work with groups of permutations, and "Group Theory Algorithms" shows how to extract information from them
without listing all elements of the group.

Representation of a Permutation
Cycles

head denoting a permutation in disjoint cyclic form

PermutationCyclesQ

validate a permutation

Representation of a permutation.

The disjoint cyclic representation of a permutation has the form Cycles @8cyc1 , cyc2 , <D in Mathematica, where the
cycles cyci are disjoint lists of positive integers. Integers are mapped under the permutation to their right neighbors, and
the last integer of a cycle is mapped to the first member of that cycle. Integers not present in the cycles are mapped onto
themselves, though they could also appear in cycles of length 1, which are called singletons or fixed points. The ordering
of cycles is immaterial, and individual cycles can be rotated without changing the permutation. Permutations are automatically canonicalized so that the smallest integer of each cycle comes first, and then cycles are sorted by their first integer.
This is a permutation with two cycles 3 8 5 3 and 6 1 6.
In[1]:=
Out[1]=

Cycles@883, 8, 5<, 86, 1<<D

Cycles@881, 6<, 83, 8, 5<<D

This represents the same permutation. Singletons and empty cycles are removed.
In[2]:=
Out[2]=

Cycles@881, 6<, 82<, 85, 3, 8<, 84<, 87<, 89<, 8<, 810<<D

Cycles@881, 6<, 83, 8, 5<<D

19882013 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com

Printed from: Wolfram Mathematica - Complete Documentation

Any permutation with only singletons represents the identity.


In[3]:=

Cycles@885<, 88<, 83<<D

Out[3]=

Cycles@8<D

With permutations containing explicit lists of numbers, there is automatic syntax checking. In other cases, you can use
the function PermutationCyclesQ to check the syntax.
The cycles must be disjoint.
In[4]:=

Cycles@881, 2<, 81, 3<<D

Cycles::reppoint : Cycles@881, 2<, 81, 3<<D contains repeated integers.

Out[4]=

Cycles@881, 2<, 81, 3<<D

Integers must be positive.


In[5]:=

Cycles@881, 0<, 8-2, 3<<D

Out[5]=

Cycles@881, 0<, 8-2, 3<<D

Cycles::pospoint : Cycles@881, 0<, 8-2, 3<<D contains integers that are not positive.

Numbers other than integers are not accepted.


In[6]:=

Cycles@881, 2<, 83, 1 2<<D

Cycles::intpoint : CyclesB:81, 2<, :3,


Out[6]=

CyclesB:81, 2<, :3,

1
2

>>F expected to contain lists of integers.

>>F

This is a valid permutation in cyclic form.


In[7]:=

PermutationCyclesQ@Cycles@881, 6<, 83, 8, 5<<DD

Out[7]=

True

These are invalid permutations, for obvious reasons.


In[8]:=

PermutationCyclesQ@Cycles@881, 2<, 80, 3<<DD

Cycles::pospoint : Cycles@881, 2<, 80, 3<<D contains integers that are not positive.
Out[8]=

False

In[9]:=

PermutationCyclesQ@Cycles@88a, 1<, 82, 3<<DD

Out[9]=

False

In[10]:=

PermutationCyclesQ@Cycles@8cycle1, cycle2<DD

Out[10]=

False

Permutation Action and Support


If a permutation perm maps the integer p to the integer q, then q is called the image of p under the permutation perm.
Images are computed with the function PermutationReplace .

19882013 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com

Printed from: Wolfram Mathematica - Complete Documentation

If a permutation perm maps the integer p to the integer q, then q is called the image of p under the permutation perm.
Images are computed with the function PermutationReplace .

PermutationReplace

image of an integer under a permutation

Standard action of permutations on other expressions.

This is the image of 3.


In[11]:=

PermutationReplace@3, Cycles@882, 3, 4<<DD

Out[11]=

These are the images of the first 6 integers.


In[12]:=
Out[12]=

PermutationReplace@Range@6D, Cycles@882, 3, 4<<DD

81, 3, 4, 2, 5, 6<

The standard action of permutations can be extended to other objects, like other permutations or arrays of integers or
permutations.
Map integers of the first permutation under the second permutation (conjugation).
In[13]:=
Out[13]=

PermutationReplace@Cycles@881, 6<, 83, 8, 5<<D, Cycles@883, 2, 1<<DD

Cycles@882, 8, 5<, 83, 6<<D

Permutations are not assumed to belong to any particular finite group, not even a particular symmetric group of some
degree. However, there is the concept of support, defined as the set of integers moved by the permutation, which better
describes where a permutation acts naturally.

PermutationSupport

set of integers moved by a permutation

PermutationLength

number of integers moved by a permutation

PermutationMax

largest integer moved by a permutation

PermutationMin

smallest integer moved by a permutation

Permutation support functions.

The following permutation moves only five points.


In[14]:=
Out[14]=

In[15]:=

perm = Cycles@884, 18, 55<, 821, 37<<D

Cycles@884, 18, 55<, 821, 37<<D


PermutationSupport@permD

Out[15]=

84, 18, 21, 37, 55<

In[16]:=

PermutationLength@permD

Out[16]=

19882013 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com

Printed from: Wolfram Mathematica - Complete Documentation

The largest moved point is 55.


In[17]:=

PermutationMax@permD

Out[17]=

55

The smallest moved point is 4.


In[18]:=

PermutationMin@permD

Out[18]=

RandomPermutation

generate pseudorandom permutations

Random generation of permutations.

Generate a random permutation moving (perhaps not all) integers 81, , 15<.
In[19]:=
Out[19]=

SeedRandom@1234D; RandomPermutation@15D

Cycles@881, 15, 4, 10, 2<, 83, 7, 8, 12, 6, 14<, 85, 13<<D

Generate several random permutations in a given group.


In[20]:=
Out[20]=

SeedRandom@1234D; RandomPermutation@AlternatingGroup@7D, 5D

8Cycles@881, 7, 4, 6, 2, 5, 3<<D, Cycles@882, 3, 7<<D,


Cycles@882, 4<, 86, 7<<D, Cycles@881, 4, 5, 7, 6<<D, Cycles@881, 3, 6, 7, 2<<D<

Permute Parts of an Expression


Permutations can be used to permute the parts of other expressions with the function Permute . Integer p being mapped
to integer q is interpreted as part p being moved to part q. Permute never changes the number of elements of an expression, it simply reorders them.

Permute

permute parts of an expression

FindPermutation

return permutation linking two expressions with the same elements

Reordering under a permutation.

Swap the first and last letters of the alphabet.


In[21]:=
Out[21]=

Permute@CharacterRange@"a", "z"D, Cycles@881, 26<<DD

8z, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, a<

Permute three parts of an expression, leaving the rest invariant.


In[22]:=

expr = f@u, v, w, x, y, zD;


perm = Cycles@882, 3, 4<<D;

In[24]:=

Permute@expr, permD

Out[24]=

f@u, x, v, w, y, zD

19882013 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com

Printed from: Wolfram Mathematica - Complete Documentation

The same result can be obtained using a permutation list representation and the function Part .
In[25]:=
Out[25]=

permlist = PermutationList@perm, 6D

81, 3, 4, 2, 5, 6<

In[26]:=

expr@@Ordering permlistDD

Out[26]=

f@u, x, v, w, y, zD

In[27]:=

PermutationCycles@permlistD === perm

Out[27]=

True

However, the length of the permutation list must match that of the expression.
In[28]:=

expr = f@"a", "b", "c", "d", "e", "f", "g", "h"D;


Permute@expr, permD

Out[29]=

f@a, d, b, c, e, f, g, hD

In[30]:=

expr@@Ordering permlistDD

Out[30]=

f@a, d, b, c, e, fD

Permutation lists can also be used with the function Permute .


In[31]:=

Permute@expr, permlistD

Out[31]=

f@a, d, b, c, e, f, g, hD

Vice versa, given two expressions with the same elements, a permutation linking them can be computed.
In[32]:=

expr1 = f@u, v, w, x, y, zD;


expr2 = f@u, x, v, w, y, zD;

In[34]:=

FindPermutation@expr1, expr2D

Out[34]=

Cycles@882, 3, 4<<D

In[35]:=

Permute@expr1, %D === expr2

Out[35]=

True

Reversing the two arguments gives the inverse permutation.


In[36]:=

FindPermutation@expr2, expr1D

Out[36]=

Cycles@882, 4, 3<<D

With a single argument, FindPermutation returns the permutation that generates an expression from its canonical order.
In[37]:=

FindPermutation@Characters@"Mathematica"DD

Out[37]=

Cycles@881, 2, 7, 9<, 83, 11, 8, 6, 4, 10<<D

In[38]:=

Permute@Sort Characters@"Mathematica"D, %D

Out[38]=

8M, a, t, h, e, m, a, t, i, c, a<

19882013 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com

Printed from: Wolfram Mathematica - Complete Documentation

Product of Permutations
PermutationProduct

product of permutations (non-commutative)

InversePermutation

inverse of a permutation

PermutationPower

integer power (product with itself or inverse) of a permutation

PermutationOrder

lowest positive power of a permutation yielding the identity

Product of permutations.

There are two possible conventions for the product of two permutations perm1 and perm2 , depending on whether

PermutationProduct @ perm1 , perm2 D means that you first use perm1 and then perm2 (this is called a left-to-right product) or
you first use perm2 and then perm1 (right-to-left product). With the convention of writing images as right neighbors in
cycles, Mathematica's PermutationProduct effectively is a left-to-right product.
The product of permutations is not commutative.
In[39]:=

perm1 = Cycles@882, 3, 4<<D;


perm2 = Cycles@881, 2<, 83, 4<<D;

In[41]:=

PermutationProduct@perm1, perm2D

Out[41]=

Cycles@881, 2, 4<<D

In[42]:=

PermutationProduct@perm2, perm1D

Out[42]=

Cycles@881, 3, 2<<D

PermutationReplace acts from the right, consistent with the left-to-right character of the product.
In[43]:=

PermutationReplace@PermutationReplace@3, perm1D, perm2D ===


PermutationReplace@3, PermutationProduct@perm1, perm2DD

Out[43]=

True

In[44]:=

PermutationReplace@PermutationReplace@3, perm1D, perm2D ===


PermutationReplace@3, PermutationProduct@perm2, perm1DD

Out[44]=

False

Permute also acts from the right.


In[45]:=

Permute@Permute@8x, y, z, t<, perm1D, perm2D === Permute@8x, y, z, t<, PermutationProduct@perm1, perm2DD

Out[45]=

True

In[46]:=

Permute@Permute@8x, y, z, t<, perm1D, perm2D === Permute@8x, y, z, t<, PermutationProduct@perm2, perm1DD

Out[46]=

False

After the permutation product law has been given, the associated concepts of inversion, power, and order can be immediately defined. For permutations of finite support, the order is always finite.
You can compute inverses of permutations.
In[47]:=

InversePermutation@perm1D

Out[47]=

Cycles@882, 4, 3<<D

19882013 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com

Printed from: Wolfram Mathematica - Complete Documentation

In[48]:=

PermutationProduct@perm1, %D

Out[48]=

Cycles@8<D

And you can compute powers of them. Negative powers are powers of the inverse.
In[49]:=

PermutationPower@perm1, 4D

Out[49]=

Cycles@882, 3, 4<<D

In[50]:=

PermutationProduct@perm1, perm1, perm1, perm1D === %

Out[50]=

True

In[51]:=

PermutationPower@perm2, -11D

Out[51]=

Cycles@881, 2<, 83, 4<<D

In[52]:=

PermutationPower@perm1, 0D

Out[52]=

Cycles@8<D

The order of a permutation is the lowest positive power such that the identity permutation is obtained.
In[53]:=

PermutationOrder@perm1D

Out[53]=

In[54]:=

PermutationPower@perm1, %D

Out[54]=

Cycles@8<D

Another example.
In[55]:=

perm3 = Cycles@
881, 9, 12, 3<, 82, 17, 4, 29, 20, 30, 26, 15, 14<, 88, 10, 16, 13, 27, 25, 22, 28, 23, 19, 24, 18, 11<<D;

In[56]:=

order = PermutationOrder@perm3D

Out[56]=

468

In[57]:=

PermutationPower@perm3, orderD

Out[57]=

Cycles@8<D

This means that (for permutations of finite support) the inverse is always a positive power of the permutation.
In[58]:=

InversePermutation@perm3D == PermutationPower@perm3, order - 1D

Out[58]=

True

Equality and Sorting of Permutations


As is standard in Mathematica, there are two types of equality tests: structural equality (SameQ ) and mathematical
equality (Equal ). The former can compare any two expressions, but the latter will only return True or False when
comparing mathematical expressions with the same type of value. The same happens when ordering expressions: there is
structural (canonical) order, implemented through Order and OrderedQ , and there is mathematical order, given by Less
and related functions.
Any two permutations of any degree can always be tested for equality and ordering. This is done by comparing sequentially the images of the integers 1, 2, 3, . The smaller permutation corresponds to smaller images, such that the
identity permutation always comes first. This defines mathematical order. Canonical order follows standard rules, and may
differ from mathematical order.
19882013 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com

Printed from: Wolfram Mathematica - Complete Documentation

Any two permutations of any degree can always be tested for equality and ordering. This is done by comparing sequentially the images of the integers 1, 2, 3, . The smaller permutation corresponds to smaller images, such that the
identity permutation always comes first. This defines mathematical order. Canonical order follows standard rules, and may
differ from mathematical order.
Take two permutations.
In[59]:=

perm1 = Cycles@882, 5, 4<<D;


perm2 = Cycles@883, 4, 7<, 85, 6<<D;
These are the images of the first 7 integers under them.

In[61]:=

PermutationReplace@Range@7D, perm1D

Out[61]=

81, 5, 3, 2, 4, 6, 7<

In[62]:=

PermutationReplace@Range@7D, perm2D

Out[62]=

81, 2, 4, 7, 6, 5, 3<

Permutation perm2 must be sorted before perm1 because its second image is smaller.
In[63]:=

perm1 < perm2

Out[63]=

False

The standard canonical order will sort smaller expressions first.


In[64]:=
Out[64]=

Sort@8Cycles@881, 3<<D, Cycles@881, 2, 3<<D, Cycles@881, 3, 2<<D<D

8Cycles@881, 3<<D, Cycles@881, 2, 3<<D, Cycles@881, 3, 2<<D<

You can sort permutations using mathematical order by using an "ordering function".
In[65]:=
Out[65]=

Sort@8Cycles@881, 3<<D, Cycles@881, 2, 3<<D, Cycles@881, 3, 2<<D<, LessEqualD

8Cycles@881, 2, 3<<D, Cycles@881, 3, 2<<D, Cycles@881, 3<<D<

You can check whether a list of permutations is sorted.


In[66]:=

OrderedQ@%, LessEqualD

Out[66]=

True

Special Types of Permutations


There are special types of permutations. In many cases the cyclic notation allows simple constructions to detect them.

Involutions contain only cycles of maximal length 2.


In[67]:=

permutationInvolutionQAperm_ ? PermutationCyclesQE := MatchQAperm, CyclesA99_, _= ...=EE

In[68]:=

permutationInvolutionQ@Cycles@881, 2<, 84, 8<, 83, 6<<DD

Out[68]=

True

In[69]:=

PermutationPower@Cycles@881, 2<, 84, 8<, 83, 6<<D, 2D

Out[69]=

Cycles@8<D
A permutation is a derangement of degree n if all integers 81, , n< are moved.

In[70]:=

permutationDerangementQAperm_ ? PermutationCyclesQ, n_E := Union First@permD === Range@nD

19882013 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com

Printed from: Wolfram Mathematica - Complete Documentation

In[71]:=

permutationDerangementQ@Cycles@881, 3, 5<, 82, 4<<D, 5D

Out[71]=

True

In[72]:=

permutationDerangementQ@Cycles@881, 2, 4, 5<<D, 5D

Out[72]=

False

A permutation is said to be circular of degree n if it consists of a single cycle of length n.


In[73]:=

circularPermutationQAperm_ ? PermutationCyclesQ, n_E :=


Length First@permD === 1 && Length First First@permD === n

In[74]:=

circularPermutationQ@Cycles@881, 5, 3, 2, 6, 4<<D, 6D

Out[74]=

True

In[75]:=

circularPermutationQ@Cycles@881, 3, 5<, 82, 4<<D, 5D

Out[75]=

False

From the cyclic notation of a permutation it is also simple to construct a decomposition into transpositions, that is, permutations with a single cycle of length 2.
These functions construct a list of transpositions whose product is the original permutation.
In[76]:=

breakCycleA9first_, rest___=E := 8first, < & 8rest<


toTranspositionsAperm_ ? PermutationCyclesQE := Cycles List Flatten@breakCycle First@permD, 1D

In[78]:=

perm = Cycles@881, 2, 5<, 83, 7<, 86, 8, 10<<D;

In[79]:=

toTranspositions@permD

Out[79]=

8Cycles@881, 2<<D, Cycles@881, 5<<D, Cycles@883, 7<<D, Cycles@886, 8<<D, Cycles@886, 10<<D<

In[80]:=

perm == PermutationProduct %

Out[80]=

True

Such decomposition into transpositions is not unique, but the parity (even or odd) of the number of transpositions is invariant.
This is called the signature of the permutation.
In[81]:=

permutationSignatureAperm_ ? PermutationCyclesQE := Apply@Times, H-1L ^ HLength First@permD - 1LD

In[82]:=

permutationSignature@Cycles@884, 3, 5, 2, 1<<DD

Out[82]=

In[83]:=

permutationSignature@Cycles@881, 2, 3<, 84, 5<<DD

Out[83]=

-1

The previous example permutation perm was decomposed into five transpositions, an odd number. Hence it has signature -1.
In[84]:=

permutationSignature@permD

Out[84]=

-1

Related Tutorials

Permutation Lists

19882013 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com

Printed from: Wolfram Mathematica - Complete Documentation

Permutation Groups

19882013 Wolfram Research, Inc. All rights reserved. http://reference.wolfram.com

10

Вам также может понравиться