SetAttributes[SP,Orderless];

SP /:  SP[p_, {mu_}] SP[q_, {mu_}] := SP[p, q];




  (*
  myTr[GammaMatrix[IndexLorentz[mu_]],GammaMatrix[IndexLorentz[nu_]] ]:=
  4 SP[IndexLorentz[mu],IndexLorentz[nu]]
   *)

myTr[a_,b_] := 4 SP[a,b];


myTr[x__] := Module[{lista,ultima,menouno,menodue,coefficienti,nuovetracce,
                     risultato, i},
		    lista = {x};
		    ultima = lista[[-1]];
		    menouno = Delete[lista,-1];
		    menodue = Table[ Delete[ menouno, i], {i,1,Length[menouno]}];
		    coefficienti = Table[ (-1)^(i+1) SP[ultima,menouno[[i]] ],
                                          {i,1,Length[menouno]}
                                        ];
		    nuovetracce = Map[ Apply[ myTr, # ] &, menodue];

                    risultato = coefficienti.nuovetracce;
                    risultato

  ] /; FreeQ[{x},G5] ;




myEps[a___,b_,c___,b_,d___] := 0
myEps /:  myEps[a___,{mu_},b___] SP[p_,{mu_}] := myEps[a,p,b];

myEps[x1_,x2_,x3_,x4_] := Signature[{x1,x2,x3,x4}] * 
                          myEps[ Apply[ Sequence, Sort[{x1,x2,x3,x4}] ] ] /; 
  {x1,x2,x3,x4} =!= Sort[{x1,x2,x3,x4}]


myTr[G5,G5] = 4;
myTr[x_,G5] = 0;
myTr[x_,y_,G5] = 0;
myTr[x_,y_,z_,G5] = 0;


myTr[a___,G5,b___,G5,c___] := (-1)^Length[{b}]*myTr[a,b,c] /; FreeQ[{b},G5] ;

myTr[a___,G5,b__] := (-1)^Length[{b}]*myTr[a,b,G5] /; FreeQ[{b},G5] ;

myTr[x__,G5] := myTr[x,{a1},{a2},{a3},{a4}] I/24 myEps[{a1},{a2},{a3},{a4}] /;
(Length[{x}]>3 && FreeQ[{x},G5]);
