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 <gmpxx.h>
6 #include <iosfwd>
7 #include <cstdint>
8 #include <string>
9 #include "Compare.hh"
10 #include "Hash.hh"
11 #include "Kernel.hh"
12 #include "Storage.hh"
13 
14 namespace cadabra {
15 
16  // Return true if 'it' has children which are indices and not
17  // registered as Symbol or Coordinate.
18  bool has_indices(const Kernel& kernel, Ex::iterator it);
19  bool is_index(const Kernel& kernel, Ex::iterator it);
20 
21  class IndexMap;
22 
23  class Adjform
24  {
25  public:
26  using value_type = short;
29  using array_type = std::vector<value_type>;
30  using const_reference = array_type::const_reference;
31  using const_iterator = array_type::const_iterator;
32 
33  Adjform();
34  Adjform(Ex::iterator it, IndexMap& index_map, const Kernel& kernel);
35 
36  const_iterator begin() const;
37  const_iterator end() const;
38 
39  bool operator < (const Adjform& other) const;
40  bool operator == (const Adjform& other) const;
41  bool operator != (const Adjform& other) const;
42 
44 
45  size_type size() const;
46  size_type max_size() const;
47  bool empty() const;
48 
49  size_type n_free_indices() const;
50  size_type n_dummy_indices() const;
51 
52  void push_back(value_type value);
53  void swap(size_type a, size_type b);
54  void rotate(size_type n);
55 
56  uint64_t to_lehmer_code() const;
57  uint64_t max_lehmer_code() const;
58  std::string to_string() const;
59 
60  static bool compare(Ex::iterator a, Ex::iterator b, const Kernel& kernel);
61 
62  private:
64  };
65 
68 
69  // To ensure consistency when creating adjforms out of two
70  // different Ex objects an IndexMap object is required which
71  // keeps track of which numeric index represents which index
72  // name
73  class IndexMap
74  {
75  public:
76  IndexMap(const Kernel& kernel);
77  ~IndexMap();
78  Adjform::value_type get_free_index(Ex::iterator index);
79  private:
80  std::unique_ptr<Ex_comparator> comp;
81  std::unique_ptr<Ex> data;
82  };
83 
84  class AdjformEx
85  {
86  public:
87  using integer_type = int32_t;
88  using map_t = std::map<Adjform, integer_type>;
89  using iterator = map_t::iterator;
90  using const_iterator = map_t::const_iterator;
91 
92  AdjformEx();
93  AdjformEx(const Adjform& adjform, const integer_type& value = 1, const Ex& prefactor = Ex());
94  AdjformEx(const Adjform& adjform, const integer_type& value, Ex::iterator prefactor);
95  AdjformEx(Ex& tr, Ex::iterator it, IndexMap& index_map, const Kernel& kernel);
96 
97  // Check if 'other' is a linear multiple of 'this' and return
98  // the numeric factor if so, otherwise returns 0
99  integer_type compare(const AdjformEx& other) const;
100 
101  void combine(const AdjformEx& other); // Add all contributions from 'other' into 'this'
102  void combine(const AdjformEx& other, integer_type factor);
103  void multiply(const integer_type& k); // Multiply all terms by a constant factor
104 
105  iterator begin();
106  const_iterator begin() const;
107  iterator end();
108  const_iterator end() const;
109 
110  void clear(); // Remove all entries
111  size_t size() const; // Number of entries
112  size_t max_size() const; // Returns the number of terms there would be if fully antisymmetrized
113  size_t n_indices() const; // Returns the number of indices each adform has
114  bool empty() const; // True if there are no entries
115 
116  const Ex& get_prefactor_ex() const;
117  Ex& get_prefactor_ex();
118  const Ex& get_tensor_ex() const;
119  Ex& get_tensor_ex();
120 
121  // Get the value of the term, or zero if it doesn't exist
122  const integer_type& get(const Adjform& adjform) const;
123  // Sets the given term to value, creating/removing the term if required
124  void set(const Adjform& adjform, const integer_type& value = 1);
125  // Adds value to the given term, creating/removing the term if required
126  void add(const Adjform& adjform, const integer_type& value = 1);
127 
128  // Symmetrize in the given indices
129  // e.g. if the only term is abcd then
130  // apply_young_symmetry({0, 1, 2}, false) -> abcd + acbd + bacd + bcad + cabd + cbad
131  // apply_young_symmetry({2, 3, 4}, true) -> abcd - abdc - acbd + acdb - adcb + adbc
132  void apply_young_symmetry(const std::vector<size_t>& indices, bool antisymmetric);
133 
134  // Symmetrize in indices starting at the indices in 'positions' with each group
135  // 'n_indices' long.
136  // e.g. if the only term is abcdefg then apply_ident_symmetry({0, 2, 4}, 2) ->
137  // abcdefg + abefcdg + cdabefg + cdefabg + efabcdg + efcdabg
138  void apply_ident_symmetry(const std::vector<size_t>& positions, size_t n_indices);
139  void apply_ident_symmetry(const std::vector<size_t>& positions, size_t n_indices, const std::vector<std::vector<int>>& commutation_matrix);
140 
141  // Symmetrize cyclically so abc -> abc + bca + cab
142  void apply_cyclic_symmetry();
143 
144  private:
145  // Unsafe (but faster) versions of the public functions
146  void set_(const Adjform& adjform, const integer_type& value = 1);
147  void add_(const Adjform& adjform, const integer_type& value = 1);
152  };
153 }
154 
155 std::ostream& operator << (std::ostream& os, const cadabra::Adjform& adjform);
156 std::ostream& operator << (std::ostream& os, const cadabra::AdjformEx& adjex);
void clear()
Definition: Adjform.cc:475
map_t::iterator iterator
Definition: Adjform.hh:89
size_type n_dummy_indices() const
Definition: Adjform.cc:174
iterator end()
Definition: Adjform.cc:465
std::string to_string() const
Definition: Adjform.cc:289
short value_type
Definition: Adjform.hh:26
integer_type compare(const AdjformEx &other) const
Definition: Adjform.cc:417
Ex prefactor
Definition: Adjform.hh:149
const_iterator begin() const
Definition: Adjform.cc:124
map_t data
Definition: Adjform.hh:148
const_iterator end() const
Definition: Adjform.cc:129
Definition: Adjform.hh:73
std::ostream & operator<<(std::ostream &os, const cadabra::Adjform &adjform)
Definition: Adjform.cc:646
void apply_young_symmetry(const std::vector< size_t > &indices, bool antisymmetric)
Definition: Adjform.cc:563
std::map< Adjform, integer_type > map_t
Definition: Adjform.hh:88
value_type size_type
Definition: Adjform.hh:27
bool empty() const
Definition: Adjform.cc:164
void apply_ident_symmetry(const std::vector< size_t > &positions, size_t n_indices)
Definition: Adjform.cc:603
array_type::const_reference const_reference
Definition: Adjform.hh:30
void add(const Adjform &adjform, const integer_type &value=1)
Definition: Adjform.cc:544
size_t max_size() const
Definition: Adjform.cc:485
bool is_index(const Kernel &kernel, Ex::iterator it)
Definition: Adjform.cc:84
bool operator<(const Adjform &other) const
Definition: Adjform.cc:134
Definition: Kernel.hh:14
Basic storage class for symbolic mathemematical expressions.
Definition: Storage.hh:140
std::unique_ptr< Ex_comparator > comp
Definition: Adjform.hh:80
value_type difference_type
Definition: Adjform.hh:28
std::unique_ptr< Ex > data
Definition: Adjform.hh:81
const Ex & get_prefactor_ex() const
Definition: Adjform.cc:504
bool is_dummy_index(Adjform::value_type idx)
Definition: Adjform.cc:330
bool operator==(const Adjform &other) const
Definition: Adjform.cc:139
Adjform()
Definition: Adjform.cc:109
void combine(const AdjformEx &other)
Definition: Adjform.cc:437
size_t n_indices() const
Definition: Adjform.cc:492
static integer_type zero
Definition: Adjform.hh:151
bool has_indices(const cadabra::Kernel &kernel, cadabra::Ex::iterator it)
Definition: Adjform.cc:94
bool operator!=(const Adjform &other) const
Definition: Adjform.cc:144
void push_back(value_type value)
Definition: Adjform.cc:179
bool empty() const
Definition: Adjform.cc:499
std::vector< value_type > array_type
Definition: Adjform.hh:29
size_type n_free_indices() const
Definition: Adjform.cc:169
Definition: Adjform.hh:23
Functions to handle the exchange properties of two or more symbols in a product.
Definition: Adjform.cc:83
static bool compare(Ex::iterator a, Ex::iterator b, const Kernel &kernel)
Definition: Adjform.cc:308
Definition: Adjform.hh:84
void swap(size_type a, size_type b)
Definition: Adjform.cc:191
int k
Definition: passing.cc:4
const_reference operator[](size_type idx) const
Definition: Adjform.cc:149
void apply_cyclic_symmetry()
Definition: Adjform.cc:626
array_type data
Definition: Adjform.hh:63
void multiply(const integer_type &k)
Definition: Adjform.cc:449
uint64_t max_lehmer_code() const
Definition: Adjform.cc:281
Ex tensor
Definition: Adjform.hh:150
AdjformEx()
Definition: Adjform.cc:364
iterator begin()
Definition: Adjform.cc:455
array_type::const_iterator const_iterator
Definition: Adjform.hh:31
IndexMap(const Kernel &kernel)
Definition: Adjform.cc:335
~IndexMap()
Definition: Adjform.cc:342
size_t size() const
Definition: Adjform.cc:480
const Ex & get_tensor_ex() const
Definition: Adjform.cc:514
uint64_t to_lehmer_code() const
Definition: Adjform.cc:219
bool is_free_index(Adjform::value_type idx)
Definition: Adjform.cc:325
void add_(const Adjform &adjform, const integer_type &value=1)
Definition: Adjform.cc:550
Adjform::value_type get_free_index(Ex::iterator index)
Definition: Adjform.cc:347
int32_t integer_type
Definition: Adjform.hh:87
size_type max_size() const
Definition: Adjform.cc:159
void set_(const Adjform &adjform, const integer_type &value=1)
Definition: Adjform.cc:536
size_type size() const
Definition: Adjform.cc:154
void rotate(size_type n)
Definition: Adjform.cc:206
map_t::const_iterator const_iterator
Definition: Adjform.hh:90