atlas  0.6
permutations_def.h
Go to the documentation of this file.
1 
6 /*
7  Copyright (C) 2004,2005 Fokko du Cloux
8  part of the Atlas of Lie Groups and Representations
9 
10  For license information see the LICENSE file
11 */
12 
13 /*
14  This file contains the definitions of the template functions declared in
15  permutations.h
16 */
17 
18 #include <cassert>
19 #include "bitmap.h"
20 
21 namespace atlas {
22 
23 namespace permutations {
24 
25 /* we leave |pull_back| with implicit instantiation, as the types to
26  substitute for |T| (|DescentStatus|, |KGB_base::KGBfields|) are complicated
27  and hard/impossible to specify in the file permutations.cpp, while the
28  contents of that file is not visible when those types \emph{are} available.
29 */
30 
31 /* Permute the entries of |v| by "pulling back" through our permutation |pi|,
32  so that |result[i]==v[pi[i]]| for all |i| afterwards.
33 */
34 template<typename T>
35 std::vector<T> Permutation::pull_back(const std::vector<T>& v) const
36 {
37  assert(v.size()==size());
38 
39  const Permutation& pi=*this;
40  std::vector<T> result; result.reserve(v.size());
41 
42  for (unsigned long i=0; i<v.size(); ++i)
43  result.push_back(v[pi[i]]);
44 
45  return result;
46 }
47 
48 /* The method |permute| also could apply to all kinds of (local) types,
49  so it is reasonable (and necessary) to instantiate it implicitly
50 */
51 
52 /*
53  Applies our permutation |pi| to the vector |v|. In other words, we send each
54  entry v[i] to the new position v[pi[i]]; this means that afterwards for all
55  |i|: |new_v[pi[i]]==old_v[i]|, or equivalently $new_v[i]=old_v[pi^{-1}[i]]$.
56  This notion of permuting an arbitrary sequence is a left action of $S_n$.
57 
58  This is pulling back (right multiplication) through the inverse permutation.
59 
60  We are able to perform this permutation essentially in-place, using an
61  auxiliary bitmap. Pushing forwards through a cycle does however require 3
62  assignments (a swap) per step, while backwards a single assignment would do
63 */
64 template<typename T> void Permutation::permute(std::vector<T>& v) const
65 {
66  assert(v.size()>=size());
67  const Permutation& pi=*this;
68  bitmap::BitMap seen(size()); // initialized empty
69 
70  for (size_t i = 0; i < size(); ++i)
71  if (not seen.isMember(i))
72  {
73  seen.insert(i);
74  if (pi[i]!=i)
75  {
76  T tmp=v[i]; // avoid accessing |v[i]| all the time
77  for (size_t j=pi[i]; j!=i; j=pi[j]) // cycle from |pi[i]| to |i|
78  {
79  std::swap(tmp,v[j]); // transpose |tmp] with other members in order
80  seen.insert(j);
81  }
82  v[i]=tmp;
83  }
84  }
85 }
86 
87 } // |namespace permutations|
88 
89 } // |namespace atlas|
unsigned long size
Definition: testprint.cpp:46
std::vector< T > pull_back(const std::vector< T > &v) const
Definition: permutations_def.h:35
Definition: permutations.h:34
void swap(simple_list< T, Alloc > &x, simple_list< T, Alloc > &y)
Definition: sl_list.h:674
void permute(std::vector< T > &v) const
Definition: permutations_def.h:64
Definitions and declarations for the BitMap class.
Definition: Atlas.h:38
Container of a large (more than twice the machine word size) set of bits.
Definition: bitmap.h:52
void insert(unsigned long n)
Definition: bitmap.h:197
bool isMember(unsigned long n) const
Definition: bitmap.h:138
Vertex v
Definition: graph.cpp:116