10 #ifndef EIGEN_CXX11_TENSORSYMMETRY_DYNAMICSYMMETRY_H 11 #define EIGEN_CXX11_TENSORSYMMETRY_DYNAMICSYMMETRY_H 14 #include "./InternalHeaderCheck.h" 20 inline explicit DynamicSGroup() : m_numIndices(1), m_elements(), m_generators(), m_globalFlags(0) {
21 m_elements.push_back(ge(Generator(0, 0, 0)));
24 : m_numIndices(o.m_numIndices),
25 m_elements(o.m_elements),
26 m_generators(o.m_generators),
27 m_globalFlags(o.m_globalFlags) {}
29 : m_numIndices(o.m_numIndices), m_elements(), m_generators(o.m_generators), m_globalFlags(o.m_globalFlags) {
30 std::swap(m_elements, o.m_elements);
33 m_numIndices = o.m_numIndices;
34 m_elements = o.m_elements;
35 m_generators = o.m_generators;
36 m_globalFlags = o.m_globalFlags;
40 m_numIndices = o.m_numIndices;
41 std::swap(m_elements, o.m_elements);
42 m_generators = o.m_generators;
43 m_globalFlags = o.m_globalFlags;
47 void add(
int one,
int two,
int flags = 0);
49 template <
typename Gen_>
50 inline void add(Gen_) {
51 add(Gen_::One, Gen_::Two, Gen_::Flags);
53 inline void addSymmetry(
int one,
int two) { add(one, two, 0); }
54 inline void addAntiSymmetry(
int one,
int two) { add(one, two, NegationFlag); }
55 inline void addHermiticity(
int one,
int two) { add(one, two, ConjugationFlag); }
56 inline void addAntiHermiticity(
int one,
int two) { add(one, two, NegationFlag | ConjugationFlag); }
58 template <
typename Op,
typename RV,
typename Index, std::size_t N,
typename... Args>
59 inline RV apply(
const std::array<Index, N>& idx, RV initial, Args&&... args)
const {
60 eigen_assert(N >= m_numIndices &&
61 "Can only apply symmetry group to objects that have at least the required amount of indices.");
62 for (std::size_t i = 0; i < size(); i++)
63 initial = Op::run(h_permute(i, idx,
typename internal::gen_numeric_list<int, N>::type()), m_elements[i].flags,
64 initial, std::forward<Args>(args)...);
68 template <
typename Op,
typename RV,
typename Index,
typename... Args>
69 inline RV apply(
const std::vector<Index>& idx, RV initial, Args&&... args)
const {
70 eigen_assert(idx.size() >= m_numIndices &&
71 "Can only apply symmetry group to objects that have at least the required amount of indices.");
72 for (std::size_t i = 0; i < size(); i++)
73 initial = Op::run(h_permute(i, idx), m_elements[i].flags, initial, std::forward<Args>(args)...);
77 inline int globalFlags()
const {
return m_globalFlags; }
78 inline std::size_t size()
const {
return m_elements.size(); }
80 template <
typename Tensor_,
typename... IndexTypes>
81 inline internal::tensor_symmetry_value_setter<Tensor_, DynamicSGroup> operator()(Tensor_& tensor,
82 typename Tensor_::Index firstIndex,
83 IndexTypes... otherIndices)
const {
84 static_assert(
sizeof...(otherIndices) + 1 == Tensor_::NumIndices,
85 "Number of indices used to access a tensor coefficient must be equal to the rank of the tensor.");
86 return operator()(tensor, std::array<typename Tensor_::Index, Tensor_::NumIndices>{{firstIndex, otherIndices...}});
89 template <
typename Tensor_>
90 inline internal::tensor_symmetry_value_setter<Tensor_, DynamicSGroup> operator()(
91 Tensor_& tensor, std::array<typename Tensor_::Index, Tensor_::NumIndices>
const& indices)
const {
92 return internal::tensor_symmetry_value_setter<Tensor_, DynamicSGroup>(tensor, *
this, indices);
97 std::vector<int> representation;
100 for (std::size_t i = 0; i < representation.size(); i++)
101 if (i != (
size_t)representation[i])
return false;
109 constexpr Generator(
int one_,
int two_,
int flags_) : one(one_), two(two_), flags(flags_) {}
112 std::size_t m_numIndices;
113 std::vector<GroupElement> m_elements;
114 std::vector<Generator> m_generators;
117 template <
typename Index, std::size_t N,
int... n>
118 inline std::array<Index, N> h_permute(std::size_t which,
const std::array<Index, N>& idx,
119 internal::numeric_list<int, n...>)
const {
120 return std::array<Index, N>{{idx[n >= m_numIndices ? n : m_elements[which].representation[n]]...}};
123 template <
typename Index>
124 inline std::vector<Index> h_permute(std::size_t which, std::vector<Index> idx)
const {
125 std::vector<Index> result;
126 result.reserve(idx.size());
127 for (
auto k : m_elements[which].representation) result.push_back(idx[k]);
128 for (std::size_t i = m_numIndices; i < idx.size(); i++) result.push_back(idx[i]);
132 inline GroupElement ge(Generator
const& g)
const {
134 result.representation.reserve(m_numIndices);
135 result.flags = g.flags;
136 for (std::size_t k = 0; k < m_numIndices; k++) {
137 if (k == (std::size_t)g.one)
138 result.representation.push_back(g.two);
139 else if (k == (std::size_t)g.two)
140 result.representation.push_back(g.one);
142 result.representation.push_back(
int(k));
147 GroupElement mul(GroupElement, GroupElement)
const;
148 inline GroupElement mul(Generator g1, GroupElement g2)
const {
return mul(ge(g1), g2); }
150 inline GroupElement mul(GroupElement g1, Generator g2)
const {
return mul(g1, ge(g2)); }
152 inline GroupElement mul(Generator g1, Generator g2)
const {
return mul(ge(g1), ge(g2)); }
154 inline int findElement(GroupElement e)
const {
155 for (
auto ee : m_elements) {
156 if (ee.representation == e.representation)
return ee.flags ^ e.flags;
161 void updateGlobalFlags(
int flagDiffOfSameGenerator);
165 template <
typename... Gen>
168 inline DynamicSGroupFromTemplateArgs() :
DynamicSGroup() { add_all(internal::type_list<Gen...>()); }
169 inline DynamicSGroupFromTemplateArgs(DynamicSGroupFromTemplateArgs
const& other) : DynamicSGroup(other) {}
170 inline DynamicSGroupFromTemplateArgs(DynamicSGroupFromTemplateArgs&& other) : DynamicSGroup(other) {}
171 inline DynamicSGroupFromTemplateArgs<Gen...>& operator=(
const DynamicSGroupFromTemplateArgs<Gen...>& o) {
172 DynamicSGroup::operator=(o);
175 inline DynamicSGroupFromTemplateArgs<Gen...>& operator=(DynamicSGroupFromTemplateArgs<Gen...>&& o) {
176 DynamicSGroup::operator=(o);
181 template <
typename Gen1,
typename... GenNext>
182 inline void add_all(internal::type_list<Gen1, GenNext...>) {
184 add_all(internal::type_list<GenNext...>());
187 inline void add_all(internal::type_list<>) {}
190 inline DynamicSGroup::GroupElement DynamicSGroup::mul(GroupElement g1, GroupElement g2)
const {
191 eigen_internal_assert(g1.representation.size() == m_numIndices);
192 eigen_internal_assert(g2.representation.size() == m_numIndices);
195 result.representation.reserve(m_numIndices);
196 for (std::size_t i = 0; i < m_numIndices; i++) {
197 int v = g2.representation[g1.representation[i]];
198 eigen_assert(v >= 0);
199 result.representation.push_back(v);
201 result.flags = g1.flags ^ g2.flags;
205 inline void DynamicSGroup::add(
int one,
int two,
int flags) {
206 eigen_assert(one >= 0);
207 eigen_assert(two >= 0);
208 eigen_assert(one != two);
210 if ((std::size_t)one >= m_numIndices || (std::size_t)two >= m_numIndices) {
211 std::size_t newNumIndices = (one > two) ? one : two + 1;
212 for (
auto& gelem : m_elements) {
213 gelem.representation.reserve(newNumIndices);
214 for (std::size_t i = m_numIndices; i < newNumIndices; i++) gelem.representation.push_back(i);
216 m_numIndices = newNumIndices;
219 Generator g{one, two, flags};
220 GroupElement e = ge(g);
223 if (m_elements.size() == 1) {
225 m_elements.push_back(e);
229 if (e.flags > 0) updateGlobalFlags(e.flags);
232 if (m_elements.size() > 1) m_generators.push_back(g);
236 int p = findElement(e);
238 updateGlobalFlags(p);
242 std::size_t coset_order = m_elements.size();
243 m_elements.push_back(e);
244 for (std::size_t i = 1; i < coset_order; i++) m_elements.push_back(mul(m_elements[i], e));
245 m_generators.push_back(g);
247 std::size_t coset_rep = coset_order;
249 for (
auto g : m_generators) {
250 e = mul(m_elements[coset_rep], g);
254 m_elements.push_back(e);
255 for (std::size_t i = 1; i < coset_order; i++) m_elements.push_back(mul(m_elements[i], e));
257 updateGlobalFlags(p);
260 coset_rep += coset_order;
261 }
while (coset_rep < m_elements.size());
264 inline void DynamicSGroup::updateGlobalFlags(
int flagDiffOfSameGenerator) {
265 switch (flagDiffOfSameGenerator) {
272 m_globalFlags |= GlobalZeroFlag;
274 case ConjugationFlag:
276 m_globalFlags |= GlobalRealFlag;
278 case (NegationFlag | ConjugationFlag):
280 m_globalFlags |= GlobalImagFlag;
292 #endif // EIGEN_CXX11_TENSORSYMMETRY_DYNAMICSYMMETRY_H Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
Dynamic symmetry group.
Definition: DynamicSymmetry.h:18