26 #ifndef TILEDARRAY_SYMM_PERMUTATION_H__INCLUDED 27 #define TILEDARRAY_SYMM_PERMUTATION_H__INCLUDED 59 template <
typename Perm,
typename Arg,
typename Result>
60 inline void permute_array(
const Perm& perm,
const Arg& arg, Result& result) {
62 if (perm.domain_size() < arg.size()) {
63 std::copy(arg.begin(), arg.end(), result.begin());
65 for(
const auto& p: perm.data()) {
68 result[p.second] = arg[p.first];
145 typedef std::map<index_type,index_type>
Map;
153 static std::ostream& print_map(std::ostream& output,
const Map& p) {
154 for (
auto i=p.cbegin(); i!=p.cend();) {
155 output << i->first <<
"->" << i->second;
165 template <
typename InIter>
166 static bool valid_permutation(InIter first, InIter last) {
168 for(; first != last; ++first) {
169 const auto value = *first;
170 result = result && value >= 0 && (std::count(first, last, *first) == 1ul);
177 template <
typename Map>
178 static bool valid_permutation(
const Map& input) {
179 std::set<index_type> keys;
180 std::set<index_type> values;
181 for(
const auto& e: input) {
182 const auto& key = e.first;
183 const auto& value = e.second;
184 if (keys.find(key) == keys.end())
190 if (values.find(value) == values.end())
191 values.insert(value);
197 return keys == values;
218 template <
typename InIter,
219 typename std::enable_if<TiledArray::detail::is_input_iterator<InIter>::value>::type* =
nullptr>
222 TA_ASSERT( valid_permutation(first, last) );
224 for(
auto e=first; e!=last; ++e, ++i) {
226 if (i != p_i) p_[i] = p_i;
294 auto e_image_iter = p_.find(e);
295 if (e_image_iter != p_.end())
296 return e_image_iter->second;
305 template <
typename Set>
308 for(
const auto& e: this->p_) {
309 result.insert(result.begin(), e.first);
320 template <
typename Integer,
321 typename std::enable_if<std::is_integral<Integer>::value>::type* =
nullptr>
323 return p_.find(i) != p_.end();
338 std::vector<std::vector<index_type> >
cycles()
const {
340 std::vector<std::vector<index_type>> result;
342 std::set<index_type> placed_in_cycle;
345 auto& p_nonconst_ =
const_cast<Map&
>(p_);
349 for(
const auto& e: p_) {
351 if (placed_in_cycle.find(i) == placed_in_cycle.end()) {
352 std::vector<index_type> cycle(1,i);
353 placed_in_cycle.insert(i);
356 while (next_i != i) {
357 cycle.push_back(next_i);
358 placed_in_cycle.insert(next_i);
359 next_i = p_nonconst_[next_i];
362 if (cycle.size() != 1) {
363 std::sort(cycle.begin(), cycle.end());
364 result.emplace_back(cycle);
379 using iset = std::set<index_type>;
380 auto product_domain = this->domain<iset>();
381 auto other_domain = other.
domain<iset>();
382 product_domain.insert(other_domain.begin(), other_domain.end());
385 for(
const auto& d: product_domain) {
386 const auto d_image = other[(*this)[d]];
401 for(
const auto& iter: p_) {
402 const auto i = iter.first;
403 const auto pi = iter.second;
404 result.insert(std::make_pair(pi,i));
431 result = result.
mult(value);
432 value = value.
mult(value);
457 template <
typename Archive>
492 if (&p1 == &p2)
return false;
493 return std::lexicographical_compare(p1.
data().begin(), p1.
data().end(),
494 p2.
data().begin(), p2.
data().end());
504 Permutation::print_map(output, p.
data());
530 return (p1 = p1 * p2);
556 template <
typename T, std::
size_t N>
558 std::array<T,N> result;
572 template <
typename T, std::
size_t N>
574 const std::array<T,N> temp = a;
588 template <
typename T,
typename A>
590 std::vector<T> result(v.size());
604 template <
typename T,
typename A>
606 const std::vector<T, A> temp = v;
617 #endif // TILEDARRAY_SYMM_PERMUTATION_H__INCLUDED Map::const_iterator const_iterator
Permutation operator-(const Permutation &perm)
Inverse permutation operator.
friend std::ostream & operator<<(std::ostream &output, const Permutation &p)
Add permutation to an output stream.
Set domain() const
Computes the domain of this permutation.
Permutation(const std::vector< index_type > &a)
std::vector constructor
index_type operator[](index_type e) const
Computes image of an element under this permutation.
unsigned int domain_size() const
Permutation domain size.
const_iterator cend() const
End element iterator factory function.
Permutation pow(int n) const
Raise this permutation to the n-th power.
const_iterator end() const
End element iterator factory function.
Permutation inv() const
Construct the inverse of this permutation.
Permutation & operator=(const Permutation &)=default
Permutation(Map p)
Construct permutation using its compressed 2-line form given by std::map.
Permutation & operator*=(Permutation &p1, const Permutation &p2)
return *this ^ other
Permutation operator*(const Permutation &p1, const Permutation &p2)
Permutation multiplication operator.
Permutation(std::initializer_list< index_type > list)
Construct permutation with an initializer list.
Permutation mult(const Permutation &other) const
Product of this permutation by other.
bool is_in_domain(Integer i) const
Test if an index is in the domain of this permutation.
bool operator!=(const Permutation &p1, const Permutation &p2)
Permutation inequality operator.
Permutation operator^(const Permutation &perm, int n)
Raise perm to the n-th power.
Permutation(InIter first, InIter last)
Construct permutation using its 1-line form given by range [first,last)
const_iterator cbegin() const
Begin element iterator factory function.
std::ostream & operator<<(std::ostream &output, const Permutation &p)
Add permutation to an output stream.
const_iterator begin() const
Begin element iterator factory function.
Permutation identity() const
Idenity permutation factory method.
DistArray< Tile, Policy > copy(const DistArray< Tile, Policy > &a)
bool operator==(const Permutation &p1, const Permutation &p2)
Permutation equality operator.
void permute_array(const Perm &perm, const Arg &arg, Result &result)
Create a permuted copy of an array.
void serialize(Archive &ar)
Serialize permutation.
bool operator<(const Permutation &p1, const Permutation &p2)
Permutation less-than operator.
const Map & data() const
Data accessor.
Permutation of a sequence of objects indexed by base-0 indices.
std::vector< std::vector< index_type > > cycles() const
Cycles decomposition.
std::map< index_type, index_type > Map