Symmetry.h
Go to the documentation of this file.
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 
10 #ifndef EIGEN_CXX11_TENSORSYMMETRY_SYMMETRY_H
11 #define EIGEN_CXX11_TENSORSYMMETRY_SYMMETRY_H
12 
13 namespace Eigen {
14 
15 enum {
16  NegationFlag = 0x01,
18 };
19 
20 enum {
24 };
25 
26 namespace internal {
27 
28 template<std::size_t NumIndices, typename... Sym> struct tensor_symmetry_pre_analysis;
29 template<std::size_t NumIndices, typename... Sym> struct tensor_static_symgroup;
30 template<bool instantiate, std::size_t NumIndices, typename... Sym> struct tensor_static_symgroup_if;
31 template<typename Tensor_> struct tensor_symmetry_calculate_flags;
32 template<typename Tensor_> struct tensor_symmetry_assign_value;
33 template<typename... Sym> struct tensor_symmetry_num_indices;
34 
35 } // end namespace internal
36 
37 template<int One_, int Two_>
38 struct Symmetry
39 {
40  static_assert(One_ != Two_, "Symmetries must cover distinct indices.");
41  constexpr static int One = One_;
42  constexpr static int Two = Two_;
43  constexpr static int Flags = 0;
44 };
45 
46 template<int One_, int Two_>
48 {
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;
53 };
54 
55 template<int One_, int Two_>
57 {
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 
64 template<int One_, int Two_>
66 {
67  static_assert(One_ != Two_, "Symmetries must cover distinct indices.");
68  constexpr static int One = One_;
69  constexpr static int Two = Two_;
70  constexpr static int Flags = ConjugationFlag | NegationFlag;
71 };
72 
86 class DynamicSGroup;
87 
98 template<typename... Gen>
100 
120 template<typename... Gen>
121 class StaticSGroup;
122 
135 template<typename... Gen>
136 class SGroup : public internal::tensor_symmetry_pre_analysis<internal::tensor_symmetry_num_indices<Gen...>::value, Gen...>::root_type
137 {
138  public:
140  typedef typename internal::tensor_symmetry_pre_analysis<NumIndices, Gen...>::root_type Base;
141 
142  // make standard constructors + assignment operators public
143  inline SGroup() : Base() { }
144  inline SGroup(const SGroup<Gen...>& other) : Base(other) { }
146  inline SGroup<Gen...>& operator=(const SGroup<Gen...>& other) { Base::operator=(other); return *this; }
147  inline SGroup<Gen...>& operator=(SGroup<Gen...>&& other) { Base::operator=(other); return *this; }
148 
149  // all else is defined in the base class
150 };
151 
152 namespace internal {
153 
154 template<typename... Sym> struct tensor_symmetry_num_indices
155 {
156  constexpr static std::size_t value = 1;
157 };
158 
159 template<int One_, int Two_, typename... Sym> struct tensor_symmetry_num_indices<Symmetry<One_, Two_>, Sym...>
160 {
161 private:
162  constexpr static std::size_t One = static_cast<std::size_t>(One_);
163  constexpr static std::size_t Two = static_cast<std::size_t>(Two_);
164  constexpr static std::size_t Three = tensor_symmetry_num_indices<Sym...>::value;
165 
166  // don't use std::max, since it's not constexpr until C++14...
167  constexpr static std::size_t maxOneTwoPlusOne = ((One > Two) ? One : Two) + 1;
168 public:
169  constexpr static std::size_t value = (maxOneTwoPlusOne > Three) ? maxOneTwoPlusOne : Three;
170 };
171 
172 template<int One_, int Two_, typename... Sym> struct tensor_symmetry_num_indices<AntiSymmetry<One_, Two_>, Sym...>
173  : public tensor_symmetry_num_indices<Symmetry<One_, Two_>, Sym...> {};
174 template<int One_, int Two_, typename... Sym> struct tensor_symmetry_num_indices<Hermiticity<One_, Two_>, Sym...>
175  : public tensor_symmetry_num_indices<Symmetry<One_, Two_>, Sym...> {};
176 template<int One_, int Two_, typename... Sym> struct tensor_symmetry_num_indices<AntiHermiticity<One_, Two_>, Sym...>
177  : public tensor_symmetry_num_indices<Symmetry<One_, Two_>, Sym...> {};
178 
226 template<std::size_t NumIndices>
228 {
230 };
231 
232 template<std::size_t NumIndices, typename Gen_, typename... Gens_>
233 struct tensor_symmetry_pre_analysis<NumIndices, Gen_, Gens_...>
234 {
235  constexpr static std::size_t max_static_generators = 4;
236  constexpr static std::size_t max_static_elements = 16;
237  typedef tensor_static_symgroup_if<(sizeof...(Gens_) + 1 <= max_static_generators), NumIndices, Gen_, Gens_...> helper;
238  constexpr static std::size_t possible_size = helper::size;
239 
240  typedef typename conditional<
241  possible_size == 0 || possible_size >= max_static_elements,
243  typename helper::type
245 };
246 
247 template<bool instantiate, std::size_t NumIndices, typename... Gens>
249 {
250  constexpr static std::size_t size = 0;
251  typedef void type;
252 };
253 
254 template<std::size_t NumIndices, typename... Gens>
255 struct tensor_static_symgroup_if<true, NumIndices, Gens...> : tensor_static_symgroup<NumIndices, Gens...> {};
256 
257 template<typename Tensor_>
259 {
260  typedef typename Tensor_::Index Index;
261  typedef typename Tensor_::Scalar Scalar;
262  constexpr static std::size_t NumIndices = Tensor_::NumIndices;
263 
264  static inline int run(const std::array<Index, NumIndices>& transformed_indices, int transformation_flags, int dummy, Tensor_& tensor, const Scalar& value_)
265  {
266  Scalar value(value_);
267  if (transformation_flags & ConjugationFlag)
269  if (transformation_flags & NegationFlag)
270  value = -value;
271  tensor.coeffRef(transformed_indices) = value;
272  return dummy;
273  }
274 };
275 
276 template<typename Tensor_>
277 struct tensor_symmetry_calculate_flags
278 {
279  typedef typename Tensor_::Index Index;
280  constexpr static std::size_t NumIndices = Tensor_::NumIndices;
281 
282  static inline int run(const std::array<Index, NumIndices>& transformed_indices, int transform_flags, int current_flags, const std::array<Index, NumIndices>& orig_indices)
283  {
284  if (transformed_indices == orig_indices) {
285  if (transform_flags & (ConjugationFlag | NegationFlag))
286  return current_flags | GlobalImagFlag; // anti-hermitian diagonal
287  else if (transform_flags & ConjugationFlag)
288  return current_flags | GlobalRealFlag; // hermitian diagonal
289  else if (transform_flags & NegationFlag)
290  return current_flags | GlobalZeroFlag; // anti-symmetric diagonal
291  }
292  return current_flags;
293  }
294 };
295 
296 template<typename Tensor_, typename Symmetry_, int Flags = 0>
298 {
299  public:
300  typedef typename Tensor_::Index Index;
301  typedef typename Tensor_::Scalar Scalar;
302  constexpr static std::size_t NumIndices = Tensor_::NumIndices;
303 
304  inline tensor_symmetry_value_setter(Tensor_& tensor, Symmetry_ const& symmetry, std::array<Index, NumIndices> const& indices)
305  : m_tensor(tensor), m_symmetry(symmetry), m_indices(indices) { }
306 
308  {
309  doAssign(value);
310  return *this;
311  }
312  private:
313  Tensor_& m_tensor;
314  Symmetry_ m_symmetry;
315  std::array<Index, NumIndices> m_indices;
316 
317  inline void doAssign(Scalar const& value)
318  {
319  #ifdef EIGEN_TENSOR_SYMMETRY_CHECK_VALUES
320  int value_flags = m_symmetry.template apply<internal::tensor_symmetry_calculate_flags<Tensor_>, int>(m_indices, m_symmetry.globalFlags(), m_indices);
321  if (value_flags & GlobalRealFlag)
323  if (value_flags & GlobalImagFlag)
325  #endif
326  m_symmetry.template apply<internal::tensor_symmetry_assign_value<Tensor_>, int>(m_indices, 0, m_tensor, value);
327  }
328 };
329 
330 } // end namespace internal
331 
332 } // end namespace Eigen
333 
334 #endif // EIGEN_CXX11_TENSORSYMMETRY_SYMMETRY_H
335 
336 /*
337  * kate: space-indent on; indent-width 2; mixedindent off; indent-mode cstyle;
338  */
Eigen::internal::tensor_symmetry_assign_value::NumIndices
constexpr static std::size_t NumIndices
Definition: Symmetry.h:262
Eigen::SGroup::Base
internal::tensor_symmetry_pre_analysis< NumIndices, Gen... >::root_type Base
Definition: Symmetry.h:140
Eigen::AntiSymmetry
Definition: Symmetry.h:47
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::GlobalRealFlag
@ GlobalRealFlag
Definition: Symmetry.h:21
Eigen::GlobalZeroFlag
@ GlobalZeroFlag
Definition: Symmetry.h:23
Eigen::internal::tensor_symmetry_value_setter::NumIndices
constexpr static std::size_t NumIndices
Definition: Symmetry.h:302
Eigen::SGroup::SGroup
SGroup(const SGroup< Gen... > &other)
Definition: Symmetry.h:144
Eigen::internal::tensor_symmetry_calculate_flags
Definition: Symmetry.h:31
Eigen::internal::tensor_symmetry_calculate_flags::NumIndices
constexpr static std::size_t NumIndices
Definition: Symmetry.h:280
Eigen::internal::tensor_symmetry_value_setter::m_indices
std::array< Index, NumIndices > m_indices
Definition: Symmetry.h:315
Eigen::Symmetry::One
constexpr static int One
Definition: Symmetry.h:41
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::internal::tensor_symmetry_pre_analysis
Definition: Symmetry.h:28
Eigen::internal::tensor_symmetry_value_setter
Definition: Symmetry.h:297
Eigen::DynamicSGroupFromTemplateArgs
Definition: DynamicSymmetry.h:154
Eigen::internal::tensor_symmetry_num_indices::value
constexpr static std::size_t value
Definition: Symmetry.h:156
Eigen::internal::tensor_static_symgroup_if::type
void type
Definition: Symmetry.h:251
Eigen::SGroup
Symmetry group, initialized from template arguments.
Definition: Symmetry.h:136
real
float real
Definition: datatypes.h:10
Eigen::AntiHermiticity::Two
constexpr static int Two
Definition: Symmetry.h:69
type
Definition: pytypes.h:1525
Eigen::internal::tensor_symmetry_calculate_flags::run
static int run(const std::array< Index, NumIndices > &transformed_indices, int transform_flags, int current_flags, const std::array< Index, NumIndices > &orig_indices)
Definition: Symmetry.h:282
Eigen::AntiSymmetry::Flags
constexpr static int Flags
Definition: Symmetry.h:52
Eigen::Symmetry::Flags
constexpr static int Flags
Definition: Symmetry.h:43
Eigen::AntiHermiticity
Definition: Symmetry.h:65
Eigen::DynamicSGroup
Dynamic symmetry group.
Definition: DynamicSymmetry.h:15
Eigen::Symmetry
Definition: Symmetry.h:38
pybind_wrapper_test_script.dummy
dummy
Definition: pybind_wrapper_test_script.py:42
Eigen::Symmetry::Two
constexpr static int Two
Definition: Symmetry.h:42
test_eigen_tensor.indices
indices
Definition: test_eigen_tensor.py:33
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Eigen::Hermiticity::Flags
constexpr static int Flags
Definition: Symmetry.h:61
Eigen::SGroup::NumIndices
constexpr static std::size_t NumIndices
Definition: Symmetry.h:139
Eigen::internal::tensor_symmetry_assign_value
Definition: Symmetry.h:32
Eigen::internal::tensor_static_symgroup_if
Definition: Symmetry.h:30
Eigen::NegationFlag
@ NegationFlag
Definition: Symmetry.h:16
Eigen::internal::tensor_symmetry_value_setter::Scalar
Tensor_::Scalar Scalar
Definition: Symmetry.h:301
Eigen::Hermiticity::Two
constexpr static int Two
Definition: Symmetry.h:60
Eigen::internal::tensor_symmetry_assign_value::run
static int run(const std::array< Index, NumIndices > &transformed_indices, int transformation_flags, int dummy, Tensor_ &tensor, const Scalar &value_)
Definition: Symmetry.h:264
Eigen::internal::tensor_symmetry_num_indices
Definition: Symmetry.h:33
Eigen::internal::tensor_symmetry_pre_analysis< NumIndices, Gen_, Gens_... >::root_type
helper::type ::type root_type
Definition: Symmetry.h:244
Eigen::internal::tensor_symmetry_assign_value::Index
Tensor_::Index Index
Definition: Symmetry.h:260
Eigen::StaticSGroup
Static symmetry group.
Definition: StaticSymmetry.h:181
imag
const EIGEN_DEVICE_FUNC ImagReturnType imag() const
Definition: CommonCwiseUnaryOps.h:109
Eigen::internal::tensor_symmetry_value_setter::operator=
tensor_symmetry_value_setter< Tensor_, Symmetry_, Flags > & operator=(Scalar const &value)
Definition: Symmetry.h:307
Eigen::internal::tensor_symmetry_assign_value::Scalar
Tensor_::Scalar Scalar
Definition: Symmetry.h:261
Eigen::internal::tensor_symmetry_pre_analysis< NumIndices >::root_type
StaticSGroup root_type
Definition: Symmetry.h:229
conj
AnnoyingScalar conj(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:104
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:490
Eigen::SGroup::SGroup
SGroup()
Definition: Symmetry.h:143
Eigen::internal::tensor_symmetry_value_setter::Index
Tensor_::Index Index
Definition: Symmetry.h:300
Eigen::internal::tensor_symmetry_value_setter::m_tensor
Tensor_ & m_tensor
Definition: Symmetry.h:313
Eigen::internal::tensor_static_symgroup
Definition: StaticSymmetry.h:115
Eigen::SGroup::operator=
SGroup< Gen... > & operator=(const SGroup< Gen... > &other)
Definition: Symmetry.h:146
Eigen::internal::tensor_symmetry_value_setter::tensor_symmetry_value_setter
tensor_symmetry_value_setter(Tensor_ &tensor, Symmetry_ const &symmetry, std::array< Index, NumIndices > const &indices)
Definition: Symmetry.h:304
Eigen::internal::conditional
Definition: Meta.h:109
Eigen::AntiHermiticity::One
constexpr static int One
Definition: Symmetry.h:68
Eigen::internal::tensor_symmetry_calculate_flags::Index
Tensor_::Index Index
Definition: Symmetry.h:279
Eigen::internal::tensor_symmetry_value_setter::m_symmetry
Symmetry_ m_symmetry
Definition: Symmetry.h:314
Eigen::AntiSymmetry::One
constexpr static int One
Definition: Symmetry.h:50
Eigen::Hermiticity
Definition: Symmetry.h:56
Eigen::internal::tensor_symmetry_value_setter::doAssign
void doAssign(Scalar const &value)
Definition: Symmetry.h:317
internal
Definition: BandTriangularSolver.h:13
Eigen::Hermiticity::One
constexpr static int One
Definition: Symmetry.h:59
Eigen::internal::tensor_symmetry_pre_analysis< NumIndices, Gen_, Gens_... >::helper
tensor_static_symgroup_if<(sizeof...(Gens_)+1<=max_static_generators), NumIndices, Gen_, Gens_... > helper
Definition: Symmetry.h:237
Base
Definition: test_virtual_functions.cpp:156
Eigen::internal::tensor_static_symgroup_if::size
constexpr static std::size_t size
Definition: Symmetry.h:250
Eigen::SGroup::operator=
SGroup< Gen... > & operator=(SGroup< Gen... > &&other)
Definition: Symmetry.h:147
Eigen::AntiHermiticity::Flags
constexpr static int Flags
Definition: Symmetry.h:70
Eigen::SGroup::SGroup
SGroup(SGroup< Gen... > &&other)
Definition: Symmetry.h:145
Eigen::GlobalImagFlag
@ GlobalImagFlag
Definition: Symmetry.h:22
Eigen::AntiSymmetry::Two
constexpr static int Two
Definition: Symmetry.h:51
test_callbacks.value
value
Definition: test_callbacks.py:160
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Eigen::ConjugationFlag
@ ConjugationFlag
Definition: Symmetry.h:17
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:05:22