Cadabra
Computer algebra system for field theory problems
substitute.hh
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "Algorithm.hh"
6 #include "lru_cache.hh"
7 
8 namespace cadabra {
9 
13 
14  class substitute : public Algorithm {
15  public:
16  substitute(const Kernel&, Ex& tr, Ex& args, bool partial=true);
17 
33 
34  virtual bool can_apply(iterator st);
35  virtual result_t apply(iterator&);
36 
38 
41  static size_t cache_size();
42 
43  private:
44  Ex& args;
45 
48 
49  std::map<iterator, bool> lhs_contains_dummies, rhs_contains_dummies;
50 
51  // For object swap testing routines:
53  bool partial;
54 
55  // Rules is a class for caching properties of substitution
56  // rules to avoid processing them in subsequent calls.
57 
58  class Rules {
59 
60  public:
61  Rules(size_t max_size_=1000, size_t cleanup_threshold_=100)
62  : properties(max_size_), max_size(max_size_), cleanup_threshold(cleanup_threshold_) {}
63 
64  // Associate rule properties with a specific object
65  void store(Ex& rules,
66  std::map<iterator, bool>& lhs_contains_dummies,
67  std::map<iterator, bool>& rhs_contains_dummies);
68 
69  // Check if rules are present
70  bool is_present(Ex& rules) const;
71 
72  // Retrieve properties from rules
73  void retrieve(Ex& rules,
74  std::map<iterator, bool>& lhs_contains_dummies,
75  std::map<iterator, bool>& rhs_contains_dummies) const;
76 
77  // Count number of rules
78  size_t size() const;
79 
80  // Eliminate rules that are expired
81  void cleanup();
82 
83  private:
84  // Map storing weak pointers to `Ex` and pairs of lhs/rhs maps as values
86  std::pair< std::map<iterator, bool>, std::map<iterator, bool> >,
87  std::owner_less<std::weak_ptr<Ex>>
89  // Max size of the rules list
90  size_t max_size;
91  // Initial size threshold to trigger cleanup_rules
93 
94 
95  };
96 
97  // Shared instance of all replacement rules.
99  static size_t cache_hits, cache_misses;
100  };
101 
102 
103  }
104 
105 
Definition: lru_cache.hh:25
Base class for all algorithms, containing generic routines and in particular the logic for index clas...
Definition: Algorithm.hh:59
Ex::iterator iterator
Definition: Algorithm.hh:70
Ex & tr
Definition: Algorithm.hh:151
A generic tree comparison class which will take into account index contractions and will also keep tr...
Definition: Compare.hh:192
Basic storage class for symbolic mathemematical expressions.
Definition: Storage.hh:142
result_t
Keeping track of what algorithms have done to this expression.
Definition: Storage.hh:164
Definition: Kernel.hh:15
Definition: sort_product.hh:8
Definition: substitute.hh:58
Rules(size_t max_size_=1000, size_t cleanup_threshold_=100)
Definition: substitute.hh:61
void store(Ex &rules, std::map< iterator, bool > &lhs_contains_dummies, std::map< iterator, bool > &rhs_contains_dummies)
Definition: substitute.cc:467
LRUcache< std::weak_ptr< Ex >, std::pair< std::map< iterator, bool >, std::map< iterator, bool > >, std::owner_less< std::weak_ptr< Ex > > > properties
Definition: substitute.hh:88
void retrieve(Ex &rules, std::map< iterator, bool > &lhs_contains_dummies, std::map< iterator, bool > &rhs_contains_dummies) const
Definition: substitute.cc:498
size_t cleanup_threshold
Definition: substitute.hh:92
bool is_present(Ex &rules) const
Definition: substitute.cc:509
size_t size() const
Definition: substitute.cc:536
void cleanup()
Definition: substitute.cc:541
size_t max_size
Definition: substitute.hh:90
Generic substitution algorithm.
Definition: substitute.hh:14
sort_product sort_product_
Definition: substitute.hh:52
static size_t cache_misses
Definition: substitute.hh:99
static Rules replacement_rules
Definition: substitute.hh:98
iterator conditions
Definition: substitute.hh:47
iterator use_rule
Definition: substitute.hh:46
bool partial
Definition: substitute.hh:53
Ex & args
Definition: substitute.hh:44
static size_t cache_hits
Definition: substitute.hh:99
static size_t cache_size()
Return the number of substitution rules which have been pre-processed and sit in the cache.
Definition: substitute.cc:461
std::map< iterator, bool > lhs_contains_dummies
Definition: substitute.hh:49
std::map< iterator, bool > rhs_contains_dummies
Definition: substitute.hh:49
virtual result_t apply(iterator &)
Definition: substitute.cc:196
virtual bool can_apply(iterator st)
Match the lhs of the replacement rule to the subtree 'st' under consideration.
Definition: substitute.cc:111
substitute(const Kernel &, Ex &tr, Ex &args, bool partial=true)
Definition: substitute.cc:20
Ex_comparator comparator
Definition: substitute.hh:37
Functions to handle the exchange properties of two or more symbols in a product.
Definition: Adjform.cc:83