Regina 7.3 Calculation Engine
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
regina::Perm< n > Class Template Reference

Represents a permutation of {0,1,...,n-1}. More...

#include <maths/perm.h>

Public Types

using Index = typename IntOfMinSize<(imageBits *n+7)/8 >::type
 Denotes a native signed integer type large enough to count all permutations on n elements. More...
 
using ImagePack = typename IntOfMinSize<(imageBits *n+7)/8 >::utype
 Indicates the native unsigned integer type used to store a single image pack. More...
 
using Code = ImagePack
 Indicates the native unsigned integer type used to store the internal permutation code. More...
 

Public Member Functions

constexpr Perm ()
 Creates the identity permutation. More...
 
constexpr Perm (int a, int b)
 Creates the transposition of a and b. More...
 
constexpr Perm (const std::array< int, n > &image)
 Creates a permutation mapping i to image[i] for each 0 ≤ i < n. More...
 
constexpr Perm (const Perm &)=default
 Creates a permutation that is a clone of the given permutation. More...
 
constexpr Code permCode () const
 Returns the internal code representing this permutation. More...
 
void setPermCode (Code code)
 Sets this permutation to that represented by the given internal code. More...
 
constexpr ImagePack imagePack () const
 Returns the image pack that represents this permutation. More...
 
Permoperator= (const Perm &cloneMe)=default
 Sets this permutation to be equal to the given permutation. More...
 
constexpr Perm operator* (const Perm &q) const
 Returns the composition of this permutation with the given permutation. More...
 
Perm cachedComp (const Perm &q) const
 An alias for the composition operator, provided to assist with writing generic code. More...
 
Perm cachedComp (const Perm &q, const Perm &r) const
 Deprecated alias for using the composition operator twice, provided to assist with writing generic code. More...
 
constexpr Perm conjugate (const Perm &q) const
 Computes the conjugate of this permutation by q. More...
 
Perm cachedConjugate (const Perm &q) const
 An alias for conjugate(), provided to assist with writing generic code. More...
 
constexpr Perm inverse () const
 Finds the inverse of this permutation. More...
 
Perm cachedInverse () const
 Finds the inverse of this permutation, optimised using precomputed "partial lookup tables". More...
 
constexpr Perm pow (long exp) const
 Computes the given power of this permutation. More...
 
Perm cachedPow (long exp) const
 An alias for pow(), provided to assist with writing generic code. More...
 
constexpr int order () const
 Returns the order of this permutation. More...
 
int cachedOrder () const
 An alias for order(), provided to assist with writing generic code. More...
 
constexpr Perm reverse () const
 Finds the reverse of this permutation. More...
 
constexpr int sign () const
 Determines the sign of this permutation. More...
 
constexpr int operator[] (int source) const
 Determines the image of the given integer under this permutation. More...
 
constexpr int pre (int image) const
 Determines the preimage of the given integer under this permutation. More...
 
constexpr bool operator== (const Perm &other) const
 Determines if this is equal to the given permutation. More...
 
constexpr bool operator!= (const Perm &other) const
 Determines if this differs from the given permutation. More...
 
constexpr int compareWith (const Perm &other) const
 Lexicographically compares the images of (0,1,...,n-1) under this and the given permutation. More...
 
constexpr bool isIdentity () const
 Determines if this is the identity permutation. More...
 
Permoperator++ ()
 A preincrement operator that changes this to be the next permutation in the array Perm<n>::Sn. More...
 
Perm operator++ (int)
 A postincrement operator that changes this to be the next permutation in the array Perm<n>::Sn. More...
 
constexpr bool operator< (const Perm &rhs) const
 Determines if this appears earlier than the given permutation in the array Perm<n>::Sn. More...
 
constexpr Index SnIndex () const
 Returns the index of this permutation in the Perm<n>::Sn array. More...
 
constexpr Index orderedSnIndex () const
 Returns the lexicographical index of this permutation. More...
 
std::string str () const
 Returns a string representation of this permutation. More...
 
std::string trunc (int len) const
 Returns a prefix of the string representation of this permutation, containing only the images of the first len integers. More...
 
void tightEncode (std::ostream &out) const
 Writes the tight encoding of this permutation to the given output stream. More...
 
std::string tightEncoding () const
 Returns the tight encoding of this permutation. More...
 
void clear (unsigned from)
 Resets the images of all integers from from onwards to the identity map. More...
 
constexpr bool isConjugacyMinimal () const
 Is this permutation minimal in its conjugacy class? More...
 

Static Public Member Functions

static void precompute ()
 Performs the precomputation necessary for using the optimised cachedInverse() routine. More...
 
static constexpr Perm fromPermCode (Code code)
 Creates a permutation from the given internal code. More...
 
