Go to the documentation of this file.
20 #ifndef TILEDARRAY_PERMUTATION_H__INCLUDED
21 #define TILEDARRAY_PERMUTATION_H__INCLUDED
37 bool operator==(
const Permutation&,
const Permutation&);
38 std::ostream&
operator<<(std::ostream&,
const Permutation&);
39 template <
typename T, std::
size_t N>
40 inline std::array<T, N>
operator*(
const Permutation&,
const std::array<T, N>&);
41 template <
typename T, std::
size_t N>
42 inline std::array<T, N>&
operator*=(std::array<T, N>&,
const Permutation&);
43 template <
typename T,
typename A>
44 inline std::vector<T>
operator*(
const Permutation&,
const std::vector<T, A>&);
45 template <
typename T,
typename A>
46 inline std::vector<T, A>&
operator*=(std::vector<T, A>&,
const Permutation&);
48 inline std::vector<T>
operator*(
const Permutation&,
49 const T* MADNESS_RESTRICT
const);
51 class BipartitePermutation;
52 bool operator==(
const BipartitePermutation&,
const BipartitePermutation&);
64 template <
typename Perm,
typename Arg,
typename Result,
65 typename = std::enable_if_t<is_permutation_v<Perm>>>
66 inline void permute_array(
const Perm& perm,
const Arg& arg, Result& result) {
69 const unsigned int n = size(arg);
70 for (
unsigned int i = 0u; i < n; ++i) {
71 const typename Perm::index_type pi = perm[i];
134 template <
typename T>
142 template <
typename InIter>
143 bool valid_permutation(InIter first, InIter last) {
145 using diff_type =
typename std::iterator_traits<InIter>::difference_type;
146 const diff_type n = std::distance(first, last);
148 for (; first != last; ++first) {
149 const diff_type value = *first;
150 result = result && value >= 0 && (value < n) &&
151 (std::count(first, last, *first) == 1ul);
180 InIter>::value>::type* =
nullptr>
182 TA_ASSERT(valid_permutation(first, last));
189 template <
typename Index,
190 typename = std::enable_if_t<detail::is_integral_range_v<Index>>>
206 template <
typename Integer,
207 std::enable_if_t<std::is_integral_v<Integer>>* =
nullptr>
272 if (not placed_in_cycle[i]) {
274 placed_in_cycle[i] =
true;
277 while (next_i != i) {
278 cycle.push_back(next_i);
279 placed_in_cycle[next_i] =
true;
283 if (cycle.size() != 1) {
284 std::sort(cycle.begin(), cycle.end());
285 result.emplace_back(cycle);
300 result.
p_.reserve(
dim);
301 for (
unsigned int i = 0u; i <
dim; ++i) result.
p_.emplace_back(i);
315 const unsigned int n =
p_.size();
318 result.
p_.reserve(n);
320 for (
unsigned int i = 0u; i < n; ++i) {
323 result.
p_.emplace_back(result_i);
337 result.
p_.resize(n, 0ul);
367 if (power & 1) result = result.
mult(value);
368 value = value.
mult(value);
378 explicit operator bool()
const {
return !
p_.empty(); }
395 template <
typename Archive>
410 std::equal(p1.
data().begin(), p1.
data().end(), p2.
data().begin());
430 return std::lexicographical_compare(p1.
data().begin(), p1.
data().end(),
431 p2.
data().begin(), p2.
data().end());
440 std::size_t n = p.
size();
442 for (
unsigned int dim = 0; dim < n - 1; ++dim)
443 output << dim <<
"->" << p.
data()[dim] <<
", ";
444 output << n - 1 <<
"->" << p.
data()[n - 1] <<
"}";
466 return (p1 = p1 * p2);
491 template <
typename T, std::
size_t N>
493 const std::array<T, N>& a) {
495 std::array<T, N> result;
509 template <
typename T, std::
size_t N>
513 const std::array<T, N> temp = a;
527 template <
typename T,
typename A>
529 const std::vector<T, A>& v) {
531 std::vector<T> result(perm.
size());
545 template <
typename T,
typename A>
548 const std::vector<T, A> temp = v;
562 template <
typename T, std::
size_t N>
564 const Permutation& perm,
const boost::container::small_vector<T, N>& v) {
566 boost::container::small_vector<T, N> result(perm.
size());
580 template <
typename T, std::
size_t N>
582 boost::container::small_vector<T, N>& v,
const Permutation& perm) {
583 const boost::container::small_vector<T, N> temp = v;
594 template <
typename T>
596 const T* MADNESS_RESTRICT
const ptr) {
597 const unsigned int n = perm.
size();
598 std::vector<T> result(n);
599 for (
unsigned int i = 0u; i < n; ++i) {
601 const T ptr_i = ptr[i];
602 result[perm_i] = ptr_i;
613 template <
typename T>
626 explicit operator bool()
const {
return static_cast<bool>(base_); }
636 [[deprecated(
"use BipartitePermutation::size()")]]
auto dim()
const {
670 return std::move(base_);
681 : base_(p), second_size_(second_partition_size) {
689 for (
auto&& v :
first) base.emplace_back(v);
690 for (
auto&& v :
second) base.emplace_back(v);
707 InIter>::value>::type* =
nullptr>
710 : base_(
first, last), second_size_(second_partition_size) {
721 template <
typename Index,
722 typename = std::enable_if_t<detail::is_integral_range_v<Index>>>
726 second_partition_size) {}
737 : base_(std::move(a)), second_size_(second_partition_size) {
757 template <
typename Archive>
759 ar& base_& second_size_;
780 for (
auto i = n_first; i <
size(); ++i)
781 temp[i - n_first] = base_[i] - n_first;
782 second_ = Permutation(temp.begin(), temp.end());
841 #endif // TILEDARRAY_PERMUTATION_H__INCLUED
BipartitePermutation()=default
auto dim() const
Domain size accessor.
index_type dim() const
Domain size accessor.
boost::container::small_vector< T, N > svector
index_type operator[](unsigned int i) const
Element accessor.
index_type size() const
Domain size accessor.
Permutation pow(int n) const
Raise this permutation to the n-th power.
void serialize(Archive &ar)
Serialize permutation.
vector< index_type > p_
One-line representation of permutation.
BipartitePermutation(const Permutation &first, const Permutation &second)
void permute_array(const Perm &perm, const Arg &arg, Result &result)
Create a permuted copy of an array.
Permutation & operator=(Permutation &&other)=default
Permutation inv() const
Construct the inverse of this permutation.
Permutation of a sequence of objects indexed by base-0 indices.
bool operator==(const BlockRange &r1, const BlockRange &r2)
BlockRange equality comparison.
static Permutation identity(const unsigned int dim)
Identity permutation factory function.
BipartitePermutation(const Index &a, index_type second_partition_size=0)
Array constructor.
constexpr bool operator!=(const DenseShape &a, const DenseShape &b)
auto cbegin() const
Begin element iterator factory function.
bool operator<(const Permutation &p1, const Permutation &p2)
Permutation less-than operator.
Permutation mult(const Permutation &other) const
Product of this permutation by other.
Permutation::index_type index_type
index_type second_size() const
auto begin() const
Begin element iterator factory function.
void serialize(Archive &ar)
Serialize permutation.
Permutation identity() const
Identity permutation factory function.
auto outer(const Permutation &p)
const_iterator end() const
End element iterator factory function.
std::array< T, N > operator*(const Permutation &, const std::array< T, N > &)
Permute a std::array.
const auto & data() const
Permutation data accessor.
const Permutation & first() const
const Permutation & second() const
#define TA_ASSERT(EXPR,...)
auto inner_size(const Permutation &p)
BipartitePermutation(const BipartitePermutation &)=default
index_type first_size() const
BipartitePermutation(InIter first, InIter last, index_type second_partition_size=0)
Construct permutation from a range [first,last)
BipartitePermutation & operator=(BipartitePermutation &&other)=default
Permutation(Permutation &&)=default
const_iterator begin() const
Begin element iterator factory function.
Permutation operator-(const Permutation &perm)
Inverse permutation operator.
Permutation(InIter first, InIter last)
Construct permutation from a range [first,last)
container::svector< T > vector
Permutation::vector< T > vector
Permutation(const Permutation &)=default
const_iterator cbegin() const
Begin element iterator factory function.
BipartitePermutation(vector< index_type > &&a, index_type second_partition_size=0)
std::vector move constructor
~BipartitePermutation()=default
Permutation operator^(const Permutation &perm, int n)
Raise perm to the n-th power.
std::ostream & operator<<(std::ostream &os, const DistArray< Tile, Policy > &a)
Add the tensor to an output stream.
friend bool operator==(const BipartitePermutation &p1, const BipartitePermutation &p2)
Permutation equality operator.
BipartitePermutation(BipartitePermutation &&)=default
BipartitePermutation & operator=(const BipartitePermutation &)=default
auto size() const
Domain size accessor.
vector< vector< index_type > > cycles() const
Cycles decomposition.
Permutation(std::initializer_list< Integer > list)
Construct permutation with an initializer list.
std::array< T, N > & operator*=(std::array< T, N > &, const Permutation &)
In-place permute a std::array.
const_iterator cend() const
End element iterator factory function.
Permutation & operator=(const Permutation &)=default
auto cend() const
End element iterator factory function.
Permutation(const Index &a)
Array constructor.
vector< index_type >::const_iterator const_iterator
Permutation of a bipartite set.
auto end() const
End element iterator factory function.
BipartitePermutation(const Permutation &p, index_type second_partition_size=0)
auto outer_size(const Permutation &p)
Permutation(vector< index_type > &&a)
std::vector move constructor
bool operator!() const
Not operator.
auto inner(const Permutation &p)