11#ifndef EIGEN_TENSORSYMMETRY_SYMMETRY_H
12#define EIGEN_TENSORSYMMETRY_SYMMETRY_H
15#include "./InternalHeaderCheck.h"
19enum { NegationFlag = 0x01, ConjugationFlag = 0x02 };
21enum { GlobalRealFlag = 0x01, GlobalImagFlag = 0x02, GlobalZeroFlag = 0x03 };
25template <std::size_t NumIndices,
typename... Sym>
26struct tensor_symmetry_pre_analysis;
27template <std::size_t NumIndices,
typename... Sym>
28struct tensor_static_symgroup;
29template <
bool instantiate, std::size_t NumIndices,
typename... Sym>
30struct tensor_static_symgroup_if;
31template <
typename Tensor_>
32struct tensor_symmetry_calculate_flags;
33template <
typename Tensor_>
34struct tensor_symmetry_assign_value;
35template <
typename... Sym>
36struct tensor_symmetry_num_indices;
40template <
int One_,
int Two_>
42 static_assert(One_ != Two_,
"Symmetries must cover distinct indices.");
43 constexpr static int One = One_;
44 constexpr static int Two = Two_;
45 constexpr static int Flags = 0;
48template <
int One_,
int Two_>
50 static_assert(One_ != Two_,
"Symmetries must cover distinct indices.");
51 constexpr static int One = One_;
52 constexpr static int Two = Two_;
53 constexpr static int Flags = NegationFlag;
56template <
int One_,
int Two_>
58 static_assert(One_ != Two_,
"Symmetries must cover distinct indices.");
59 constexpr static int One = One_;
60 constexpr static int Two = Two_;
61 constexpr static int Flags = ConjugationFlag;
64template <
int One_,
int Two_>
65struct AntiHermiticity {
66 static_assert(One_ != Two_,
"Symmetries must cover distinct indices.");
67 constexpr static int One = One_;
68 constexpr static int Two = Two_;
69 constexpr static int Flags = ConjugationFlag | NegationFlag;
97template <
typename... Gen>
98class DynamicSGroupFromTemplateArgs;
119template <
typename... Gen>
134template <
typename... Gen>
135class SGroup :
public internal::tensor_symmetry_pre_analysis<internal::tensor_symmetry_num_indices<Gen...>::value,
138 constexpr static std::size_t NumIndices = internal::tensor_symmetry_num_indices<Gen...>::value;
139 using Base =
typename internal::tensor_symmetry_pre_analysis<NumIndices, Gen...>::root_type;
142 inline SGroup() =
default;
143 inline SGroup(
const SGroup<Gen...>& other) =
default;
144 inline SGroup(SGroup<Gen...>&& other) : Base(other) {}
145 inline SGroup<Gen...>& operator=(
const SGroup<Gen...>& other) {
146 Base::operator=(other);
149 inline SGroup<Gen...>& operator=(SGroup<Gen...>&& other) {
150 Base::operator=(other);
159template <
typename... Sym>
160struct tensor_symmetry_num_indices {
161 constexpr static std::size_t value = 1;
164template <
int One_,
int Two_,
typename... Sym>
165struct tensor_symmetry_num_indices<Symmetry<One_, Two_>, Sym...> {
167 constexpr static std::size_t One =
static_cast<std::size_t
>(One_);
168 constexpr static std::size_t Two =
static_cast<std::size_t
>(Two_);
169 constexpr static std::size_t Three = tensor_symmetry_num_indices<Sym...>::value;
171 constexpr static std::size_t maxOneTwoPlusOne = ((One > Two) ? One : Two) + 1;
174 constexpr static std::size_t value = (maxOneTwoPlusOne > Three) ? maxOneTwoPlusOne : Three;
177template <
int One_,
int Two_,
typename... Sym>
178struct tensor_symmetry_num_indices<AntiSymmetry<One_, Two_>, Sym...>
179 :
public tensor_symmetry_num_indices<Symmetry<One_, Two_>, Sym...> {};
180template <
int One_,
int Two_,
typename... Sym>
181struct tensor_symmetry_num_indices<Hermiticity<One_, Two_>, Sym...>
182 :
public tensor_symmetry_num_indices<Symmetry<One_, Two_>, Sym...> {};
183template <
int One_,
int Two_,
typename... Sym>
184struct tensor_symmetry_num_indices<AntiHermiticity<One_, Two_>, Sym...>
185 :
public tensor_symmetry_num_indices<Symmetry<One_, Two_>, Sym...> {};
234template <std::
size_t NumIndices>
235struct tensor_symmetry_pre_analysis<NumIndices> {
236 using root_type = StaticSGroup<>;
239template <std::size_t NumIndices,
typename Gen_,
typename... Gens_>
240struct tensor_symmetry_pre_analysis<NumIndices, Gen_, Gens_...> {
241 constexpr static std::size_t max_static_generators = 4;
242 constexpr static std::size_t max_static_elements = 16;
243 using helper = tensor_static_symgroup_if<(
sizeof...(Gens_) + 1 <= max_static_generators), NumIndices, Gen_, Gens_...>;
244 constexpr static std::size_t possible_size = helper::size;
246 using root_type = std::conditional_t<possible_size == 0 || possible_size >= max_static_elements,
247 DynamicSGroupFromTemplateArgs<Gen_, Gens_...>,
typename helper::type>;
250template <
bool instantiate, std::size_t NumIndices,
typename... Gens>
251struct tensor_static_symgroup_if {
252 constexpr static std::size_t size = 0;
256template <std::size_t NumIndices,
typename... Gens>
257struct tensor_static_symgroup_if<true, NumIndices, Gens...> : tensor_static_symgroup<NumIndices, Gens...> {};
259template <
typename Tensor_>
260struct tensor_symmetry_assign_value {
261 using Index =
typename Tensor_::Index;
262 using Scalar =
typename Tensor_::Scalar;
263 constexpr static std::size_t NumIndices = Tensor_::NumIndices;
265 static inline int run(
const std::array<Index, NumIndices>& transformed_indices,
int transformation_flags,
int dummy,
266 Tensor_& tensor,
const Scalar& value_) {
267 Scalar value(value_);
268 if (transformation_flags & ConjugationFlag) value = numext::conj(value);
269 if (transformation_flags & NegationFlag) value = -value;
270 tensor.coeffRef(transformed_indices) = value;
275template <
typename Tensor_>
276struct tensor_symmetry_calculate_flags {
277 using Index =
typename Tensor_::Index;
278 constexpr static std::size_t NumIndices = Tensor_::NumIndices;
280 static inline int run(
const std::array<Index, NumIndices>& transformed_indices,
int transform_flags,
281 int current_flags,
const std::array<Index, NumIndices>& orig_indices) {
282 if (transformed_indices == orig_indices) {
283 if ((transform_flags & (ConjugationFlag | NegationFlag)) == (ConjugationFlag | NegationFlag))
284 return current_flags | GlobalImagFlag;
285 else if (transform_flags & ConjugationFlag)
286 return current_flags | GlobalRealFlag;
287 else if (transform_flags & NegationFlag)
288 return current_flags | GlobalZeroFlag;
290 return current_flags;
294template <
typename Tensor_,
typename Symmetry_,
int Flags = 0>
295class tensor_symmetry_value_setter {
297 using Index =
typename Tensor_::Index;
298 using Scalar =
typename Tensor_::Scalar;
299 constexpr static std::size_t NumIndices = Tensor_::NumIndices;
301 inline tensor_symmetry_value_setter(Tensor_& tensor, Symmetry_
const& symmetry,
302 std::array<Index, NumIndices>
const& indices)
303 : m_tensor(tensor), m_symmetry(symmetry), m_indices(indices) {}
305 inline tensor_symmetry_value_setter<Tensor_, Symmetry_, Flags>& operator=(Scalar
const& value) {
312 Symmetry_ m_symmetry;
313 std::array<Index, NumIndices> m_indices;
315 inline void doAssign(Scalar
const& value) {
316#ifdef EIGEN_TENSOR_SYMMETRY_CHECK_VALUES
317 int value_flags = m_symmetry.template apply<internal::tensor_symmetry_calculate_flags<Tensor_>,
int>(
318 m_indices, m_symmetry.globalFlags(), m_indices);
319 if (value_flags & GlobalRealFlag) eigen_assert(numext::imag(value) == 0);
320 if (value_flags & GlobalImagFlag) eigen_assert(numext::real(value) == 0);
322 m_symmetry.template apply<internal::tensor_symmetry_assign_value<Tensor_>,
int>(m_indices, 0, m_tensor, value);
Dynamic symmetry group.
Definition DynamicSymmetry.h:19
Static symmetry group.
Definition StaticSymmetry.h:165
Namespace containing all symbols from the Eigen library.