static constexpr bool isPermCode (Code code)
 Determines whether the given integer is a valid internal permutation code. More...
 
static constexpr Perm fromImagePack (ImagePack pack)
 Creates a permutation from the given image pack. More...
 
static constexpr bool isImagePack (ImagePack pack)
 Determines whether the given argument is the image pack of some n-element permutation. More...
 
static constexpr Perm rot (int i)
 Returns the ith rotation. More...
 
static Perm rand (bool even=false)
 Returns a random permutation on n elements. More...
 
template<class URBG >
static Perm rand (URBG &&gen, bool even=false)
 Returns a random permutation on n elements, using the given uniform random bit generator. More...
 
static Perm tightDecoding (const std::string &enc)
 Reconstructs a permutation from its given tight encoding. More...
 
static Perm tightDecode (std::istream &input)
 Reconstructs a permutation from its given tight encoding. More...
 
template<int k>
static constexpr Perm extend (Perm< k > p)
 Extends a k-element permutation to an n-element permutation, where 2 ≤ k < n. More...
 
template<int k>
static constexpr Perm contract (Perm< k > p)
 Restricts a k-element permutation to an n-element permutation, where k > n. More...
 

Static Public Attributes

static constexpr int imageBits = regina::bitsRequired(n)
 Indicates the number of bits used in an image pack to store the image of a single integer. More...
 
static constexpr ImagePack imageMask
 A bitmask whose lowest imageBits bits are 1, and whose remaining higher order bits are all 0. More...
 
static constexpr PermCodeType codeType = PERM_CODE_IMAGES
 Indicates what type of internal permutation code is used by this instance of the Perm class template. More...
 
static constexpr Index nPerms = factorial(n)
 The total number of permutations on n elements. More...
 
static constexpr Index nPerms_1 = factorial(n-1)
 The total number of permutations on n-1 elements. More...
 
static constexpr SnLookup Sn {}
 Gives array-like access to all possible permutations of n elements. More...
 
static constexpr OrderedSnLookup orderedSn {}
 Gives array-like access to all possible permutations of n elements in lexicographical order. More...
 

Protected Member Functions

constexpr Perm (Code code)
 Creates a permutation from the given internal code. More...
 

Protected Attributes

Code code_
 The internal code representing this permutation. More...
 

Detailed Description

template<int n>
class regina::Perm< n >

Represents a permutation of {0,1,...,n-1}.

Amongst other things, such permutations are used to describe simplex gluings in (n-1)-manifold triangulations.

Perm objects are small enough to pass by value and swap with std::swap(), with no need to use references, specialised move operations or custom swap functions. The trade-off is that, for this to be possible, the Perm template class can only work with n ≤ 16.

Each permutation has an internal code, which is a single native integer that is sufficient to reconstruct the entire permutation. Thus the internal code may be a useful means for passing permutation objects to and from the engine. These codes are constructed as follows:

For n = 2,...,5 (which appear throughout 2-, 3- and 4-manifold triangulations), this template is specialised: the code is highly optimised and also offers some extra functionality. For n = 6,7, this template is again specialised and highly optimised, and it offers some extra functionality but not as much as Perm<5> and below. For n ≥ 8, this template is generic and most operations require more time (in particular, there are no harded-coded lookup tables).

Python
Python does not support templates. For each n = 2,...,16, this class is available in Python under the corresponding name Perm2, Perm3, ..., Perm16.
Template Parameters
nthe number of objects being permuted. This must be between 2 and 16 inclusive.

Member Typedef Documentation

◆ Code

template<int n>
using regina::Perm< n >::Code = ImagePack

Indicates the native unsigned integer type used to store the internal permutation code.

This type alias is present for most values of n, though its precise size depends on how the permutation code is constructed. However, for n = 4,...,7 it is replaced by two type aliases Code1 and Code2, which describe the older first-generation and newer second-generation permutation codes respectively.

◆ ImagePack

template<int n>
using regina::Perm< n >::ImagePack = typename IntOfMinSize<(imageBits * n + 7) / 8>::utype

Indicates the native unsigned integer type used to store a single image pack.

See the class notes for more information on image packs, and how they are used as permutation codes for n ≥ 8.

◆ Index

template<int n>
using regina::Perm< n >::Index = typename IntOfMinSize<(imageBits * n + 7) / 8>::type

Denotes a native signed integer type large enough to count all permutations on n elements.

In other words, this is a native signed integer type large enough to store (n!).

Note
This type is not hyper-optimised: for very small n where Index is hard-coded this is just an int, and for larger n where Index is derived it is actually large enough to hold an entire image pack. If at any stage there are plans to optimise this type, be careful of the special case of n = 8, where (8!) can be stored in an unsigned 16-bit type but not a signed 16-bit type.

