Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Symmetry.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_SYMMETRY_H
12#define EIGEN_TENSORSYMMETRY_SYMMETRY_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19enum { NegationFlag = 0x01, ConjugationFlag = 0x02 };
20
21enum { GlobalRealFlag = 0x01, GlobalImagFlag = 0x02, GlobalZeroFlag = 0x03 };
22
23namespace internal {
24
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;
37
38} // end namespace internal
39
40template <int One_, int Two_>
41struct Symmetry {
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;
46};
47
48template <int One_, int Two_>
49struct AntiSymmetry {
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;
54};
55
56template <int One_, int Two_>
57struct Hermiticity {
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;
62};
63
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;
70};
71
85class DynamicSGroup;
86
97template <typename... Gen>
98class DynamicSGroupFromTemplateArgs;
99
119template <typename... Gen>
120class StaticSGroup;
121
134template <typename... Gen>
135class SGroup : public internal::tensor_symmetry_pre_analysis<internal::tensor_symmetry_num_indices<Gen...>::value,
136 Gen...>::root_type {
137 public:
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;
140
141 // make standard constructors + assignment operators public
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);
147 return *this;
148 }
149 inline SGroup<Gen...>& operator=(SGroup<Gen...>&& other) {
150 Base::operator=(other);
151 return *this;
152 }
153
154 // all else is defined in the base class
155};
156
157namespace internal {
158
159template <typename... Sym>
160struct tensor_symmetry_num_indices {
161 constexpr static std::size_t value = 1;
162};
163
164template <int One_, int Two_, typename... Sym>
165struct tensor_symmetry_num_indices<Symmetry<One_, Two_>, Sym...> {
166 private:
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;
170
171 constexpr static std::size_t maxOneTwoPlusOne = ((One > Two) ? One : Two) + 1;
172
173 public:
174 constexpr static std::size_t value = (maxOneTwoPlusOne > Three) ? maxOneTwoPlusOne : Three;
175};
176
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...> {};
186
234template <std::size_t NumIndices>
235struct tensor_symmetry_pre_analysis<NumIndices> {
236 using root_type = StaticSGroup<>;
237};
238
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;
245
246 using root_type = std::conditional_t<possible_size == 0 || possible_size >= max_static_elements,
247 DynamicSGroupFromTemplateArgs<Gen_, Gens_...>, typename helper::type>;
248};
249
250template <bool instantiate, std::size_t NumIndices, typename... Gens>
251struct tensor_static_symgroup_if {
252 constexpr static std::size_t size = 0;
253 using type = void;
254};
255
256template <std::size_t NumIndices, typename... Gens>
257struct tensor_static_symgroup_if<true, NumIndices, Gens...> : tensor_static_symgroup<NumIndices, Gens...> {};
258
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;
264
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;
271 return dummy;
272 }
273};
274
275template <typename Tensor_>
276struct tensor_symmetry_calculate_flags {
277 using Index = typename Tensor_::Index;
278 constexpr static std::size_t NumIndices = Tensor_::NumIndices;
279
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; // anti-hermitian diagonal
285 else if (transform_flags & ConjugationFlag)
286 return current_flags | GlobalRealFlag; // hermitian diagonal
287 else if (transform_flags & NegationFlag)
288 return current_flags | GlobalZeroFlag; // anti-symmetric diagonal
289 }
290 return current_flags;
291 }
292};
293
294template <typename Tensor_, typename Symmetry_, int Flags = 0>
295class tensor_symmetry_value_setter {
296 public:
297 using Index = typename Tensor_::Index;
298 using Scalar = typename Tensor_::Scalar;
299 constexpr static std::size_t NumIndices = Tensor_::NumIndices;
300
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) {}
304
305 inline tensor_symmetry_value_setter<Tensor_, Symmetry_, Flags>& operator=(Scalar const& value) {
306 doAssign(value);
307 return *this;
308 }
309
310 private:
311 Tensor_& m_tensor;
312 Symmetry_ m_symmetry;
313 std::array<Index, NumIndices> m_indices;
314
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);
321#endif
322 m_symmetry.template apply<internal::tensor_symmetry_assign_value<Tensor_>, int>(m_indices, 0, m_tensor, value);
323 }
324};
325
326} // end namespace internal
327
328} // end namespace Eigen
329
330#endif // EIGEN_TENSORSYMMETRY_SYMMETRY_H
331
332/*
333 * kate: space-indent on; indent-width 2; mixedindent off; indent-mode cstyle;
334 */
Dynamic symmetry group.
Definition DynamicSymmetry.h:19
Static symmetry group.
Definition StaticSymmetry.h:165
Namespace containing all symbols from the Eigen library.