Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseContainer.hh
Go to the documentation of this file.
1 ///
2 /// @file SparseContainer.hh
3 /// @brief Sparse tensor data container
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_SPARSECONTAINER
13 #define HAMMER_MATH_MULTIDIM_SPARSECONTAINER
14 
15 #include <complex>
16 #include <map>
17 #include <memory>
18 #include <set>
19 #include <type_traits>
20 #include <utility>
21 #include <vector>
22 
26 
27 namespace Hammer {
28 
29  class Log;
30 
31  namespace Serial {
32 
33  struct FBSingleTensor;
34 
35  }
36 
37  namespace MultiDimensional {
38 
39  namespace Ops {
40 
41  class Sum;
42  class Multiply;
43  class Divide;
44  class Trace;
45  class Dot;
46  class AddAt;
47  class Convert;
48 
49  } // namespace Ops
50 
51  class SparseContainer final : public ISingleContainer {
52  private:
53  using DataType = std::map<PositionType, ElementType>;
54 
55  public:
56  SparseContainer(const IndexList& dimensions, const LabelsList& labels);
58  SparseContainer(const Serial::FBSingleTensor* input);
59 
60  ElementType value(const IndexList& indices) const;
61  ElementType value(IndexList::const_iterator first, IndexList::const_iterator last) const;
62  void setValue(const IndexList& indices, ElementType value = 0.);
63  void setValue(IndexList::const_iterator first, IndexList::const_iterator last, ElementType value = 0.);
64 
65  using iterator = DataType::iterator;
66  using const_iterator = DataType::const_iterator;
67 
68 
69  iterator begin();
70  const_iterator begin() const;
71 
72  iterator end();
73  const_iterator end() const;
74 
76 
78 
79  public:
80  size_t rank() const override;
81  IndexList dims() const override;
82  LabelsList labels() const override;
83  size_t numValues() const override;
84  size_t dataSize() const override;
85  size_t entrySize() const override;
86  IndexType labelToIndex(IndexLabel label) const override;
87 
89  const UniqueLabelsList& indices) const override;
90  IndexPairList getSpinLabelPairs() const override;
91 
92  bool isSameShape(const IContainer& other) const override;
93  bool canAddAt(const IContainer& subContainer, IndexLabel coord, IndexType position) const override;
94 
95  reference element(const IndexList& coords = {}) override;
96  ElementType element(const IndexList& coords = {}) const override;
97 
98  reference element(IndexList::const_iterator start, IndexList::const_iterator end) override;
99  ElementType element(IndexList::const_iterator start, IndexList::const_iterator end) const override;
100 
101  bool compare(const IContainer& other) const override;
102  TensorData clone() const override;
103  void clear() override;
104 
105  IContainer& operator*=(double value) override;
106  IContainer& operator*=(const ElementType value) override;
107 
108  IContainer& conjugate() override;
109 
110  SerialType write(flatbuffers::FlatBufferBuilder* msgwriter) const override;
111 
112  NonZeroIt firstNonZero() const override;
113  NonZeroIt endNonZero() const override;
114 
115  protected:
116  /// @brief logging facility
117  /// @return stream to be used for logging
118  Log& getLog() const;
119 
120  private:
121 
122  class ItAligned : public ItBase {
123  public:
124  ItAligned(DataType::const_iterator it);
125 
126  IContainer::ElementType value() const override;
127  PositionType position() const override;
128  void next(int n = 1) override;
129  bool isSame(const ItBase& other) const override;
130  bool isAligned() const override;
131  ptrdiff_t distanceFrom(const ItBase& other) const override;
132 
133  private:
134  friend class SparseContainer;
135  DataType::const_iterator _it;
136  };
137 
138  // all these classes are friends to be able to use getIndexing (not necessary, but cleaner)
139  friend class Ops::Sum;
140  friend class Ops::Multiply;
141  friend class Ops::Divide;
142  friend class Ops::Trace;
143  friend class Ops::Dot;
144  friend class Ops::AddAt;
145  friend class Ops::Convert;
146 
148 
149  private:
150 
151  mutable DataType _data;
153 
154  };
155 
156  TensorData makeEmptySparse(const IndexList& dimensions, const LabelsList& labels);
158 
159  } // namespace MultiDimensional
160 
161 } // namespace Hammer
162 
163 
164 #endif
Log & getLog() const
logging facility
TensorData makeEmptySparse(const IndexList &dimensions, const LabelsList &labels)
std::pair< flatbuffers::Offset< void >, Serial::FBTensorTypes > SerialType
Definition: IContainer.hh:68
bool compare(const IContainer &other) const override
const LabeledIndexing< AlignedIndexing > & getIndexing() const
SerialType write(flatbuffers::FlatBufferBuilder *msgwriter) const override
std::map< PositionType, ElementType > DataType
std::vector< IndexPair > IndexPairList
size_t PositionType
std::complex< double > ElementType
Definition: IContainer.hh:34
uint16_t IndexType
IndexPairList getSameLabelPairs(const IContainer &other, const UniqueLabelsList &indices) const override
std::unique_ptr< IContainer > TensorData
IContainer::ElementType value() const override
LabeledIndexing< AlignedIndexing > _indexing
std::vector< IndexType > IndexList
ElementType value(const IndexList &indices) const
Logging class.
Definition: Logging.hh:33
Tensor labeled indexer.
bool canAddAt(const IContainer &subContainer, IndexLabel coord, IndexType position) const override
IndexLabel
label identifiers of tensor indices they are used to determine which indices can be contracted togeth...
Definition: IndexLabels.hh:27
Interface class for single container data structure.
ptrdiff_t distanceFrom(const ItBase &other) const override
std::vector< IndexLabel > LabelsList
std::set< IndexLabel > UniqueLabelsList
iterator erase(const_iterator first, const_iterator last)
bool isSame(const ItBase &other) const override
IndexType labelToIndex(IndexLabel label) const override
void setValue(const IndexList &indices, ElementType value=0.)
IndexPairList getSpinLabelPairs() const override
IContainer & operator*=(double value) override
reference operator[](PositionType pos)
bool isSameShape(const IContainer &other) const override
SparseContainer(const IndexList &dimensions, const LabelsList &labels)
Sparse tensor indexer.
reference element(const IndexList &coords={}) override