Constructor & Destructor Documentation

◆ Perm() [1/5]

template<int n>
constexpr regina::Perm< n >::Perm
inlineconstexpr

Creates the identity permutation.

◆ Perm() [2/5]

template<int n>
constexpr regina::Perm< n >::Perm ( int  a,
int  b 
)
inlineconstexpr

Creates the transposition of a and b.

Note that a and b need not be distinct.

Precondition
0 ≤ a,b < n.
Parameters
athe element to switch with b.
bthe element to switch with a.

◆ Perm() [3/5]

template<int n>
constexpr regina::Perm< n >::Perm ( const std::array< int, n > &  image)
inlineconstexpr

Creates a permutation mapping i to image[i] for each 0 ≤ i < n.

Precondition
The elements of image are 0,...,n-1 in some order.
Parameters
imagethe array of images.

◆ Perm() [4/5]

template<int n>
constexpr regina::Perm< n >::Perm ( const Perm< n > &  )
constexprdefault

Creates a permutation that is a clone of the given permutation.

◆ Perm() [5/5]

template<int n>
constexpr regina::Perm< n >::Perm ( Code  code)
inlineconstexprprotected

Creates a permutation from the given internal code.

Precondition
the given code is a valid permutation code; see isPermCode() for details.
Parameters
codethe internal code from which the new permutation will be created.

Member Function Documentation

◆ cachedComp() [1/2]

template<int n>
Perm< n > regina::Perm< n >::cachedComp ( const Perm< n > &  q) const
inline

An alias for the composition operator, provided to assist with writing generic code.

This generic Perm<n> class does not use precomputation to compute compositions. The only point of having cachedComp() in this generic Perm<n> class is to make it easier to write generic code that works with Perm<n> for any n.

  • If you know you are only working with the generic Perm<n>, you should just use the composition operator instead.
  • If you are writing generic code, you must remember to call precompute() at least once in the lifetime of this program before using cachedComp().

The permutation that is returned is the same as you would obtain by calling (*this) * q.

Precondition
You must have called precompute() at least once in the lifetime of this program before calling cachedComp(). For generic Perm<n>, precompute() does not affect compositions; however, for other Perm<n> classes a failure to do this will almost certainly crash your program.
Parameters
qthe permutation to compose this with.
Returns
the composition of both permutations.

◆ cachedComp() [2/2]

template<int n>
Perm< n > regina::Perm< n >::cachedComp ( const Perm< n > &  q,
const Perm< n > &  r 
) const
inline

Deprecated alias for using the composition operator twice, provided to assist with writing generic code.

The permutation that is returned is the same as you would obtain by calling (*this) * q * r.

Deprecated:
The three-way cachedComp() was originally written to support conjugation. If you are indeed conjugating, then call cachedConjugate() instead; otherwise just call the two-way cachedComp() twice.
Precondition
You must have called precompute() at least once in the lifetime of this program before calling cachedComp(). For generic Perm<n>, precompute() does not affect compositions; however, for other Perm<n> classes a failure to do this will almost certainly crash your program.
Parameters
qthe first permutation to compose this with.
rthe second permutation to compose this with.
Returns
the composition of both permutations.

◆ cachedConjugate()

template<int n>
Perm< n > regina::Perm< n >::cachedConjugate ( const Perm< n > &  q) const
inline

An alias for conjugate(), provided to assist with writing generic code.

This generic Perm<n> class does not use precomputation to compute conjugates. The only point of having cachedConjugate() in this generic Perm<n> class is to make it easier to write generic code that works with Perm<n> for any n.

  • If you know you are only working with the generic Perm<n>, you should just call conjugate() instead.
  • If you are writing generic code, you must remember to call precompute() at least once in the lifetime of this program before using cachedConjugate().
Precondition
You must have called precompute() at least once in the lifetime of this program before calling cachedConjugate(). For generic Perm<n>, precompute() does not affect conjugate computations; however, for other Perm<n> classes a failure to do this will almost certainly crash your program.
Parameters
qthe permutation to conjugate this by.
Returns
the conjugate of this permutation by q.

◆ cachedInverse()

template<int n>
Perm< n > regina::Perm< n >::cachedInverse
inline

Finds the inverse of this permutation, optimised using precomputed "partial lookup tables".

The advantage of this routine is speed: calling cachedInverse() involves two table lookups and some simple arithmetic to combine the results, whereas inverse() requires time linear in n.

The disadvantages of this routine are that (1) you must remember to call precompute() in advance, and (2) the precomputed lookup tables will consume additional memory for the lifetime of your program. See precompute() for details on just how much memory these tables will consume for each n.

The permutation that is returned is the same as you would obtain by calling inverse().

