Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BlockIndexing.hh
Go to the documentation of this file.
1 ///
2 /// @file BlockIndexing.hh
3 /// @brief Outer product tensor indexer
4 ///
5 
6 //**** This file is a part of the HAMMER library
7 //**** Copyright (C) 2016 - 2020 The HAMMER Collaboration
8 //**** HAMMER is licensed under version 3 of the GPL; see COPYING for details
9 //**** Please note the MCnet academic guidelines; see GUIDELINES for details
10 
11 // -*- C++ -*-
12 #ifndef HAMMER_MATH_MULTIDIM_BLOCKINDEXING
13 #define HAMMER_MATH_MULTIDIM_BLOCKINDEXING
14 
15 #include <vector>
16 
21 
22 namespace Hammer {
23 
24 
25  namespace MultiDimensional {
26 
28  public:
29  using EntryType = std::vector<std::pair<SharedTensorData, bool>>;
30 
31  OuterElemIterator(const EntryType& entry);
32 
35  OuterElemIterator(OuterElemIterator&& other) = default;
36 
37  private:
39 
40  public:
41  OuterElemIterator begin() const;
42 
43  OuterElemIterator end() const;
44 
46 
48 
50  PositionType position(IndexType idx) const;
51  bool isAligned(IndexType idx) const;
52 
53  bool isSame(const OuterElemIterator& other) const;
54 
55 
56  private:
57  void incrementEntry(size_t position, int n);
58  void setInitialState();
59  void setFinalState();
60 
61  using IteratorList = std::vector<ISingleContainer::NonZeroIt>;
62  using ContainerList = std::vector<ISingleContainer*>;
63 
64  const EntryType& _entry;
68  };
69 
70  inline bool operator==(const OuterElemIterator& lhs,
71  const OuterElemIterator& rhs) {
72  return lhs.isSame(rhs);
73  }
74 
75  inline bool operator!=(const OuterElemIterator& lhs,
76  const OuterElemIterator& rhs) {
77  return !(lhs == rhs);
78  }
79 
80  using DotGroupType = std::tuple<IndexList, IndexList, IndexPairList>;
81  using DotGroupList = std::vector<DotGroupType>;
82 
83  class BlockIndexing {
84  public:
85  BlockIndexing();
86  BlockIndexing(const std::vector<IndexList>& dims, const std::vector<LabelsList>& labels);
88 
89  BlockIndexing(const BlockIndexing&) = default;
90  BlockIndexing(BlockIndexing&&) = default;
91  BlockIndexing& operator=(const BlockIndexing&) = default;
93 
94  ~BlockIndexing() = default;
95 
96  /// @brief rank of the tensor
97  /// @return the number of components
98  size_t rank() const;
99 
100  /// @brief dimension of a specific component
101  /// @param[in] index the component index
102  /// @return the dimension
103  IndexType dim(IndexType index) const;
104 
105  /// @brief dimension of a specific component by label.
106  /// If multiple components with the same label exists,
107  /// returns the dimension of first (they should all be the same
108  /// by consistency)
109  /// @param[in] label the component label
110  /// @return the dimension
111  IndexType dim(IndexLabel label) const;
112 
113  /// @brief get the labels of all the indices at once
114  /// @return the list of labels
115  LabelsList labels() const;
116 
117  /// @brief returns only the labels corresponding to spin indices
118  /// @return the set of labels of the spin indices (squashes repetitions if present)
120 
121  /// @brief get the dimensions of all the indices at once
122  /// @return the list of dimensions
123  IndexList dims() const;
124 
125  public:
126  /// @brief the number of elements (product of all the dimensions)
127  PositionType numValues() const;
128 
129  std::vector<IndexList> splitIndices(const IndexList& indices) const;
130 
131  std::vector<IndexList::const_iterator> splitIndices(IndexList::const_iterator first, IndexList::const_iterator last) const;
132 
133  /// @brief check that the indices are within range for each component
134  /// @param[in] indices the element indices
135  /// @return true if all indices are OK
136  bool checkValidIndices(const IndexList& indices) const;
137 
138  bool checkValidIndices(IndexList::const_iterator first, IndexList::const_iterator last) const;
139 
140  bool checkValidIndices(const std::vector<IndexList>& splits) const;
141 
142  /// @brief returns the position of the indices in the two tensor (this and another) that can be
143  /// contracted together, given a set of allowed index labels
144  /// @param[in] otherLabels the list of labels of the other tensor
145  /// @param[in] indices the list of labels of the allowed indices to be contracted
146  /// @param[in] sortedBySecond whether the result should be sorted according to the second element of the
147  /// pair
148  /// @return pairs of indices corresponding to coordinates in this and other tensor that needs to be
149  /// contracted together
150  IndexPairList getSameLabelPairs(const LabelsList& otherLabels, const UniqueLabelsList& indices,
151  bool sortedBySecond = true) const;
152 
153  /// @brief returns the position of the indices that can be traced together, given a set of allowed index
154  /// labels
155  /// @param[in] indices the list of labels of the allowed indices to be traced
156  /// @return pairs of coordinate indices that needs to be traced together
158 
159  IndexType labelIndex(IndexLabel label) const;
160 
161  bool isSameLabelShape(const LabelsList& otherLabels, const IndexList& otherIndices) const;
162 
163  template <typename S>
164  bool isSameLabelShape(const LabeledIndexing<S>& other) const;
165 
166  bool isSameLabelShape(const BlockIndexing& other, bool includeBlockShapes = false) const;
167 
168  void flipLabels();
169 
170  IndexPair getElementIndex(IndexType position) const;
171 
173 
174  size_t numSubIndexing() const;
175 
176  size_t maxSubRank() const;
177 
178  std::vector<std::tuple<IndexList, std::vector<bool>, PositionType>>
179  processShifts(const DotGroupList& chunks, IndexPairMember which) const;
180 
181  PositionType splitPosition(const OuterElemIterator& currentPosition, const DotGroupType& chunk,
182  const IndexList& outerShiftsInnerPositions, const std::vector<bool>& isOuter,
183  IndexList& innerList, std::vector<bool>& innerAdded,
184  bool shouldCompare = false) const;
185 
186  PositionType buildFullPosition(const OuterElemIterator& current, const IndexList& chunkIndices) const;
187 
188  private:
189  void calc();
190 
191  std::vector<LabeledIndexing<AlignedIndexing>> _subIndexing;
192  std::vector<IndexType> _splitIndices;
193  std::vector<PositionType> _splitPads;
195 
196  };
197 
198  template <typename S>
200  return isSameLabelShape(other.labels(), other.dims());
201  }
202 
203  } // namespace MultiDimensional
204 
205 } // namespace Hammer
206 
207 #endif
IndexType labelIndex(IndexLabel label) const
bool operator!=(const OuterElemIterator &lhs, const OuterElemIterator &rhs)
std::pair< IndexType, IndexType > IndexPair
bool isSame(const OuterElemIterator &other) const
bool checkValidIndices(const IndexList &indices) const
check that the indices are within range for each component
std::tuple< IndexList, IndexList, IndexPairList > DotGroupType
IndexPairList getSameLabelPairs(const LabelsList &otherLabels, const UniqueLabelsList &indices, bool sortedBySecond=true) const
returns the position of the indices in the two tensor (this and another) that can be contracted toget...
std::vector< IndexList > splitIndices(const IndexList &indices) const
std::vector< IndexPair > IndexPairList
IndexType dim(IndexType index) const
dimension of a specific component
size_t PositionType
IndexList dims() const
get the dimensions of all the indices at once
PositionType position(IndexType idx) const
UniqueLabelsList spinIndices() const
returns only the labels corresponding to spin indices
bool isSameLabelShape(const LabelsList &otherLabels, const IndexList &otherIndices) const
void incrementEntry(size_t position, int n)
std::vector< std::pair< SharedTensorData, bool >> EntryType
std::complex< double > ElementType
Definition: IContainer.hh:34
uint16_t IndexType
bool operator==(const OuterElemIterator &lhs, const OuterElemIterator &rhs)
PositionType splitPosition(const OuterElemIterator &currentPosition, const DotGroupType &chunk, const IndexList &outerShiftsInnerPositions, const std::vector< bool > &isOuter, IndexList &innerList, std::vector< bool > &innerAdded, bool shouldCompare=false) const
const LabelsList & labels() const
get the labels of all the indices at once
Forward declarations of types used in the tensor classes.
std::vector< ISingleContainer * > ContainerList
PositionType buildFullPosition(const OuterElemIterator &current, const IndexList &chunkIndices) const
std::vector< ISingleContainer::NonZeroIt > IteratorList
std::vector< LabeledIndexing< AlignedIndexing > > _subIndexing
std::vector< DotGroupType > DotGroupList
std::vector< IndexType > IndexList
IndexPairList getOppositeLabelPairs(const UniqueLabelsList &indices) const
returns the position of the indices that can be traced together, given a set of allowed index labels ...
OuterElemIterator & operator=(const OuterElemIterator &)=delete
Tensor labeled indexer.
std::vector< PositionType > _splitPads
IndexLabel
label identifiers of tensor indices they are used to determine which indices can be contracted togeth...
Definition: IndexLabels.hh:27
LabeledIndexing< AlignedIndexing > _globalIndexing
std::vector< std::tuple< IndexList, std::vector< bool >, PositionType > > processShifts(const DotGroupList &chunks, IndexPairMember which) const
BlockIndexing & operator=(const BlockIndexing &)=default
Interface class for single container data structure.
std::vector< IndexType > _splitIndices
size_t rank() const
rank of the tensor
const LabeledIndexing< AlignedIndexing > & getSubIndexing(IndexType position) const
std::vector< IndexLabel > LabelsList
PositionType numValues() const
the number of elements (product of all the dimensions)
std::set< IndexLabel > UniqueLabelsList
LabelsList labels() const
get the labels of all the indices at once
IndexPair getElementIndex(IndexType position) const
Sparse tensor indexer.