Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SequentialIndexing.hh
Go to the documentation of this file.
1 ///
2 /// @file SequentialIndexing.hh
3 /// @brief Non-sparse 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_SEQUENTIALINDEXING
13 #define HAMMER_MATH_MULTIDIM_SEQUENTIALINDEXING
14 
15 #include <algorithm>
16 
18 
19 namespace Hammer {
20 
21  namespace MultiDimensional {
22 
24  public:
26 
27  SequentialIndexing(IndexList dimensions);
28 
29  SequentialIndexing(const SequentialIndexing&) = default;
33 
34  ~SequentialIndexing() noexcept = default;
35 
36  public:
37  using StrideMap = std::vector<std::tuple<PositionType, long, bool>>;
38 
39  /// @brief rank of the tensor
40  /// @return the number of components
41  size_t rank() const;
42 
43  /// @brief dimension of a specific component
44  /// @param[in] index the component index
45  /// @return the dimension
46  IndexType dim(IndexType index) const;
47 
48  /// @brief get the dimensions of all the indices at once
49  /// @return the list of dimensions
50  const IndexList& dims() const;
51 
52  /// @brief stride of a specific component, i.e. the product of the dimension of
53  /// the indices to right of the given one (row-major convention).
54  /// The last index has stride equal to 1.
55  /// @param[in] index the component index
56  /// @return the stride
57  PositionType stride(IndexType index) const;
58 
59  /// @brief list of strides, i.e. the product of the dimension of
60  /// the indices to right of the given one (row-major convention).
61  /// The last index has stride equal to 1.
62  /// @return the strides
63  const PositionList& strides() const;
64 
65  /// @brief the number of elements (product of all the dimensions)
66  PositionType numValues() const;
67 
68  /// @brief convert the indices into the position indicizing
69  /// a sparse tensor container organized as row-major
70  /// @param[in] indices the element indices
71  /// @return the absolute position
72  PositionType indicesToPos(const IndexList& indices) const;
73 
74  PositionType indicesToPos(IndexList::const_iterator first, IndexList::const_iterator last) const;
75 
76  /// @brief check that the indices are within range for each component
77  /// @param[in] indices the element indices
78  /// @return true if all indices are OK
79  bool checkValidIndices(const IndexList& indices) const;
80 
81  bool checkValidIndices(IndexList::const_iterator first, IndexList::const_iterator last) const;
82 
83  /// @brief convert the absolute position (in row-major convention)
84  /// into the list of indices
85  /// @param[in] position the absolute position
86  /// @param[out] result the indices
87  void posToIndices(PositionType position, IndexList& result) const;
88 
89  /// @brief extract the value of the i-th index from an absolute position
90  /// @param[in] position the absolute position
91  /// @param[in] indexPosition the index coordinate i
92  /// @return the index value
93  IndexType ithIndexInPos(PositionType position, IndexType indexPosition) const;
94 
95  PositionType build2ndPosition(PositionType reducedPosition, PositionType innerPosition,
96  const PositionPairList& conversion) const;
97 
98  PositionPair splitPosition(PositionType position, const StrideMap& conversion) const;
99 
100  /// @brief extends an absolute position from a rank N-1 tensor to the corresponding
101  /// position for this rank N tensor given the missing index and its value.
102  /// Used in AddAt
103  /// @param position the sub-tensor position
104  /// @param indexPosition the missing index coordinate
105  /// @param indexValue the missing index value
106  /// @return the corresponding absolute position
107  PositionType extendPosition(PositionType position, IndexType indexPosition, IndexType indexValue) const;
108 
109  /// @brief Get the Inner Outer Strides object
110  ///
111  /// @param positions
112  /// @param secondStrides
113  /// @param flipSecond
114  /// @return
115  StrideMap getInnerOuterStrides(const IndexPairList& positions, const PositionList& secondStrides,
116  bool flipSecond = false) const;
117  // StrideMap getInnerOuterStrides(const IndexBoolPairList& positions) const;
118 
119  /// @brief Get the Outer Strides2nd object
120  ///
121  /// @param positions
122  /// @return
123  PositionPairList getOuterStrides2nd(const IndexPairList& positions) const;
124 
125  PositionType reducedNumValues(const IndexPairList& indices) const;
126 
127  public:
128  template<typename BasicIndexing>
129  bool isSameShape(const BasicIndexing& other) const;
130 
131  bool isSameShape(const IndexList& indices) const;
132 
133  protected:
134  StrideMap buildStrideMap(const std::map<IndexType, long> innerMap) const;
135 
136  private:
137  void calcPadding();
138 
139  IndexList _dimensions; ///< the dimensions of each tensor index
140  PositionList _strides; ///< the strides for each tensor index (necessary to convert coordinates to position in `_data`)
142  };
143 
144 
145  template <typename BasicIndexing>
147  return isSameShape(other.dims());
148  }
149 
150  } // namespace MultiDimensional
151 
152 } // namespace Hammer
153 
154 #endif
PositionPairList getOuterStrides2nd(const IndexPairList &positions) const
Get the Outer Strides2nd object.
SequentialIndexing & operator=(const SequentialIndexing &)=default
StrideMap buildStrideMap(const std::map< IndexType, long > innerMap) const
std::vector< IndexPair > IndexPairList
PositionList _strides
the strides for each tensor index (necessary to convert coordinates to position in _data) ...
size_t PositionType
const IndexList & dims() const
get the dimensions of all the indices at once
std::pair< PositionType, PositionType > PositionPair
PositionType extendPosition(PositionType position, IndexType indexPosition, IndexType indexValue) const
extends an absolute position from a rank N-1 tensor to the corresponding position for this rank N ten...
uint16_t IndexType
std::vector< std::tuple< PositionType, long, bool >> StrideMap
PositionPair splitPosition(PositionType position, const StrideMap &conversion) const
PositionType indicesToPos(const IndexList &indices) const
convert the indices into the position indicizing a sparse tensor container organized as row-major ...
Forward declarations of types used in the tensor classes.
void posToIndices(PositionType position, IndexList &result) const
convert the absolute position (in row-major convention) into the list of indices
size_t rank() const
rank of the tensor
bool isSameShape(const BasicIndexing &other) const
PositionType reducedNumValues(const IndexPairList &indices) const
std::vector< IndexType > IndexList
IndexType ithIndexInPos(PositionType position, IndexType indexPosition) const
extract the value of the i-th index from an absolute position
IndexType dim(IndexType index) const
dimension of a specific component
PositionType numValues() const
the number of elements (product of all the dimensions)
std::vector< PositionPair > PositionPairList
const PositionList & strides() const
list of strides, i.e.
StrideMap getInnerOuterStrides(const IndexPairList &positions, const PositionList &secondStrides, bool flipSecond=false) const
Get the Inner Outer Strides object.
bool checkValidIndices(const IndexList &indices) const
check that the indices are within range for each component
PositionType build2ndPosition(PositionType reducedPosition, PositionType innerPosition, const PositionPairList &conversion) const
PositionType stride(IndexType index) const
stride of a specific component, i.e.
IndexList _dimensions
the dimensions of each tensor index
std::vector< PositionType > PositionList