Precondition
You must have called the routine precompute() at least once in the lifetime of the program before using cachedInverse(). Otherwise this routine will almost certainly crash your program.
Returns
the inverse of this permutation.

◆ cachedOrder()

template<int n>
int regina::Perm< n >::cachedOrder
inline

An alias for order(), provided to assist with writing generic code.

This specialised Perm<n> class does not use precomputation to compute orders. The only point of having cachedOrder() in this generic Perm<n> class is to make it easier to write generic code that works with Perm<n> for any n.

  • If you know you are only working with the generic Perm<n>, you should just call order() instead.
  • If you are writing generic code, you must remember to call precompute() at least once in the lifetime of this program before using cachedOrder().
Precondition
You must have called precompute() at least once in the lifetime of this program before calling cachedOrder(). For generic Perm<n>, precompute() does not affect order computations; however, for other Perm<n> classes a failure to do this will almost certainly crash your program.
Returns
the order of this permutation.

◆ cachedPow()

template<int n>
Perm< n > regina::Perm< n >::cachedPow ( long  exp) const
inline

An alias for pow(), provided to assist with writing generic code.

This specialised Perm<n> class does not use precomputation to compute powers. The only point of having cachedPow() in this generic Perm<n> class is to make it easier to write generic code that works with Perm<n> for any n.

  • If you know you are only working with the generic Perm<n>, you should just call pow() instead.
  • If you are writing generic code, you must remember to call precompute() at least once in the lifetime of this program before using cachedPow().
Precondition
You must have called precompute() at least once in the lifetime of this program before calling cachedPow(). For generic Perm<n>, precompute() does not affect powers; however, for other Perm<n> classes a failure to do this will almost certainly crash your program.
Parameters
expthe exponent; this may be positive, zero or negative.
Returns
this permutation raised to the power of exp.

◆ clear()

template<int n>
void regina::Perm< n >::clear ( unsigned  from)
inline

Resets the images of all integers from from onwards to the identity map.

Specifically, for each i in the range from,...,n-1, this routine will ensure that image[i] == i. The images of 0,1,...,from-1 will not be altered.

Precondition
The images of from,...,n-1 are exactly from,...,n-1, but possibly in a different order.
Parameters
fromthe first integer whose image should be reset. This must be between 0 and n inclusive.

◆ compareWith()

template<int n>
constexpr int regina::Perm< n >::compareWith ( const Perm< n > &  other) const
constexpr

Lexicographically compares the images of (0,1,...,n-1) under this and the given permutation.

Parameters
otherthe permutation with which to compare this.
Returns
-1 if this permutation produces a smaller image, 0 if the permutations are equal, and 1 if this permutation produces a greater image.

◆ conjugate()

template<int n>
constexpr Perm< n > regina::Perm< n >::conjugate ( const Perm< n > &  q) const
inlineconstexpr

Computes the conjugate of this permutation by q.

Specifically, calling p.conjugate(q) is equivalent to computing q * p * q.inverse(). The resulting permutation will have the same cycle structure as p, but with the cycle elements translated according to q.

Parameters
qthe permutation to conjugate this by.
Returns
the conjugate of this permutation by q.

◆ contract()

template<int n>
template<int k>
static constexpr Perm regina::Perm< n >::contract ( Perm< k >  p)
staticconstexpr

Restricts a k-element permutation to an n-element permutation, where k > n.

The resulting permutation will map 0,...,n-1 to their respective images under p, and will ignore the "unused" images p[n],...,p[k-1].

Precondition
The given permutation maps 0,...,n-1 to 0,...,n-1 in some order.
Template Parameters
kthe number of elements for the input permutation; this must be strictly greater than n.
Parameters
pa permutation on k elements.
Returns
the same permutation restricted to a permutation on n elements.

◆ extend()

template<int n>
template<int k>
static constexpr Perm regina::Perm< n >::extend ( Perm< k >  p)
staticconstexpr

Extends a k-element permutation to an n-element permutation, where 2 ≤ k < n.

The resulting permutation will map 0,...,k-1 to their respective images under p, and will map the "unused" elements k,...,n-1 to themselves.

Template Parameters
kthe number of elements for the input permutation; this must be at least 2, and strictly less than n.
Parameters
pa permutation on k elements.
Returns
the same permutation expressed as a permutation on n elements.

◆ fromImagePack()

template<int n>
constexpr Perm< n > regina::Perm< n >::fromImagePack ( ImagePack  pack)
inlinestaticconstexpr

Creates a permutation from the given image pack.

See the class notes for more information on image packs, and how they are used to build permutation codes.

For n ≥ 8, this routine is identical to fromPermCode().

Precondition
The argument pack is a valid image pack; see isImagePack() for details.
Parameters
packan image pack that describes a permutation.
Returns
the permutation represented by the given image pack.

