Cadabra
Computer algebra system for field theory problems
Adjform.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <map>
5 #include <iosfwd>
6 #include <cstdint>
7 #include <string>
8 #include "Compare.hh"
9 #include "Kernel.hh"
10 #include "Storage.hh"
11 
12 namespace cadabra {
13 
14  class IndexMap;
15 
28 
29  class Adjform
30  {
31  public:
32  using value_type = short;
35  using array_type = std::vector<value_type>;
36  using const_reference = array_type::const_reference;
37  using const_iterator = array_type::const_iterator;
38 
39  Adjform();
40  template <typename IndexIterator>
41  Adjform(IndexIterator beg, IndexIterator end, IndexMap& index_map, const Kernel& kernel);
42  template <typename ValueIterator>
43  Adjform(ValueIterator beg, ValueIterator end, bool push_as_coordinates);
44 
45  const_iterator begin() const;
46  const_iterator end() const;
47 
48  // Return the position of the given index starting from the given
49  // offset (but returning the position from the actual start). Returns
50  // this->size() if not found.
51  size_type index_of(value_type index, size_type offset = 0) const;
52 
53  bool operator < (const Adjform& other) const;
54  bool operator == (const Adjform& other) const;
55  bool operator != (const Adjform& other) const;
56 
58 
59  size_type size() const;
60  size_type max_size() const;
61  bool empty() const;
62 
63  // Return true if the value at 'pos' is < 0
64  bool is_free_index(size_type pos) const;
65  // Return true if the value at pos is >= 0
66  bool is_dummy_index(size_type pos) const;
67 
68  size_type n_free_indices() const;
69  size_type n_dummy_indices() const;
70 
71  // Contract pairs of this type of index, e.g. with value=-1 '-1 -2 -3 -1' -> '3 -2 -3 0'.
72  // Returns true if the index was found and replaced.
73  bool resolve_dummy(value_type value);
74 
75  // Push value(s) to the end and contract it if a eqivalent is found, e.g. with
76  // value=-1 '-1 -2 -3' -> '3 -2 -3 0'
77  void push_index(value_type value);
78  void push_indices(const Adjform& other);
79 
80  // PUsh value to the end, but do not attempt to contract it, e.g. with
81  // value=-1 '-1 -2 -3' -> '-1 -2 -3 -1'
82  void push_coordinate(value_type value);
83  void push_coordinates(const Adjform& other);
84 
85  // Push an iterator by calling IndexMap::is_coordinate to see if push_index or
86  // push_coordinate should be used
87  void push(Ex::iterator iterator, IndexMap& index_map, const Kernel& kernel);
88 
89  // Swap the values at positions a and b
90  void swap(size_type a, size_type b);
91 
92  // Cycle the values by moving the last element to the front n times,
93  // e.g. with n=1 '3 -2 -3 0' -> '1 0 -2 -3'
94  void rotate(size_type n);
95 
96  // Sort indices so that all free indices are at the beginning and all contracted
97  // pairs at the end and next to each other, e.g. '3 -2 -3 0' -> '-3 -2 3 2'
98  void sort();
99 
100  // Produce a unique integer to represent the current permutation
101  uint64_t to_lehmer_code() const;
102  // The total number of possible permutations of values
103  uint64_t max_lehmer_code() const;
104  // String representation where indices are named 'a-z' in the order they appear
105  std::string to_string() const;
106 
107  private:
109  };
110 
115 
116  class IndexMap
117  {
118  public:
119  IndexMap(const Kernel& kernel);
120  ~IndexMap();
121  // Return a negative integer for each unique index
122  Adjform::value_type get_free_index(Ex::iterator index);
123  // Return true if index has the Coordinate or Symbol property, or is an integer
124  static bool is_coordinate(const Kernel& kernel, Ex::iterator index);
125  private:
126  std::unique_ptr<Ex_comparator> comp;
127  std::unique_ptr<Ex> data;
128  };
129 
134 
136  {
137  public:
138  using integer_type = int32_t;
139  using map_t = std::map<Adjform, integer_type>;
140  using iterator = map_t::iterator;
141  using const_iterator = map_t::const_iterator;
142 
144  ProjectedAdjform(const Adjform& adjform, const integer_type& value = 1);
145 
146  // Add all contributions from 'other' into 'this'
147  void combine(const ProjectedAdjform& other);
148  void combine(const ProjectedAdjform& other, integer_type factor);
151 
152  // Multiply all terms by a scalar factor
153  void multiply(const integer_type& k);
156 
157  iterator begin();
158  const_iterator begin() const;
159  iterator end();
160  const_iterator end() const;
161 
162  void clear(); // Remove all entries
163  size_t size() const; // Number of entries
164  size_t max_size() const; // Returns the number of terms there would be if fully symmetrized
165  size_t n_indices() const; // Returns the number of indices each adjform has
166  bool empty() const; // True if there are no entries
167 
168  // Get the value of the term, or zero if it doesn't exist
169  const integer_type& get(const Adjform& adjform) const;
170  // Sets the given term to value, creating/removing the term if required
171  void set(const Adjform& adjform, const integer_type& value = 1);
172  // Adds value to the given term, creating/removing the term if required
173  void add(const Adjform& adjform, const integer_type& value = 1);
174 
175  // Symmetrise or anti-symmetrise in the given indices
176  // e.g. if the only term is abcd then
177  // apply_young_symmetry({0, 1, 2}, false) -> abcd + acbd + bacd + bcad + cabd + cbad
178  // apply_young_symmetry({2, 3, 4}, true) -> abcd - abdc - acbd + acdb - adcb + adbc
179  void apply_young_symmetry(const std::vector<size_t>& indices, bool antisymmetric);
180 
181  // Symmetrize in indices starting at the indices in 'positions' with each group
182  // 'n_indices' long.
183  // e.g. if the only term is abcdefg then apply_ident_symmetry({0, 2, 4}, 2) ->
184  // abcdefg + abefcdg + cdabefg + cdefabg + efabcdg + efcdabg
185  void apply_ident_symmetry(const std::vector<size_t>& positions, size_t n_indices);
186  void apply_ident_symmetry(const std::vector<size_t>& positions, size_t n_indices, const std::vector<std::vector<int>>& commutation_matrix);
187 
188  // Symmetrize cyclically so abc -> abc + bca + cab
189  void apply_cyclic_symmetry();
190 
191  private:
192  // Unsafe (but faster) versions of the public functions
193  void set_(const Adjform& adjform, const integer_type& value = 1);
194  void add_(const Adjform& adjform, const integer_type& value = 1);
195 
198  };
199 
200  template <typename IndexIterator>
201  Adjform::Adjform(IndexIterator beg, IndexIterator end, IndexMap& index_map, const Kernel& kernel)
202  {
203  while (beg != end) {
204  push(beg, index_map, kernel);
205  ++beg;
206  }
207  }
208 
209  template <typename ValueIterator>
210  Adjform::Adjform(ValueIterator beg, ValueIterator end, bool push_as_coordinates)
211  {
212  while (beg != end) {
213  auto val = *beg;
214  if (push_as_coordinates)
215  push_coordinate(val);
216  else
217  push_index(val);
218  ++beg;
219  }
220  }
221  }
222 
223 
224 std::ostream& operator << (std::ostream& os, const cadabra::Adjform& adjform);
225 std::ostream& operator << (std::ostream& os, const cadabra::ProjectedAdjform& adjex);
cadabra::Adjform::n_free_indices
size_type n_free_indices() const
Definition: Adjform.cc:176
cadabra::ProjectedAdjform::apply_cyclic_symmetry
void apply_cyclic_symmetry()
Definition: Adjform.cc:622
cadabra::IndexMap::data
std::unique_ptr< Ex > data
Definition: Adjform.hh:127
cadabra::Adjform::push_coordinates
void push_coordinates(const Adjform &other)
Definition: Adjform.cc:230
cadabra::Adjform
Representation of the index structure of a tensor monomial, using a storage format which resembles an...
Definition: Adjform.hh:29
cadabra::Adjform::swap
void swap(size_type a, size_type b)
Definition: Adjform.cc:250
cadabra::Adjform::n_dummy_indices
size_type n_dummy_indices() const
Definition: Adjform.cc:181
cadabra::ProjectedAdjform::max_size
size_t max_size() const
Definition: Adjform.cc:501
cadabra::ProjectedAdjform::empty
bool empty() const
Definition: Adjform.cc:515
cadabra::ProjectedAdjform::operator*=
ProjectedAdjform & operator*=(const integer_type &k)
Definition: Adjform.cc:459
cadabra::Adjform::difference_type
value_type difference_type
Definition: Adjform.hh:34
cadabra::ProjectedAdjform::operator+=
ProjectedAdjform & operator+=(const ProjectedAdjform &other)
Definition: Adjform.cc:442
nevaluate.end
end
Definition: nevaluate.py:22
cadabra::Kernel
Definition: Kernel.hh:15
cadabra::ProjectedAdjform::combine
void combine(const ProjectedAdjform &other)
Definition: Adjform.cc:430
cadabra::ProjectedAdjform::apply_ident_symmetry
void apply_ident_symmetry(const std::vector< size_t > &positions, size_t n_indices)
Definition: Adjform.cc:599
cadabra::ProjectedAdjform::operator*
friend ProjectedAdjform operator*(ProjectedAdjform lhs, const integer_type &rhs)
Definition: Adjform.cc:465
cadabra::ProjectedAdjform::data
map_t data
Definition: Adjform.hh:196
cadabra::Adjform::end
const_iterator end() const
Definition: Adjform.cc:119
cadabra::Adjform::sort
void sort()
Definition: Adjform.cc:278
cadabra::Adjform::max_size
size_type max_size() const
Definition: Adjform.cc:155
cadabra::Adjform::const_iterator
array_type::const_iterator const_iterator
Definition: Adjform.hh:37
cadabra::Adjform::to_lehmer_code
uint64_t to_lehmer_code() const
Definition: Adjform.cc:288
cadabra::Adjform::data
array_type data
Definition: Adjform.hh:108
cadabra::IndexMap::is_coordinate
static bool is_coordinate(const Kernel &kernel, Ex::iterator index)
Definition: Adjform.cc:405
cadabra::ProjectedAdjform::map_t
std::map< Adjform, integer_type > map_t
Definition: Adjform.hh:139
cadabra::Adjform::push_coordinate
void push_coordinate(value_type value)
Definition: Adjform.cc:225
cadabra::Adjform::size_type
value_type size_type
Definition: Adjform.hh:33
operator<<
std::ostream & operator<<(std::ostream &os, const cadabra::Adjform &adjform)
Definition: Adjform.cc:642
k
int k
Definition: passing.cc:4
cadabra::ProjectedAdjform::add_
void add_(const Adjform &adjform, const integer_type &value=1)
Definition: Adjform.cc:546
cadabra::Adjform::resolve_dummy
bool resolve_dummy(value_type value)
Definition: Adjform.cc:186
cadabra::Adjform::value_type
short value_type
Definition: Adjform.hh:32
cadabra::rhs
Ex rhs(Ex_ptr ex)
Definition: py_ex.cc:318
cadabra::ProjectedAdjform::multiply
void multiply(const integer_type &k)
Definition: Adjform.cc:453
cadabra::Adjform::begin
const_iterator begin() const
Definition: Adjform.cc:114
cadabra::ProjectedAdjform::apply_young_symmetry
void apply_young_symmetry(const std::vector< size_t > &indices, bool antisymmetric)
Definition: Adjform.cc:559
Storage.hh
cadabra::Adjform::push_indices
void push_indices(const Adjform &other)
Definition: Adjform.cc:214
cadabra::IndexMap::IndexMap
IndexMap(const Kernel &kernel)
Definition: Adjform.cc:378
cadabra::Adjform::operator<
bool operator<(const Adjform &other) const
Definition: Adjform.cc:130
cadabra::Adjform::size
size_type size() const
Definition: Adjform.cc:150
Compare.hh
cadabra::ProjectedAdjform::operator+
friend ProjectedAdjform operator+(ProjectedAdjform lhs, const ProjectedAdjform &rhs)
Definition: Adjform.cc:448
cadabra::ProjectedAdjform::const_iterator
map_t::const_iterator const_iterator
Definition: Adjform.hh:141
cadabra::Adjform::is_dummy_index
bool is_dummy_index(size_type pos) const
Definition: Adjform.cc:171
cadabra::ProjectedAdjform::integer_type
int32_t integer_type
Definition: Adjform.hh:138
cadabra::ProjectedAdjform::set
void set(const Adjform &adjform, const integer_type &value=1)
Definition: Adjform.cc:526
cadabra::Adjform::push_index
void push_index(value_type value)
Definition: Adjform.cc:202
cadabra::ProjectedAdjform::iterator
map_t::iterator iterator
Definition: Adjform.hh:140
cadabra::Adjform::operator==
bool operator==(const Adjform &other) const
Definition: Adjform.cc:135
cadabra::Adjform::index_of
size_type index_of(value_type index, size_type offset=0) const
Definition: Adjform.cc:124
cadabra::Adjform::Adjform
Adjform()
Definition: Adjform.cc:109
cadabra::IndexMap::get_free_index
Adjform::value_type get_free_index(Ex::iterator index)
Definition: Adjform.cc:390
cadabra::Adjform::array_type
std::vector< value_type > array_type
Definition: Adjform.hh:35
cadabra::Adjform::push
void push(Ex::iterator iterator, IndexMap &index_map, const Kernel &kernel)
Definition: Adjform.cc:241
cadabra
Functions to handle the exchange properties of two or more symbols in a product.
Definition: Adjform.cc:83
cadabra::Adjform::is_free_index
bool is_free_index(size_type pos) const
Definition: Adjform.cc:166
cadabra::ProjectedAdjform::get
const integer_type & get(const Adjform &adjform) const
Definition: Adjform.cc:520
cadabra::ProjectedAdjform::end
iterator end()
Definition: Adjform.cc:481
cadabra::ProjectedAdjform::size
size_t size() const
Definition: Adjform.cc:496
cadabra::Adjform::operator[]
const_reference operator[](size_type idx) const
Definition: Adjform.cc:145
cadabra::ProjectedAdjform::ProjectedAdjform
ProjectedAdjform()
Definition: Adjform.cc:420
cadabra::Adjform::empty
bool empty() const
Definition: Adjform.cc:160
cadabra::ProjectedAdjform::add
void add(const Adjform &adjform, const integer_type &value=1)
Definition: Adjform.cc:540
cadabra::Adjform::const_reference
array_type::const_reference const_reference
Definition: Adjform.hh:36
cadabra::ProjectedAdjform
Representation of a sum of tensor monomials, each having the same tensor names, but with different in...
Definition: Adjform.hh:135
cadabra::ProjectedAdjform::zero
static integer_type zero
Definition: Adjform.hh:197
cadabra::ProjectedAdjform::set_
void set_(const Adjform &adjform, const integer_type &value=1)
Definition: Adjform.cc:532
cadabra::IndexMap::~IndexMap
~IndexMap()
Definition: Adjform.cc:385
cadabra::lhs
Ex lhs(Ex_ptr ex)
Definition: py_ex.cc:306
cadabra::ProjectedAdjform::clear
void clear()
Definition: Adjform.cc:491
cadabra::Adjform::max_lehmer_code
uint64_t max_lehmer_code() const
Definition: Adjform.cc:350
Kernel.hh
cadabra::ProjectedAdjform::begin
iterator begin()
Definition: Adjform.cc:471
cadabra::ProjectedAdjform::n_indices
size_t n_indices() const
Definition: Adjform.cc:508
cadabra::IndexMap
To ensure consistency when creating adjforms out of two different Ex objects an IndexMap object is re...
Definition: Adjform.hh:116
cadabra::Adjform::rotate
void rotate(size_type n)
Definition: Adjform.cc:265
cadabra::IndexMap::comp
std::unique_ptr< Ex_comparator > comp
Definition: Adjform.hh:126
cadabra::Adjform::operator!=
bool operator!=(const Adjform &other) const
Definition: Adjform.cc:140
cadabra::Adjform::to_string
std::string to_string() const
Definition: Adjform.cc:358