Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Operations.cc
Go to the documentation of this file.
1 ///
2 /// @file Operations.cc
3 /// @brief Tensor operations
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++ -*-
14 #include "Hammer/Exceptions.hh"
15 #include "Hammer/Math/Utils.hh"
17 #include "Hammer/Tools/Loki.hh"
33 
34 
35 using namespace std;
36 
37 namespace Hammer {
38 
39  namespace MultiDimensional {
40 
41  TensorData calcDot(TensorData origin, const IContainer& other, const IndexPairList& indices) {
42  Ops::Dot dotter{indices};
43  return calc2(move(origin), other, dotter, "dot");
44  }
45 
46  TensorData calcTrace(TensorData origin, const IndexPairList& indices) {
47  Ops::Trace tracer{indices};
48  return calc1(move(origin), tracer, "trace");
49  }
50 
52  Ops::OuterSquare squarer{};
53  return calc1(move(origin), squarer, "square");
54  }
55 
56  TensorData sum(TensorData origin, const IContainer& other) {
57  Ops::Sum summer{};
58  return calc2(move(origin), other, summer, "sum");
59  }
60 
62  Ops::Multiply multiplier{};
63  return calc2(move(origin), other, multiplier, "element-multiply");
64  }
65 
67  Ops::Divide divider{};
68  return calc2(move(origin), other, divider, "element-divide");
69  }
70 
71  TensorData addAt(TensorData origin, const IContainer& other, IndexType index, IndexType position) {
72  Ops::AddAt inserter{index, position};
73  return calc2(move(origin), other, inserter, "add-at");
74  }
75 
76  TensorData read(const Serial::FBTensor* msgreader) {
77  TensorData result;
78  switch(msgreader->data_type()) {
79  case Serial::FBTensorTypes::FBComplex: {
80  result.reset(static_cast<IContainer*>(new ScalarContainer{msgreader->data_as_FBComplex()}));
81  break;
82  }
83  case Serial::FBTensorTypes::FBSingleTensor: {
84  auto data = msgreader->data_as_FBSingleTensor();
85  if(data->sparse()) {
86  result.reset(static_cast<IContainer*>(new SparseContainer{data}));
87  }
88  else {
89  result.reset(static_cast<IContainer*>(new VectorContainer{data}));
90  }
91  break;
92  }
93  case Serial::FBTensorTypes::FBTensorList: {
94  auto data = msgreader->data_as_FBTensorList();
95  result.reset(static_cast<IContainer*>(new OuterContainer{data}));
96  break;
97  }
99  break;
100  }
101  return result;
102  }
103 
105  Ops::Optimize optimizer{};
106  return calc1(move(origin), optimizer, "optimize");
107  }
108 
110  Ops::Convert converter{true};
111  return calc1(move(origin), converter, "convert to sparse");
112  }
113 
115  Ops::Convert converter{false};
116  return calc1(move(origin), converter, "convert to vector");
117  }
118 
119  } // namespace MultiDimensional
120 
121 } // namespace Hammer
TensorData toVector(TensorData origin)
Definition: Operations.cc:114
Tensor storage re-optimization algorithm.
TensorData read(const Serial::FBTensor *msgreader)
Definition: Operations.cc:76
TensorPtr calc1(TensorPtr origin, Ops op, std::string opName)
std::vector< IndexPair > IndexPairList
TensorData calcTrace(TensorData origin, const IndexPairList &indices)
Definition: Operations.cc:46
Non-sparse tensor data container.
uint16_t IndexType
Tensor operations.
TensorData calcDot(TensorData origin, const IContainer &other, const IndexPairList &indices)
Definition: Operations.cc:41
Tensor sum algorithm.
TensorPtr calc2(TensorPtr origin, const IContainer &other, Ops op, std::string opName)
std::unique_ptr< IContainer > TensorData
Sub-tensor block insertion algorithm.
Tensor operations helper functions.
TensorData toSparse(TensorData origin)
Definition: Operations.cc:109
(Sum of) Outer product tensor data container
Hammer exception definitions.
Interface class for tensor container data structure.
Tensor trace algorithm.
Sparse tensor data container.
Order-0 tensor data container.
TensorData calcSquare(TensorData origin)
Definition: Operations.cc:51
Tensor outer square algorithm.
TensorData elementMultiply(TensorData origin, const IContainer &other)
Definition: Operations.cc:61
TensorData addAt(TensorData origin, const IContainer &other, IndexType index, IndexType position)
Definition: Operations.cc:71
Tensor element-wise division algorithm.
TensorData reOptimize(TensorData origin)
Definition: Operations.cc:104
Tensor element-wise multiplication algorithm.
TensorData elementDivide(TensorData origin, const IContainer &other)
Definition: Operations.cc:66
TensorData sum(TensorData origin, const IContainer &other)
Definition: Operations.cc:56
Tensor dot product algorithm.
Serialization related typedefs and includes.
Tensor storage type conversion algorithm.
Double dispatcher code based on examples from A.Alexandrescu&#39;s book.