◆ fromPermCode()

template<int n>
constexpr Perm< n > regina::Perm< n >::fromPermCode ( Code  code)
inlinestaticconstexpr

Creates a permutation from the given internal code.

Precondition
the given code is a valid permutation code; see isPermCode() for details.
Parameters
codethe internal code for the new permutation.
Returns
the permutation reprsented by the given internal code.

◆ imagePack()

template<int n>
constexpr Perm< n >::ImagePack regina::Perm< n >::imagePack
inlineconstexpr

Returns the image pack that represents this permutation.

See the class notes for more information on image packs, and how they are used to build permutation codes.

For n ≥ 8, this routine is identical to permCode().

Returns
the image pack for this permutation.

◆ inverse()

template<int n>
constexpr Perm< n > regina::Perm< n >::inverse
inlineconstexpr

Finds the inverse of this permutation.

For permutations of seven and fewer objects, inversion is extremely fast because it uses hard-coded lookup tables. However, for this generic Perm<n> class, inversion cannot uses these lookup tables (for multiple reasons), and so inverse() takes time linear in n.

If you are going to make significant use of the generic Perm<n> class for some particular value of n, you should instead:

Returns
the inverse of this permutation.

◆ isConjugacyMinimal()

template<int n>
constexpr bool regina::Perm< n >::isConjugacyMinimal
constexpr

Is this permutation minimal in its conjugacy class?

Here "minimal" means that, amongst all its conjugates, this permutation has the smallest index in the array Perm<n>::Sn.

See Sn for further information on how permutations are indexed.

Returns
true if and only if this permutation is minimal in its conjugacy class.

◆ isIdentity()

template<int n>
constexpr bool regina::Perm< n >::isIdentity
inlineconstexpr

Determines if this is the identity permutation.

This is true if and only if every integer 0 ≤ i < n is mapped to itself.

Returns
true if and only if this is the identity permutation.

◆ isImagePack()

template<int n>
constexpr bool regina::Perm< n >::isImagePack ( ImagePack  pack)
inlinestaticconstexpr

Determines whether the given argument is the image pack of some n-element permutation.

See the class notes for more information on image packs, and how they are used to build permutation codes.

For n ≥ 8, this routine is identical to isPermCode().

Parameters
packthe candidate image pack to test.
Returns
true if and only if pack is a valid image pack.

◆ isPermCode()

template<int n>
constexpr bool regina::Perm< n >::isPermCode ( Code  code)
staticconstexpr

Determines whether the given integer is a valid internal permutation code.

Valid permutation codes can be passed to setPermCode() or fromPermCode(), and are returned by permCode().

Parameters
codethe permutation code to test.
Returns
true if and only if the given code is a valid internal permutation code.

◆ operator!=()

template<int n>
constexpr bool regina::Perm< n >::operator!= ( const Perm< n > &  other) const
inlineconstexpr

Determines if this differs from the given permutation.

This is true if and only if the two permutations have different images for some 0 ≤ i < n.

Parameters
otherthe permutation with which to compare this.
Returns
true if and only if this and the given permutation differ.

◆ operator*()

template<int n>
constexpr Perm< n > regina::Perm< n >::operator* ( const Perm< n > &  q) const
inlineconstexpr

Returns the composition of this permutation with the given permutation.

If this permutation is p, the resulting permutation will be pq, and will satisfy (p*q)[x] == p[q[x]].

Parameters
qthe permutation to compose this with.
Returns
the composition of both permutations.

◆ operator++() [1/2]

template<int n>
Perm & regina::Perm< n >::operator++ ( )

A preincrement operator that changes this to be the next permutation in the array Perm<n>::Sn.

If this is the last such permutation then this will wrap around to become the first permutation in Perm<n>::Sn, which is the identity.

Python
Not present. The postincrement operator is present in Python as the member function inc().
Returns
a reference to this permutation after the increment.

◆ operator++() [2/2]

template<int n>
Perm< n > regina::Perm< n >::operator++ ( int  )
inline

A postincrement operator that changes this to be the next permutation in the array Perm<n>::Sn.

If this is the last such permutation then this will wrap around to become the first permutation in Perm<n>::Sn, which is the identity.

Python
This routine is named inc() since python does not support the increment operator.
Returns
a copy of this permutation before the increment took place.

◆ operator<()

template<int n>
constexpr bool regina::Perm< n >::operator< ( const Perm< n > &  rhs) const
constexpr

Determines if this appears earlier than the given permutation in the array Perm<n>::Sn.

Note that this is not the same ordering of permutations as the ordering implied by compareWith(). This is, however, consistent with the ordering implied by the ++ operators.

