10#ifndef EIGEN_CXX11_TENSORSYMMETRY_SYMMETRY_H
11#define EIGEN_CXX11_TENSORSYMMETRY_SYMMETRY_H
14#include "./InternalHeaderCheck.h"
18enum { NegationFlag = 0x01, ConjugationFlag = 0x02 };
20enum { GlobalRealFlag = 0x01, GlobalImagFlag = 0x02, GlobalZeroFlag = 0x03 };
24template <std::size_t NumIndices,
typename... Sym>
25struct tensor_symmetry_pre_analysis;
26template <std::size_t NumIndices,
typename... Sym>
27struct tensor_static_symgroup;
28template <
bool instantiate, std::size_t NumIndices,
typename... Sym>
29struct tensor_static_symgroup_if;
30template <
typename Tensor_>
31struct tensor_symmetry_calculate_flags;
32template <
typename Tensor_>
33struct tensor_symmetry_assign_value;
34template <
typename... Sym>
35struct tensor_symmetry_num_indices;
39template <
int One_,
int Two_>
41 static_assert(One_ != Two_,
"Symmetries must cover distinct indices.");
42 constexpr static int One = One_;
43 constexpr static int Two = Two_;
44 constexpr static int Flags = 0;
47template <
int One_,
int Two_>
49 static_assert(One_ != Two_,
"Symmetries must cover distinct indices.");
50 constexpr static int One = One_;
51 constexpr static int Two = Two_;
52 constexpr static int Flags = NegationFlag;
55template <
int One_,
int Two_>
57 static_assert(One_ != Two_,
"Symmetries must cover distinct indices.");
58 constexpr static int One = One_;
59 constexpr static int Two = Two_;
60 constexpr static int Flags = ConjugationFlag;
63template <
int One_,
int Two_>
64struct AntiHermiticity {
65 static_assert(One_ != Two_,
"Symmetries must cover distinct indices.");
66 constexpr static int One = One_;
67 constexpr static int Two = Two_;
68 constexpr static int Flags = ConjugationFlag | NegationFlag;
96template <
typename... Gen>
97class DynamicSGroupFromTemplateArgs;
118template <
typename... Gen>
133template <
typename... Gen>
134class SGroup :
public internal::tensor_symmetry_pre_analysis<internal::tensor_symmetry_num_indices<Gen...>::value,
137 constexpr static std::size_t NumIndices = internal::tensor_symmetry_num_indices<Gen...>::value;
138 typedef typename internal::tensor_symmetry_pre_analysis<NumIndices, Gen...>::root_type Base;
141 inline SGroup() : Base() {}
142 inline SGroup(
const SGroup<Gen...>& other) : Base(other) {}
143 inline SGroup(SGroup<Gen...>&& other) : Base(other) {}
144 inline SGroup<Gen...>& operator=(
const SGroup<Gen...>& other) {
145 Base::operator=(other);
148 inline SGroup<Gen...>& operator=(SGroup<Gen...>&& other) {
149 Base::operator=(other);
158template <
typename... Sym>
159struct tensor_symmetry_num_indices {
160 constexpr static std::size_t value = 1;
163template <
int One_,
int Two_,
typename... Sym>
164struct tensor_symmetry_num_indices<Symmetry<One_, Two_>, Sym...> {
166 constexpr static std::size_t One =
static_cast<std::size_t
>(One_);
167 constexpr static std::size_t Two =
static_cast<std::size_t
>(Two_);
168 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 typedef StaticSGroup<> root_type;
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 typedef tensor_static_symgroup_if<(
sizeof...(Gens_) + 1 <= max_static_generators), NumIndices, Gen_, Gens_...> helper;
244 constexpr static std::size_t possible_size = helper::size;
246 typedef std::conditional_t<possible_size == 0 || possible_size >= max_static_elements,
247 DynamicSGroupFromTemplateArgs<Gen_, Gens_...>,
typename helper::type>
251template <
bool instantiate, std::size_t NumIndices,
typename... Gens>
252struct tensor_static_symgroup_if {
253 constexpr static std::size_t size = 0;
257template <std::size_t NumIndices,
typename... Gens>
258struct tensor_static_symgroup_if<true, NumIndices, Gens...> : tensor_static_symgroup<NumIndices, Gens...> {};
260template <
typename Tensor_>
261struct tensor_symmetry_assign_value {
262 typedef typename Tensor_::Index Index;
263 typedef typename Tensor_::Scalar Scalar;
264 constexpr static std::size_t NumIndices = Tensor_::NumIndices;
266 static inline int run(
const std::array<Index, NumIndices>& transformed_indices,
int transformation_flags,
int dummy,
267 Tensor_& tensor,
const Scalar& value_) {
268 Scalar value(value_);
269 if (transformation_flags & ConjugationFlag) value = numext::conj(value);
270 if (transformation_flags & NegationFlag) value = -value;
271 tensor.coeffRef(transformed_indices) = value;
276template <
typename Tensor_>
277struct tensor_symmetry_calculate_flags {
278 typedef typename Tensor_::Index Index;
279 constexpr static std::size_t NumIndices = Tensor_::NumIndices;
281 static inline int run(
const std::array<Index, NumIndices>& transformed_indices,
int transform_flags,
282 int current_flags,
const std::array<Index, NumIndices>& orig_indices) {
283 if (transformed_indices == orig_indices) {
284 if (transform_flags & (ConjugationFlag | NegationFlag))
285 return current_flags | GlobalImagFlag;
286 else if (transform_flags & ConjugationFlag)
287 return current_flags | GlobalRealFlag;
288 else if (transform_flags & NegationFlag)
289 return current_flags | GlobalZeroFlag;
291 return current_flags;
295template <
typename Tensor_,
typename Symmetry_,
int Flags = 0>
296class tensor_symmetry_value_setter {
298 typedef typename Tensor_::Index Index;
299 typedef typename Tensor_::Scalar Scalar;
300 constexpr static std::size_t NumIndices = Tensor_::NumIndices;
302 inline tensor_symmetry_value_setter(Tensor_& tensor, Symmetry_
const& symmetry,
303 std::array<Index, NumIndices>
const& indices)
304 : m_tensor(tensor), m_symmetry(symmetry), m_indices(indices) {}
306 inline tensor_symmetry_value_setter<Tensor_, Symmetry_, Flags>& operator=(Scalar
const& value) {
313 Symmetry_ m_symmetry;
314 std::array<Index, NumIndices> m_indices;
316 inline void doAssign(Scalar
const& value) {
317#ifdef EIGEN_TENSOR_SYMMETRY_CHECK_VALUES
318 int value_flags = m_symmetry.template apply<internal::tensor_symmetry_calculate_flags<Tensor_>,
int>(
319 m_indices, m_symmetry.globalFlags(), m_indices);
320 if (value_flags & GlobalRealFlag) eigen_assert(numext::imag(value) == 0);
321 if (value_flags & GlobalImagFlag) eigen_assert(numext::real(value) == 0);
323 m_symmetry.template apply<internal::tensor_symmetry_assign_value<Tensor_>,
int>(m_indices, 0, m_tensor, value);
Dynamic symmetry group.
Definition DynamicSymmetry.h:18
Static symmetry group.
Definition StaticSymmetry.h:172
Namespace containing all symbols from the Eigen library.