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