Unlike the smaller permutation classes that use Sn indices as internal permutation codes, for this generic Perm class the ordering defined here is slower to compute than compareWith(). It is recommended that, unless you specifically need to align your ordering with Sn indices, you either (i) use compareWith() for lexicographical ordering (which is a little faster), or else (ii) just compare permutation codes if you are happy with an arbitrary ordering (which will be much faster).

Parameters
rhsthe permutation to compare this against.
Returns
true if and only if this appears before rhs in Sn.

◆ operator=()

template<int n>
Perm & regina::Perm< n >::operator= ( const Perm< n > &  cloneMe)
default

Sets this permutation to be equal to the given permutation.

Parameters
cloneMethe permutation whose value will be assigned to this permutation.
Returns
a reference to this permutation.

◆ operator==()

template<int n>
constexpr bool regina::Perm< n >::operator== ( const Perm< n > &  other) const
inlineconstexpr

Determines if this is equal to the given permutation.

This is true if and only if both permutations have the same images for all 0 ≤ i < n.

Parameters
otherthe permutation with which to compare this.
Returns
true if and only if this and the given permutation are equal.

◆ operator[]()

template<int n>
constexpr int regina::Perm< n >::operator[] ( int  source) const
inlineconstexpr

Determines the image of the given integer under this permutation.

Parameters
sourcethe integer whose image we wish to find. This should be between 0 and n-1 inclusive.
Returns
the image of source.

◆ order()

template<int n>
constexpr int regina::Perm< n >::order
constexpr

Returns the order of this permutation.

In other words; this routine returns the smallest positive integer k for which the kth power of this permutation is the identity.

Note that the largest possible order for the largest supported n (n = 16) is 140. See OEIS sequence A000793 for details.

Returns
the order of this permutation.

◆ orderedSnIndex()

template<int n>
constexpr Perm< n >::Index regina::Perm< n >::orderedSnIndex
constexpr

Returns the lexicographical index of this permutation.

This will be the index of this permutation in the Perm<n>::orderedSn array.

See orderedSn for further information on lexicographical ordering.

Returns
the lexicographical index of this permutation. This will be between 0 and n!-1 inclusive.

◆ permCode()

template<int n>
constexpr Perm< n >::Code regina::Perm< n >::permCode
inlineconstexpr

Returns the internal code representing this permutation.

Note that the internal code is sufficient to reproduce the entire permutation.

The code returned will be a valid permutation code as determined by isPermCode().

Returns
the internal code.

◆ pow()

template<int n>
constexpr Perm< n > regina::Perm< n >::pow ( long  exp) const
constexpr

Computes the given power of this permutation.

This routine runs in time linear in n (in particular, the running time does not depend upon the given exponent).

Parameters
expthe exponent; this may be positive, zero or negative.
Returns
this permutation raised to the power of exp.

◆ pre()

template<int n>
constexpr int regina::Perm< n >::pre ( int  image) const
inlineconstexpr

Determines the preimage of the given integer under this permutation.

Parameters
imagethe integer whose preimage we wish to find. This should be between 0 and n-1 inclusive.
Returns
the preimage of image.

◆ precompute()

template<int n>
static void regina::Perm< n >::precompute ( )
static

Performs the precomputation necessary for using the optimised cachedInverse() routine.

This must be called before calling cachedInverse().

This only needs to be done once in the lifetime of the program. If you do try to call precompute() a second time then it will do nothing and return immediately.

The precomputed tables will consume roughly:

  • 33 kB for n = 8;
  • 8.9 MB for n = 9;
  • 17 MB for n = 10;
  • 143 MB for n = 11;
  • 268 MB for n = 12;
  • 2.3 GB for n = 13;
  • 4.3 GB for n = 14;
  • 37 GB for n = 15;
  • 69 GB for n = 16.

In particular, a 32-bit machine will not be able to store these tables for n ≥ 13, a 24-bit machine will not be these tables for n ≥ 9, and a 16-bit machine will not be able to store these tables for any n ≥ 8.

Precondition
There is enough memory available to store the precomputed tables; see above for the estimated space requirements.
Exceptions
FailedPreconditionThere was not enough memory to available to store the precomputed tables.

This routine is thread-safe.

◆ rand() [1/2]

template<int n>
Perm< n > regina::Perm< n >::rand ( bool  even = false)
static

Returns a random permutation on n elements.

All permutations are returned with equal probability.

This routine is thread-safe, and uses RandomEngine for its random number generation.

Warning
This routine is expensive, since it locks and unlocks the mutex protecting Regina's global uniform random bit generator. If you are calling this many times in quick succession, consider creating a single RandomEngine object yourself and then calling rand(randomEngine.engine(), even).
Parameters
evenif true, then the resulting permutation is guaranteed to be even (and again all even permutations are returned with equal probability).
Returns
a random permutation.

