7 #include "ginac/parse_context.h"
16 alignas(2)
static ex SP_reader(
const exvector& ev) {
17 return SP(ev[0], ev[1]).subs(
SP_map);
20 alignas(2)
static ex LC_reader(
const exvector& ev) {
21 if(ev.size()==4)
return (-I)*
LC(ev[0], ev[1], ev[2], ev[3]);
25 alignas(2)
static ex SUNT_reader(
const exvector& ev) {
27 if(n==3)
return SUNT(ev[0], ev[1], ev[2]);
30 for(
int i=0; i<n-2; i++)
as.append(ev[i]);
31 return SUNT(
as,ev[n-2],ev[n-1]);
32 }
else throw Error(
"SUNT_reader: number of arguments less than 3.");
35 alignas(2)
static ex TTR_reader(
const exvector& ev) {
36 if(ev.size()==1)
return TTR(ev[0]);
38 for(
auto item : ev)
as.append(item);
42 alignas(2)
static ex DGamma_reader(
const exvector& ev) {
44 if(n==1)
return GAS(ev[0]);
47 for(
int i=1; i<n; i++) ret = ret *
GAS(ev[i],rl);
51 alignas(2)
static ex gi_reader(
const exvector& ev) {
53 if(n==0)
return GAS(1);
54 else if(n==1)
return GAS(1,
ex2int(ev[0]));
55 else throw Error(
"DGamma_reader: number of arguments more than 2.");
58 alignas(2)
static ex Matrix_reader(
const exvector& ev) {
59 return Matrix(ev[0],ev[1],ev[2]);
68 struct mapGamma :
public map_function {
70 ex operator()(
const ex &e) {
71 if(is_a<DGamma>(e))
return DGamma(ex_to<DGamma>(e), gline);
73 else return e.map(*
this);
75 mapGamma(
int _gline) : gline(_gline) { };
80 struct mapTR :
public map_function {
82 ex operator()(
const ex &e) {
86 trs = mapGamma(gline)(trs);
87 trs = DGamma(1, gline) * trs;
90 if(
glmax>128)
throw Error(
"too large index with glmax>128.");
93 }
else if(is_a<add>(e)) {
96 unsigned glmax = gline;
104 }
else return e.map(*
this);
111 string init_script = R
"EOF(
112 CFunction pow,sqrt,gamma,HF,Matrix,WF;
113 Tensor TTR(cyclic), f(antisymmetric), T, f4, colTp;
114 Symbols reX,I2R,NF,NA,d,I,Pi;
115 AutoDeclare Symbols gCF, trcN;
117 AutoDeclare Index colA;
119 AutoDeclare Index colF;
125 id,once,TTR(?a) = T(?a,colF1,colF1);
128 id,once,T(colA1?,colA2?,?a,colF1?,colF2?) = T(colA1,colF1,colF3)*T(colA2,?a,colF3,colF2);
130 id,once,f4(colA1?,colA2?,colA3?,colA4?) = f(colA1,colA2,colA5) * f(colA5,colA3,colA4);
136 if ( count(f,1) || match(T(colA1?,colF1?,colF2?)*T(colA1?,colF3?,colF4?)) ) redefine colXi "0";
137 id,once,f(colA1?,colA2?,colA3?) = 1/I2R/i_*T(colA1,colF1,colF2)*T(colA2,colF2,colF3)*T(colA3,colF3,colF1)-1/I2R/i_*T(colA3,colF1,colF2)*T(colA2,colF2,colF3)*T(colA1,colF3,colF1);
138 sum colF1,colF2,colF3;
139 id T(colA1?,colF1?,colF2?)*T(colA1?,colF3?,colF4?) = colTp(colF1,colF2,colF3,colF4);
141 if ( count(colTp,1) ) redefine colXj "0";
143 id,once,colTp(colF1?,colF2?,colF3?,colF4?) = I2R*(d_(colF1,colF4)*d_(colF2,colF3)-d_(colF1,colF2)*d_(colF3,colF4)/NF);
148 id T(colA1?,?a,colF1?,colF2?)*T(colA2?,?b,colF2?,colF3?) = T(colA1,?a,colA2,?b,colF1,colF3);
150 id T(?a,colF1?,colF1?) = TTR(?a);
152 id TTR(colA1?,colA2?) = I2R*d_(colA1,colA2);
164 ex runform(
const ex &expr_in,
int verb) {
165 static map<pid_t, Form> form_map;
167 if((form_map.find(pid)==form_map.end())) {
169 ss << init_script << endl;
170 form_map[pid].Init(
"form");
171 form_map[pid].Execute(ss.str());
173 Form &fprc = form_map[pid];
175 ex expr = expr_in.subs(
SP_map);
180 ids <<
"id " << kv.first <<
"=" << kv.second <<
";" << endl;
181 all_expr += iWF(kv.first) * iWF(kv.second);
183 string idstr = sss.str();
185 lst vec_lst, VD_lst, CF_lst, CA_lst, sym_lst;
186 for(const_preorder_iterator i = all_expr.preorder_begin(); i != all_expr.preorder_end(); ++i) {
187 if(is_a<Vector>(*i)) vec_lst.append(*i);
188 else if(is_a<Index>(*i)) {
189 if(ex_to<Index>(*i).type==Index::Type::VD) VD_lst.append(*i);
192 }
else if(is_a<symbol>(*i)) sym_lst.append(*i);
193 else if(is_a<GiNaC::function>(*i)) {
194 static vector<string> fun_vec = {
195 "iWF",
"WF",
"TR",
"sin",
"cos",
"HF",
"TTR",
"Matrix"
197 auto func = ex_to<GiNaC::function>(*i).get_name();
199 for(
auto fi : fun_vec) {
200 if(fi==func) { ok =
true;
break; }
203 cout << (*i) << endl;
204 throw Error(
"runform: Functions not defined in FORM: "+func);
208 vec_lst.sort(); vec_lst.unique();
209 VD_lst.sort(); VD_lst.unique();
210 CF_lst.sort(); CF_lst.unique();
211 CA_lst.sort(); CA_lst.unique();
213 sym_lst.sort(); sym_lst.unique();
217 ff <<
".store" << endl;
219 if(sym_lst.nops()>0) {
221 for(
auto sx : sym_lst) {
222 auto s = ex_to<symbol>(sx);
223 ff <<
" " << s.get_name();
224 st[s.get_name()] = sx;
228 if(vec_lst.nops()>0) {
230 for(
auto vx : vec_lst) {
231 auto v = ex_to<Vector>(vx);
233 st[v.name.get_name()] = v;
234 for(
auto vvx : vec_lst) {
235 auto vv = ex_to<Vector>(vvx);
236 st[v.name.get_name()+
"__"+vv.name.get_name()] =
SP(v,vv).subs(
SP_map);
241 if(VD_lst.nops()>0) {
243 else ff <<
"Dimension d;" << endl;
245 for(
auto ix : VD_lst) {
246 auto i = ex_to<Index>(ix);
248 st[i.name.get_name()] = i;
254 if(CF_lst.nops()>0) {
255 ff <<
"Dimension NF;" << endl;
257 for(
auto ix : CF_lst) {
258 auto i = ex_to<Index>(ix);
260 st[i.name.get_name()] = i;
266 if(CA_lst.nops()>0) {
267 ff <<
"Dimension NA;" << endl;
269 for(
auto ix : CA_lst) {
270 auto i = ex_to<Index>(ix);
272 st[i.name.get_name()] = i;
278 ff <<
".global" << endl;
282 bool islst = is_a<lst>(expr);
284 if(islst) expr_lst = ex_to<lst>(expr);
285 else expr_lst.append(expr);
286 auto total = expr_lst.nops();
293 map<ex,int,ex_is_less> e2i_map;
294 for(
auto it : expr_lst) {
295 ff <<
".store" << endl;
297 item = MapFunction([](
const ex & e, MapFunction &
self)->ex{
298 if(!e.has(TR(
w)))
return e;
299 else if(e.match(TR(
w))) {
300 ex nd = e.op(0).normal().numer_denom();
301 return TR(nd.op(0))/nd.op(1);
302 }
else return e.map(
self);
306 item = collect_common_factors(item);
308 auto ckey = item.op(0);
309 if(!is_a<numeric>(ckey)) {
311 if(e2i_map.find(ckey)==e2i_map.end()) {
315 }
else ckid = e2i_map[ckey];
316 string gCF =
"gCF" + to_string(ckid);
317 st[gCF] = item.op(0);
318 item = Symbol(gCF) * item.op(1);
320 item = ckey * item.op(1);
327 for(
int i=0; i<cv_lst.nops(); i++) {
328 auto it = cv_lst.op(i);
331 color_vec.push_back(vv);
332 item += cc * Symbol(
"[cl"+to_string(i)+
"]");
336 for(
int i=0; i<color_vec.size(); i++) {
337 coss <<
" [cl" << i <<
"]";
338 ff <<
"L [cl" << i <<
"]=" << color_vec[i] <<
";" << endl;
339 ff <<
".sort" << endl;
340 ff <<
"#call SUNTrace" << endl;
341 ff <<
".sort" << endl;
343 ff <<
"id NF^reX?=3^reX;" << endl;
344 ff <<
"id I2R=1/2;" << endl;
345 ff <<
"id NA^reX?=8^reX;" << endl;
346 ff <<
".sort" << endl;
354 find(item,TR(
w),trs);
362 ff <<
"L [o]=" << item <<
";" << endl;
363 ff <<
".sort" << endl;
364 ff <<
"#do i = 1,1" << endl;
365 ff <<
"id once HF(reX?)=reX;" << endl;
367 ff <<
"if(count(HF,1)>0) redefine i \"0\";" << endl;
368 ff <<
".sort" << endl;
369 ff <<
"#enddo" << endl;
370 ff <<
"contract 0;" << endl;
371 ff <<
".sort" << endl;
372 ff << idstr <<
".sort" << endl;
374 for(
int gl=1; gl<=tr.glmax; gl++) {
376 ff <<
".sort" << endl;
378 else ff <<
"Dimension d;" << endl;
379 ff <<
"Indices [g5_i1], [g5_i2], [g5_i3], [g5_i4];" << endl;
380 ff <<
"id g_(" << gl <<
",5_) = e_([g5_i1],[g5_i2],[g5_i3],[g5_i4])*g_(" << gl <<
",[g5_i1],[g5_i2],[g5_i3],[g5_i4])/24;" << endl;
381 ff <<
"sum [g5_i1],[g5_i2],[g5_i3],[g5_i4];" << endl;
382 ff <<
".sort" << endl;
385 else ff <<
"tracen " << gl <<
";" << endl;
386 ff <<
".sort" << endl;
387 ff << idstr <<
".sort" << endl;
394 tr2v[tr] = Symbol(
"[tr"+to_string(trn)+
"]");
395 auto tri = mapGamma(gid)(tr.op(0));
396 trvec.push_back(tri);
399 item = item.subs(tr2v);
400 for(
int i=0; i<trvec.size(); i++) {
401 ff <<
"L [tr" << i <<
"]=" << trvec[i] <<
";" << endl;
403 ff <<
".sort" << endl;
405 else ff <<
"Dimension d;" << endl;
406 ff <<
"Indices [g5_i1], [g5_i2], [g5_i3], [g5_i4];" << endl;
407 ff <<
"id g_(" << gid <<
",5_) = e_([g5_i1],[g5_i2],[g5_i3],[g5_i4])*g_(" << gid <<
",[g5_i1],[g5_i2],[g5_i3],[g5_i4])/24;" << endl;
408 ff <<
"sum [g5_i1],[g5_i2],[g5_i3],[g5_i4];" << endl;
411 else ff <<
"tracen " << gid <<
";" << endl;
412 ff <<
".sort" << endl;
413 ff << idstr <<
".sort" << endl;
415 ff <<
"L [o]=" << item <<
";" << endl;
416 ff <<
".sort" << endl;
417 ff <<
"#do i = 1,1" << endl;
418 ff <<
"id once HF(reX?)=reX;" << endl;
420 ff <<
"if(count(HF,1)>0) redefine i \"0\";" << endl;
421 ff <<
".sort" << endl;
422 ff <<
"#enddo" << endl;
423 ff << idstr <<
".sort" << endl;
430 tr2v[tr] = Symbol(
"trcN"+to_string(trn));
431 auto tri = mapGamma(gid)(tr.op(0));
432 trvec.push_back(tri);
435 item = item.subs(tr2v);
437 if(color_vec.size()>0) ff <<
"drop" << coss.str() <<
";" << endl;
438 ff <<
"L [o]=" << item <<
";" << endl;
439 ff <<
".sort" << endl;
440 ff <<
"#do i = 1,1" << endl;
441 ff <<
"id once HF(reX?)=reX;" << endl;
443 ff <<
"if(count(HF,1)>0) redefine i \"0\";" << endl;
444 ff <<
".sort" << endl;
445 ff <<
"#enddo" << endl;
448 for(
int i=0; i<trvec.size(); i++) {
449 ff <<
"L [tr" << i <<
"]=" << trvec[i] <<
";" << endl;
451 ff <<
".sort" << endl;
453 else ff <<
"Dimension d;" << endl;
454 ff <<
"Indices [g5_i1], [g5_i2], [g5_i3], [g5_i4];" << endl;
455 ff <<
"id g_(" << gid <<
",5_) = e_([g5_i1],[g5_i2],[g5_i3],[g5_i4])*g_(" << gid <<
",[g5_i1],[g5_i2],[g5_i3],[g5_i4])/24;" << endl;
456 ff <<
"sum [g5_i1],[g5_i2],[g5_i3],[g5_i4];" << endl;
459 else ff <<
"tracen " << gid <<
";" << endl;
460 ff <<
".sort" << endl;
461 ff << idstr <<
".sort" << endl;
462 ff <<
"drop [tr" << i <<
"];" << endl;
463 ff <<
"id trcN" << i <<
"=[tr" << i <<
"];" << endl;
464 ff <<
".sort" << endl;
465 ff << idstr <<
".sort" << endl;
469 throw Error(
"runform: unsupported form_trace_mode = " + to_string(
form_trace_mode));
472 ff <<
"contract 0;" << endl;
473 ff <<
".sort" << endl;
474 ff << idstr <<
".sort" << endl;
478 cout <<
PRE <<
"\\--Form Script @ " << c <<
" / " << total << flush;
480 cout <<
"--------------------------------------" << endl;
481 cout <<
"Form Script @ " << c <<
" / " << total << endl;
482 cout <<
"--------------------------------------" << endl;
483 cout << ss.str() << endl;
486 auto script = ss.str();
490 auto otmp = fprc.Execute(script);
492 cout <<
"--------------------------------------" << endl;
493 cout <<
"Form Output @ " << c <<
" / " << total << endl;
494 cout <<
"--------------------------------------" << endl;
495 cout << otmp << endl;
502 if(c<total) ostr +=
",";
504 }
catch(Error& err) {
509 if(verb==1) cout << endl;
516 for(
auto v : vec_lst) {
517 string pat(ex_to<Vector>(v).name.get_name());
518 string from = pat+
"(";
519 string to =
"d_("+pat+
",";
544 fp.FTable.insert({{
"SP", 2}, reader_func(SP_reader)});
545 fp.FTable.insert({{
"LC", 4}, reader_func(LC_reader)});
546 fp.FTable.insert({{
"Matrix", 3}, reader_func(Matrix_reader)});
547 for(
int i=1; i<30; i++) fp.FTable.insert({{
"T", i}, reader_func(SUNT_reader)});
548 for(
int i=1; i<30; i++) fp.FTable.insert({{
"TTRX", i}, reader_func(TTR_reader)});
549 for(
int i=1; i<30; i++) fp.FTable.insert({{
"DG", i}, reader_func(DGamma_reader)});
550 fp.FTable.insert({{
"GI", 0}, reader_func(gi_reader)});
551 fp.FTable.insert({{
"GI", 1}, reader_func(gi_reader)});
552 ex ret = fp.Read(ostr);
553 if(!islst) ret = ret.op(0);
563 ex
form(
const ex & iexpr,
int verb) {
565 if(!is_a<lst>(expr)) {
567 if(!e.has(TR(
w)))
return e;
568 else if(e.match(TR(
w))) {
569 if(is_a<mul>(e.op(0))) {
571 for(auto const & ei : e.op(0)) {
572 if(DGamma::has(ei)) v *= ei;
577 }
else return e.map(
self);
585 for(
auto cv : cv_lst) to_lst.append(cv.op(0)*cv.op(1));
586 lst out_lst = ex_to<lst>(runform(to_lst, verb));
589 for(
int i=0; i<cv_lst.nops(); i++) ret += out_lst.op(i);
594 return e.has(TR(w)) || Index::hasc(e);
597 for(
auto cv : cv_lst) to_lst.append(cv.op(0)*cv.op(1));
598 lst out_lst = ex_to<lst>(runform(to_lst, verb));
601 for(
int i=0; i<cv_lst.nops(); i++) ret += out_lst.op(i);
606 return e.has(TR(w)) || Index::hasv(e);
609 for(
auto cv : cv_lst) to_lst.append(cv.op(0)*cv.op(1));
610 lst out_lst = ex_to<lst>(runform(to_lst, verb));
613 for(
int i=0; i<cv_lst.nops(); i++) ret += out_lst.op(i);
618 return e.has(TR(w)) || SUNT::has(e) || SUNF::has(e) || SUNF4::has(e) || Index::has(e) || DGamma::has(e);
621 for(
auto cv : cv_lst) to_lst.append(cv.op(1));
622 lst out_lst = ex_to<lst>(runform(to_lst, verb));
625 for(
int i=0; i<cv_lst.nops(); i++) ret += cv_lst.op(i).op(0) * out_lst.op(i);
628 }
else throw Error(
"form: unsupported form_expand_mode = " + to_string(
form_expand_mode));
638 if(expr.has(Matrix(
w1,
w2,
w3)))
throw Error(
"charge_conjugate: Matrix found.");
639 if(!DGamma::has(expr))
return expr;
640 if(is_a<DGamma>(expr)) {
641 DGamma g = ex_to<DGamma>(expr);
642 if(is_a<Vector>(g.
pi) || is_a<Index>(g.
pi))
return -expr;
643 else if(is_zero(g.
pi-1) || is_zero(g.
pi-5))
return expr;
646 if(is_a<add>(expr)) {
650 }
else if(is_a<mul>(expr)) {
654 }
else if(is_a<ncmul>(expr)) {
660 cout << DGamma::has(expr) <<
" : " << expr << endl;
661 throw Error(
"charge_conjugate: unexpected region.");
class for Dirac Gamma object
static bool has(const ex &e)
static bool has(const ex &e)
class used to wrap error message
static bool hasc(const ex &e)
static GiNaC::exmap Dimension
static bool has(const ex &e)
class to wrap map_function of GiNaC
lst CoPat(const ex &e, std::function< bool(const ex &)> f)
const int form_expand_none
ex GAS(const ex &expr, unsigned rl)
function similar to GAD/GSD in FeynClac
const int form_trace_each_all
const int form_expand_all
ex charge_conjugate(const ex &expr)
make the charge conjugate operaton, M -> C^{-1} . M^T . C w.r.t. a Matrix object
lst collect_lst(ex const &expr_in, std::function< bool(const ex &)> has_func, int opt)
the collect function like Mathematica, reture the lst { {c1,v1}, {c2,v2}, ... }
void string_replace_all(string &str, const string &from, const string &to)
const int form_trace_each_each
const int form_trace_auto
ex LC(ex pi1, ex pi2, ex pi3, ex pi4)
function similar to LCD in FeynCalc
ex form(const ex &iexpr, int verb)
evalulate expr in form program, see also the form_trace_mode and form_expand_mode
ex SP(const ex &a, bool use_map=false)
int ex2int(ex num)
ex to integer