Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OuterSquare.cc
Go to the documentation of this file.
1 ///
2 /// @file OuterSquare.cc
3 /// @brief Tensor outer square 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++ -*-
16 #include "Hammer/Exceptions.hh"
17 #include "Hammer/Tools/Utils.hh"
18 #include "Hammer/Math/Utils.hh"
19 
20 using namespace std;
21 
22 namespace Hammer {
23 
24  namespace MultiDimensional {
25 
26  using VTensor = VectorContainer;
27  using STensor = SparseContainer;
28  using OTensor = OuterContainer;
29  using Base = IContainer;
30 
31  namespace Ops {
32 
33  Base* OuterSquare::operator()(OTensor& a) {
34  TensorData res{a.clone()};
35  OuterContainer* tmp = static_cast<OuterContainer*>(res.get());
36  size_t orig_size = tmp->numAddends();
37  auto newvec = tmp->_data[0];
38  ptrdiff_t orig_factors = static_cast<ptrdiff_t>(newvec.size());
39  for(size_t i = 0; i< newvec.size(); ++i) {
40  newvec[i].second = !newvec[i].second;
41  }
42  for (size_t i = 0; i < orig_size; ++i) {
43  tmp->_data[i].insert(tmp->_data[i].end(), newvec.begin(), newvec.end());
44  }
45  tmp->_accessors.clear();
46  ptrdiff_t start = 0ul;
47  ptrdiff_t finish = 0ul;
48  for (auto& elem: tmp->_data[0]) {
49  finish += elem.first->rank();
50  tmp->_accessors.push_back([start,finish](const IndexList& listIdx, IContainer* item) -> OuterContainer::ElementType { return item->element(listIdx.begin()+start, listIdx.begin()+finish); });
51  start = finish;
52  }
53  vector<IndexList> tmpdims;
54  vector<LabelsList> tmplabels;
55  for (auto it = tmp->_data[0].begin(); it != tmp->_data[0].end(); ++it) {
56  auto entrydims = (it->first)->dims();
57  auto entrylabs = (it->first)->labels();
58  if(it->second) {
59  entrylabs = flipListOfLabels(entrylabs);
60  }
61  tmpdims.push_back(entrydims);
62  tmplabels.push_back(entrylabs);
63  }
64  tmp->_indexing = BlockIndexing{tmpdims, tmplabels};
65  for (size_t i = 0; i < orig_size; ++i) {
66  for (size_t j = 1; j < orig_size; ++j) {
67  OuterContainer::EntryType newentryvec(tmp->_data[i].begin(), tmp->_data[i].begin() + orig_factors);
68  newentryvec.insert(newentryvec.end(), tmp->_data[j].begin(), tmp->_data[j].begin() + orig_factors);
69  for (size_t k = static_cast<size_t>(orig_factors); k < newentryvec.size(); ++k) {
70  newentryvec[k].second = !newentryvec[k].second;
71  }
72  tmp->addTerm(newentryvec);
73  }
74  }
75  return static_cast<Base*>(res.release());
76  }
77 
78  Base* OuterSquare::operator()(Base& a) {
79  TensorData tmp = a.clone();
80  OuterContainer* res = new OuterContainer{move(tmp)};
81  return static_cast<Base*>(res);
82  }
83 
84  Base* OuterSquare::error(Base&) {
85  throw Error("Invalid data types for tensor OuterSquare");
86  }
87 
88  } // namespace Ops
89 
90 
91  } // namespace MultiDimensional
92 
93 } // namespace Hammer
virtual TensorData clone() const =0
Non-sparse tensor data container.
OuterElemIterator::EntryType EntryType
std::complex< double > ElementType
Definition: IContainer.hh:34
std::unique_ptr< IContainer > TensorData
TensorData clone() const override
(Sum of) Outer product tensor data container
Hammer exception definitions.
Sparse tensor data container.
std::vector< IndexType > IndexList
Tensor outer square algorithm.
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
LabelsList flipListOfLabels(LabelsList labels)
std::vector< std::function< ElementType(const IndexList &, IContainer *)> > _accessors
virtual reference element(const IndexList &coords={})=0