FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
permutation.hpp
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#pragma once
7
8// includes, FEAT
10#include <kernel/util/random.hpp>
11
12//includes, system
13#include<vector>
14
15namespace FEAT
16{
17 namespace Adjacency
18 {
25 {
26 public:
27 using IndexVector = std::vector < Index>;
28
30 enum class ConstrType
31 {
33 none,
37 perm,
39 swap,
44 };
45
46 private:
48 IndexVector _perm_pos;
50 IndexVector _swap_pos;
51
52 public:
55 _perm_pos(),
56 _swap_pos()
57 {
58 }
59
81 explicit Permutation(
82 Index num_entries,
83 ConstrType constr_type = ConstrType::none,
84 const Index* v = nullptr);
85
97 explicit Permutation(
98 Index num_entries,
99 Random& random);
100
102 Permutation(Permutation&& other);
105
107 virtual ~Permutation();
108
115 {
116 if(!_perm_pos.empty())
117 return Permutation(this->size(), ConstrType::perm, _perm_pos.data());
118 else
119 return Permutation();
120 }
121
128 {
129 if(!_perm_pos.empty())
130 return Permutation(this->size(), ConstrType::inv_perm, _perm_pos.data());
131 else
132 return Permutation();
133 }
134
136 Index size() const
137 {
138 XASSERTM(_perm_pos.size()==_swap_pos.size(), "perm_pos and swap_pos have different sizes");
139 return Index(_perm_pos.size());
140 }
141
143 bool empty() const
144 {
145 return _perm_pos.empty();
146 }
147
150 {
151 return _perm_pos.data();
152 }
153
155 const Index* get_perm_pos() const
156 {
157 return _perm_pos.data();
158 }
159
162 {
163 return _swap_pos.data();
164 }
165
167 const Index* get_swap_pos() const
168 {
169 return _swap_pos.data();
170 }
171
172 template<typename IT_>
173 IT_ map(IT_ i) const
174 {
175 return IT_(_perm_pos.at(std::size_t(i)));
176 }
177
179 void calc_swap_from_perm();
180
182 void calc_perm_from_swap();
183
197 template<typename Tx_>
198 void apply(Tx_* x, bool invert = false) const
199 {
200 XASSERT(x != nullptr);
201
202 if(!invert)
203 {
204 // apply in-situ swapping
205 for(Index i(0); i+1 < this->size(); ++i)
206 {
207 Index j(_swap_pos[i]);
208 ASSERTM((j >= i) && (j < this->size()), "invalid swap position");
209 if(j > i)
210 {
211 Tx_ t(x[i]);
212 x[i] = x[j];
213 x[j] = t;
214 }
215 }
216 }
217 else
218 {
219 // apply inverse in-situ swapping
220 for(Index i(this->size() -1); i > 0; --i)
221 {
222 Index j(_swap_pos[i-1]);
223 ASSERTM((j >= i-1) && (j < this->size()), "invalid swap position");
224 if(j > i-1)
225 {
226 Tx_ t(x[i-1]);
227 x[i-1] = x[j];
228 x[j] = t;
229 }
230 }
231 }
232 }
233
250 template<typename Ty_, typename Tx_>
251 // void operator()(Ty_* y, const Tx_* x, bool invert = false) const
252 void apply(Ty_* y, const Tx_* x, bool invert = false) const
253 {
254 XASSERT(y != nullptr);
255 XASSERT(x != nullptr);
256 if(!invert)
257 {
258 for(Index i(0); i < this->size(); ++i)
259 {
260 y[i] = Ty_(x[_perm_pos[i]]);
261 }
262 }
263 else
264 {
265 for(Index i(0); i < this->size(); ++i)
266 {
267 y[_perm_pos[i]] = Ty_(x[i]);
268 }
269 }
270 }
271
281 void concat(const Permutation& p);
282
283 }; // class Permutation
284 } // namespace Adjacency
285} // namespace FEAT
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
Definition: assertion.hpp:230
#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
const Index * get_perm_pos() const
returns the permute-position array
Index * get_perm_pos()
returns the permute-position array
IndexVector _swap_pos
the swap-position vector
Definition: permutation.hpp:50
bool empty() const
Checks whether the permutation is empty.
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
Index * get_swap_pos()
returns the swap-position array
Permutation inverse() const
Computes the inverse permutation.
void apply(Ty_ *y, const Tx_ *x, bool invert=false) const
Applies permutation.
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
const Index * get_swap_pos() const
returns the swap-position array
void apply(Tx_ *x, bool invert=false) const
Applies In-Situ permutation.
Permutation clone() const
Clones this 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.