11#ifndef EIGEN_TENSORSYMMETRY_STATICSYMMETRY_H
12#define EIGEN_TENSORSYMMETRY_STATICSYMMETRY_H
15#include "./InternalHeaderCheck.h"
21template <
typename indices_,
int flags_>
22struct tensor_static_symgroup_element {
23 using indices = indices_;
24 constexpr static int flags = flags_;
27template <
typename Gen,
int... indices>
28constexpr std::integer_sequence<int,
29 ((indices == Gen::One) ? Gen::Two : ((indices == Gen::Two) ? Gen::One : indices))...>
30tensor_static_symgroup_swapped_indices(std::integer_sequence<int, indices...>) {
34template <
typename Gen,
int N>
35struct tensor_static_symgroup_element_ctor {
36 using type = tensor_static_symgroup_element<
37 decltype(tensor_static_symgroup_swapped_indices<Gen>(std::make_integer_sequence<int, N>{})), Gen::Flags>;
41struct tensor_static_symgroup_identity_ctor {
42 using type = tensor_static_symgroup_element<std::make_integer_sequence<int, N>, 0>;
45template <
typename iib>
46struct tensor_static_symgroup_multiply_helper {
48 constexpr static std::integer_sequence<int, get<iia, iib>::value...> helper(std::integer_sequence<int, iia...>) {
53template <
typename A,
typename B>
54struct tensor_static_symgroup_multiply {
56 using iia =
typename A::indices;
57 using iib =
typename B::indices;
58 constexpr static int ffa = A::flags;
59 constexpr static int ffb = B::flags;
62 static_assert(iia::size() == iib::size(),
"Cannot multiply symmetry elements with different number of indices.");
65 tensor_static_symgroup_element<
decltype(tensor_static_symgroup_multiply_helper<iib>::helper(iia())), ffa ^ ffb>;
68template <
typename A,
typename B>
69struct tensor_static_symgroup_equality {
70 using iia =
typename A::indices;
71 using iib =
typename B::indices;
72 constexpr static int ffa = A::flags;
73 constexpr static int ffb = B::flags;
74 static_assert(iia::size() == iib::size(),
"Cannot compare symmetry elements with different number of indices.");
76 constexpr static bool value = std::is_same<iia, iib>::value;
82 constexpr static int flags_cmp_ = ffa ^ ffb;
87 constexpr static bool is_zero = value && flags_cmp_ == NegationFlag;
88 constexpr static bool is_real = value && flags_cmp_ == ConjugationFlag;
89 constexpr static bool is_imag = value && flags_cmp_ == (NegationFlag | ConjugationFlag);
92 constexpr static int global_flags =
93 (is_real ? GlobalRealFlag : 0) | (is_imag ? GlobalImagFlag : 0) | (is_zero ? GlobalZeroFlag : 0);
96template <std::size_t NumIndices,
typename... Gen>
97struct tensor_static_symgroup {
98 using type = StaticSGroup<Gen...>;
99 constexpr static std::size_t size = type::static_size;
102template <
typename Index, std::size_t N,
int... ii,
int... jj>
103constexpr static std::array<Index, N> tensor_static_symgroup_index_permute(
const std::array<Index, N>& idx,
104 std::integer_sequence<int, ii...>,
105 std::integer_sequence<int, jj...>) {
106 return {{idx[ii]..., idx[
sizeof...(ii) + jj]...}};
109template <
typename Index,
int... ii>
110static inline std::vector<Index> tensor_static_symgroup_index_permute(
const std::vector<Index>& idx,
111 std::integer_sequence<int, ii...>) {
112 std::vector<Index> result{{idx[ii]...}};
113 std::size_t target_size = idx.size();
114 for (std::size_t i = result.size(); i < target_size; i++) result.push_back(idx[i]);
119struct tensor_static_symgroup_do_apply;
121template <
typename first,
typename... next>
122struct tensor_static_symgroup_do_apply<internal::type_list<first, next...>> {
123 template <
typename Op,
typename RV, std::size_t SGNumIndices,
typename Index, std::size_t NumIndices,
125 static inline RV run(
const std::array<Index, NumIndices>& idx, RV initial, Args&&... args) {
126 static_assert(NumIndices >= SGNumIndices,
127 "Can only apply symmetry group to objects that have at least the required amount of indices.");
128 initial = Op::run(tensor_static_symgroup_index_permute(
129 idx,
typename first::indices(), std::make_integer_sequence<int, NumIndices - SGNumIndices>{}),
130 first::flags, initial, std::forward<Args>(args)...);
131 return tensor_static_symgroup_do_apply<internal::type_list<next...>>::template run<Op, RV, SGNumIndices>(
132 idx, initial, args...);
135 template <
typename Op,
typename RV, std::size_t SGNumIndices,
typename Index,
typename... Args>
136 static inline RV run(
const std::vector<Index>& idx, RV initial, Args&&... args) {
137 eigen_assert(idx.size() >= SGNumIndices &&
138 "Can only apply symmetry group to objects that have at least the required amount of indices.");
139 initial = Op::run(tensor_static_symgroup_index_permute(idx,
typename first::indices()), first::flags, initial,
140 std::forward<Args>(args)...);
141 return tensor_static_symgroup_do_apply<internal::type_list<next...>>::template run<Op, RV, SGNumIndices>(
142 idx, initial, args...);
147struct tensor_static_symgroup_do_apply<internal::type_list<>> {
148 template <
typename Op,
typename RV, std::size_t SGNumIndices,
typename Index, std::size_t NumIndices,
150 static inline RV run(
const std::array<Index, NumIndices>&, RV initial, Args&&...) {
155 template <
typename Op,
typename RV, std::size_t SGNumIndices,
typename Index,
typename... Args>
156 static inline RV run(
const std::vector<Index>&, RV initial, Args&&...) {
164template <
typename... Gen>
166 constexpr static std::size_t NumIndices = internal::tensor_symmetry_num_indices<Gen...>::value;
167 using group_elements = internal::group_theory::enumerate_group_elements<
168 internal::tensor_static_symgroup_multiply, internal::tensor_static_symgroup_equality,
169 typename internal::tensor_static_symgroup_identity_ctor<NumIndices>::type,
170 internal::type_list<typename internal::tensor_static_symgroup_element_ctor<Gen, NumIndices>::type...>>;
171 using ge =
typename group_elements::type;
174 constexpr StaticSGroup() =
default;
175 constexpr StaticSGroup(
const StaticSGroup<Gen...>&) =
default;
176 constexpr StaticSGroup(StaticSGroup<Gen...>&&) =
default;
178 template <
typename Op,
typename RV,
typename Index, std::size_t N,
typename... Args>
179 static inline RV apply(
const std::array<Index, N>& idx, RV initial, Args&&... args) {
180 return internal::tensor_static_symgroup_do_apply<ge>::template run<Op, RV, NumIndices>(idx, initial, args...);
183 template <
typename Op,
typename RV,
typename Index,
typename... Args>
184 static inline RV apply(
const std::vector<Index>& idx, RV initial, Args&&... args) {
185 eigen_assert(idx.size() >= NumIndices);
186 return internal::tensor_static_symgroup_do_apply<ge>::template run<Op, RV, NumIndices>(idx, initial, args...);
189 constexpr static std::size_t static_size = ge::count;
191 constexpr static std::size_t size() {
return ge::count; }
192 constexpr static int globalFlags() {
return group_elements::global_flags; }
194 template <
typename Tensor_,
typename... IndexTypes>
195 inline internal::tensor_symmetry_value_setter<Tensor_, StaticSGroup<Gen...>> operator()(
196 Tensor_& tensor,
typename Tensor_::Index firstIndex, IndexTypes... otherIndices)
const {
197 static_assert(
sizeof...(otherIndices) + 1 == Tensor_::NumIndices,
198 "Number of indices used to access a tensor coefficient must be equal to the rank of the tensor.");
199 return operator()(tensor, std::array<typename Tensor_::Index, Tensor_::NumIndices>{{firstIndex, otherIndices...}});
202 template <
typename Tensor_>
203 inline internal::tensor_symmetry_value_setter<Tensor_, StaticSGroup<Gen...>> operator()(
204 Tensor_& tensor, std::array<typename Tensor_::Index, Tensor_::NumIndices>
const& indices)
const {
205 return internal::tensor_symmetry_value_setter<Tensor_, StaticSGroup<Gen...>>(tensor, *
this, indices);
Namespace containing all symbols from the Eigen library.