HepLib
Loading...
Searching...
No Matches
Functions.cpp
Go to the documentation of this file.
1
6#include "BASIC.h"
7#include "ginac/parse_context.h"
8
9namespace HepLib {
10
12 REGISTER_FUNCTION(y, do_not_evalf_params())
13 REGISTER_FUNCTION(z, do_not_evalf_params())
14
15 #ifndef DOXYGEN_SKIP
16
17 unsigned F1_SERIAL::serial = GiNaC::function::register_new(function_options("F",1).do_not_evalf_params().overloaded(2));
18 unsigned F2_SERIAL::serial = GiNaC::function::register_new(function_options("F",2).do_not_evalf_params().overloaded(2));
19
20 unsigned WF1_SERIAL::serial = GiNaC::function::register_new(function_options("WF",1).do_not_evalf_params().overloaded(5));
21 unsigned WF2_SERIAL::serial = GiNaC::function::register_new(function_options("WF",2).do_not_evalf_params().overloaded(5));
22 unsigned WF3_SERIAL::serial = GiNaC::function::register_new(function_options("WF",3).do_not_evalf_params().overloaded(5));
23 unsigned WF4_SERIAL::serial = GiNaC::function::register_new(function_options("WF",4).do_not_evalf_params().overloaded(5));
24 unsigned WF5_SERIAL::serial = GiNaC::function::register_new(function_options("WF",5).do_not_evalf_params().overloaded(5));
25 // iWF function, up to 5 arguments
26 unsigned iWF1_SERIAL::serial = GiNaC::function::register_new(function_options("iWF",1).do_not_evalf_params().overloaded(5));
27 unsigned iWF2_SERIAL::serial = GiNaC::function::register_new(function_options("iWF",2).do_not_evalf_params().overloaded(5));
28 unsigned iWF3_SERIAL::serial = GiNaC::function::register_new(function_options("iWF",3).do_not_evalf_params().overloaded(5));
29 unsigned iWF4_SERIAL::serial = GiNaC::function::register_new(function_options("iWF",4).do_not_evalf_params().overloaded(5));
30 unsigned iWF5_SERIAL::serial = GiNaC::function::register_new(function_options("iWF",5).do_not_evalf_params().overloaded(5));
31
32 #endif
33
34 /*-----------------------------------------------------*/
35 // MapFunction Class
36 /*-----------------------------------------------------*/
37 MapFunction::MapFunction(std::function<ex(const ex &, MapFunction &)> func) : Function(func) { }
38 ex MapFunction::operator()(const ex &e) {
39 return Function(e, *this);
40 }
41
42 ex MapFunction::subs(const ex & expr, const ex & pat, std::function<ex(const ex &)>f) {
43 MapFunction map([pat,f](const ex & e, MapFunction &self)->ex{
44 if(!e.has(pat)) return e;
45 else if(e.match(pat)) return f(e);
46 else return e.map(self);
47 });
48 return map(expr);
49 }
50
51 /*-----------------------------------------------------*/
52 // Parser Class
53 /*-----------------------------------------------------*/
54 // copy from GiNaC parser
55 namespace {
56 class functions_hack : public GiNaC::function {
57 public:
58 static const std::vector<function_options>& get_registered_functions() {
59 return function::registered_functions();
60 }
61 };
62
63 static ex sqrt_reader(const exvector& ev) {
64 return GiNaC::sqrt(ev[0]);
65 }
66
67 static ex pow_reader(const exvector& ev) {
68 return GiNaC::pow(ev[0], ev[1]);
69 }
70
71 static ex power_reader(const exvector& ev) {
72 return GiNaC::power(ev[0], ev[1]);
73 }
74
75 static ex lst_reader(const exvector& ev) {
76 return GiNaC::lst(ev.begin(), ev.end());
77 }
78
79 const prototype_table& ginac_reader() {
80 using std::make_pair;
81 static bool initialized = false;
82 static prototype_table reader;
83 if (!initialized) {
84 reader.insert({{"sqrt", 1}, reader_func(sqrt_reader)});
85 reader.insert({{"pow", 2}, reader_func(pow_reader)});
86 reader.insert({{"power", 2}, reader_func(power_reader)});
87 reader.insert({{"lst", 0}, reader_func(lst_reader)});
88 enum { log, exp, sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, atan2,
89 Li2, Li3, zetaderiv, Li, S, H, lgamma, tgamma, beta, factorial, binomial, Order,
90 NFUNCTIONS
91 };
92 auto it = functions_hack::get_registered_functions().begin();
93 unsigned serial = 0;
94 for ( ; serial<NFUNCTIONS; ++it, ++serial ) {
95 reader.insert({{it->get_name(), it->get_nparams()}, reader_func(serial)});
96 }
97 initialized = true;
98 }
99 return reader;
100 }
101 }
102
103 // note iSymbol will override Symbol if they share same name
104 Parser::Parser() : STable(Symbol::Table), FTable(ginac_reader()) {
105 for(auto item : iSymbol::Table) STable[item.first] = item.second;
106 }
107 Parser::Parser(symtab st) : STable(Symbol::Table), FTable(ginac_reader()) {
108 for(auto item : iSymbol::Table) STable[item.first] = item.second;
109 for(auto item : st) STable[item.first] = item.second;
110 }
111 ex Parser::Read(const string & in_str, bool s2S) {
112 string instr(in_str);
113 string_replace_all(instr, "==", "=");
114 unsigned serial = 0;
115 for (auto & it : functions_hack::get_registered_functions()) {
116 FTable.insert({{it.get_name(), it.get_nparams()}, reader_func(serial)});
117 ++serial;
118 }
119 parser ginac_parser(STable, false, FTable);
120 ex ret = ginac_parser(instr);
121 if(!s2S) return ret;
122 // check & replace symbol to Symbol object
123 auto st = ginac_parser.get_syms();
124 bool redo = false;
125 exmap repl;
126 for(auto kv : st) {
127 if(is_exactly_a<symbol>(kv.second) && STable.find(kv.first)==STable.end()) {
128 string ss = kv.first;
129 STable[ss] = Symbol(ss);
130 redo = true;
131 repl[kv.second] = Symbol(ss);
132 }
133 }
134 if(redo) ret = ret.subs(repl,subs_options::no_pattern);
135 //ReShare(ret);
136 return ret;
137 }
138 ex Parser::ReadFile(string filename, bool s2S) {
139 ifstream ifs(filename);
140 string ostr((istreambuf_iterator<char>(ifs)), (istreambuf_iterator<char>()));
141 ifs.close();
142 return Read(ostr,s2S);
143 }
144
145 /*-----------------------------------------------------*/
146 // string Functions
147 /*-----------------------------------------------------*/
148 void string_replace_all(string &str, const string &from, const string &to) {
149 size_t start_pos = 0;
150 while((start_pos = str.find(from, start_pos)) != string::npos) {
151 str.replace(start_pos, from.length(), to);
152 start_pos += to.length();
153 }
154 }
155
156 void string_trim(string &ostr) {
157 const char* WhiteSpace = " \t\v\r\n";
158 if(!ostr.empty()) {
159 ostr.erase(0, ostr.find_first_not_of(WhiteSpace));
160 ostr.erase(ostr.find_last_not_of(WhiteSpace)+1);
161 }
162 }
163
164 bool string_start_with(const string &fstr, const string & sstr) {
165 if (fstr.length() >= sstr.length()) {
166 return (0 == fstr.compare (0, sstr.length(), sstr));
167 } else {
168 return false;
169 }
170 }
171
172 bool string_end_with(const string &fstr, const string & estr) {
173 if (fstr.length() >= estr.length()) {
174 return (0 == fstr.compare (fstr.length() - estr.length(), estr.length(), estr));
175 } else {
176 return false;
177 }
178 }
179
180 bool string_contain(const string &fstr, const string & mstr) {
181 return (fstr.find(mstr) != std::string::npos);
182 }
183
184 void Combinations(int n, int m, std::function<void(const int*)> f) {
185 if(m<1 || m>n) return;
186 std::string bitmask(m,1);
187 bitmask.resize(n,0);
188 do {
189 int is[m]; int j=0;
190 for (int i=0; i<n; ++i) {
191 if(bitmask[i]) { is[j]=i; j++; }
192 }
193 f(is);
194 } while (std::prev_permutation(bitmask.begin(), bitmask.end()));
195 }
196
197 void CombinationsR(int n, int m, std::function<void(const int*)> f) {
198 if(m<1) return;
199 Combinations(n+m-1, n-1, [n,m,f](const int* isr) {
200 int is[m];
201 int mi=m;
202 for(int ni=0; ni<n; ni++) {
203 int c=0;
204 if(ni==n-1) c=(n+m-2)-isr[n-2];
205 else if(ni==0) c=isr[ni];
206 else c=isr[ni]-isr[ni-1]-1;
207 for(int j=0; j<c; j++) is[--mi] = n-1-ni;
208 }
209 f(is);
210 });
211 }
212
213 void Permutations(int n, std::function<void(const int*)> f) {
214 if(n<1) return;
215 int pis[n];
216 for(int i=0; i<n; i++) pis[i]=i;
217 do { f(pis); } while(std::next_permutation(pis,pis+n));
218 }
219
220 void Permutations(int n, int m, std::function<void(const int*)> f) {
221 if(m<1 || m>n) return;
222 Combinations(n, m, [m,f](const int *ns1) {
223 Permutations(m, [m,f,ns1](const int *ns2) {
224 int ns[m];
225 for(int i=0; i<m; i++) ns[i] = ns1[ns2[i]];
226 f(ns);
227 });
228 });
229 }
230
231 namespace {
232 struct Generator {
233 public:
234 int *a;
235
236 Generator(int s, int v) : cSlots(s) , cValues(v) {
237 a = new int[s];
238 for(int i = 0; i<cSlots-1; i++) a[i]=1-1;
239 a[cSlots-1]=0-1;
240 nextInd = cSlots;
241 }
242
243 ~Generator() { delete [] a; }
244
245 bool doNext() {
246 for (;;) {
247 if (a[nextInd-1]==cValues-1) {
248 nextInd--;
249 if(nextInd==0) return false;
250 } else {
251 a[nextInd-1]++;
252 while (nextInd<cSlots) {
253 nextInd++;
254 a[nextInd-1]=1-1;
255 }
256 return true;
257 }
258 }
259 }
260
261 private:
262 int cSlots;
263 int cValues;
264 int nextInd;
265 };
266 }
267
268 void PermutationsR(int n, int m, std::function<void(const int*)> f) {
269 Generator g(m,n);
270 while (g.doNext()) f(g.a);
271 }
272
273 bool isSorted(const lst & exs) {
274 for(int i=0; i<exs.nops()-1; i++) {
275 if(exs.op(i).is_equal(exs.op(i+1))) continue;
276 if(!ex_less(exs.op(i),exs.op(i+1))) return false;
277 }
278 return true;
279 }
280
281 bool isSorted(int n, const ex exs[]) {
282 for(int i=0; i<n-1; i++) {
283 if(exs[i].is_equal(exs[i+1])) continue;
284 if(!ex_less(exs[i],exs[i+1])) return false;
285 }
286 return true;
287 }
288
289 int ACSort(lst & exs) {
290 int ac = 0;
291 int n = exs.nops();
292 for(int i=0; i<n-1; i++)
293 for(int j=n-1; j>i; j--)
294 if(ex_less(exs.op(j),exs.op(j-1))) {
295 auto tmp = exs.op(j-1);
296 exs.let_op(j-1) = exs.op(j);
297 exs.let_op(j) = tmp;
298 ac++;
299 }
300 for(int i=0; i<n-1; i++) if(exs.op(i).is_equal(exs.op(i+1))) return 0;
301 return (ac%2==1) ? -1 : 1;
302 }
303
304 int ACSort(int n, ex exs[]) {
305 int ac = 0;
306 for(int i=0; i<n-1; i++)
307 for(int j=n-1; j>i; j--)
308 if(ex_less(exs[j],exs[j-1])) {
309 auto tmp = exs[j-1];
310 exs[j-1] = exs[j];
311 exs[j] = tmp;
312 ac++;
313 }
314 for(int i=0; i<n-1; i++) if(exs[i].is_equal(exs[i+1])) return 0;
315 return (ac%2==1) ? -1 : 1;
316 }
317
318}
319
int * a
Basic header file.
class to wrap map_function of GiNaC
Definition BASIC.h:632
static ex subs(const ex &expr, const ex &pat, std::function< ex(const ex &)> f)
Definition Functions.cpp:42
ex operator()(const ex &e)
Definition Functions.cpp:38
MapFunction(std::function< ex(const ex &, MapFunction &)>)
Definition Functions.cpp:37
prototype_table FTable
Definition BASIC.h:647
ex ReadFile(string filename, bool s2S=true)
symtab STable
Definition BASIC.h:648
ex Read(const string &instr, bool s2S=true)
class extended to GiNaC symbol class, represent a positive symbol
Definition BASIC.h:113
static std::map< std::string, ex > Table
Definition BASIC.h:230
do_not_evalf_params().expl_derivative_func(zd2D).derivative_func(zp2D)) REGISTER_FUNCTION(CT
HepLib namespace.
Definition BASIC.cpp:17
bool ex_less(const ex &a, const ex &b)
Definition Sort.cpp:10
bool string_contain(const string &fstr, const string &mstr)
void Combinations(int n, int m, std::function< void(const int *)> f)
bool isSorted(const lst &exs)
bool string_start_with(const string &fstr, const string &sstr)
int ACSort(lst &exs)
void CombinationsR(int n, int m, std::function< void(const int *)> f)
void Permutations(int n, std::function< void(const int *)> f)
void string_replace_all(string &str, const string &from, const string &to)
bool string_end_with(const string &fstr, const string &estr)
REGISTER_FUNCTION(GMat, do_not_evalf_params().print_func< FCFormat >(&GMat_fc_print).conjugate_func(mat_conj).set_return_type(return_types::commutative)) bool IsZero(const ex &e)
Definition Basic.cpp:1002
void PermutationsR(int n, int m, std::function< void(const int *)> f)
void string_trim(string &str)