Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
StaticSymmetry.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2013 Christian Seiler <christian@iwakd.de>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_TENSORSYMMETRY_STATICSYMMETRY_H
12#define EIGEN_TENSORSYMMETRY_STATICSYMMETRY_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21template <typename indices_, int flags_>
22struct tensor_static_symgroup_element {
23 using indices = indices_;
24 constexpr static int flags = flags_;
25};
26
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...>) {
31 return {};
32}
33
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>;
38};
39
40template <int N>
41struct tensor_static_symgroup_identity_ctor {
42 using type = tensor_static_symgroup_element<std::make_integer_sequence<int, N>, 0>;
43};
44
45template <typename iib>
46struct tensor_static_symgroup_multiply_helper {
47 template <int... iia>
48 constexpr static std::integer_sequence<int, get<iia, iib>::value...> helper(std::integer_sequence<int, iia...>) {
49 return {};
50 }
51};
52
53template <typename A, typename B>
54struct tensor_static_symgroup_multiply {
55 private:
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;
60
61 public:
62 static_assert(iia::size() == iib::size(), "Cannot multiply symmetry elements with different number of indices.");
63
64 using type =
65 tensor_static_symgroup_element<decltype(tensor_static_symgroup_multiply_helper<iib>::helper(iia())), ffa ^ ffb>;
66};
67
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.");
75
76 constexpr static bool value = std::is_same<iia, iib>::value;
77
78 private:
79 /* this should be zero if they are identical, or else the tensor
80 * will be forced to be pure real, pure imaginary or even pure zero
81 */
82 constexpr static int flags_cmp_ = ffa ^ ffb;
83
84 /* either they are not equal, then we don't care whether the flags
85 * match, or they are equal, and then we have to check
86 */
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);
90
91 public:
92 constexpr static int global_flags =
93 (is_real ? GlobalRealFlag : 0) | (is_imag ? GlobalImagFlag : 0) | (is_zero ? GlobalZeroFlag : 0);
94};
95
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;
100};
101
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]...}};
107}
108
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]);
115 return result;
116}
117
118template <typename T>
119struct tensor_static_symgroup_do_apply;
120
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,
124 typename... Args>
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...);
133 }
134
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...);
143 }
144};
145
146template <>
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,
149 typename... Args>
150 static inline RV run(const std::array<Index, NumIndices>&, RV initial, Args&&...) {
151 // do nothing
152 return initial;
153 }
154
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&&...) {
157 // do nothing
158 return initial;
159 }
160};
161
162} // end namespace internal
163
164template <typename... Gen>
165class StaticSGroup {
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;
172
173 public:
174 constexpr StaticSGroup() = default;
175 constexpr StaticSGroup(const StaticSGroup<Gen...>&) = default;
176 constexpr StaticSGroup(StaticSGroup<Gen...>&&) = default;
177
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...);
181 }
182
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...);
187 }
188
189 constexpr static std::size_t static_size = ge::count;
190
191 constexpr static std::size_t size() { return ge::count; }
192 constexpr static int globalFlags() { return group_elements::global_flags; }
193
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...}});
200 }
201
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);
206 }
207};
208
209} // end namespace Eigen
210
211#endif // EIGEN_TENSORSYMMETRY_STATICSYMMETRY_H
212
213/*
214 * kate: space-indent on; indent-width 2; mixedindent off; indent-mode cstyle;
215 */
Namespace containing all symbols from the Eigen library.