◆ rand() [2/2]

template<int n>
template<class URBG >
static Perm regina::Perm< n >::rand ( URBG &&  gen,
bool  even = false 
)
static

Returns a random permutation on n elements, using the given uniform random bit generator.

All permutations are returned with equal probability.

The thread safety of this routine is of course dependent on the thread safety of your uniform random bit generator gen.

Template Parameters
URBGA type which, once any references are removed, must adhere to the C++ UniformRandomBitGenerator concept.
Python
Not present. Python users are still able to use the non-thread-safe variant without the gen argument.
Parameters
genthe source of randomness to use (e.g., one of the many options provided in the C++ standard random header).
evenif true, then the resulting permutation is guaranteed to be even (and again all even permutations are returned with equal probability).
Returns
a random permutation.

◆ reverse()

template<int n>
constexpr Perm< n > regina::Perm< n >::reverse
inlineconstexpr

Finds the reverse of this permutation.

Here reverse means that we reverse the images of 0,...,n-1. In other words, if permutation q is the reverse of p, then p[i] == q[n - 1 - i] for all i.

◆ rot()

template<int n>
constexpr Perm< n > regina::Perm< n >::rot ( int  i)
staticconstexpr

Returns the ith rotation.

This maps k to k + i (mod n) for all k.

Parameters
ithe image of 0; this must be between 0 and n-1 inclusive.
Returns
the ith rotation.

◆ setPermCode()

template<int n>
void regina::Perm< n >::setPermCode ( Code  code)
inline

Sets this permutation to that represented by the given internal code.

Precondition
the given code is a valid permutation code; see isPermCode() for details.
Parameters
codethe internal code that will determine the new value of this permutation.

◆ sign()

template<int n>
constexpr int regina::Perm< n >::sign
constexpr

Determines the sign of this permutation.

Returns
1 if this permutation is even, or -1 if this permutation is odd.

◆ SnIndex()

template<int n>
constexpr Perm< n >::Index regina::Perm< n >::SnIndex
constexpr

Returns the index of this permutation in the Perm<n>::Sn array.

See Sn for further information on how these permutations are indexed.

Returns
the index i for which this permutation is equal to Perm<n>::Sn[i]. This will be between 0 and n!-1 inclusive.

◆ str()

template<int n>
std::string regina::Perm< n >::str

Returns a string representation of this permutation.

The representation will consist of n adjacent digits representing the images of 0,...,n-1 respectively. If n > 10, then lower-case hexadecimal digits will be used.

An example of a string representation for n = 5 is 30421.

Returns
a string representation of this permutation.

◆ tightDecode()

template<int n>
Perm< n > regina::Perm< n >::tightDecode ( std::istream &  input)
inlinestatic

Reconstructs a permutation from its given tight encoding.

See the page on tight encodings for details.

The tight encoding will be read from the given input stream. If the input stream contains leading whitespace then it will be treated as an invalid encoding (i.e., this routine will throw an exception). The input routine may contain further data: if this routine is successful then the input stream will be left positioned immediately after the encoding, without skipping any trailing whitespace.

Tight encodings are fast to work with for small permutation classes (n ≤ 7), but slower for larger permutation classes (8 ≤ n ≤ 16). See tightEncoding() for further details.

Exceptions
InvalidInputThe given input stream does not begin with a tight encoding of an n-element permutation.
Python
Not present. Use tightDecoding() instead, which takes a string as its argument.
Parameters
inputan input stream that begins with the tight encoding for an n-element permutation.
Returns
the permutation represented by the given tight encoding.

◆ tightDecoding()

template<int n>
Perm< n > regina::Perm< n >::tightDecoding ( const std::string &  enc)
inlinestatic

Reconstructs a permutation from its given tight encoding.

See the page on tight encodings for details.

The tight encoding will be given as a string. If this string contains leading whitespace or any trailing characters at all (including trailing whitespace), then it will be treated as an invalid encoding (i.e., this routine will throw an exception).

Tight encodings are fast to work with for small permutation classes (n ≤ 7), but slower for larger permutation classes (8 ≤ n ≤ 16). See tightEncoding() for further details.

Exceptions
InvalidArgumentThe given string is not a tight encoding of an n-element permutation.
Parameters
encthe tight encoding for an n-element permutation.
Returns
the permutation represented by the given tight encoding.

◆ tightEncode()

template<int n>
void regina::Perm< n >::tightEncode ( std::ostream &  out) const

Writes the tight encoding of this permutation to the given output stream.

See the page on tight encodings for details.

For all permutation classes Perm<n>, the tight encoding is based on the index into the full permutation group S_n. For smaller permutation classes (n ≤ 7), such encodings are very fast to work with since the S_n index is used as the internal permutation code. For larger permutation classes however (8 ≤ n ≤ 16), the S_n index requires some non-trivial work to compute.

