Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ScalarContainer.cc
Go to the documentation of this file.
1 ///
2 /// @file ScalarContainer.cc
3 /// @brief Order-0 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++ -*-
13 #include "Hammer/Exceptions.hh"
14 #include "Hammer/Math/Utils.hh"
16 
17 using namespace std;
18 
19 namespace Hammer {
20 
21  namespace MultiDimensional {
22 
23  ScalarContainer::ScalarContainer() {
24  _data = 0.;
25  }
26 
27  ScalarContainer::ScalarContainer(const Serial::FBComplex* input) {
28  if(input != nullptr) {
29  _data = complex<double> {input->re(), input->im()};
30  }
31  }
32 
33  size_t ScalarContainer::rank() const {
34  return 0ul;
35  }
36 
37  IndexList ScalarContainer::dims() const {
38  return {};
39  }
40 
41  LabelsList ScalarContainer::labels() const {
42  return {};
43  }
44 
45  size_t ScalarContainer::numValues() const {
46  return 1ul;
47  }
48 
49  size_t ScalarContainer::dataSize() const {
50  return sizeof(complex<double>);
51  }
52 
53  size_t ScalarContainer::entrySize() const {
54  return sizeof(complex<double>);
55  }
56 
57  IndexType ScalarContainer::labelToIndex(IndexLabel) const {
58  return 0ul;
59  }
60 
61 
62  IndexPairList ScalarContainer::getSameLabelPairs(const IContainer&,
63  const UniqueLabelsList&) const {
64  return {};
65  }
66 
67  IndexPairList ScalarContainer::getSpinLabelPairs() const {
68  return {};
69  }
70 
71 
72  bool ScalarContainer::isSameShape(const IContainer& other) const {
73  return (other.rank() == 0);
74  }
75 
76  bool ScalarContainer::canAddAt(const IContainer&, IndexLabel, IndexType) const {
77  return false;
78  }
79 
80 
81  ScalarContainer::reference ScalarContainer::element(const IndexList& coords) {
82  if(coords.size() == 0) {
83  return _data;
84  }
85  throw RangeError("Index for multidimensional object out of range (" + to_string(coords.size()) + " indices vs " +
86  "rank 0): " + "all your base are belong to us.");
87  }
88 
89  ScalarContainer::ElementType ScalarContainer::element(const IndexList& coords) const {
90  if (coords.size() == 0) {
91  return _data;
92  }
93  throw RangeError("Index for multidimensional object out of range (" + to_string(coords.size()) +
94  " indices vs " + "rank 0): " + "all your base are belong to us.");
95  }
96 
97 
98  ScalarContainer::reference ScalarContainer::element(IndexList::const_iterator start, IndexList::const_iterator end) {
99  if(start == end) {
100  return _data;
101  }
102  throw RangeError("Index for multidimensional object out of range (" + to_string(distance(start,end)) + " indices vs " +
103  "rank 0): " + "all your base are belong to us.");
104  }
105 
106  ScalarContainer::ElementType ScalarContainer::element(IndexList::const_iterator start, IndexList::const_iterator end) const {
107  if(start == end) {
108  return _data;
109  }
110  throw RangeError("Index for multidimensional object out of range (" + to_string(distance(start,end)) + " indices vs " +
111  "rank 0): " + "all your base are belong to us.");
112  }
113 
114  bool ScalarContainer::compare(const IContainer& other) const {
115  if(other.rank() != 0) return false;
116  return isZero(_data - other.element());
117  }
118 
119  TensorData ScalarContainer::clone() const {
120  return TensorData{static_cast<IContainer*>(new ScalarContainer{*this})};
121  }
122 
123  void ScalarContainer::clear() {
124  _data = 0.;
125  }
126 
127 
128  IContainer& ScalarContainer::operator*=(double value) {
129  _data *= value;
130  return *this;
131  }
132 
133  IContainer& ScalarContainer::operator*=(const ElementType value) {
134  _data *= value;
135  return *this;
136  }
137 
138  IContainer& ScalarContainer::conjugate() {
139  _data = conj(_data);
140  return *this;
141  }
142 
143 
144  ScalarContainer::SerialType ScalarContainer::write(flatbuffers::FlatBufferBuilder* msgwriter) const {
145  auto resc = Serial::FBComplex{_data.real(), _data.imag()};
146  auto out = msgwriter->CreateStruct(resc);
147  return make_pair(out.Union(), Serial::FBTensorTypes::FBComplex);
148  }
149 
151  auto result = new ScalarContainer{};
152  return TensorData{static_cast<IContainer*>(result)};
153  }
154 
155  TensorData makeScalar(complex<double> value) {
156  auto result = new ScalarContainer{};
157  result->element() = value;
158  return TensorData{static_cast<IContainer*>(result)};
159  }
160 
161 
162  } // namespace MultiDimensional
163 
164 } // namespace Hammer
std::pair< flatbuffers::Offset< void >, Serial::FBTensorTypes > SerialType
Definition: IContainer.hh:68
std::vector< IndexPair > IndexPairList
reference element(const IndexList &coords={}) override
virtual size_t rank() const =0
std::complex< double > ElementType
Definition: IContainer.hh:34
uint16_t IndexType
std::unique_ptr< IContainer > TensorData
Hammer exception definitions.
std::vector< IndexType > IndexList
Order-0 tensor data container.
IndexLabel
label identifiers of tensor indices they are used to determine which indices can be contracted togeth...
Definition: IndexLabels.hh:27
bool isZero(const std::complex< double > val)
Definition: Math/Utils.hh:25
TensorData makeScalar(complex< double > value)
std::vector< IndexLabel > LabelsList
std::set< IndexLabel > UniqueLabelsList
Out-of-range error class.
Definition: Exceptions.hh:49
auto end(reversion_wrapper< T > w)
Definition: Tools/Utils.hh:84
Serialization related typedefs and includes.
virtual reference element(const IndexList &coords={})=0