HepLib
Loading...
Searching...
No Matches
Basic.cpp
Go to the documentation of this file.
1
6#include "HEP.h"
7#include "flint/ulong_extras.h"
8
9namespace HepLib {
10
11 DEFAULT_CTOR(Index)
12 IMPLEMENT_HAS(Index)
13 IMPLEMENT_ALL(Index)
14
15 DEFAULT_CTOR(Vector)
16 IMPLEMENT_HAS(Vector)
17 IMPLEMENT_ALL(Vector)
18
19 DEFAULT_CTOR(SUNT)
20 IMPLEMENT_HAS(SUNT)
21 IMPLEMENT_ALL(SUNT)
22
23 DEFAULT_CTOR(SUNF)
24 IMPLEMENT_HAS(SUNF)
25 IMPLEMENT_ALL(SUNF)
26
27 DEFAULT_CTOR(SUNF4)
28 IMPLEMENT_HAS(SUNF4)
29 IMPLEMENT_ALL(SUNF4)
30
31 //-----------------------------------------------------------
32 // FormFormat Output
33 //-----------------------------------------------------------
34 FormFormat::FormFormat(ostream &os, unsigned opt) : print_dflt(os, opt) {}
35 FormFormat::FormFormat() : print_dflt(std::cout) {}
36 GINAC_IMPLEMENT_PRINT_CONTEXT(FormFormat, print_dflt)
37
38 const FormFormat & FormFormat::operator << (const basic & v) const {
39 v.print(*this);
40 return *this;
41 }
42 const FormFormat & FormFormat::operator << (const ex & v) const {
43 v.print(*this);
44 return *this;
45 }
46 const FormFormat & FormFormat::operator << (const lst & v) const {
47 v.print(*this);
48 return *this;
49 }
50 const FormFormat & FormFormat::operator<<(std::ostream& (*v)(std::ostream&)) const {
51 s << v;
52 return *this;
53 }
54
55 void FormFormat::power_print(const power & p, const FormFormat & c, unsigned level) {
56 if(p.op(1)==2 && !DGamma::has(p)) {
57 c << "((" << p.op(0) << ")*(" << p.op(0) << "))";
58 } else {
59 c << "((" << p.op(0) << ")^(" << p.op(1) << "))";
60 }
61 }
62
63 //-----------------------------------------------------------
64 // FCFormat Output
65 //-----------------------------------------------------------
66 FCFormat::FCFormat(ostream &os, unsigned opt) : print_dflt(os, opt) {}
67 FCFormat::FCFormat() : print_dflt(std::cout) {}
68 GINAC_IMPLEMENT_PRINT_CONTEXT(FCFormat, print_dflt)
69
70 const FCFormat & FCFormat::operator << (const basic & v) const {
71 v.print(*this);
72 return *this;
73 }
74 const FCFormat & FCFormat::operator << (const ex & v) const {
75 v.print(*this);
76 return *this;
77 }
78 const FCFormat & FCFormat::operator << (const lst & v) const {
79 v.print(*this);
80 return *this;
81 }
82 const FCFormat & FCFormat::operator<<(std::ostream& (*v)(std::ostream&)) const {
83 s << v;
84 return *this;
85 }
86
87 const FCFormat & FCFormat::operator << (const matrix & mat) const {
88 s << "{";
89 int nr = mat.rows();
90 int nc = mat.cols();
91 for(int r=0; r<nr; r++) {
92 s << "{";
93 for(int c=0; c<nc; c++) {
94 mat(r,c).print(*this);
95 if(c+1!=nc) s << ",";
96 }
97 s << "}";
98 if(r+1!=nr) s << ",";
99 }
100 s << "}";
101 return *this;
102 }
103
104 const FCFormat & FCFormat::operator << (const exvector & e) const {
105 auto i = e.begin();
106 auto vend = e.end();
107 if (i==vend) { s << "{}"; return *this; }
108 s << "{";
109 while (true) {
110 i->print(*this);
111 ++i;
112 if(i==vend) break;
113 s << ",";
114 }
115 s << "}";
116 return *this;
117 }
118
119 const FCFormat & FCFormat::operator << (const exset & e) const {
120 auto i = e.begin();
121 auto send = e.end();
122 if (i==send) { s << "{}"; return *this; }
123 s << "{";
124 while (true) {
125 i->print(*this);
126 ++i;
127 if(i==send) break;
128 s << ",";
129 }
130 s << "}";
131 return *this;
132 }
133
134 const FCFormat & FCFormat::operator << (const exmap & e) const {
135 auto i = e.begin();
136 auto mend = e.end();
137 if (i==mend) { s << "{}"; return *this; }
138 s << "{";
139 while (true) {
140 i->first.print(*this);
141 s << "->";
142 i->second.print(*this);
143 ++i;
144 if(i==mend) break;
145 s << ",";
146 }
147 s << "}";
148 return *this;
149 }
150
151 namespace {
152 class ncmul_hack : public ncmul { // due to printseq is protected
153 public:
154 ncmul_hack(ncmul _nm) : ncmul(_nm){ }
155 void print(const FCFormat & c, unsigned level) {
156 printseq(c, '(', '.', ')', precedence(), level);
157 }
158 };
159 ex mat_conj(const ex & e1, const ex & e2, const ex & e3) {
160 return GMat(e1.conjugate(), e3, e2);
161 }
162 }
163 void FCFormat::ncmul_print(const ncmul & nm, const FCFormat & c, unsigned level) {
164 ncmul_hack(nm).print(c, level);
165 }
166
167 //-----------------------------------------------------------
168 // Index Class
169 //-----------------------------------------------------------
170 //GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(Index, basic,print_func<print_context>(&Index::print))
171 GiNaC::registered_class_info & Index::get_class_info_static() { return reg_info; }
173 Index * Index::duplicate() const { Index * bp = new Index(*this); bp->setflag(GiNaC::status_flags::dynallocated); return bp; }
174 void Index::accept(GiNaC::visitor & v) const { if (visitor *p = dynamic_cast<visitor *>(&v)) p->visit(*this); else inherited::accept(v); }
175 const GiNaC::registered_class_info &Index::get_class_info() const { return get_class_info_static(); }
176 GiNaC::registered_class_info &Index::get_class_info() { return get_class_info_static(); }
177 const char *Index::class_name() const { return get_class_info_static().options.get_name(); }
178 //GINAC_IMPLEMENT_REGISTERED_CLASS END
179
180 Index::Index(const string &s, const Type t) : name(s), type(t) { }
181 int Index::compare_same_type(const basic &other) const {
182 if(!is_a<Index>(other)) throw Error("Index::compare_same_type");
183 const Index &o = static_cast<const Index &>(other);
184 auto ret = name.get_name().compare(o.name.get_name());
185 if(ret==0) return 0;
186 else if(ret<0) return -1;
187 else return 1;
188 }
189
190 bool Index::is_equal_same_type(const basic & other) const {
191 if(!is_a<Index>(other)) throw Error("Index::is_equal_same_type");
192 const Index &o = static_cast<const Index &>(other);
193 return (name.get_name() == o.name.get_name());
194 }
195
196 void Index::print(const print_context &c, unsigned level) const {
197 c.s << name;
198 }
199
201 return Pair(*this, i);
202 }
203
205 return Pair(p, *this);
206 }
207
208 void Index::archive(archive_node & n) const {
209 inherited::archive(n);
210 n.add_string("name", name.get_name());
211 n.add_unsigned("type", type);
212 }
213
214 void Index::read_archive(const archive_node& n) {
215 inherited::read_archive(n);
216 string nstr;
217 unsigned t;
218 n.find_string("name", nstr);
219 name = Symbol(nstr);
220 n.find_unsigned("type", t);
221 type = (Type)t;
222 }
223
224 ex Index::derivative(const symbol & s) const {
225 return 0;
226 }
227
228 bool Index::hasc(const ex & e) {
229 for(const_preorder_iterator i = e.preorder_begin(); i != e.preorder_end(); ++i)
230 if(is_a<Index>(*i) && ex_to<Index>(*i).type!=Index::Type::VD) return true;
231 return false;
232 }
233
234 bool Index::hasv(const ex & e) {
235 for(const_preorder_iterator i = e.preorder_begin(); i != e.preorder_end(); ++i)
236 if(is_a<Index>(*i) && ex_to<Index>(*i).type==Index::Type::VD) return true;
237 return false;
238 }
239
240 //-----------------------------------------------------------
241 // Vector Class
242 //-----------------------------------------------------------
243 //GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(Vector, basic,print_func<print_context>(&Vector::print))
244 GiNaC::registered_class_info & Vector::get_class_info_static() { return reg_info; }
246 Vector * Vector::duplicate() const { Vector * bp = new Vector(*this); bp->setflag(GiNaC::status_flags::dynallocated); return bp; }
247 void Vector::accept(GiNaC::visitor & v) const { if (visitor *p = dynamic_cast<visitor *>(&v)) p->visit(*this); else inherited::accept(v); }
248 const GiNaC::registered_class_info &Vector::get_class_info() const { return get_class_info_static(); }
249 GiNaC::registered_class_info &Vector::get_class_info() { return get_class_info_static(); }
250 const char *Vector::class_name() const { return get_class_info_static().options.get_name(); }
251 //GINAC_IMPLEMENT_REGISTERED_CLASS END
252
253 Vector::Vector(const string &s) : name(s) { }
254 int Vector::compare_same_type(const basic &other) const {
255 if(!is_a<Vector>(other)) throw Error("Vector::compare_same_type");
256 const Vector &o = static_cast<const Vector &>(other);
257 auto ret = name.get_name().compare(o.name.get_name());
258 if(ret==0) return 0;
259 else if(ret<0) return -1;
260 else return 1;
261 }
262
263 bool Vector::is_equal_same_type(const basic & other) const {
264 if(!is_a<Vector>(other)) throw Error("Vector::is_equal_same_type");
265 const Vector &o = static_cast<const Vector &>(other);
266 return (name.get_name() == o.name.get_name());
267 }
268
269 void Vector::print(const print_context &c, unsigned level) const {
270 c.s << name;
271 }
272
274 return Pair(*this, p);
275 }
276
278 return Pair(*this, i);
279 }
280
281 void Vector::archive(archive_node & n) const {
282 inherited::archive(n);
283 n.add_string("name", name.get_name());
284 }
285
286 void Vector::read_archive(const archive_node& n) {
287 inherited::read_archive(n);
288 string nstr;
289 unsigned t;
290 n.find_string("name", nstr);
291 name = Symbol(nstr);
292 }
293
294 ex Vector::derivative(const symbol & s) const {
295 return 0;
296 }
297
298 //GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(SUNT, basic,print_func<print_dflt>(&SUNT::print).print_func<FormFormat>(&SUNT::form_print).print_func<FCFormat>(&SUNT::fc_print))
299 GiNaC::registered_class_info & SUNT::get_class_info_static() { return reg_info; }
301 SUNT * SUNT::duplicate() const { SUNT * bp = new SUNT(*this); bp->setflag(GiNaC::status_flags::dynallocated); return bp; }
302 void SUNT::accept(GiNaC::visitor & v) const { if (visitor *p = dynamic_cast<visitor *>(&v)) p->visit(*this); else inherited::accept(v); }
303 const GiNaC::registered_class_info &SUNT::get_class_info() const { return get_class_info_static(); }
304 GiNaC::registered_class_info &SUNT::get_class_info() { return get_class_info_static(); }
305 const char *SUNT::class_name() const { return get_class_info_static().options.get_name(); }
306 //GINAC_IMPLEMENT_REGISTERED_CLASS END
307
308 SUNT::SUNT(ex a, ex i, ex j) : aij{a,i,j} { }
309 int SUNT::compare_same_type(const basic &other) const {
310 if(!is_a<SUNT>(other)) throw Error("SUNT::compare_same_type");
311 const SUNT &o = static_cast<const SUNT &>(other);
312 for(int i=0; i<3; i++) {
313 auto c = aij[i].compare(o.aij[i]);
314 if(c!=0) return c;
315 }
316 return 0;
317 }
318
319 bool SUNT::is_equal_same_type(const basic & other) const {
320 if(!is_a<SUNT>(other)) throw Error("SUNT::is_equal_same_type");
321 const SUNT &o = static_cast<const SUNT &>(other);
322 for(int i=0; i<3; i++) {
323 if(!aij[i].is_equal(o.aij[i])) return false;
324 }
325 return true;
326 }
327
328 void SUNT::form_print(const FormFormat &c, unsigned level) const {
329 if(is_a<lst>(aij[0])) {
330 bool first = true;
331 for(auto item : aij[0]) {
332 if(first) { first=false; c << "T(" << item; }
333 else c << "," << item;
334 }
335 } else c << "T(" << aij[0];
336 c << "," << aij[1] << "," << aij[2] << ")";
337 }
338
339 void SUNT::fc_print(const FCFormat &c, unsigned level) const {
340 c << "SUNTF[" << aij[0] << "," << aij[1] << "," << aij[2] << "]";
341 }
342
343 void SUNT::print(const print_dflt &c, unsigned level) const {
344 c.s << "T(" << aij[0] << "," << aij[1] << "," << aij[2] << ")";
345 }
346
347 size_t SUNT::nops() const { return 3; }
348 ex SUNT::op(size_t i) const {
349 return aij[i];
350 }
351 ex & SUNT::let_op(size_t i) {
352 ensure_if_modifiable();
353 return aij[i];
354 }
355
356 void SUNT::archive(archive_node & n) const {
357 inherited::archive(n);
358 n.add_ex("a", aij[0]);
359 n.add_ex("i", aij[1]);
360 n.add_ex("j", aij[2]);
361 }
362
363 void SUNT::read_archive(const archive_node& n) {
364 inherited::read_archive(n);
365 ex o;
366 n.find_ex("a", o);
367 aij[0] = ex_to<Index>(o);
368 n.find_ex("i", o);
369 aij[1] = ex_to<Index>(o);
370 n.find_ex("j", o);
371 aij[2] = ex_to<Index>(o);
372 }
373
374 ex SUNT::derivative(const symbol & s) const {
375 return 0;
376 }
377
378 ex SUNT::conjugate() const {
379 if(!is_a<lst>(aij[0])) return SUNT(aij[0], aij[2], aij[1]);
380 lst argv = ex_to<lst>(aij[0]);
381 lst as;
382 for(auto it=argv.rbegin(); it!=argv.rend(); ++it) as.append(*it);
383 return SUNT(as, aij[2], aij[1]);
384 }
385
386 //GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(SUNF, basic,print_func<print_dflt>(&SUNF::print).print_func<FormFormat>(&SUNF::form_print).print_func<FCFormat>(&SUNF::fc_print))
387 GiNaC::registered_class_info & SUNF::get_class_info_static() { return reg_info; }
389 SUNF * SUNF::duplicate() const { SUNF * bp = new SUNF(*this); bp->setflag(GiNaC::status_flags::dynallocated); return bp; }
390 void SUNF::accept(GiNaC::visitor & v) const { if (visitor *p = dynamic_cast<visitor *>(&v)) p->visit(*this); else inherited::accept(v); }
391 const GiNaC::registered_class_info &SUNF::get_class_info() const { return get_class_info_static(); }
392 GiNaC::registered_class_info &SUNF::get_class_info() { return get_class_info_static(); }
393 const char *SUNF::class_name() const { return get_class_info_static().options.get_name(); }
394 //GINAC_IMPLEMENT_REGISTERED_CLASS END
395
396 SUNF::SUNF(ex i, ex j, ex k) : ijk{i,j,k} { }
397 int SUNF::compare_same_type(const basic &other) const {
398 if(!is_a<SUNF>(other)) throw Error("SUNF::compare_same_type");
399 const SUNF &o = static_cast<const SUNF &>(other);
400 for(int i=0; i<3; i++) {
401 auto c = ijk[i].compare(o.ijk[i]);
402 if(c!=0) return c;
403 }
404 return 0;
405 }
406
407 bool SUNF::is_equal_same_type(const basic & other) const {
408 if(!is_a<SUNF>(other)) throw Error("SUNF::is_equal_same_type");
409 const SUNF &o = static_cast<const SUNF &>(other);
410 for(int i=0; i<3; i++) {
411 if(!ijk[i].is_equal(o.ijk[i])) return false;
412 }
413 return true;
414 }
415
416 ex SUNF::eval() const {
417 if(flags & status_flags::evaluated) return *this;
418 if(ijk[0].is_equal(ijk[1]) || ijk[1].is_equal(ijk[2]) || ijk[0].is_equal(ijk[2])) return 0;
419 bool c01 = ex_less(ijk[0],ijk[1]);
420 bool c12 = ex_less(ijk[1],ijk[2]);
421 if(c01 && c12) return this->hold();
422 bool c02 = ex_less(ijk[0],ijk[2]);
423 if(!c01 && c02) return -SUNF(ijk[1],ijk[0],ijk[2]);
424 else if(c02 && !c12) return -SUNF(ijk[0],ijk[2],ijk[1]);
425 else if(!c02 && c01) return SUNF(ijk[2],ijk[0],ijk[1]);
426 else if(c12 && !c02) return SUNF(ijk[1],ijk[2],ijk[0]);
427 else if(!c12 && !c01) return -SUNF(ijk[2],ijk[1],ijk[0]);
428 else return this->hold();
429 }
430
431 void SUNF::print(const print_dflt &c, unsigned) const {
432 c.s << "f(" << ijk[0] << "," << ijk[1] << "," << ijk[2] << ")";
433 }
434
435 void SUNF::form_print(const FormFormat &c, unsigned) const {
436 c << "f(" << ijk[0] << "," << ijk[1] << "," << ijk[2] << ")";
437 }
438
439 void SUNF::fc_print(const FCFormat &c, unsigned) const {
440 c << "SUNF[" << ijk[0] << "," << ijk[1] << "," << ijk[2] << "]";
441 }
442
443 size_t SUNF::nops() const { return 3; }
444 ex SUNF::op(size_t i) const {
445 return ijk[i];
446 }
447 ex & SUNF::let_op(size_t i) {
448 ensure_if_modifiable();
449 return ijk[i];
450 }
451
452 void SUNF::archive(archive_node & n) const {
453 inherited::archive(n);
454 n.add_ex("i", ijk[0]);
455 n.add_ex("j", ijk[1]);
456 n.add_ex("k", ijk[2]);
457 }
458
459 void SUNF::read_archive(const archive_node& n) {
460 inherited::read_archive(n);
461 ex o;
462 n.find_ex("i", o);
463 ijk[0] = ex_to<Index>(o);
464 n.find_ex("j", o);
465 ijk[1] = ex_to<Index>(o);
466 n.find_ex("k", o);
467 ijk[2] = ex_to<Index>(o);
468 }
469
475 ex SUNF::derivative(const symbol & s) const {
476 return 0;
477 }
478
479 //GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(SUNF4, basic,print_func<print_dflt>(&SUNF4::print).print_func<FormFormat>(&SUNF4::form_print).print_func<FCFormat>(&SUNF4::fc_print))
480 GiNaC::registered_class_info & SUNF4::get_class_info_static() { return reg_info; }
482 SUNF4 * SUNF4::duplicate() const { SUNF4 * bp = new SUNF4(*this); bp->setflag(GiNaC::status_flags::dynallocated); return bp; }
483 void SUNF4::accept(GiNaC::visitor & v) const { if (visitor *p = dynamic_cast<visitor *>(&v)) p->visit(*this); else inherited::accept(v); }
484 const GiNaC::registered_class_info &SUNF4::get_class_info() const { return get_class_info_static(); }
485 GiNaC::registered_class_info &SUNF4::get_class_info() { return get_class_info_static(); }
486 const char *SUNF4::class_name() const { return get_class_info_static().options.get_name(); }
487 //GINAC_IMPLEMENT_REGISTERED_CLASS END
488
489 SUNF4::SUNF4(ex i, ex j, ex k, ex l) : ijkl{i,j,k,l} { }
490 int SUNF4::compare_same_type(const basic &other) const {
491 if(!is_a<SUNF4>(other)) throw Error("SUNF4::compare_same_type");
492 const SUNF4 &o = static_cast<const SUNF4 &>(other);
493 for(int i=0; i<4; i++) {
494 auto c = ijkl[i].compare(o.ijkl[i]);
495 if(c!=0) return c;
496 }
497 return 0;
498 }
499
500 bool SUNF4::is_equal_same_type(const basic & other) const {
501 if(!is_a<SUNF4>(other)) throw Error("SUNF4::is_equal_same_type");
502 const SUNF4 &o = static_cast<const SUNF4 &>(other);
503 for(int i=0; i<4; i++) {
504 if(!ijkl[i].is_equal(o.ijkl[i])) return false;
505 }
506 return true;
507 }
508
513 ex SUNF4::eval() const {
514 if(flags & status_flags::evaluated) return *this;
515 if(ijkl[0].is_equal(ijkl[1]) || ijkl[2].is_equal(ijkl[3])) return 0;
516 bool c01 = ex_less(ijkl[0],ijkl[1]);
517 bool c23 = ex_less(ijkl[2],ijkl[3]);
518 if(c01 && c23) return this->hold(); // 01-23
519 else if(!c01 && c23) return -SUNF4(ijkl[1],ijkl[0],ijkl[2],ijkl[3]);
520 else if(!c01 && !c23) return SUNF4(ijkl[1],ijkl[0],ijkl[3],ijkl[2]);
521 else if(c01 && !c23) return -SUNF4(ijkl[0],ijkl[1],ijkl[3],ijkl[2]);
522 else return this->hold();
523 }
524
530 void SUNF4::print(const print_dflt &c, unsigned o) const {
531 c.s << "f(" << ijkl[0] << "," << ijkl[1] << "," << ijkl[2] << "," << ijkl[3] << ")";
532 }
533
534 void SUNF4::fc_print(const FCFormat &c, unsigned o) const {
535 c << "SUNF[" << ijkl[0] << "," << ijkl[1] << "," << ijkl[2] << "," << ijkl[3] << "]";
536 }
537
543 void SUNF4::form_print(const FormFormat &c, unsigned o) const {
544 c << "f4(" << ijkl[0] << "," << ijkl[1] << "," << ijkl[2] << "," << ijkl[3] << ")";
545 }
546
547 size_t SUNF4::nops() const { return 4; }
548 ex SUNF4::op(size_t i) const {
549 return ijkl[i];
550 }
551 ex & SUNF4::let_op(size_t i) {
552 ensure_if_modifiable();
553 return ijkl[i];
554 }
555
560 void SUNF4::archive(archive_node & n) const {
561 inherited::archive(n);
562 n.add_ex("i", ijkl[0]);
563 n.add_ex("j", ijkl[1]);
564 n.add_ex("k", ijkl[2]);
565 n.add_ex("l", ijkl[3]);
566 }
567
572 void SUNF4::read_archive(const archive_node& n) {
573 inherited::read_archive(n);
574 ex o;
575 n.find_ex("i", o);
576 ijkl[0] = ex_to<Index>(o);
577 n.find_ex("j", o);
578 ijkl[1] = ex_to<Index>(o);
579 n.find_ex("k", o);
580 ijkl[2] = ex_to<Index>(o);
581 n.find_ex("l", o);
582 ijkl[3] = ex_to<Index>(o);
583 }
584
590 ex SUNF4::derivative(const symbol & s) const {
591 return 0;
592 }
593
594 ex ncmul_expand(const ex & expr) {
595 MapFunction inner_expand([](const ex & e, MapFunction & self)->ex{
596 if(is_a<add>(e)) {
597 ex res = 0;
598 for(auto ei : e) res += ncmul_expand(ei);
599 return res;
600 } else if(is_a<mul>(e) || is_a<ncmul>(e)) {
601 lst res = lst{ 1 };
602 for(auto ei : e) {
603 ex rei = ncmul_expand(ei);
604 if(!is_a<add>(rei)) rei = lst{ rei };
605 lst ores = res;
606 res = lst{ };
607 for(auto oi : ores) for(auto ri : rei) res.append(oi * ri);
608 }
609 ex ret = 0;
610 for(auto ri : res) ret += ri;
611 return ret;
612 } else return e.map(self);
613 });
614 return inner_expand(expr);
615 }
616
623 ex GMatContract(const ex & expr_in, bool auto_tr) {
624 if(!expr_in.has(GMat(w1,w2,w3))) return expr_in;
625
626 auto expr = expr_in.subs(pow(GMat(w1,w2,w3),2)==GMat(w1,w2,w3)*GMat(w1,w3,w2));
627 expr = expr.subs(GMat(w1,w2,w2)==TR(w1));
628 auto cv_lst = collect_lst(expr, GMat(w1, w2, w3));
629 expr = 0;
630
631 for(auto cv : cv_lst) {
632 auto e = cv.op(1);
633 if(is_zero(e-1) || e.match(GMat(w1, w2, w3))) {
634 if(e.match(GMat(w1, w2, w2))) expr += cv.op(0) * TR(e.op(0));
635 else expr += cv.op(0) * e;
636 continue;
637 } else if(!is_a<mul>(e)) throw Error("GMatContract: collect error: " + ex2str(e));
638
639 lst mats;
640 for(auto item : e) mats.append(item);
641
642 std::map<ex,int,ex_is_less> to_map, from_map;
643 std::set<int> todo;
644 lst mats_idx;
645
646 start:
647 for(int i=0; i<mats.nops(); i++) {
648 auto item = mats.op(i);
649 if(item.op(0).return_type()==return_types::commutative || item.op(0).is_equal(GAS(1))) {
650 mats_idx.append(lst{item,i});
651 } else {
652 if(!item.match(GMat(w1,w2,w3))) {
653 cout << "item in GMatContract: " << item << endl;
654 throw Error("GMatContract faild!");
655 }
656 if(to_map[item.op(1)]!=0 || from_map[item.op(2)]!=0) {
657 if(!auto_tr) throw Error("GMatContract: index conflict for mats: "+ex2str(mats));
658 lst mats2; // to avoid dead-loop
659 mats2.append(GMatT(item));
660 for(int j=0; j<mats.nops(); j++) if(j!=i) mats2.append(mats.op(j));
661 mats = mats2;
662 to_map.clear();
663 from_map.clear();
664 mats_idx.remove_all();
665 goto start;
666 }
667 to_map[item.op(1)] = i+10; // avoid 0 in map
668 from_map[item.op(2)] = i+10; // avoid 0 in map
669 }
670 todo.insert(i);
671 }
672
673 // update to_map/from_map w.r.t mats_idx
674 bool checked = false;
675 while(true) {
676 lst mats_idx2;
677 bool ok = true; // double check
678 for(int i=0; i<mats_idx.nops(); i++) {
679 auto item = mats_idx.op(i).op(0);
680 int ii = ex_to<numeric>(mats_idx.op(i).op(1)).to_int();
681 if(!checked &&
682 to_map[item.op(1)]==0 && from_map[item.op(2)]==0 &&
683 to_map[item.op(2)]==0 && from_map[item.op(1)]==0) {
684 mats_idx2.append(mats_idx.op(i));
685 continue;
686 }
687 ok = false;
688 checked = false;
689 if(to_map[item.op(1)]==0 && from_map[item.op(2)]==0) {
690 to_map[item.op(1)] = ii+10; // avoid 0 in map
691 from_map[item.op(2)] = ii+10; // avoid 0 in map
692 } else if(to_map[item.op(2)]==0 && from_map[item.op(1)]==0) {
693 to_map[item.op(2)] = ii+10; // avoid 0 in map
694 from_map[item.op(1)] = ii+10; // avoid 0 in map
695 // need to swap the 2nd and 3rd index
696 auto li = get_op(mats, ii, 1);
697 auto ri = get_op(mats, ii, 2);
698 let_op(mats, ii, 1, ri);
699 let_op(mats, ii, 2, li);
700 } else {
701 throw Error("GMatContract: index conflict (2).");
702 }
703 }
704 if(mats_idx2.nops()<1) break;
705 mats_idx = mats_idx2;
706 if(ok) checked=true;
707 }
708
709 ex retMat = 1;
710 while(todo.size()>0) {
711 int c = *(todo.begin());
712 todo.erase(c);
713 ex curMat = mats.op(c).op(0);
714 auto li=mats.op(c).op(1);
715 auto ri=mats.op(c).op(2);
716 while(true) {
717 if(li.is_equal(ri)) {
718 retMat *= TR(curMat);
719 break;
720 }
721 int ti = to_map[ri];
722 int fi = from_map[li];
723 if(ti==0 && fi==0) {
724 retMat *= GMat(curMat, li, ri);
725 break;
726 }
727 if(ti!=0) {
728 auto mat = mats.op(ti-10).op(0);
729 if(curMat.is_equal(GAS(1))) curMat = mat;
730 else if(!mat.is_equal(GAS(1))) curMat = curMat * mat;
731 ri = mats.op(ti-10).op(2);
732 todo.erase(ti-10);
733 continue;
734 }
735 if(fi!=0) {
736 auto mat = mats.op(fi-10).op(0);
737 if(curMat.is_equal(GAS(1))) curMat = mat;
738 else if(!mat.is_equal(GAS(1))) curMat = mat * curMat;
739 li = mats.op(fi-10).op(1);
740 todo.erase(fi-10);
741 continue;
742 }
743 }
744 }
745 expr += cv.op(0) * retMat;
746 }
747
748 return expr;
749 }
750
751 ex Contract(const ex & ei) {
752 lst idx_lst;
753 MapFunction get_idx([&idx_lst](const ex & e, MapFunction & self)->ex{
754 if(!Index::has(e) || !Pair::has(e) || e.match(GMat(w1,w2,w3))) return 1; // skip GMat object
755 else if(is_a<Pair>(e)) {
756 if(is_a<Index>(e.op(0)) || is_a<Index>(e.op(1))) idx_lst.append(e);
757 return 1;
758 } else return e.map(self);
759 });
760 get_idx(ei);
761 idx_lst.sort();
762 idx_lst.unique();
763 if(idx_lst.nops()==0) return ei;
764 auto cvs = collect_lst(ei, idx_lst);
765 ex res = 0;
766 for(auto cv : cvs) {
767 auto c = cv.op(0);
768 auto v = form(cv.op(1)); // contract on itself
769 if(!Index::has(v)) res += c * v;
770 else {
771 if(!is_a<mul>(v)) v = lst{ v };
772 exmap repl;
773 ex r = 1; // uncontracted remained index
774 for(auto vi : v) {
775 if(!is_a<Pair>(vi)) r *= vi; // contract may result in a non-Pair object
776 else if(is_a<Index>(vi.op(1)) && c.has(vi.op(1))) repl[vi.op(1)] = vi.op(0);
777 else if(is_a<Index>(vi.op(0)) && c.has(vi.op(0))) repl[vi.op(0)] = vi.op(1);
778 else r *= vi;
779 }
780 res += r * c.subs(repl);
781 }
782 }
783 return res.subs(SP_map);
784 }
785
786 ex GMatOut(const ex & expr_in) {
787 MapFunction inner_out([&](const ex & e, MapFunction & self)->ex {
788 if(e.match(GMat(w1,w2,w3))) {
789 auto e0 = e.op(0);
790 if(is_a<mul>(e0)) {
791 ex c = 1, v = 1;
792 for(auto item : e0) {
793 if(item.return_type()==return_types::commutative) c *= item;
794 else {
795 if(!v.is_equal(1)) {
796 cout << "c=" << c << ", " << "v=" << v << endl;
797 throw Error("GMatOut: v != 1"); // make sure only one non-commutative object
798 }
799 v = item;
800 }
801 }
802 if(v.is_equal(1)) v = GAS(1);
803 return c * GMatOut(GMat(v, e.op(1), e.op(2)));
804 } else return e;
805 } else return e.map(self);
806 });
807 return inner_out(expr_in);
808 }
809
810 ex GMatExpand(const ex & expr_in) {
811 MapFunction inner_expand([&](const ex & e, MapFunction & self)->ex {
812 if(!e.has(GMat(w1,w2,w3))) return e;
813 else if(e.match(GMat(w1,w2,w3))) {
814 auto e0 = e.op(0);
815 if(is_a<add>(e0)) {
816 ex res = 0;
817 for(auto item : e0) res += GMatExpand(GMat(item, e.op(1), e.op(2)));
818 return res;
819 } else if(is_a<mul>(e0)) {
820 ex c = 1, v = 1;
821 for(auto item : e0) {
822 if(item.return_type()==return_types::commutative) c *= item;
823 else {
824 if(!v.is_equal(1)) {
825 cout << "c=" << c << ", " << "v=" << v << endl;
826 throw Error("GMatExpand: v != 1"); // make sure only one non-commutative object
827 }
828 v = item;
829 }
830 }
831 if(v.is_equal(1)) v = GAS(1);
832 return c * GMatExpand(GMat(v, e.op(1), e.op(2)));
833 } else if(is_a<ncmul>(e0)) { // expand ncmul
834 ex res;
835 bool first = true;
836 for(auto item : e0) {
837 if(first) {
838 res = item;
839 first = false;
840 continue;
841 }
842 ex ncL = res; // previous result
843 if(!is_a<add>(ncL)) ncL = lst{ ncL };
844 ex ncR = item;
845 if(!is_a<add>(ncR)) ncR = lst{ ncR };
846 res = 0; // current result
847 for(auto iL : ncL) for(auto iR : ncR) res += iL * iR;
848 }
849 ex rs = res;
850 res = 0;
851 if(!is_a<add>(rs)) rs = lst{ rs };
852 for(auto item : rs) { // pull out commutative coefficient
853 ex c = 1, v = 1;
854 if(is_a<mul>(item)) {
855 if(item.nops()==1) throw Error("GMatExpand: item.nops == 1"); // make sure
856 for(auto it : item) {
857 if(it.return_type()==return_types::commutative) c *= it;
858 else {
859 if(!v.is_equal(1)) throw Error("GMatExpand: v != 1"); // make sure only one non-commutative object
860 v = it;
861 }
862 }
863 } else v = item;
864
865 while(true) { // recursive replace ɣ.P * ɣ.P -> P^2 and ɣ.mu * ɣ.mu -> d @ v
866 bool to_exit = true;
867 if(is_a<ncmul>(v)) {
868 bool first = true;
869 ex last = 1, vv = 1;
870 for(auto vi : v) {
871 if(first) {
872 first = false;
873 last = vi;
874 } else {
875 if(last==vi && is_a<DGamma>(vi)) {
876 first = true;
877 last = 1;
878 if(is_a<Vector>(vi.op(0))) c *= SP(vi.op(0));
879 else if(is_a<Index>(vi.op(0))) c *= d;
880 else if(vi.op(0).is_equal(1) || vi.op(0).is_equal(5)) c *= 1; // GAS(1)*GAS(1) = GAS(5)*GAS(5) = 1
881 else throw Error("GMatExpand: only GAS(i/p/1/5) supported.");
882 to_exit = false; // need to cycle again
883 } else {
884 if(last!=GAS(1) && !last.is_equal(1)) vv = vv * last;
885 last = vi;
886 }
887 }
888 }
889 if(!last.is_equal(1) && last!=GAS(1)) v = vv * last; // check last item
890 else v = vv;
891 }
892 if(to_exit) break;
893 }
894 if(v.is_equal(1)) v = GAS(1); // identity matrix
895 res += c * GMat(v, e.op(1), e.op(2));
896 }
897 return res;
898 } else return e;
899 } else return e.map(self);
900 });
901 return inner_expand(expr_in);
902 }
903
904 ex GMatShift(const ex & expr, const ex & g, bool to_right) {
905 if(!expr.has(g)) return expr;
906 MapFunction inner_shift([g,to_right](const ex & e, MapFunction & self)->ex{
907 if(!e.has(g) || !e.has(GMat(w1,w2,w3))) return e;
908 else if(e.match(GMat(w1,w2,w3))) {
909 ex eg = e.op(0);
910 if(!is_a<ncmul>(eg)) eg = lst{ eg };
911 int gi = -1;
912 if(to_right) {
913 for(int i=0; i<eg.nops()-1; i++) if(eg.op(i)==g) { gi = i; break; }
914 if(gi==-1) return e;
915 } else {
916 for(int i=eg.nops()-1; i>0; i--) if(eg.op(i)==g) { gi = i; break; }
917 if(gi==-1) return e;
918 }
919 int gj = gi + ( to_right ? 1 : -1 );
920 ex rem = 1, rem2 = 1;
921 for(int i=0; i<eg.nops(); i++) {
922 if(i!=gi && i!=gj) {
923 rem *= eg.op(i);
924 rem2 *= eg.op(i);
925 }
926 if(i==gi) {
927 if(to_right) rem2 *= eg.op(gj)*eg.op(gi);
928 else rem2 *= eg.op(gi)*eg.op(gj);
929 }
930 }
931 if(eg.op(gi).is_equal(eg.op(gj))) {
932 ex ip = eg.op(gi).op(0);
933 if(rem.is_equal(1)) rem = GAS(1);
934 ex res = GMat(rem, e.op(1), e.op(2));
935 res = GMatShift(res, g, to_right);
936 ex c;
937 if(is_a<Vector>(ip)) c = SP(ip);
938 else if(is_a<Index>(ip)) c = d;
939 else if(eg.op(gi).is_equal(GAS(5))) c = 1;
940 else throw Error("GMatShift: only GAS(i/p/5) supproted.");
941 return c * res;
942 }
943 if(rem.is_equal(1)) rem = GAS(1);
944 if(rem2.is_equal(1)) rem2 = GAS(1);
945 ex res = 0;
946 if(!eg.op(gi).is_equal(GAS(5)) && !eg.op(gj).is_equal(GAS(5))) {
947 res = 2*SP(eg.op(gi).op(0), eg.op(gj).op(0)) * GMat(rem, e.op(1), e.op(2));
948 }
949 res = res - GMat(rem2, e.op(1), e.op(2));
950 return GMatShift(res, g, to_right);
951 } else return e.map(self);
952 });
953 ex res = GMatExpand(Contract(expr)); // add Contract & GMatExpand here
954 res = collect_ex(res, GMat(w1,w2,w3));
955 res = inner_shift(res);
956 return res;
957 }
958
959 ex GMatSimplify(const ex & expr) {
960 ex res = GMatContract(expr);
961 res = GMatShift(res);
962 res = Contract(res);
963 return res;
964 }
965
966 namespace {
967 // 1st and last should be equal
968 lst shift_1st_to_right(const ex & e) { // return a list of {coeff, gammas}
969 if(!is_a<ncmul>(e)) throw Error("input is not a ncmul.");
970 if(e.nops()==2) {
971 ex e0 = e.op(0);
972 if(e.op(0)!=e.op(1)) throw Error("shift_1st_to_right: the 2 items are not equal!");
973 else if(is_a<Index>(e0.op(0))) return lst{ lst{ d, 1 }};
974 else if(is_a<Vector>(e0.op(0))) return lst{ lst{ SP(e.op(0).op(0)), 1 }};
975 else if(e0.is_equal(GAS(5))) return lst{ lst{ 1, 1 }};
976 else {
977 cout << endl << e << endl;
978 throw Error("shift_1st_to_right: only GAS(i/p/5) supproted.");
979 }
980 }
981 ex rem = 1;
982 int n = e.nops();
983 for(int i=2; i<n; i++) rem *= e.op(i);
984 lst res = shift_1st_to_right(e.op(0)*rem);
985 n = res.nops();
986 for(int i=0; i<n; i++) {
987 res.let_op(i).let_op(0) = -res.op(i).op(0);
988 res.let_op(i).let_op(1) = e.op(1) * res.op(i).op(1);
989 }
990 if(!e.op(0).is_equal(GAS(5)) && !e.op(1).is_equal(GAS(5))) {
991 if(!is_a<Index>(e.op(0).op(0)) && !is_a<Vector>(e.op(0).op(0))) {
992 cout << e << endl;
993 throw Error("shift_12_right: not a Vector or Index");
994 }
995 if(!is_a<Index>(e.op(1).op(0)) && !is_a<Vector>(e.op(1).op(0))) {
996 cout << e << endl;
997 throw Error("shift_12_right: not a Vector or Index");
998 }
999 res.append(lst{ 2*SP(e.op(0).op(0), e.op(1).op(0)), rem });
1000 }
1001 return res;
1002 }
1003 }
1004 ex GMatShift(const ex & expr) {
1005 MapFunction inner_shift([](const ex & e, MapFunction & self)->ex{
1006 if(!e.has(GMat(w1,w2,w3))) return e;
1007 else if(e.match(GMat(w1,w2,w3))) {
1008 ex eg = e.op(0);
1009 if(!is_a<ncmul>(eg)) eg = lst{ eg };
1010
1011 int gi = -1, gj = -1;
1012 for(int i=0; i<eg.nops(); i++) for(int j=i+1; j<eg.nops(); j++) {
1013 if(eg.op(i).is_equal(eg.op(j))) {
1014 gi = i;
1015 gj = j;
1016 goto done;
1017 }
1018 }
1019 return e;
1020 done: ;
1021
1022 ex exL = 1, exM=1, exR = 1;
1023 for(int i=0; i<eg.nops(); i++) {
1024 if(i<gi) exL *= eg.op(i);
1025 else if(i>gj) exR *= eg.op(i);
1026 else exM *= eg.op(i);
1027 }
1028 lst cvs = shift_1st_to_right(exM);
1029
1030 ex res = 0;
1031 for(auto cv : cvs) {
1032 ex item = exL*cv.op(1)*exR;
1033 if(item.is_equal(1)) item = GAS(1);
1034 res += cv.op(0)*GMatShift(GMat(item, e.op(1), e.op(2)));
1035 }
1036
1037 return res;
1038 } else return e.map(self);
1039 });
1040 ex res = GMatExpand(Contract(expr)); // add Contract & GMatExpand here
1041 res = collect_ex(res, GMat(w1,w2,w3));
1042 res = inner_shift(res);
1043 return res;
1044 }
1045
1046 ex GMatECC(const ex & expr) {
1047 if(!expr.has(DGamma::C)) return expr;
1048 MapFunction inner_ecc([](const ex & e, MapFunction & self)->ex{
1049 if(!e.has(DGamma::C)) return e;
1050 else if(e.match(GMat(w1,w2,w3)) || e.match(TR(w))) {
1051 ex eg = e.op(0), cc = 1;
1052 if(is_a<mul>(e.op(0))) {
1053 eg = 1;
1054 for(auto item : e.op(0)) {
1055 if(item.return_type()==return_types::noncommutative) {
1056 if(eg.is_equal(1)) eg = item;
1057 else throw Error("GMatECC:: 2 more noncommutative objects found.");
1058 } else cc *= item;
1059 }
1060 if(eg.is_equal(1)) throw Error("GMatECC:: eg is 1, NOT expected.");
1061 }
1062 if(!is_a<ncmul>(eg)) eg = lst{ eg }; // only one item
1063 int ci = -1;
1064 for(int i=0; i<eg.nops(); i++) if(eg.op(i)==DGamma::C) { ci = i; break; }
1065 if(ci==-1) return e; // not found C
1066 int cj = -1;
1067 for(int i=ci+1; i<eg.nops(); i++) if(eg.op(i)==DGamma::C) { cj = i; break; }
1068 if(cj==-1) return e; // not found C
1069 int cnt = 0; // remaining C
1070 for(int i=cj+1; i<eg.nops(); i++) if(eg.op(i)==DGamma::C) { cnt++; }
1071 ex res = 1;
1072 for(int i=0; i<ci; i++) res *= eg.op(i);
1073 ex m = 1;
1074 for(int i=ci+1; i<cj; i++) m *= eg.op(i);
1075 cc *= -1; // C = -C^{-1}
1076 if(!m.is_equal(1)) res *= gamma_transpose(charge_conjugate(m));
1077 for(int i=cj+1; i<eg.nops(); i++) res *= eg.op(i);
1078 if(e.nops()==3) res = cc * GMat(res, e.op(1), e.op(2));
1079 else res = cc * TR(res);
1080 return cnt<2 ? res : self(res);
1081 } else return e.map(self);
1082 });
1083 ex res = collect_ex(expr, GMat(w1,w2,w3));
1084 res = inner_ecc(res);
1085 if(res.has(TR(w))) { // replace TR(e^T) = TR(e)
1086 res = MapFunction([](const ex & e, MapFunction & self)->ex{
1087 if(!e.has(TR(w))) return e;
1088 else if(e.match(TR(w))) {
1089 auto gs = DGamma::all(e.op(0));
1090 bool ok = true;
1091 for(auto item : gs) {
1092 auto gi = ex_to<DGamma>(item);
1093 if(!item.is_equal(DGamma::C) && !gi.isTr) {
1094 ok = false;
1095 break;
1096 }
1097 }
1098 if(ok) return TR(gamma_transpose(e.op(0)));
1099 else return e;
1100 } else return e.map(self);
1101 })(res);
1102 }
1103 return res;
1104 }
1105
1106 ex GMatT(const ex & expr) {
1107 MapFunction inner_transpose([](const ex & e, MapFunction & self)->ex{
1108 if(!e.has(GMat(w1,w2,w3))) return e;
1109 else if(e.match(GMat(w1,w2,w3))) {
1110 return GMat(gamma_transpose(e.op(0)), e.op(2), e.op(1));
1111 } else return e.map(self);
1112 });
1113 ex res = collect_ex(expr, GMat(w1,w2,w3));
1114 res = inner_transpose(res);
1115 return res;
1116 }
1117
1118 namespace {
1119 void GMat_fc_print(const ex &arg1, const ex &arg2, const ex &arg3, const print_context &c0) {
1120 auto c = static_cast<const FCFormat &>(c0);
1121 c << "GMat[" << arg1 << "," << arg2 << "," << arg3 << "]";
1122 }
1123 }
1124
1125 REGISTER_FUNCTION(GMat, do_not_evalf_params().print_func<FCFormat>(&GMat_fc_print).conjugate_func(mat_conj).set_return_type(return_types::commutative))
1126
1127 bool IsZero(const ex & e) {
1128 try {
1129 exset vs;
1130 for(const_preorder_iterator i = e.preorder_begin(); i != e.preorder_end(); ++i) {
1131 if(is_a<symbol>(*i) || is_a<Pair>(*i)) vs.insert(*i);
1132 }
1133
1134 int n = 13;
1135 for(int i=0; i<5; i++) {
1136 exmap nsubs;
1137 for(auto item : vs) {
1138 nsubs[item] = ex(1)/n_nth_prime(n);
1139 n++;
1140 }
1141 ex ret = e.subs(nsubs);
1142 if(!normal(e).is_zero()) return false;
1143 }
1144
1145 auto ret = exnormal(e);
1146 return ret.is_zero();
1147 } catch(...) { }
1148 return is_zero(exnormal(e));
1149 }
1150
1151 ex ToCF(const ex & e) {
1152 ex res = e;
1153 bool todo = true;
1154 while(todo) {
1155 todo = false;
1156 auto cvs = collect_lst(res, lst{NF,TF});
1157 res = 0;
1158 for(auto cv : cvs) {
1159 int degTF = cv.op(1).degree(TF);
1160 int degNF = cv.op(1).ldegree(NF);
1161 if(degTF>0 && degNF<0) {
1162 todo = true;
1163 int n = degTF;
1164 if(degTF+degNF>0) n = -degNF;
1165 res += cv.op(0) * cv.op(1) * pow(TF/NF,-n) * pow(TF*NF-CF,n);
1166 } else if(degTF>0 && degNF>1) {
1167 todo = true;
1168 int n = degTF;
1169 if(degTF>degNF/2) n = degNF/2;
1170 res += cv.op(0) * cv.op(1) * pow(TF*NF*NF,-n) * pow(CF*NF+TF,n);
1171 } else res += cv.op(0) * cv.op(1);
1172 }
1173 }
1174 return res;
1175 }
1176
1177 ex ca_neg_pow_sub(const ex & expr) {
1178 static MapFunction ca_map([](const ex & e, MapFunction & self)->ex {
1179 if(!e.has(CA)) return e;
1180 else if(e.match(pow(CA,w)) && e.op(1).info(info_flags::negint)) return pow(CA-2*CF,-e.op(1));
1181 else return e.map(self);
1182 });
1183 return ca_map(expr);
1184 }
1185
1186 ex ToCACF(const ex & e) { // from FeynCalc
1187 ex res = e.subs(lst{NA==(NF*NF-1),CF==(NF*NF-1)/(2*NF),TF==ex(1)/2});
1188 res = exfactor(res);
1189 // SUNN -> CA
1190 res = res.subs(NF==CA);
1191 // (-1+CA^2)->(-2 CA CF)
1192 res = res.subs(lst{ w*(1-CA)*(1+CA)==-w*2*CA*CF, w*(-1+CA)*(1+CA)==w*2*CA*CF });
1193 res = res.subs(lst{ w1*pow(1-CA,w2)*pow(1+CA,w2)==w1*pow(-2*CA*CF,w2), w1*pow(-1+CA,w2)*pow(1+CA,w2)==w1*pow(2*CA*CF,w2) });
1194 // (((2 - CA^2) CF )/CA ) ->(CF (CA - 4 CF))
1195 res = res.subs(lst{ w*(2-CA*CA)*CF/CA==w*CF*(CA-4*CF), w*(-2+CA*CA)*CF/CA==w*CF*(-CA+4*CF) });
1196 // (1-CA^2) -> (-2 CA CF)
1197 res = res.subs(lst{ w*(1-CA)*(1+CA)==-w*2*CA*CF, w*(-1+CA)*(1+CA)==w*2*CA*CF });
1198 res = res.subs(lst{ w1*pow(1-CA,w2)*pow(1+CA,w2)==-w1*pow(2*CA*CF,w2), w1*pow(-1+CA,w2)*pow(1+CA,w2)==w1*pow(2*CA*CF,w2) });
1199 // (1/CA)^n -> (CA - 2 CF)^n
1200 //res = res.subs(lst{ w/CA==w*(CA-2*CF) });
1201 res = ca_neg_pow_sub(res);
1202 // ((1 - CA^2)*(CA - 2*CF)) -> (-2*CF)
1203 res = res.subs(lst{ w*(1-CA)*(1+CA)*(CA-2*CF)==-2*w*CF, w*(-1+CA)*(1+CA)*(CA-2*CF)==2*w*CF });
1204 // (CA (CA-2 CF)) -> 1
1205 res = res.subs(lst{ w*CA*(CA-2*CF)==w, w*CA*(-CA+2*CF)==-w });
1206 res = res.subs(lst{ w1*pow(CA,w2)*pow(CA-2*CF,w2)==w1, w1*pow(CA,w2)*pow(-CA+2*CF,w2)==w*pow(-1,w2) });
1207 // (CA^2+c)(CA-2CF) -> CA+c(CA-2CF)
1208 res = res.subs(lst{ w0*(CA*CA+w1)*(CA-2*CF)==w0*(CA+w1*(CA-2*CF)), w0*(CA*CA+w1)*(-CA+2*CF)==-w0*(CA+w1*(CA-2*CF)) });
1209 res = res.subs(lst{ w0*(CA*CA+w1)*pow(CA-2*CF,w2)==w0*(CA+w1*(CA-2*CF))*pow(CA-2*CF,w2-1), w0*(CA*CA+w1)*pow(-CA+2*CF,w2)==-w0*(CA+w1*(CA-2*CF))*pow(-CA+2*CF,w2-1) });
1210 return res;
1211 }
1212
1213 ex HomCACF(const ex & e) {
1214 ex res = e.subs(lst{NA==(NF*NF-1),CA==NF,CF==(NF*NF-1)/(2*NF),TF==ex(1)/2});
1215 res = exfactor(res);
1216 if(!is_a<mul>(res)) res = lst{ res };
1217 ex c=1, v=1;
1218 for(auto item : res) {
1219 if(item.has(NF)) c *= item;
1220 else v *= item;
1221 }
1222 c = collect_ex(c, NF);
1223 int deg = c.degree(NF);
1224 int ldeg = -c.ldegree(NF);
1225 if(ldeg>deg) deg = ldeg;
1226 if(deg>0) {
1227 lst vars;
1228 ex eqn = c;
1229 for(int i=0; i<=deg; i++) {
1230 symbol xi;
1231 eqn -= xi * pow(NF,i) * pow((NF*NF-1)/(2*NF), deg-i);
1232 vars.append(xi);
1233 }
1234 eqn = collect_ex(eqn, NF);
1235 int nH = eqn.degree(NF);
1236 int nL = eqn.ldegree(NF);
1237 lst eqns;
1238 for(int i=nL; i<=nH; i++) {
1239 ex cc = eqn.coeff(NF, i);
1240 if(cc.is_zero()) continue;
1241 eqns.append(cc==0);
1242 }
1243 auto sol = lsolve(eqns, vars);
1244 if(sol.nops()!=deg+1) {
1245 cout << "c=" << c << endl;
1246 cout << "sol=" << sol << endl;
1247 throw Error("HomCACF: no solution found!");
1248 }
1249 c = 0;
1250 for(int i=0; i<=deg; i++) c += vars.op(i).subs(sol) * pow(CA,i) * pow(CF, deg-i);
1251 }
1252 return c * v;
1253 }
1254
1255 ex DoColor(const ex & expr, const ex & pref, int method) {
1256 auto cvs = collect_lst(expr, [](const ex &e)->bool{ return Index::hasc(e); });
1257 ex res = 0;
1258 for(auto const & cv : cvs) {
1259 auto cc = cv.op(0);
1260 auto vv = cv.op(1);
1261 if(method==0) vv = HomCACF(form(vv)/pref)*pref;
1262 else vv = ToCACF(form(vv)/pref)*pref;
1263 res += cc * vv;
1264 }
1265 return res;
1266 }
1267}
1268
int * a
#define IMPLEMENT_HAS(classname)
Definition BASIC.h:24
#define DEFAULT_CTOR(classname)
Definition BASIC.h:21
#define IMPLEMENT_ALL(classname)
Definition BASIC.h:30
HEP header file.
static lst all(const ex &e)
static ex C
Definition HEP.h:487
static bool has(const ex &e)
class used to wrap error message
Definition BASIC.h:242
class for FCFormat Output
Definition HEP.h:71
static void ncmul_print(const ncmul &p, const FCFormat &c, unsigned level=0)
Definition Basic.cpp:163
FCFormat(ostream &os, unsigned opt=0)
Definition Basic.cpp:66
const FCFormat & operator<<(const T &v) const
Definition HEP.h:77
class for FormFormat Output
Definition HEP.h:44
static void power_print(const power &p, const FormFormat &c, unsigned level=0)
Definition Basic.cpp:55
const FormFormat & operator<<(const T &v) const
Definition HEP.h:50
FormFormat(ostream &os, unsigned opt=0)
Definition Basic.cpp:34
class for index object
Definition HEP.h:104
int compare_same_type(const GiNaC::basic &other) const override
Definition Basic.cpp:181
const char * class_name() const override
Definition Basic.cpp:177
static bool hasv(const ex &e)
Definition Basic.cpp:234
void archive(archive_node &n) const override
Definition Basic.cpp:208
bool is_equal_same_type(const basic &other) const override
Definition Basic.cpp:190
static GiNaC::registered_class_info & get_class_info_static()
Definition Basic.cpp:171
Symbol name
Definition HEP.h:133
Type type
Definition HEP.h:134
void print(const print_context &c, unsigned level=0) const
Definition Basic.cpp:196
void read_archive(const archive_node &n) override
Definition Basic.cpp:214
static bool hasc(const ex &e)
Definition Basic.cpp:228
ex derivative(const symbol &s) const override
Definition Basic.cpp:224
const GiNaC::registered_class_info & get_class_info() const override
Definition Basic.cpp:175
Pair operator()(const Index &i)
Definition Basic.cpp:200
void accept(GiNaC::visitor &v) const override
Definition Basic.cpp:174
Index * duplicate() const override
Definition Basic.cpp:173
static bool has(const ex &e)
class to wrap map_function of GiNaC
Definition BASIC.h:632
class for Pair object
Definition HEP.h:322
static bool has(const ex &e)
class for SUNF4 object
Definition HEP.h:278
void archive(archive_node &n) const override
save to archvie
Definition Basic.cpp:560
static GiNaC::registered_class_info & get_class_info_static()
Definition Basic.cpp:480
void form_print(const FormFormat &c, unsigned level=0) const
print the Form Format
Definition Basic.cpp:543
void accept(GiNaC::visitor &v) const override
Definition Basic.cpp:483
const GiNaC::registered_class_info & get_class_info() const override
Definition Basic.cpp:484
size_t nops() const override
Definition Basic.cpp:547
void fc_print(const FCFormat &c, unsigned level=0) const
Definition Basic.cpp:534
void print(const print_dflt &c, unsigned level=0) const
normal priint
Definition Basic.cpp:530
ex op(size_t i) const override
Definition Basic.cpp:548
ex & let_op(size_t i) override
Definition Basic.cpp:551
SUNF4 * duplicate() const override
Definition Basic.cpp:482
ex ijkl[4]
Definition HEP.h:303
ex eval() const override
automatical evaluation of SUNF4
Definition Basic.cpp:513
ex derivative(const symbol &s) const override
set derivative of SUNF4 to 0
Definition Basic.cpp:590
bool is_equal_same_type(const basic &other) const override
Definition Basic.cpp:500
const char * class_name() const override
Definition Basic.cpp:486
void read_archive(const archive_node &n) override
read from archive
Definition Basic.cpp:572
int compare_same_type(const GiNaC::basic &other) const override
Definition Basic.cpp:490
class for SUNF object
Definition HEP.h:233
void archive(archive_node &n) const override
Definition Basic.cpp:452
void print(const print_dflt &c, unsigned level=0) const
Definition Basic.cpp:431
const char * class_name() const override
Definition Basic.cpp:393
int compare_same_type(const GiNaC::basic &other) const override
Definition Basic.cpp:397
ex derivative(const symbol &s) const override
set derivative of SUNF to 0
Definition Basic.cpp:475
bool is_equal_same_type(const basic &other) const override
Definition Basic.cpp:407
void form_print(const FormFormat &c, unsigned level=0) const
Definition Basic.cpp:435
static GiNaC::registered_class_info & get_class_info_static()
Definition Basic.cpp:387
ex & let_op(size_t i) override
Definition Basic.cpp:447
SUNF * duplicate() const override
Definition Basic.cpp:389
ex eval() const override
Definition Basic.cpp:416
const GiNaC::registered_class_info & get_class_info() const override
Definition Basic.cpp:391
void read_archive(const archive_node &n) override
Definition Basic.cpp:459
size_t nops() const override
Definition Basic.cpp:443
void accept(GiNaC::visitor &v) const override
Definition Basic.cpp:390
void fc_print(const FCFormat &c, unsigned level=0) const
Definition Basic.cpp:439
ex op(size_t i) const override
Definition Basic.cpp:444
ex ijk[3]
Definition HEP.h:258
class for SUNT object
Definition HEP.h:189
static GiNaC::registered_class_info & get_class_info_static()
Definition Basic.cpp:299
bool is_equal_same_type(const basic &other) const override
Definition Basic.cpp:319
ex & let_op(size_t i) override
Definition Basic.cpp:351
void archive(archive_node &n) const override
Definition Basic.cpp:356
size_t nops() const override
Definition Basic.cpp:347
void form_print(const FormFormat &c, unsigned level=0) const
Definition Basic.cpp:328
ex op(size_t i) const override
Definition Basic.cpp:348
const GiNaC::registered_class_info & get_class_info() const override
Definition Basic.cpp:303
void fc_print(const FCFormat &c, unsigned level=0) const
Definition Basic.cpp:339
void print(const print_dflt &c, unsigned level=0) const
Definition Basic.cpp:343
int compare_same_type(const GiNaC::basic &other) const override
Definition Basic.cpp:309
void read_archive(const archive_node &n) override
Definition Basic.cpp:363
SUNT * duplicate() const override
Definition Basic.cpp:301
const char * class_name() const override
Definition Basic.cpp:305
void accept(GiNaC::visitor &v) const override
Definition Basic.cpp:302
ex derivative(const symbol &s) const override
Definition Basic.cpp:374
ex aij[3]
Definition HEP.h:214
ex conjugate() const override
Definition Basic.cpp:378
class extended to GiNaC symbol class, represent a positive symbol
Definition BASIC.h:113
class for vector object
Definition HEP.h:149
Vector * duplicate() const override
Definition Basic.cpp:246
bool is_equal_same_type(const basic &other) const override
Definition Basic.cpp:263
Pair operator()(const Vector &p)
Definition Basic.cpp:273
const char * class_name() const override
Definition Basic.cpp:250
ex derivative(const symbol &s) const override
Definition Basic.cpp:294
void print(const print_context &c, unsigned level=0) const
Definition Basic.cpp:269
static GiNaC::registered_class_info & get_class_info_static()
Definition Basic.cpp:244
int compare_same_type(const GiNaC::basic &other) const override
Definition Basic.cpp:254
void read_archive(const archive_node &n) override
Definition Basic.cpp:286
void accept(GiNaC::visitor &v) const override
Definition Basic.cpp:247
void archive(archive_node &n) const override
Definition Basic.cpp:281
const GiNaC::registered_class_info & get_class_info() const override
Definition Basic.cpp:248
Symbol name
Definition HEP.h:176
HepLib namespace.
Definition BASIC.cpp:17
ex exfactor(const ex &expr_in, int opt)
factorize a expression
Definition BASIC.cpp:1854
const Symbol NA
bool ex_less(const ex &a, const ex &b)
Definition Sort.cpp:10
ex DoColor(const ex &expr, const ex &pref, int method)
Definition Basic.cpp:1255
void let_op(ex &ex_in, int index1, int index2, const ex item)
update index1-th.index2-th of expression with item
Definition BASIC.cpp:1560
ex HomCACF(const ex &e)
Definition Basic.cpp:1213
const Symbol gs
ex w0
Definition BASIC.h:499
const Symbol NF
ex GMatShift(const ex &expr, const ex &g, bool to_right)
Definition Basic.cpp:904
exmap SP_map
Definition Init.cpp:182
ex collect_ex(ex const &expr_in, std::function< bool(const ex &)> has_func, int opt)
the collect function like Mathematica
Definition BASIC.cpp:1202
ex GMatSimplify(const ex &expr)
Definition Basic.cpp:959
ex GAS(const ex &expr, unsigned rl)
function similar to GAD/GSD in FeynClac
Definition DGamma.cpp:281
ex GMatExpand(const ex &expr_in)
Definition Basic.cpp:810
ex GMatContract(const ex &expr_in, bool auto_tr)
make contract on matrix, i.e., GMat(a,i1,i2)*GMat(b,i2,i3) -> GMat(a*b,i1,i3)
Definition Basic.cpp:623
ex Contract(const ex &ei)
Definition Basic.cpp:751
ex w
Definition Init.cpp:93
const Symbol vs
const Symbol d
ex get_op(const ex ex_in, int index1, int index2)
return index1-th.index2-th of expression
Definition BASIC.cpp:1606
ex ToCACF(const ex &e)
Definition Basic.cpp:1186
ex GMatOut(const ex &expr_in)
Definition Basic.cpp:786
const Symbol CA
const Symbol TF
ex exnormal(const ex &expr, int opt)
normalize a expression
Definition BASIC.cpp:1916
ex gamma_transpose(const ex &expr)
make the transpose operaton M --> M^T
Definition Form.cpp:683
const Symbol CF
const Symbol as
ex charge_conjugate(const ex &expr)
make the charge conjugate operaton, M -> C^{-1} . M^T . C w.r.t. a GMat object
Definition Form.cpp:637
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}, ... }
Definition BASIC.cpp:1222
ex ToCF(const ex &e)
Definition Basic.cpp:1151
string ex2str(const ex &expr)
convert ex to output string, the defalut printer format will be used
Definition BASIC.cpp:715
bool IsZero(const ex &e)
const Symbol nL
const Symbol nH
ex w1
Definition BASIC.h:499
ex w3
Definition BASIC.h:499
ex ncmul_expand(const ex &expr)
Definition Basic.cpp:594
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:1125
ex w2
Definition BASIC.h:499
ex GMatECC(const ex &expr)
Definition Basic.cpp:1046
ex GMatT(const ex &expr)
Definition Basic.cpp:1106
ex form(const ex &iexpr, int verb)
evalulate expr in form program, see also the form_trace_mode and form_expand_mode
Definition Form.cpp:563
ex SP(const ex &a, bool use_map=false)
Definition Pair.cpp:166
ex ca_neg_pow_sub(const ex &expr)
Definition Basic.cpp:1177