Python
Not present. Use tightEncoding() instead, which returns a string.
Parameters
outthe output stream to which the encoded string will be written.

◆ tightEncoding()

template<int n>
std::string regina::Perm< n >::tightEncoding

Returns the tight encoding of this permutation.

See the page on tight encodings for details.

For all permutation classes Perm<n>, the tight encoding is based on the index into the full permutation group S_n. For smaller permutation classes (n ≤ 7), such encodings are very fast to work with since the S_n index is used as the internal permutation code. For larger permutation classes however (8 ≤ n ≤ 16), the S_n index requires some non-trivial work to compute.

Returns
the resulting encoded string.

◆ trunc()

template<int n>
std::string regina::Perm< n >::trunc ( int  len) const

Returns a prefix of the string representation of this permutation, containing only the images of the first len integers.

Parameters
lenthe length of the prefix required; this must be between 0 and n inclusive.
Returns
the corresponding prefix of the string representation of this permutation.

Member Data Documentation

◆ code_

template<int n>
Code regina::Perm< n >::code_
protected

The internal code representing this permutation.

◆ codeType

template<int n>
constexpr PermCodeType regina::Perm< n >::codeType = PERM_CODE_IMAGES
staticconstexpr

Indicates what type of internal permutation code is used by this instance of the Perm class template.

◆ imageBits

template<int n>
constexpr int regina::Perm< n >::imageBits = regina::bitsRequired(n)
staticconstexpr

Indicates the number of bits used in an image pack to store the image of a single integer.

A full image pack combines n such images together, and so uses n * imageBits bits in total.

◆ imageMask

template<int n>
constexpr ImagePack regina::Perm< n >::imageMask
staticconstexpr
Initial value:
=
(static_cast<ImagePack>(1) << Perm<n>::imageBits) - 1
typename IntOfMinSize<(imageBits *n+7)/8 >::utype ImagePack
Indicates the native unsigned integer type used to store a single image pack.
Definition: perm.h:279
static constexpr int imageBits
Indicates the number of bits used in an image pack to store the image of a single integer.
Definition: perm.h:257

A bitmask whose lowest imageBits bits are 1, and whose remaining higher order bits are all 0.

This may be useful when creating or analysing image packs.

◆ nPerms

template<int n>
constexpr Index regina::Perm< n >::nPerms = factorial(n)
staticconstexpr

The total number of permutations on n elements.

This is the size of the symmetric group Sn.

◆ nPerms_1

template<int n>
constexpr Index regina::Perm< n >::nPerms_1 = factorial(n-1)
staticconstexpr

The total number of permutations on n-1 elements.

This is the size of the symmetric group Sn-1.

◆ orderedSn

template<int n>
constexpr OrderedSnLookup regina::Perm< n >::orderedSn {}
staticconstexpr

Gives array-like access to all possible permutations of n elements in lexicographical order.

To access the permutation at index i, you simply use the square bracket operator: orderedSn[i]. The index i must be between 0 and n!-1 inclusive.

Lexicographical ordering treats each permutation p as the n-tuple (p[0], p[1], ..., p[n-1]).

This array is different from Perm<n>::Sn, since orderedSn stores permutations in lexicographical order, whereas Sn alternates between even and odd permutations.

This is a lightweight object, and it is defined in the headers only. In particular, you cannot make a reference to it (but it is cheap to make a copy).

Warning
For n ≤ 7, the square bracket operator is a very fast constant-time routine. However, for n ≥ 8, this is not constant time; the current implementation is quadratic in n.

◆ Sn

template<int n>
constexpr SnLookup regina::Perm< n >::Sn {}
staticconstexpr

Gives array-like access to all possible permutations of n elements.

To access the permutation at index i, you simply use the square bracket operator: Sn[i]. The index i must be between 0 and n!-1 inclusive.

The permutations with even indices in the array are the even permutations, and those with odd indices in the array are the odd permutations.

This array is different from Perm<n>::orderedSn, since Sn alternates between even and odd permutations, whereas orderedSn stores permutations in lexicographical order.

This is a lightweight object, and it is defined in the headers only. In particular, you cannot make a reference to it (but it is cheap to make a copy).

Warning
For n ≤ 7, the square bracket operator is a very fast constant-time routine. However, for n ≥ 8, this is not constant time; the current implementation is quadratic in n.

The documentation for this class was generated from the following file:

Copyright © 1999-2023, The Regina development team
This software is released under the GNU General Public License, with some additional permissions; see the source code for details.
For further information, or to submit a bug or other problem, please contact Ben Burton (bab@maths.uq.edu.au).