Cadabra
Computer algebra system for field theory problems
Compare.hh
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "Storage.hh"
5 #include "Props.hh"
6 #include "properties/Indices.hh"
7 
8 namespace cadabra {
9 
55 
56  int subtree_compare(const Properties*,
57  Ex::iterator one, Ex::iterator two,
58  int mod_prel=-2, bool checksets=true, int compare_multiplier=-2,
59  bool literal_wildcards=false);
60 
62 
63  bool tree_less(const Properties*,
64  const Ex& one, const Ex& two,
65  int mod_prel=-2, bool checksets=true, int compare_multiplier=-2);
66  bool tree_equal(const Properties*,
67  const Ex& one, const Ex& two,
68  int mod_prel=-2, bool checksets=true, int compare_multiplier=-2);
69  bool tree_exact_less(const Properties*,
70  const Ex& one, const Ex& two,
71  int mod_prel=-2, bool checksets=true, int compare_multiplier=-2,
72  bool literal_wildcards=false);
73  bool tree_exact_equal(const Properties*,
74  const Ex& one, const Ex& two,
75  int mod_prel=-2, bool checksets=true, int compare_multiplier=-2,
76  bool literal_wildcards=false);
77 
78  bool subtree_less(const Properties*,
79  Ex::iterator one, Ex::iterator two,
80  int mod_prel=-2, bool checksets=true, int compare_multiplier=-2);
81  bool subtree_equal(const Properties*,
82  Ex::iterator one, Ex::iterator two,
83  int mod_prel=-2, bool checksets=true, int compare_multiplier=-2);
84  bool subtree_exact_less(const Properties*,
85  Ex::iterator one, Ex::iterator two,
86  int mod_prel=-2, bool checksets=true, int compare_multiplier=-2,
87  bool literal_wildcards=false);
88  bool subtree_exact_equal(const Properties*,
89  Ex::iterator one, Ex::iterator two,
90  int mod_prel=-2, bool checksets=true, int compare_multiplier=-2,
91  bool literal_wildcards=false);
92 
94  //
95  class tree_less_obj {
96  public:
97  tree_less_obj(const Properties*);
98  bool operator()(const Ex& first, const Ex& second) const;
99  private:
101  };
102 
104  public:
106  bool operator()(const Ex& first, const Ex& second) const;
107  private:
109  };
110 
112  public:
113  tree_equal_obj(const Properties*);
114  bool operator()(const Ex& first, const Ex& second) const;
115  private:
117  };
118 
120  //
122  public:
124  bool operator()(const Ex& first, const Ex& second) const;
125  private:
127  };
128 
130  public:
132  bool operator()(const Ex& first, const Ex& second) const;
133  private:
135  };
136 
138  public:
140  bool operator()(const Ex& first, const Ex& second) const;
141  private:
143  };
144 
146  public:
148  bool operator()(const Ex& first, const Ex& second) const;
149  private:
151  };
152 
159 
161  public:
162  bool operator()(const Ex& first, const Ex& second) const;
163  };
164 
166  //
168  public:
169  tree_exact_less_no_wildcards_obj(); // disables property handling
171  bool operator()(const Ex& first, const Ex& second) const;
172  private:
174  };
175 
177  public:
179  bool operator()(const Ex& first, const Ex& second) const;
180  private:
182  };
183 
184 
191 
193  public:
194  Ex_comparator(const Properties&);
195 
196  enum class match_t {
197  node_match=0, // a single node matches
198  subtree_match=1, // identical match, including index names
199  match_index_less=2, // structure match, indices in same set, but different names
201  no_match_indexpos_less=4, // mismatch but only for index positions
203  no_match_less=6, // more serious mismatch
205  };
206 
207  enum class useprops_t {
208  always=0, // always use property info
209  not_at_top, // don't use property info at top level of the expression
210  never=2 // never use property info
211  };
212 
214 
215  void clear();
216 
224 
225  void set_value_matches_index(bool);
226 
235 
236  match_t equal_subtree(Ex::iterator i1, Ex::iterator i2,
237  useprops_t use_props=useprops_t::always, bool ignore_parent_rel=false);
238 
241 
242  match_t match_subtree(const Ex&, Ex::iterator i1, Ex::iterator i2, Ex::iterator conditions);
243 
250 
251  match_t match_subproduct(const Ex&,
252  Ex::sibling_iterator lhs, Ex::sibling_iterator tofind,
253  Ex::sibling_iterator st, Ex::iterator conditions);
254 
261 
262  match_t match_subsum(const Ex&,
263  Ex::sibling_iterator lhs, Ex::sibling_iterator tofind,
264  Ex::sibling_iterator st, Ex::iterator conditions);
265 
269 
270  bool satisfies_conditions(Ex::iterator conditions, std::string& error);
271 
273 
274  typedef std::map<Ex, Ex, tree_exact_less_no_wildcards_obj> replacement_map_t;
276 
278 
279  typedef std::map<nset_t::iterator, Ex::iterator, nset_it_less> subtree_replacement_map_t;
281 
284 
286 
291 
292  std::vector<Ex::sibling_iterator> factor_locations;
293  std::vector<int> factor_moving_signs;
295 
300 
303 
304  bool should_swap(Ex::iterator obj, match_t subtree_comparison) ;
305 
315 
316  int can_swap(Ex::iterator one, Ex::iterator two, match_t subtree_comparison,
317  bool ignore_implicit_indices=false);
318 
323 
324  int can_swap_components(Ex::iterator one, Ex::iterator two,
325  match_t subtree_comparison);
326 
330 
331  int can_move_adjacent(Ex::iterator prod, Ex::sibling_iterator one,
332  Ex::sibling_iterator two, bool fix_one=false) ;
333 
334 
337  int can_move_to_front(Ex&, Ex::iterator prod, Ex::sibling_iterator one);
338 
341 
342  int can_move_adjacent(Ex::iterator prod, const std::vector<Ex::sibling_iterator>& factors, Ex::sibling_iterator to_move);
343 
344  protected:
346 
348 
353 
354  match_t compare(const Ex::iterator&, const Ex::iterator&,
355  bool nobrackets=false,
356  useprops_t use_props=useprops_t::always,
357  bool ignore_parent_rel=false);
358 
359  // Internal functions used by can_swap.
360  int can_swap_prod_obj(Ex::iterator prod, Ex::iterator obj, bool) ;
361  int can_swap_prod_prod(Ex::iterator prod1, Ex::iterator prod2, bool) ;
362  int can_swap_sum_obj(Ex::iterator sum, Ex::iterator obj, bool) ;
363  int can_swap_prod_sum(Ex::iterator prod, Ex::iterator sum, bool) ;
364  int can_swap_sum_sum(Ex::iterator sum1, Ex::iterator sum2, bool) ;
365  int can_swap_ilist_ilist(Ex::iterator obj1, Ex::iterator obj2);
366  bool can_swap_different_indexsets(Ex::iterator obj1, Ex::iterator obj2);
367 
368  std::string tab() const;
369  match_t report(match_t r) const;
370 
373  bool name_match_with_autodeclare(Ex::sibling_iterator one, Ex::sibling_iterator two) const;
374 
375  static int offset;
376  };
377 
381 
383  public:
385  bool operator()(const Ex&, const Ex&);
386  private:
388  };
389 
390  class Ex_is_less {
391  public:
392  Ex_is_less(const Properties&, int mod_prel);
393  bool operator()(const Ex&, const Ex&);
394  private:
396  int mod_prel;
397  };
398 
399 
400 
401  }
402 
403 bool operator<(const cadabra::Ex::iterator&, const cadabra::Ex::iterator&);
404 bool operator<(const cadabra::Ex&, const cadabra::Ex&);
405 std::ostream& operator<<(std::ostream&, cadabra::Ex_comparator::useprops_t up);
cadabra::Ex_comparator::Ex_comparator
Ex_comparator(const Properties &)
Definition: Compare.cc:414
cadabra::Ex_is_equivalent::operator()
bool operator()(const Ex &, const Ex &)
Definition: Compare.cc:1691
Indices.hh
cadabra::tree_exact_equal_mod_prel_obj::properties
const Properties * properties
Definition: Compare.hh:150
cadabra::tree_exact_equal_obj
Definition: Compare.hh:137
cadabra::tree_less_obj::properties
const Properties * properties
Definition: Compare.hh:100
cadabra::Ex_comparator::factor_moving_signs
std::vector< int > factor_moving_signs
Definition: Compare.hh:293
cadabra::tree_less_modprel_obj::operator()
bool operator()(const Ex &first, const Ex &second) const
Definition: Compare.cc:205
cadabra::tree_exact_less_mod_prel_obj::tree_exact_less_mod_prel_obj
tree_exact_less_mod_prel_obj(const Properties *)
Definition: Compare.cc:250
cadabra::Ex_comparator::set_value_matches_index
void set_value_matches_index(bool)
Determine whether Coordinates in the pattern (first argument to functions below) can match against In...
Definition: Compare.cc:419
cadabra::tree_exact_equal
bool tree_exact_equal(const Properties *properties, const Ex &one, const Ex &two, int mod_prel, bool checksets, int compare_multiplier, bool literal_wildcards)
Definition: Compare.cc:155
cadabra::tree_exact_less_mod_prel_obj::properties
const Properties * properties
Definition: Compare.hh:134
cadabra::subtree_equal
bool subtree_equal(const Properties *properties, Ex::iterator one, Ex::iterator two, int mod_prel, bool checksets, int compare_multiplier)
Definition: Compare.cc:167
cadabra::tree_less_modprel_obj
Definition: Compare.hh:103
cadabra::Ex_comparator::match_t::subtree_match
cadabra::Ex_comparator::tab
std::string tab() const
Definition: Compare.cc:424
cadabra::Ex_is_equivalent::properties
const Properties & properties
Definition: Compare.hh:387
cadabra::Ex_comparator::can_swap_prod_sum
int can_swap_prod_sum(Ex::iterator prod, Ex::iterator sum, bool)
Definition: Compare.cc:1357
cadabra::Ex_comparator::match_t
match_t
Definition: Compare.hh:196
cadabra::Ex_comparator::useprops_t::always
cadabra::Ex_is_less::operator()
bool operator()(const Ex &, const Ex &)
Definition: Compare.cc:1703
cadabra::tree_exact_less_no_wildcards_obj
Compare two trees exactly, treat wildcard names as ordinary names.
Definition: Compare.hh:167
cadabra::Ex_comparator::useprops_t
useprops_t
Definition: Compare.hh:207
cadabra::Ex_comparator::replacement_map
replacement_map_t replacement_map
Definition: Compare.hh:275
operator<<
std::ostream & operator<<(std::ostream &, cadabra::Ex_comparator::useprops_t up)
Definition: Compare.cc:1722
cadabra::Ex_comparator::match_t::match_index_less
cadabra::tree_exact_less_no_wildcards_obj::tree_exact_less_no_wildcards_obj
tree_exact_less_no_wildcards_obj()
Definition: Compare.cc:230
cadabra::Ex_comparator::can_swap_prod_obj
int can_swap_prod_obj(Ex::iterator prod, Ex::iterator obj, bool)
Definition: Compare.cc:1297
cadabra::Ex_comparator::properties
const Properties & properties
Definition: Compare.hh:345
cadabra::tree_exact_less_obj::tree_exact_less_obj
tree_exact_less_obj(const Properties *)
Definition: Compare.cc:220
cadabra::Ex_is_less::properties
const Properties & properties
Definition: Compare.hh:395
cadabra::subtree_less
bool subtree_less(const Properties *properties, Ex::iterator one, Ex::iterator two, int mod_prel, bool checksets, int compare_multiplier)
Definition: Compare.cc:160
cadabra::Ex_comparator::match_t::match_index_greater
cadabra::Ex_is_less::Ex_is_less
Ex_is_less(const Properties &, int mod_prel)
Definition: Compare.cc:1698
cadabra::tree_equal_obj::operator()
bool operator()(const Ex &first, const Ex &second) const
Definition: Compare.cc:215
cadabra::tree_exact_less_obj
Compare two trees exactly, i.e. including exact index names.
Definition: Compare.hh:121
cadabra::Ex_comparator::factor_locations
std::vector< Ex::sibling_iterator > factor_locations
Information to keep track of where individual factors/terms in a sub-product/sub-sum were found,...
Definition: Compare.hh:292
cadabra::Ex_comparator::lhs_contains_dummies
bool lhs_contains_dummies
Flag to indicate whether additional care must be taken to handle dummies in the lhs of the pattern.
Definition: Compare.hh:299
cadabra::tree_less_modprel_obj::properties
const Properties * properties
Definition: Compare.hh:108
cadabra::subtree_exact_equal
bool subtree_exact_equal(const Properties *properties, Ex::iterator one, Ex::iterator two, int mod_prel, bool checksets, int compare_multiplier, bool literal_wildcards)
Definition: Compare.cc:182
cadabra::tree_exact_less_no_wildcards_mod_prel_obj::properties
const Properties * properties
Definition: Compare.hh:181
cadabra::Ex_comparator::offset
static int offset
Definition: Compare.hh:375
cadabra::Ex_comparator::satisfies_conditions
bool satisfies_conditions(Ex::iterator conditions, std::string &error)
Check whether the a match found by calling equal_subtree or match_subproduct satisfies the conditions...
Definition: Compare.cc:1541
cadabra::subtree_compare
int subtree_compare(const Properties *properties, Ex::iterator one, Ex::iterator two, int mod_prel, bool, int compare_multiplier, bool literal_wildcards)
Definition: Compare.cc:28
cadabra::Properties
Definition: Props.hh:233
cadabra::tree_exact_equal_obj::tree_exact_equal_obj
tree_exact_equal_obj(const Properties *)
cadabra::Ex_comparator::can_swap_sum_sum
int can_swap_sum_sum(Ex::iterator sum1, Ex::iterator sum2, bool)
Definition: Compare.cc:1374
cadabra::Ex_comparator::value_matches_index
bool value_matches_index
Definition: Compare.hh:347
cadabra::Ex_comparator::replacement_map_t
std::map< Ex, Ex, tree_exact_less_no_wildcards_obj > replacement_map_t
Map for the replacement of nodes (indices, patterns).
Definition: Compare.hh:274
cadabra::Ex_comparator::can_swap
int can_swap(Ex::iterator one, Ex::iterator two, match_t subtree_comparison, bool ignore_implicit_indices=false)
Determine whether obj and obj+1 be exchanged? If yes, return the sign, if no return zero.
Definition: Compare.cc:1446
cadabra::Ex_comparator::subtree_replacement_map
subtree_replacement_map_t subtree_replacement_map
Definition: Compare.hh:280
cadabra::tree_exact_equal_mod_prel_obj
Definition: Compare.hh:145
cadabra::Ex_comparator::can_swap_ilist_ilist
int can_swap_ilist_ilist(Ex::iterator obj1, Ex::iterator obj2)
Definition: Compare.cc:1391
cadabra::Ex_comparator::term_ratio
multiplier_t term_ratio
Definition: Compare.hh:294
cadabra::Ex_comparator::match_subtree
match_t match_subtree(const Ex &, Ex::iterator i1, Ex::iterator i2, Ex::iterator conditions)
Match two subtrees, new-style equal_subtree that handles conditions; this is what substitute uses.
Definition: Compare.cc:285
cadabra::Ex_comparator::useprops_t::not_at_top
Storage.hh
cadabra::Ex_comparator
Definition: Compare.hh:192
cadabra::tree_exact_less_no_wildcards_mod_prel_obj::operator()
bool operator()(const Ex &first, const Ex &second) const
Definition: Compare.cc:240
cadabra::tree_equal
bool tree_equal(const Properties *properties, const Ex &one, const Ex &two, int mod_prel, bool checksets, int compare_multiplier)
Definition: Compare.cc:145
cadabra::tree_exact_less_no_wildcards_obj::properties
const Properties * properties
Definition: Compare.hh:173
cadabra::tree_less
bool tree_less(const Properties *properties, const Ex &one, const Ex &two, int mod_prel, bool checksets, int compare_multiplier)
Various comparison functions, some exact, some with pattern logic.
Definition: Compare.cc:140
cadabra::Ex_comparator::match_t::no_match_less
cadabra::Ex_comparator::index_value_map
replacement_map_t index_value_map
Map for matching of index names to index values.
Definition: Compare.hh:285
cadabra::one
void one(rset_t::iterator &num)
Definition: Storage.cc:1014
cadabra::Ex_comparator::can_move_adjacent
int can_move_adjacent(Ex::iterator prod, Ex::sibling_iterator one, Ex::sibling_iterator two, bool fix_one=false)
Determine whether object 'one' and 'two' can be moved next to each other by moving either one or the ...
Definition: Compare.cc:1123
cadabra::Ex_is_equivalent::Ex_is_equivalent
Ex_is_equivalent(const Properties &)
Definition: Compare.cc:1686
cadabra::subtree_exact_less
bool subtree_exact_less(const Properties *properties, Ex::iterator one, Ex::iterator two, int mod_prel, bool checksets, int compare_multiplier, bool literal_wildcards)
Definition: Compare.cc:174
Props.hh
cadabra::tree_exact_less_no_wildcards_obj::operator()
bool operator()(const Ex &first, const Ex &second) const
Definition: Compare.cc:235
operator<
bool operator<(const cadabra::Ex::iterator &, const cadabra::Ex::iterator &)
Definition: Compare.cc:1712
cadabra::tree_exact_less
bool tree_exact_less(const Properties *properties, const Ex &one, const Ex &two, int mod_prel, bool checksets, int compare_multiplier, bool literal_wildcards)
Definition: Compare.cc:150
cadabra::Ex_comparator::can_move_to_front
int can_move_to_front(Ex &, Ex::iterator prod, Ex::sibling_iterator one)
Determine whether object 'one' can be moved to be the first factor in the given product.
Definition: Compare.cc:1178
cadabra::tree_exact_less_no_wildcards_mod_prel_obj::tree_exact_less_no_wildcards_mod_prel_obj
tree_exact_less_no_wildcards_mod_prel_obj(const Properties *)
cadabra::tree_equal_obj::properties
const Properties * properties
Definition: Compare.hh:116
cadabra::tree_exact_less_obj::properties
const Properties * properties
Definition: Compare.hh:126
cadabra::tree_equal_obj::tree_equal_obj
tree_equal_obj(const Properties *)
Definition: Compare.cc:210
cadabra::Ex_comparator::match_t::no_match_indexpos_greater
cadabra::Ex_comparator::match_t::no_match_indexpos_less
cadabra
Functions to handle the exchange properties of two or more symbols in a product.
Definition: Adjform.cc:83
cadabra::Ex_comparator::equal_subtree
match_t equal_subtree(Ex::iterator i1, Ex::iterator i2, useprops_t use_props=useprops_t::always, bool ignore_parent_rel=false)
Match two subtrees taking into account symbol properties.
Definition: Compare.cc:299
cadabra::Ex
Definition: Storage.hh:140
cadabra::tree_exact_equal_mod_prel_obj::operator()
bool operator()(const Ex &first, const Ex &second) const
Definition: Compare.cc:260
cadabra::tree_equal_obj
Definition: Compare.hh:111
cadabra::Ex_comparator::match_subproduct
match_t match_subproduct(const Ex &, Ex::sibling_iterator lhs, Ex::sibling_iterator tofind, Ex::sibling_iterator st, Ex::iterator conditions)
Find a sub-product in a product.
Definition: Compare.cc:897
cadabra::Ex_comparator::match_subsum
match_t match_subsum(const Ex &, Ex::sibling_iterator lhs, Ex::sibling_iterator tofind, Ex::sibling_iterator st, Ex::iterator conditions)
Find a sub-sum in a sum.
Definition: Compare.cc:991
cadabra::Ex_comparator::match_t::node_match
cadabra::Ex_comparator::can_swap_different_indexsets
bool can_swap_different_indexsets(Ex::iterator obj1, Ex::iterator obj2)
Definition: Compare.cc:1420
cadabra::Ex_is_less::mod_prel
int mod_prel
Definition: Compare.hh:396
cadabra::Ex_comparator::subtree_replacement_map_t
std::map< nset_t::iterator, Ex::iterator, nset_it_less > subtree_replacement_map_t
Map for the replacement of entire subtrees (object patterns).
Definition: Compare.hh:279
cadabra::Ex_comparator::report
match_t report(match_t r) const
Definition: Compare.cc:432
cadabra::multiplier_t
mpq_class multiplier_t
Definition: Storage.hh:38
cadabra::tree_less_modprel_obj::tree_less_modprel_obj
tree_less_modprel_obj(const Properties *)
Definition: Compare.cc:200
cadabra::tree_exact_less_no_wildcards_mod_prel_obj
Definition: Compare.hh:176
cadabra::tree_exact_less_mod_prel_obj
Definition: Compare.hh:129
cadabra::tree_exact_less_mod_prel_obj::operator()
bool operator()(const Ex &first, const Ex &second) const
Definition: Compare.cc:255
cadabra::lhs
Ex lhs(Ex_ptr ex)
Definition: py_ex.cc:304
cadabra::Ex_comparator::compare
match_t compare(const Ex::iterator &, const Ex::iterator &, bool nobrackets=false, useprops_t use_props=useprops_t::always, bool ignore_parent_rel=false)
Internal entry point.
Definition: Compare.cc:468
cadabra::tree_less_obj
Compare two trees by pattern logic, i.e. modulo index names.
Definition: Compare.hh:95
cadabra::tree_exact_less_for_indexmap_obj::operator()
bool operator()(const Ex &first, const Ex &second) const
Definition: Compare.cc:265
cadabra::Ex_comparator::match_t::no_match_greater
cadabra::Ex_comparator::can_swap_prod_prod
int can_swap_prod_prod(Ex::iterator prod1, Ex::iterator prod2, bool)
Definition: Compare.cc:1322
cadabra::Ex_comparator::should_swap
bool should_swap(Ex::iterator obj, match_t subtree_comparison)
Determine whether two objects should be swapped according to the available SortOrder properties.
Definition: Compare.cc:1265
cadabra::tree_exact_equal_mod_prel_obj::tree_exact_equal_mod_prel_obj
tree_exact_equal_mod_prel_obj(const Properties *)
cadabra::tree_less_obj::tree_less_obj
tree_less_obj(const Properties *)
Definition: Compare.cc:190
cadabra::Ex_comparator::clear
void clear()
Reset the object for a new match.
Definition: Compare.cc:276
cadabra::tree_exact_less_for_indexmap_obj
Compare for indexmap_t.
Definition: Compare.hh:160
snoop::error
const char error[]
Definition: Snoop.hh:338
cadabra::tree_less_obj::operator()
bool operator()(const Ex &first, const Ex &second) const
Definition: Compare.cc:195
cadabra::tree_exact_equal_obj::properties
const Properties * properties
Definition: Compare.hh:142
cadabra::Ex_comparator::can_swap_sum_obj
int can_swap_sum_obj(Ex::iterator sum, Ex::iterator obj, bool)
Definition: Compare.cc:1338
cadabra::Ex_comparator::can_swap_components
int can_swap_components(Ex::iterator one, Ex::iterator two, match_t subtree_comparison)
Wrapper for can_swap which is meant for objects that have implicit indices.
Definition: Compare.cc:1188
cadabra::Ex_comparator::name_match_with_autodeclare
bool name_match_with_autodeclare(Ex::sibling_iterator one, Ex::sibling_iterator two) const
Match the name elements of a node, but take into account that one of them can be an autodeclare name ...
Definition: Compare.cc:886
cadabra::Ex_comparator::useprops_t::never
cadabra::Ex_is_less
Definition: Compare.hh:390
cadabra::tree_exact_less_obj::operator()
bool operator()(const Ex &first, const Ex &second) const
Definition: Compare.cc:225
cadabra::Ex_is_equivalent
Definition: Compare.hh:382
cadabra::tree_exact_equal_obj::operator()
bool operator()(const Ex &first, const Ex &second) const
Definition: Compare.cc:245