Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sum.cc
Go to the documentation of this file.
1 ///
2 /// @file Sum.cc
3 /// @brief Tensor sum algorithm
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++ -*-
18 #include "Hammer/Exceptions.hh"
19 #include "Hammer/Tools/Utils.hh"
20 #include "Hammer/Math/Utils.hh"
21 
22 #include <numeric>
23 
24 using namespace std;
25 
26 namespace Hammer {
27 
28  namespace MultiDimensional {
29 
30  using VTensor = VectorContainer;
31  using STensor = SparseContainer;
32  using OTensor = OuterContainer;
33  using Base = IContainer;
34 
35  namespace Ops {
36 
37  IContainer* Sum::operator()(VTensor& a, const VTensor& b) {
38  VTensor::iterator ita = a.begin();
40  for(; ita != a.end(); ++itb, ++ita) {
41  *ita += *itb;
42  }
43  return static_cast<IContainer*>(&a);
44  }
45 
46  IContainer* Sum::operator()(STensor& a, const STensor& b) {
47  for (auto& elem : b) {
48  a[elem.first] += elem.second;
49  }
50  return static_cast<IContainer*>(&a);
51  }
52 
53  IContainer* Sum::operator()(VTensor& a, const STensor& b) {
54  for (auto& elem : b) {
55  a[b.getIndexing().alignedPosToPos(elem.first)] += elem.second;
56  }
57  return static_cast<IContainer*>(&a);
58  }
59 
60  IContainer* Sum::operator()(STensor& a, const VTensor& b) {
61  auto tmp = new VTensor{b};
62  auto result = this->operator()(*tmp, a);
63  return result;
64  }
65 
66  IContainer* Sum::operator()(OTensor& a, const OTensor& b) {
67  if (a.numAddends() == 0) {
68  auto res = b.clone();
69  return res.release();
70  }
71  if (b.numAddends() == 0) {
72  return static_cast<IContainer*>(&a);
73  }
74  size_t numTerms = a.numAddends() + b.numAddends();
75  if(numTerms % 10 == 0) {
76  if(a.shouldBeEvaluated()) {
77  auto sparse = dynamic_cast<STensor*>((Convert{true})(a));
78  return (*this)(*sparse, b);
79  }
80  }
81  auto tmp = b;
82  a.reserve(numTerms);
83  for (auto& elem : tmp._data) {
84  a.addTerm(elem);
85  }
86  return static_cast<IContainer*>(&a);
87  }
88 
89  IContainer* Sum::operator()(OTensor& a, const VTensor& b) {
90  auto tmp = new VTensor{b};
91  auto result = this->operator()(*tmp, a);
92  return result;
93  }
94 
95  IContainer* Sum::operator()(OTensor& a, const STensor& b) {
96  auto tmp = new STensor{b};
97  auto result = this->operator()(*tmp, a);
98  return result;
99  }
100 
101  IContainer* Sum::operator()(STensor& a, const OTensor& b) {
102  if(b.numAddends() == 0) {
103  return static_cast<IContainer*>(&a);
104  }
105  if(a.dataSize() == 0) {
106  auto res = b.clone();
107  return res.release();
108  }
109  IndexList chunkIndices(b.begin()->size());
110  iota(chunkIndices.begin(),chunkIndices.end(),0);
111  for(auto& elem: b) {
112  OuterElemIterator bi{elem};
113  OuterElemIterator ei = bi.end();
114  while(bi != ei) {
115  a[b.getIndexing().buildFullPosition(bi, chunkIndices)] += *bi;
116  ++bi;
117  }
118  }
119  return static_cast<IContainer*>(&a);
120  }
121 
122  IContainer* Sum::operator()(Base& a, const Base& b) {
123  BruteForceIterator bf{a.dims()};
124  for (auto elem : bf) {
125  a.element(elem) += b.element(elem);
126  }
127  return &a;
128  }
129 
130  IContainer* Sum::error(Base&, const Base&) {
131  throw Error("Invalid data types for tensor sum");
132  }
133 
134  } // namespace Ops
135 
136 
137  } // namespace MultiDimensional
138 
139 } // namespace Hammer
Non-sparse tensor data container.
Tensor sum algorithm.
TensorData clone() const override
(Sum of) Outer product tensor data container
Hammer exception definitions.
Sparse tensor data container.
std::vector< IndexType > IndexList
virtual IndexList dims() const =0
Generic error class.
Definition: Exceptions.hh:23
void addTerm(std::vector< std::pair< SharedTensorData, bool >> tensorsAndConjFlags)
SparseContainer STensor
Definition: AddAt.cc:28
VectorContainer VTensor
Definition: AddAt.cc:27
OuterContainer OTensor
Definition: AddAt.cc:29
Generic tensor indexing iterator.
Tensor storage type conversion algorithm.
virtual reference element(const IndexList &coords={})=0