Documentation of CSL
libraryevaluator.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef LIBRARY_EVALUATOR_H_INCLUDED
24 #define LIBRARY_EVALUATOR_H_INCLUDED
25 
26 #include "abstract.h"
27 #include "index.h"
28 #include <string>
29 
30 namespace csl {
31 
32 class LibEval {
33 
34 public:
35 
36  using ID_t = long long int;
37  friend class LibEvalSession;
38 
39  inline static const std::string name = "IT";
40 
41  LibEval(Expr const &t_init, ID_t t_id);
42 
43  LibEval(LibEval const &other) = default;
44  LibEval &operator=(LibEval const &other) = default;
45 
46  Expr getInit() const;
47  Expr getExpr() const;
48  ID_t getID() const;
49  std::vector<Expr> const &getUnEval() const;
50  std::vector<Expr> &getUnEval();
51  std::vector<csl::Tensor> const &getTensors() const;
52  std::vector<csl::Tensor> &getTensors();
53 
54  void setInit(Expr const &t_init);
55  void setExpr(Expr const &t_expr);
56  void setID(ID_t t_id);
57 
58  bool operator==(LibEval const &other) const;
59  bool operator==(Expr const &other) const;
60  bool operator!=(LibEval const &other) const;
61  bool operator!=(Expr const &other) const;
62 
63  static bool isInstance(Expr const &expr);
64 
65  void replaceTensors();
66 
67  void print(std::ostream &out) const;
68 
69  void printLib(
70  std::ostream &out,
71  int indent = 1,
72  bool complexParameters = false
73  );
74 
75  static void printExpression(
76  std::ostream &out,
77  Expr &expr,
78  int indent,
79  std::string const &begin
80  );
81 
82  friend
83  std::ostream &operator<<(
84  std::ostream &out,
85  LibEval const &eval
86  );
87 
88 private:
89 
90  static Expr make_init(Expr const &t_init);
91 
92  static Expr make_expr(ID_t t_id);
93 
94  static bool compare_expr(
95  Expr const &A,
96  Expr const &B
97  );
98 
99  static std::string idToString(ID_t id);
100 
101 private:
102 
103  Expr init;
104  Expr expr;
105  ID_t id;
106  mutable std::vector<Expr> unEval;
107  mutable std::vector<csl::Tensor> tensors;
108  csl::IndexStructure indices;
109 };
110 
112 
113 public:
114 
115  using ID_t = LibEval::ID_t;
116 
117  static inline size_t maxLeafs = 20;
118 
119  struct Perf {
120 
121  size_t nEval;
122  size_t nOperations;
123  std::vector<LibEval> evals;
124  };
125 
126  LibEvalSession();
127 
128  Expr addEval(Expr init);
129 
130  std::vector<LibEval> &getEval();
131  std::vector<LibEval> const &getEval() const;
132  std::vector<Expr> const &getUnEval() const;
133  std::vector<Expr> &getUnEval();
134  std::vector<csl::Tensor> const &getTensors() const;
135  std::vector<csl::Tensor> &getTensors();
136 
137  void setEval(std::vector<LibEval> const &t_eval);
138  void setEval(std::vector<LibEval> &&t_eval);
139 
140  Perf getPerf(Expr &init);
141 
142  void merge();
143  void removeUnique();
144 
145  void simplify();
146  void simplifyProds(size_t min_factors = 2);
147  void simplifySums();
148 
149  static LibEvalSession parseExpression(
150  Expr &expr,
151  bool findIntermediates = true
152  );
153 
154  void parse(
155  Expr &expr,
156  bool findIntermediates,
157  std::map<csl::AbstractParent const*, csl::Expr> &parsedAbbrevs
158  );
159 
160  void parse(
161  Expr &expr,
162  bool findIntermediates = true
163  );
164 
165  void parseProduct(
166  Expr &iprod,
167  bool findIntermediates,
168  std::map<csl::AbstractParent const*, csl::Expr> &parsedAbbrevs
169  );
170 
171  void gatherUnEvalAndTensors(Expr const &result);
172 
173  void print(std::ostream &out) const;
174 
175  void printLib(
176  Expr &init,
177  Perf &perf,
178  std::ostream &out,
179  bool onylDep = false
180  );
181 
182 public:
183 
184  static csl::IndexStructure getFreeStructure(Expr const &t_init);
185 
186  static csl::IndexStructure getFreeStructure(
187  csl::IndexStructure const &t_init
188  );
189 
190 protected:
191 
192  void getPerf(
193  LibEval const &init,
194  std::vector<LibEval> &newEvals,
195  std::vector<int> &nOccurences
196  );
197 
198  static Expr expandIProd(Expr const &iprod);
199  void addUnEval(Expr const& expr) const;
200  void addTensor(csl::Tensor const& parent) const;
201 
202 public:
203 
204  bool expand = false;
205 
206 protected:
207 
208  ID_t maxID = 0;
209 
210  std::vector<LibEval> eval;
211 
212  std::map<csl::AbstractParent const *, Expr> abbreviations;
213 
214  mutable std::vector<Expr> unEval;
215 
216  mutable std::vector<csl::Tensor> tensors;
217 };
218 
219 inline
220 std::ostream &operator<<(
221  std::ostream &out,
223  )
224 {
225  return out << perf.nEval << " evals, "
226  << perf.nOperations << " operations.\n";
227 }
228 
229 } // End of namespace csl
230 
231 #endif
Namespace for csl library.
Definition: abreviation.h:34
Definition: libraryevaluator.h:32
bool operator==(const Expr &a, const Expr &b)
see Abstract::operator==()
Definition: abstract.cpp:1398
Definition: libraryevaluator.h:111
Base classes for all exprs in the program.
Definition: indicial.h:675
bool operator!=(const Expr &a, const Expr &b)
see Abstract::operator!=()
Definition: abstract.cpp:1404
Manages a std::vector of Index, to be used by an TensorElement.
Definition: index.h:472
Definition: libraryevaluator.h:119
Expression type/.
Definition: abstract.h:1573