(ex, sort=True)
simplify products ``G(i)*p(-i)*G(j)*p(-j) -> p(i)*p(-i)``

        Examples
        ========

        >>> from sympy.physics.hep.gamma_matrices import GammaMatrix as G
        >>> from sympy.tensor.tensor import tensor_indices, tensorhead
        >>> p, q = tensorhead('p, q', [G.LorentzIndex], [[1]])
        >>> i0,i1,i2,i3,i4,i5 = tensor_indices('i0:6', G.LorentzIndex)
        >>> ps = p(i0)*G(-i0)
        >>> qs = q(i0)*G(-i0)
        >>> G.simplify_gpgp(ps*qs*qs)
        gamma(-L_0, auto_left, -auto_right)*p(L_0)*q(L_1)*q(-L_1)
simplify products ``G(i)*p(-i)*G(j)*p(-j) -> p(i)*p(-i)``
simplify products G ( i ) * p ( - i ) * G ( j ) * p ( - j ) - > p ( i ) * p ( - i )
def simplify_gpgp(ex, sort=True):
        """
        simplify products ``G(i)*p(-i)*G(j)*p(-j) -> p(i)*p(-i)``

        Examples
        ========

        >>> from sympy.physics.hep.gamma_matrices import GammaMatrix as G
        >>> from sympy.tensor.tensor import tensor_indices, tensorhead
        >>> p, q = tensorhead('p, q', [G.LorentzIndex], [[1]])
        >>> i0,i1,i2,i3,i4,i5 = tensor_indices('i0:6', G.LorentzIndex)
        >>> ps = p(i0)*G(-i0)
        >>> qs = q(i0)*G(-i0)
        >>> G.simplify_gpgp(ps*qs*qs)
        gamma(-L_0, auto_left, -auto_right)*p(L_0)*q(L_1)*q(-L_1)
        """
        def _simplify_gpgp(ex):
            tids = ex._tids
            components = tids.components
            a = []
            for i in range(len(components)):
                if not isinstance(components[i], GammaMatrixHead):
                    continue
                dum = tids.dum
                for dx in dum:
                    if dx[2] == i:
                        p_pos1 = dx[3]
                    elif dx[3] == i:
                        p_pos1 = dx[2]
                    else:
                        continue
                    comp1 = components[p_pos1]
                    if comp1.comm == 0 and comp1.rank == 1:
                        a.append((i, p_pos1))
            if not a:
                return ex
            elim = set()
            tv = []
            hit = True
            coeff = S.One
            ta = None
            while hit:
                hit = False
                for i, ai in enumerate(a[:-1]):
                    if ai[0] in elim:
                        continue
                    if ai[0] != a[i + 1][0] - 1:
                        continue
                    if components[ai[1]] != components[a[i + 1][1]]:
                        continue
                    elim.add(ai[0])
                    elim.add(ai[1])
                    elim.add(a[i + 1][0])
                    elim.add(a[i + 1][1])
                    if not ta:
                        ta = ex.split()
                        mu = TensorIndex('mu', GammaMatrix.LorentzIndex)
                    ind1 = ta[ai[0]].args[-1][1]
                    ind2 = ta[ai[0] + 1].args[-1][2]
                    hit = True
                    if i == 0:
                        coeff = ex.coeff
                    tx = components[ai[1]](mu)*components[ai[1]](-mu)
                    tv.append(tx*DiracSpinorIndex.delta(ind1, ind2))
                    break

            if tv:
                a = [x for j, x in enumerate(ta) if j not in elim]
                a.extend(tv)
                t = tensor_mul(*a)*coeff
                t = t.contract_metric(DiracSpinorIndex.delta)
                return t
            else:
                return ex

        if sort:
            ex = ex.sorted_components()
        while 1:
            t = _simplify_gpgp(ex)
            if t != ex:
                ex = t
            else:
                return t
def simplify_gpgp ( ex , sort = True ) : def _simplify_gpgp ( ex ) : tids = ex . _tids components = tids . components a = [ ] for i in range ( len ( components ) ) : if not isinstance ( components [ i ] , GammaMatrixHead ) : continue dum = tids . dum for dx in dum : if dx [ 2 ] == i : p_pos1 = dx [ 3 ] elif dx [ 3 ] == i : p_pos1 = dx [ 2 ] else : continue comp1 = components [ p_pos1 ] if comp1 . comm == 0 and comp1 . rank == 1 : a . append ( ( i , p_pos1 ) ) if not a : return ex elim = set ( ) tv = [ ] hit = True coeff = S . One ta = None while hit : hit = False for i , ai in enumerate ( a [ : - 1 ] ) : if ai [ 0 ] in elim : continue if ai [ 0 ] != a [ i + 1 ] [ 0 ] - 1 : continue if components [ ai [ 1 ] ] != components [ a [ i + 1 ] [ 1 ] ] : continue elim . add ( ai [ 0 ] ) elim . add ( ai [ 1 ] ) elim . add ( a [ i + 1 ] [ 0 ] ) elim . add ( a [ i + 1 ] [ 1 ] ) if not ta : ta = ex . split ( ) mu = TensorIndex ( 'mu' , GammaMatrix . LorentzIndex ) ind1 = ta [ ai [ 0 ] ] . args [ - 1 ] [ 1 ] ind2 = ta [ ai [ 0 ] + 1 ] . args [ - 1 ] [ 2 ] hit = True if i == 0 : coeff = ex . coeff tx = components [ ai [ 1 ] ] ( mu ) * components [ ai [ 1 ] ] ( - mu ) tv . append ( tx * DiracSpinorIndex . delta ( ind1 , ind2 ) ) break if tv : a = [ x for j , x in enumerate ( ta ) if j not in elim ] a . extend ( tv ) t = tensor_mul ( * a ) * coeff t = t . contract_metric ( DiracSpinorIndex . delta ) return t else : return ex if sort : ex = ex . sorted_components ( ) while 1 : t = _simplify_gpgp ( ex ) if t != ex : ex = t else : return t