10#ifndef EIGEN_CXX11_TENSORSYMMETRY_STATICSYMMETRY_H
11#define EIGEN_CXX11_TENSORSYMMETRY_STATICSYMMETRY_H
14#include "./InternalHeaderCheck.h"
20template <
typename list>
21struct tensor_static_symgroup_permutate;
24struct tensor_static_symgroup_permutate<numeric_list<int, nn...>> {
25 constexpr static std::size_t N =
sizeof...(nn);
28 constexpr static std::array<T, N> run(
const std::array<T, N>& indices) {
29 return {{indices[nn]...}};
33template <
typename indices_,
int flags_>
34struct tensor_static_symgroup_element {
35 typedef indices_ indices;
36 constexpr static int flags = flags_;
39template <
typename Gen,
int N>
40struct tensor_static_symgroup_element_ctor {
41 typedef tensor_static_symgroup_element<typename gen_numeric_list_swapped_pair<int, N, Gen::One, Gen::Two>::type,
47struct tensor_static_symgroup_identity_ctor {
48 typedef tensor_static_symgroup_element<typename gen_numeric_list<int, N>::type, 0> type;
51template <
typename iib>
52struct tensor_static_symgroup_multiply_helper {
54 constexpr static numeric_list<int, get<iia, iib>::value...> helper(numeric_list<int, iia...>) {
55 return numeric_list<int, get<iia, iib>::value...>();
59template <
typename A,
typename B>
60struct tensor_static_symgroup_multiply {
62 typedef typename A::indices iia;
63 typedef typename B::indices iib;
64 constexpr static int ffa = A::flags;
65 constexpr static int ffb = B::flags;
68 static_assert(iia::count == iib::count,
"Cannot multiply symmetry elements with different number of indices.");
70 typedef tensor_static_symgroup_element<
decltype(tensor_static_symgroup_multiply_helper<iib>::helper(iia())),
75template <
typename A,
typename B>
76struct tensor_static_symgroup_equality {
77 typedef typename A::indices iia;
78 typedef typename B::indices iib;
79 constexpr static int ffa = A::flags;
80 constexpr static int ffb = B::flags;
81 static_assert(iia::count == iib::count,
"Cannot compare symmetry elements with different number of indices.");
83 constexpr static bool value = is_same<iia, iib>::value;
89 constexpr static int flags_cmp_ = ffa ^ ffb;
94 constexpr static bool is_zero = value && flags_cmp_ == NegationFlag;
95 constexpr static bool is_real = value && flags_cmp_ == ConjugationFlag;
96 constexpr static bool is_imag = value && flags_cmp_ == (NegationFlag | ConjugationFlag);
99 constexpr static int global_flags =
100 (is_real ? GlobalRealFlag : 0) | (is_imag ? GlobalImagFlag : 0) | (is_zero ? GlobalZeroFlag : 0);
103template <std::size_t NumIndices,
typename... Gen>
104struct tensor_static_symgroup {
105 typedef StaticSGroup<Gen...> type;
106 constexpr static std::size_t size = type::static_size;
109template <
typename Index, std::size_t N,
int... ii,
int... jj>
110constexpr static std::array<Index, N> tensor_static_symgroup_index_permute(std::array<Index, N> idx,
111 internal::numeric_list<int, ii...>,
112 internal::numeric_list<int, jj...>) {
113 return {{idx[ii]..., idx[jj]...}};
116template <
typename Index,
int... ii>
117static inline std::vector<Index> tensor_static_symgroup_index_permute(std::vector<Index> idx,
118 internal::numeric_list<int, ii...>) {
119 std::vector<Index> result{{idx[ii]...}};
120 std::size_t target_size = idx.size();
121 for (std::size_t i = result.size(); i < target_size; i++) result.push_back(idx[i]);
126struct tensor_static_symgroup_do_apply;
128template <
typename first,
typename... next>
129struct tensor_static_symgroup_do_apply<internal::type_list<first, next...>> {
130 template <
typename Op,
typename RV, std::size_t SGNumIndices,
typename Index, std::size_t NumIndices,
132 static inline RV run(
const std::array<Index, NumIndices>& idx, RV initial, Args&&... args) {
133 static_assert(NumIndices >= SGNumIndices,
134 "Can only apply symmetry group to objects that have at least the required amount of indices.");
135 typedef typename internal::gen_numeric_list<int, NumIndices - SGNumIndices, SGNumIndices>::type remaining_indices;
136 initial = Op::run(tensor_static_symgroup_index_permute(idx,
typename first::indices(), remaining_indices()),
137 first::flags, initial, std::forward<Args>(args)...);
138 return tensor_static_symgroup_do_apply<internal::type_list<next...>>::template run<Op, RV, SGNumIndices>(
139 idx, initial, args...);
142 template <
typename Op,
typename RV, std::size_t SGNumIndices,
typename Index,
typename... Args>
143 static inline RV run(
const std::vector<Index>& idx, RV initial, Args&&... args) {
144 eigen_assert(idx.size() >= SGNumIndices &&
145 "Can only apply symmetry group to objects that have at least the required amount of indices.");
146 initial = Op::run(tensor_static_symgroup_index_permute(idx,
typename first::indices()), first::flags, initial,
147 std::forward<Args>(args)...);
148 return tensor_static_symgroup_do_apply<internal::type_list<next...>>::template run<Op, RV, SGNumIndices>(
149 idx, initial, args...);
153template <EIGEN_TPL_PP_SPEC_HACK_DEF(
typename, empty)>
154struct tensor_static_symgroup_do_apply<internal::type_list<EIGEN_TPL_PP_SPEC_HACK_USE(empty)>> {
155 template <
typename Op,
typename RV, std::size_t SGNumIndices,
typename Index, std::size_t NumIndices,
157 static inline RV run(
const std::array<Index, NumIndices>&, RV initial, Args&&...) {
162 template <
typename Op,
typename RV, std::size_t SGNumIndices,
typename Index,
typename... Args>
163 static inline RV run(
const std::vector<Index>&, RV initial, Args&&...) {
171template <
typename... Gen>
173 constexpr static std::size_t NumIndices = internal::tensor_symmetry_num_indices<Gen...>::value;
174 typedef internal::group_theory::enumerate_group_elements<
175 internal::tensor_static_symgroup_multiply, internal::tensor_static_symgroup_equality,
176 typename internal::tensor_static_symgroup_identity_ctor<NumIndices>::type,
177 internal::type_list<typename internal::tensor_static_symgroup_element_ctor<Gen, NumIndices>::type...>>
179 typedef typename group_elements::type ge;
182 constexpr StaticSGroup() {}
183 constexpr StaticSGroup(
const StaticSGroup<Gen...>&) {}
184 constexpr StaticSGroup(StaticSGroup<Gen...>&&) {}
186 template <
typename Op,
typename RV,
typename Index, std::size_t N,
typename... Args>
187 static inline RV apply(
const std::array<Index, N>& idx, RV initial, Args&&... args) {
188 return internal::tensor_static_symgroup_do_apply<ge>::template run<Op, RV, NumIndices>(idx, initial, args...);
191 template <
typename Op,
typename RV,
typename Index,
typename... Args>
192 static inline RV apply(
const std::vector<Index>& idx, RV initial, Args&&... args) {
193 eigen_assert(idx.size() == NumIndices);
194 return internal::tensor_static_symgroup_do_apply<ge>::template run<Op, RV, NumIndices>(idx, initial, args...);
197 constexpr static std::size_t static_size = ge::count;
199 constexpr static std::size_t size() {
return ge::count; }
200 constexpr static int globalFlags() {
return group_elements::global_flags; }
202 template <
typename Tensor_,
typename... IndexTypes>
203 inline internal::tensor_symmetry_value_setter<Tensor_, StaticSGroup<Gen...>> operator()(
204 Tensor_& tensor,
typename Tensor_::Index firstIndex, IndexTypes... otherIndices)
const {
205 static_assert(
sizeof...(otherIndices) + 1 == Tensor_::NumIndices,
206 "Number of indices used to access a tensor coefficient must be equal to the rank of the tensor.");
207 return operator()(tensor, std::array<typename Tensor_::Index, Tensor_::NumIndices>{{firstIndex, otherIndices...}});
210 template <
typename Tensor_>
211 inline internal::tensor_symmetry_value_setter<Tensor_, StaticSGroup<Gen...>> operator()(
212 Tensor_& tensor, std::array<typename Tensor_::Index, Tensor_::NumIndices>
const& indices)
const {
213 return internal::tensor_symmetry_value_setter<Tensor_, StaticSGroup<Gen...>>(tensor, *
this, indices);
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index