Cadabra
Computer algebra system for field theory problems
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Algorithm.hh
Go to the documentation of this file.
1 /*
2 
3  Cadabra: a field-theory motivated computer algebra system.
4  Copyright (C) 2001-2014 Kasper Peeters <kasper.peeters@phi-sci.com>
5 
6  This program is free software: you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation, either version 3 of the
9  License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #pragma once
22 
23 #include "Stopwatch.hh"
24 #include "Storage.hh"
25 #include "Compare.hh"
26 #include "Props.hh"
27 #include "Exceptions.hh"
28 #include "Kernel.hh"
29 #include "IndexIterator.hh"
30 #include "ProgressMonitor.hh"
31 
32 #include <map>
33 #include <fstream>
34 #include <cstddef>
35 
36 namespace cadabra {
37 
57 
58 class Algorithm {
59  public:
64 
65  Algorithm(const Kernel&, Ex&);
66 
67  virtual ~Algorithm();
68 
69  typedef Ex::iterator iterator;
70  typedef Ex::post_order_iterator post_order_iterator;
71  typedef Ex::sibling_iterator sibling_iterator;
73 
75 
78 
80 
88 
89  result_t apply_generic(bool deep=true, bool repeat=false, unsigned int depth=0);
90  result_t apply_generic(iterator&, bool deep, bool repeat, unsigned int depth);
91 
96 
97  result_t apply_pre_order(bool repeat=false);
98 
99  // Global information
100  unsigned int number_of_calls;
104 
106  bool check_consistency(iterator) const;
107  bool check_index_consistency(iterator) const;
110 
111  void report_progress(const std::string&, int todo, int done, int count=2);
112 
116 
119 
120  // The number of indices of a node, taking into account IndexInherit-ance. These
121  // indices do therefore not all have to be direct child nodes of 'it', they can
122  // sit deeper down the tree.
123  unsigned int number_of_indices(iterator it);
124  static unsigned int number_of_indices(const Properties&, iterator it);
125 
126  // The number of indices of a node, counting only the direct ones (i.e. not those
127  // inherited from child nodes).
128  static unsigned int number_of_direct_indices(iterator it);
129 
130  bool rename_replacement_dummies(iterator, bool still_inside_algo=false);
131 
132 
138  typedef std::multimap<Ex, Ex::iterator, tree_exact_less_for_indexmap_obj> index_map_t;
139 
141  typedef std::map<Ex::iterator, int, Ex::iterator_base_less> index_position_map_t;
142 
143 
144  protected:
145  const Kernel& kernel;
146  Ex& tr;
148 
149  // The main entry point which is used by the public entry points listed
150  // above. Override these in any subclass.
151  //
152  virtual bool can_apply(iterator)=0;
153  virtual result_t apply(iterator&)=0;
154 
155  // Index stuff
156  int index_parity(iterator) const;
157  static bool less_without_numbers(nset_t::iterator, nset_t::iterator);
158  static bool equal_without_numbers(nset_t::iterator, nset_t::iterator);
159 
161  typedef std::pair<sibling_iterator, sibling_iterator> range_t;
162  typedef std::vector<range_t> range_vector_t;
163 
165  void find_argument_lists(range_vector_t&, bool only_comma_lists=true) const;
166  template<class Iter>
167  range_vector_t::iterator find_arg_superset(range_vector_t&, Iter st, Iter nd);
168  range_vector_t::iterator find_arg_superset(range_vector_t&, sibling_iterator it);
169 
170  // Locate objects inside the tree (formerly in the 'locate' algorithm).
171  unsigned int locate_single_object(Ex::iterator obj_to_find,
172  Ex::iterator st, Ex::iterator nd,
173  std::vector<unsigned int>& store);
174  bool locate_object_set(const Ex& objs,
175  Ex::iterator st, Ex::iterator nd,
176  std::vector<unsigned int>& store);
177  static bool compare_(const str_node&, const str_node&);
178 
179 
181  bool is_termlike(iterator);
182  bool is_factorlike(iterator);
183 
186  bool is_single_term(iterator);
190 
193  void force_prod_wrap(iterator&);
194 
199  bool separated_by_derivative(iterator, iterator, iterator check_dependence) const;
200 
201  // Given a node with non-zero multiplier, distribute this
202  // multiplier up the tree when the node is a \sum node, or push it into the
203  // \prod node if that is the parent. Do this recursively
204  // in case a child is a sum as well. Note that 'pushup' is actually 'pushdown'
205  // in the case of sums.
206  // This never changes the tree structure, only the distribution of multipliers.
207 
208  // FIXME: this is now superseded by code in Cleanup.cc, and the generic way
209  // to make a tree consistent is to call cleanup_dispatch, not pick-and-match from
210  // the various algorithms.
212 
213  // Return the number of elements in the first range for which an identical element
214  // is present in the second range.
215  template<class BinaryPredicate>
217  sibling_iterator, sibling_iterator, BinaryPredicate) const;
218 
219  // Turn a node into a '1' or '0' node.
220  void node_zero(iterator);
221  void node_one(iterator);
222  void node_integer(iterator, int);
223 
234  void print_classify_indices(std::ostream&, iterator) const;
236  bool move_out=false) const;
237  void classify_add_index(iterator it, index_map_t& ind_free, index_map_t& ind_dummy) const;
238  void classify_indices_up(iterator, index_map_t& ind_free, index_map_t& ind_dummy) const;
239  void classify_indices(iterator, index_map_t& ind_free, index_map_t& ind_dummy) const;
240  int max_numbered_name_one(const std::string& nm, const index_map_t * one) const;
241  int max_numbered_name(const std::string&, const index_map_t *m1, const index_map_t *m2=0,
242  const index_map_t *m3=0, const index_map_t *m4=0, const index_map_t *m5=0) const;
243  Ex get_dummy(const list_property *, const index_map_t *m1, const index_map_t *m2=0,
244  const index_map_t *m3=0, const index_map_t *m4=0, const index_map_t *m5=0) const;
245  Ex get_dummy(const list_property *, iterator) const;
246  Ex get_dummy(const list_property *, iterator, iterator) const;
247 
248  bool index_in_set(Ex, const index_map_t *) const;
249 
251  index_map_t::iterator find_modulo_parent_rel(iterator it, index_map_t& imap) const;
252 
253  private:
254  // Single or deep-scan apply operations. Do not call directly.
255  result_t apply_once(Ex::iterator& it);
256  result_t apply_deep(Ex::iterator& it);
257 
265  void dumpmap(std::ostream&, const index_map_t&) const;
266 
267 // bool cleanup_anomalous_products(Ex& tr, Ex::iterator& it);
268 };
269 
270 
273 template<class BinaryPredicate>
276  BinaryPredicate fun) const
277  {
278  unsigned int ret=0;
279  sibling_iterator it1=from1;
280  while(it1!=to1) {
281  sibling_iterator it2=from2;
282  while(it2!=to2) {
283  if(fun(*it1,*it2))
284  ++ret;
285  ++it2;
286  }
287  ++it1;
288  }
289  return ret;
290  }
291 
292 
293 }
void node_one(iterator)
Definition: Algorithm.cc:417
int index_parity(iterator) const
Definition: Algorithm.cc:432
void propagate_zeroes(post_order_iterator &, const iterator &)
Given a node with zero multiplier, propagate this zero upwards in the tree.
Definition: Algorithm.cc:286
Base class for all algorithms, containing generic routines and in particular the logic for index clas...
Definition: Algorithm.hh:58
bool is_single_term(iterator)
Take a single non-product node in a sum and wrap it in a product node, so it can be handled on the sa...
Definition: Algorithm.cc:1417
const Kernel & kernel
Definition: Algorithm.hh:145
bool separated_by_derivative(iterator, iterator, iterator check_dependence) const
Figure out whether two objects (commonly indices) are separated by a derivative operator, as in .
Definition: Algorithm.cc:1482
Basic storage class for symbolic mathemematical expressions.
Definition: Storage.hh:130
result_t apply_pre_order(bool repeat=false)
Apply algorithm with alternative traversal: starting from the top node, traverse the tree pre-order (...
Definition: Algorithm.cc:61
bool prod_wrap_single_term(iterator &)
Definition: Algorithm.cc:1445
Stopwatch index_sw
Definition: Algorithm.hh:113
bool locate_object_set(const Ex &objs, Ex::iterator st, Ex::iterator nd, std::vector< unsigned int > &store)
Definition: Algorithm.cc:1577
void fill_map(index_map_t &, sibling_iterator, sibling_iterator) const
Definition: Algorithm.cc:905
Definition: ProgressMonitor.hh:10
void set_progress_monitor(ProgressMonitor *)
Provide the algorithm with a ProgressMonitor object on which to register (nested) progress informatio...
Definition: Algorithm.cc:56
index_iterator end_index(iterator it) const
Definition: Algorithm.cc:462
bool check_degree_consistency(iterator) const
Given an expression top node, check differential form degree consistency.
Definition: Algorithm.cc:477
void dumpmap(std::ostream &, const index_map_t &) const
Definition: Algorithm.cc:1118
static bool less_without_numbers(nset_t::iterator, nset_t::iterator)
Definition: Algorithm.cc:1641
static bool equal_without_numbers(nset_t::iterator, nset_t::iterator)
Definition: Algorithm.cc:1662
static bool compare_(const str_node &, const str_node &)
Definition: Algorithm.cc:1687
bool is_factorlike(iterator)
Definition: Algorithm.cc:1408
Definition: Stopwatch.hh:32
int max_numbered_name(const std::string &, const index_map_t *m1, const index_map_t *m2=0, const index_map_t *m3=0, const index_map_t *m4=0, const index_map_t *m5=0) const
Definition: Algorithm.cc:720
index_map_t::iterator find_modulo_parent_rel(iterator it, index_map_t &imap) const
Find an index in the set, not taking into account index position.
Definition: Algorithm.cc:973
bool rename_replacement_dummies(iterator, bool still_inside_algo=false)
Definition: Algorithm.cc:599
unsigned int locate_single_object(Ex::iterator obj_to_find, Ex::iterator st, Ex::iterator nd, std::vector< unsigned int > &store)
Definition: Algorithm.cc:1559
void classify_add_index(iterator it, index_map_t &ind_free, index_map_t &ind_dummy) const
Definition: Algorithm.cc:988
virtual bool can_apply(iterator)=0
void fill_index_position_map(iterator, const index_map_t &, index_position_map_t &) const
Index manipulation and classification.
Definition: Algorithm.cc:881
result_t apply_generic(bool deep=true, bool repeat=false, unsigned int depth=0)
The main entry points for running algorithms, which traverse the tree post-order ('child before paren...
Definition: Algorithm.cc:89
std::pair< sibling_iterator, sibling_iterator > range_t
Finding objects in sets.
Definition: Algorithm.hh:161
Stopwatch get_dummy_sw
Definition: Algorithm.hh:114
void find_argument_lists(range_vector_t &, bool only_comma_lists=true) const
void fun(int *&p)
Definition: passing.cc:6
void node_integer(iterator, int)
Definition: Algorithm.cc:424
std::vector< range_t > range_vector_t
Definition: Algorithm.hh:162
bool index_in_set(Ex, const index_map_t *) const
Definition: Algorithm.cc:746
Elementary building block for a mathematical expression.
Definition: Storage.hh:55
void determine_intersection(index_map_t &one, index_map_t &two, index_map_t &target, bool move_out=false) const
Definition: Algorithm.cc:920
void one(rset_t::iterator &num)
Definition: Storage.cc:912
static unsigned int number_of_direct_indices(iterator it)
Definition: Algorithm.cc:1629
bool is_nonprod_factor_in_prod(iterator)
Definition: Algorithm.cc:1432
An iterator which iterates over indices even if they are at lower levels, i.e.
Definition: IndexIterator.hh:16
virtual result_t apply(iterator &)=0
bool is_termlike(iterator)
Determine structure (version-2)
Definition: Algorithm.cc:1393
std::map< Ex::iterator, int, Ex::iterator_base_less > index_position_map_t
A map from the position of each index to the sequential index.
Definition: Algorithm.hh:141
Ex::iterator iterator
Definition: Algorithm.hh:69
Ex::sibling_iterator sibling_iterator
Definition: Algorithm.hh:71
Ex::post_order_iterator post_order_iterator
Definition: Algorithm.hh:70
bool contains(sibling_iterator from, sibling_iterator to, sibling_iterator arg)
Definition: Algorithm.cc:1339
result_t apply_deep(Ex::iterator &it)
Definition: Algorithm.cc:180
Stopwatch report_progress_stopwatch
Definition: Algorithm.hh:115
range_vector_t::iterator find_arg_superset(range_vector_t &, Iter st, Iter nd)
Definition: Algorithm.cc:1372
bool suppress_normal_output
Definition: Algorithm.hh:102
Ex get_dummy(const list_property *, const index_map_t *m1, const index_map_t *m2=0, const index_map_t *m3=0, const index_map_t *m4=0, const index_map_t *m5=0) const
Definition: Algorithm.cc:765
unsigned int number_of_modifications
Definition: Algorithm.hh:101
unsigned int number_of_calls
Definition: Algorithm.hh:100
bool discard_command_node
Definition: Algorithm.hh:103
index_iterator begin_index(iterator it) const
Definition: Algorithm.cc:457
bool check_index_consistency(iterator) const
Definition: Algorithm.cc:470
void node_zero(iterator)
Definition: Algorithm.cc:410
Algorithm(const Kernel &, Ex &)
Initialise the algorithm with a reference to the expression tree, but do not yet do anything with thi...
Definition: Algorithm.cc:41
void report_progress(const std::string &, int todo, int done, int count=2)
Definition: Algorithm.cc:566
Ex::result_t result_t
Definition: Algorithm.hh:72
void classify_indices_up(iterator, index_map_t &ind_free, index_map_t &ind_dummy) const
Definition: Algorithm.cc:1025
bool check_consistency(iterator) const
Given an expression top node, check index consistency.
Definition: Algorithm.cc:482
bool interrupted
Definition: Algorithm.hh:74
Something cannot be both a list property and a normal property at the same time, so we can safely inh...
Definition: Props.hh:168
result_t apply_once(Ex::iterator &it)
Definition: Algorithm.cc:166
int max_numbered_name_one(const std::string &nm, const index_map_t *one) const
Definition: Algorithm.cc:699
bool prod_unwrap_single_term(iterator &)
Definition: Algorithm.cc:1467
Definition: Kernel.hh:15
unsigned int number_of_indices(iterator it)
Definition: Algorithm.cc:445
void force_prod_wrap(iterator &)
Wrap a term in a product, irrespective of its parent (it usually makes more sense to call the safer p...
Definition: Algorithm.cc:1454
void print_classify_indices(std::ostream &, iterator) const
Definition: Algorithm.cc:849
result_t
Keeping track of what algorithms have done to this expression.
Definition: Storage.hh:149
unsigned int intersection_number(sibling_iterator, sibling_iterator, sibling_iterator, sibling_iterator, BinaryPredicate) const
Determine the number of elements in the first range which also occur in the second range...
Definition: Algorithm.hh:274
Ex & tr
Definition: Algorithm.hh:146
std::multimap< Ex, Ex::iterator, tree_exact_less_for_indexmap_obj > index_map_t
A map from a pattern to the position where it occurs in the tree.
Definition: Algorithm.hh:138
ProgressMonitor * pm
Definition: Algorithm.hh:147
Class holding a collection of properties attached to expressions.
Definition: Props.hh:203
void classify_indices(iterator, index_map_t &ind_free, index_map_t &ind_dummy) const
Definition: Algorithm.cc:1130
void pushup_multiplier(iterator)
Definition: Algorithm.cc:371
virtual ~Algorithm()
Definition: Algorithm.cc:52