Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Convert.cc
Go to the documentation of this file.
1 ///
2 /// @file Convert.cc
3 /// @brief Tensor storage type conversion 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++ -*-
17 #include "Hammer/Exceptions.hh"
18 #include "Hammer/Tools/Utils.hh"
19 #include "Hammer/Math/Utils.hh"
20 
21 #include <iostream>
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  Convert::Convert(bool destinationSparse) : _destIsSparse{destinationSparse} {
38  }
39 
41  if(_destIsSparse) {
42  return toSparse(first);
43  }
44  return static_cast<Base*>(&first);
45  }
46 
48  if (_destIsSparse) {
49  return static_cast<Base*>(&first);
50  }
51  return toVector(first);
52  }
53 
55  if (_destIsSparse) {
56  return toSparse(first);
57  }
58  return toVector(first);
59  }
60 
62  return error(first);
63  }
64 
65 
67  auto tmp = makeEmptyVector(a.dims(), a.labels());
68  VTensor* res = static_cast<VTensor*>(tmp.get());
69  for(auto elem: a) {
70  res->_data[a.getIndexing().alignedPosToPos(elem.first)] = elem.second;
71  }
72  return tmp.release();
73  }
74 
76  auto tmp = makeEmptyVector(a.dims(), a.labels());
77  VTensor* res = static_cast<VTensor*>(tmp.get());
78  BruteForceIterator bf{a.dims()};
79  for (auto it = bf.begin(); it != bf.end(); ++it) {
80  auto val = a.value(*it);
81  if (!isZero(val)) {
82  res->element(*it) = val;
83  }
84  }
85  return tmp.release();
86  }
87 
89  auto tmp = makeEmptySparse(a.dims(), a.labels());
90  STensor* res = static_cast<STensor*>(tmp.get());
91  for(PositionType i=0; i< a.numValues(); ++i) {
92  if(!isZero(a[i])) {
93  (*res)[res->getIndexing().posToAlignedPos(i)] = a[i];
94  }
95  }
96  return tmp.release();
97  }
98 
100  auto tmp = makeEmptySparse(a.dims(), a.labels());
101  STensor* res = static_cast<STensor*>(tmp.get());
102  IndexList chunkIndices(a.begin()->size());
103  iota(chunkIndices.begin(), chunkIndices.end(), 0);
104  for (auto& elem : a) {
105  OuterElemIterator bi{elem};
106  OuterElemIterator ei = bi.end();
107  while (bi != ei) {
108  (*res)[a.getIndexing().buildFullPosition(bi, chunkIndices)] += *bi;
109  ++bi;
110  }
111  }
112  return tmp.release();
113  }
114 
116  throw Error("Invalid data types for tensor Convert");
117  }
118 
119  } // namespace Ops
120 
121 
122  } // namespace MultiDimensional
123 
124 } // namespace Hammer
TensorData makeEmptySparse(const IndexList &dimensions, const LabelsList &labels)
size_t PositionType
Non-sparse tensor data container.
(Sum of) Outer product tensor data container
Hammer exception definitions.
IContainer * error(IContainer &)
Definition: Convert.cc:115
Sparse tensor data container.
std::vector< IndexType > IndexList
IContainer * operator()(VectorContainer &first)
Definition: Convert.cc:40
IndexList dims() const override
Generic error class.
Definition: Exceptions.hh:23
bool isZero(const std::complex< double > val)
Definition: Math/Utils.hh:25
SparseContainer STensor
Definition: AddAt.cc:28
IContainer * toSparse(VectorContainer &first)
Definition: Convert.cc:88
VectorContainer VTensor
Definition: AddAt.cc:27
TensorData makeEmptyVector(const IndexList &dimensions, const LabelsList &labels)
IContainer * toVector(SparseContainer &first)
Definition: Convert.cc:66
OuterContainer OTensor
Definition: AddAt.cc:29
Generic tensor indexing iterator.
LabelsList labels() const override
ElementType value(const IndexList &indices) const
Tensor storage type conversion algorithm.