FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
permutation.cpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6// includes, FEAT
7#include <kernel/adjacency/permutation.hpp>
8#include <kernel/util/random.hpp>
9
10namespace FEAT
11{
12 namespace Adjacency
13 {
14 Permutation::Permutation(Index num_entries, ConstrType constr_type, const Index* v) :
15 _perm_pos(num_entries),
16 _swap_pos(num_entries)
17 {
18 XASSERTM(num_entries > 0, "cannot create empty permutation");
19 switch(constr_type)
20 {
22 // leave vectors uninitialized
23 return;
24
26 // initialize identity permutation
27 for(Index i(0); i < num_entries; ++i)
28 {
29 _perm_pos[i] = _swap_pos[i] = i;
30 }
31 return;
32
33 default:
34 // The GCC warns if this pointless default-case is missing...
35 break;
36 }
37
38 // for any other construction type we need an input array
39 XASSERTM(v != nullptr, "invalid input array");
40
41 switch(constr_type)
42 {
44 // construct from permutation array
45 for(Index i(0); i < num_entries; ++i)
46 {
47 _perm_pos[i] = v[i];
48 }
50 break;
51
53 // construct from inverse permutation array
54 for(Index i(0); i < num_entries; ++i)
55 {
56 _perm_pos[v[i]] = i;
57 }
59 break;
60
62 // construct from swap array
63 for(Index i(0); i < num_entries; ++i)
64 {
65 _swap_pos[i] = v[i];
66 }
68 break;
69
71 // construct from inverse swap array;
72 // initialize identity permutation
73 for(Index i(0); i < num_entries; ++i)
74 {
75 _perm_pos[i] = i;
76 }
77 // apply swapping in reverse manner
78 for(Index i(num_entries-1); i > 0; --i)
79 {
80 Index t(_perm_pos[i-1]);
81 _perm_pos[i-1] = _perm_pos[v[i-1]];
82 _perm_pos[v[i-1]] = t;
83 }
85 break;
86
87 default:
88 // The GCC warns if this pointless default-case is missing...
89 break;
90 }
91 }
92
93 Permutation::Permutation(Index num_entries, Random& random) :
94 _perm_pos(num_entries),
95 _swap_pos(num_entries)
96 {
97 XASSERTM(num_entries > 0, "cannot create empty random permutation");
98 for(Index i(0); i+1 < num_entries; ++i)
99 {
100 _swap_pos[i] = random(i, num_entries-1);
101 }
102 _swap_pos[num_entries-1] = num_entries-1;
104 }
105
107 _perm_pos(std::forward<IndexVector>(other._perm_pos)),
108 _swap_pos(std::forward<IndexVector>(other._swap_pos))
109 {}
110
112 {
113 // avoid self-move
114 if(this == &other)
115 return *this;
116
117 _perm_pos = std::forward<IndexVector>(other._perm_pos);
118 _swap_pos = std::forward<IndexVector>(other._swap_pos);
119
120 return *this;
121 }
122
124 {}
125
127 {
128 for(Index i(0); i < this->size(); ++i)
129 {
130 // fetch the permutation position and trace it through the swap array
131 Index j(_perm_pos[i]);
132 while(j < i)
133 {
134 j = _swap_pos[j];
135 }
136 _swap_pos[i] = j;
137 }
138 }
139
141 {
142 // initialize identity permuation
143 for(Index i(0); i < this->size(); ++i)
144 {
145 _perm_pos[i] = i;
146 }
147
148 // apply swapping to permutation
149 this->apply(_perm_pos.data());
150 }
151
153 {
154 XASSERT(this->size() == p.size());
155 Index* p1 = this->get_perm_pos();
156 const Index* p2 = p.get_perm_pos();
157
158 for (Index i(0); i < this->size(); ++i)
159 {
160 p1[i] = p2[p1[i]];
161 }
162 this->calc_swap_from_perm();
163 }
164 } // namespace Adjacency
165} // namespace FEAT
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Index size() const
returns the size of the permutation
Index * get_perm_pos()
returns the permute-position array
IndexVector _swap_pos
the swap-position vector
Definition: permutation.hpp:50
void calc_swap_from_perm()
Computes the swap-position vector from the permutation-position vector.
virtual ~Permutation()
virtual destructor
void concat(const Permutation &p)
concatenates two permutations
Permutation & operator=(Permutation &&)
move-assign operator
void calc_perm_from_swap()
Computes the permutation-position vector from the swap-position vector.
ConstrType
Construction type enumeration.
Definition: permutation.hpp:31
@ inv_perm
create from inverse permutation array
@ none
create uninitialized permutation
@ inv_swap
create from inverse swap array
@ perm
create from permutation array
@ identity
create identity permutation
void apply(Tx_ *x, bool invert=false) const
Applies In-Situ permutation.
IndexVector _perm_pos
the permute-position vector
Definition: permutation.hpp:48
Pseudo-Random Number Generator.
Definition: random.hpp:54
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.