def test_mul(): from sympy.abc import x Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') a, b, c, d = tensor_indices('a,b,c,d', Lorentz) sym = tensorsymmetry([1] * 2) t = TensMul.from_data(S.One, [], [], []) assert str(t) == '1' A, B = tensorhead('A B', [Lorentz] * 2, [[1] * 2]) t = (1 + x) * A(a, b) assert str(t) == '(x + 1)*A(a, b)' assert t.types == [Lorentz] assert t.rank == 2 assert t.dum == [] assert t.coeff == 1 + x assert sorted(t.free) == [(a, 0, 0), (b, 1, 0)] assert t.components == [A] t = A(-b, a) * B(-a, c) * A(-c, d) t1 = tensor_mul(*t.split()) assert t == t(-b, d) assert t == t1 assert tensor_mul(*[]) == TensMul.from_data(S.One, [], [], []) t = TensMul.from_data(1, [], [], []) zsym = tensorsymmetry() typ = TensorType([], zsym) C = typ('C') assert str(C()) == 'C' assert str(t) == '1' assert t.split()[0] == t raises(ValueError, lambda: TIDS.free_dum_from_indices(a, a)) raises(ValueError, lambda: TIDS.free_dum_from_indices(-a, -a)) raises(ValueError, lambda: A(a, b) * A(a, c)) t = A(a, b) * A(-a, c) raises(ValueError, lambda: t(a, b, c))
def test_mul(): from sympy.abc import x Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') a, b, c, d = tensor_indices('a,b,c,d', Lorentz) sym = tensorsymmetry([1]*2) t = TensMul.from_data(S.One, [], [], []) assert str(t) == '1' A, B = tensorhead('A B', [Lorentz]*2, [[1]*2]) t = (1 + x)*A(a, b) assert str(t) == '(x + 1)*A(a, b)' assert t.types == [Lorentz] assert t.rank == 2 assert t.dum == [] assert t.coeff == 1 + x assert sorted(t.free) == [(a, 0, 0), (b, 1, 0)] assert t.components == [A] t = A(-b, a)*B(-a, c)*A(-c, d) t1 = tensor_mul(*t.split()) assert t == t(-b, d) assert t == t1 assert tensor_mul(*[]) == TensMul.from_data(S.One, [], [], []) t = TensMul.from_data(1, [], [], []) zsym = tensorsymmetry() typ = TensorType([], zsym) C = typ('C') assert str(C()) == 'C' assert str(t) == '1' assert t.split()[0] == t raises(ValueError, lambda: TIDS.free_dum_from_indices(a, a)) raises(ValueError, lambda: TIDS.free_dum_from_indices(-a, -a)) raises(ValueError, lambda: A(a, b)*A(a, c)) t = A(a, b)*A(-a, c) raises(ValueError, lambda: t(a, b, c))
def simplify_lines(ex): """ simplify a product of gamma matrices Examples ======== >>> from sympy.physics.hep.gamma_matrices import GammaMatrix, DiracSpinorIndex >>> from sympy.tensor.tensor import tensor_indices >>> i0,i1,i2,i3,i4,i5 = tensor_indices('i0:6', GammaMatrix.LorentzIndex) >>> s0,s1,s2,s3,s4,s5,s6,s7 = tensor_indices('s0:8', DiracSpinorIndex) >>> G = GammaMatrix >>> t = G(i1,s1,-s2)*G(i4,s7,-s6)*G(i2,s2,-s3)*G(i3,s4,-s5)*G(i5,s6,-s7) >>> G.simplify_lines(t) 4*gamma(i3, s4, -s5)*gamma(i1, s1, -S_0)*gamma(i2, S_0, -s3)*metric(i4, i5) """ lines, traces, rest = get_lines(ex, DiracSpinorIndex) a = ex.split() trest = tensor_mul(*[x for i, x in enumerate(a) if i in rest]) tlines = [] for line in lines: first = a[line[0]] last = a[line[-1]] first = [x[0] for x in first.free if x[1] == 1][0] last = [x[0] for x in last.free if x[1] == 2][0] tx = tensor_mul(*[x for i, x in enumerate(a) if i in line]) tx1 = GammaMatrixHead._simplify_single_line(tx) tlines.append(tx1) traces = [GammaMatrix._trace_single_line(tensor_mul(*[x for i, x in enumerate(a) if i in line])) for line in traces] res = tensor_mul(*([trest] + tlines + traces)) return res
def _trace_single_line1(t): t = t.sorted_components() components = t.components ncomps = len(components) g = LorentzIndex.metric # gamma matirices are in a[i:j] hit = 0 for i in range(ncomps): if components[i] == GammaMatrix: hit = 1 break for j in range(i + hit, ncomps): if components[j] != GammaMatrix: break else: j = ncomps numG = j - i if numG == 0: tcoeff = t.coeff return t.nocoeff if tcoeff else t if numG % 2 == 1: return TensMul.from_data(S.Zero, [], [], []) elif numG > 4: # find the open matrix indices and connect them: a = t.split() ind1 = a[i].get_indices()[0] ind2 = a[i + 1].get_indices()[0] aa = a[:i] + a[i + 2:] t1 = tensor_mul(*aa) * g(ind1, ind2) t1 = t1.contract_metric(g) args = [t1] sign = 1 for k in range(i + 2, j): sign = -sign ind2 = a[k].get_indices()[0] aa = a[:i] + a[i + 1:k] + a[k + 1:] t2 = sign * tensor_mul(*aa) * g(ind1, ind2) t2 = t2.contract_metric(g) t2 = simplify_gpgp(t2, False) args.append(t2) t3 = TensAdd(*args) t3 = _trace_single_line(t3) return t3 else: a = t.split() t1 = _gamma_trace1(*a[i:j]) a2 = a[:i] + a[j:] t2 = tensor_mul(*a2) t3 = t1 * t2 if not t3: return t3 t3 = t3.contract_metric(g) return t3
def _trace_single_line1(t): t = t.sorted_components() components = t.components ncomps = len(components) g = LorentzIndex.metric # gamma matirices are in a[i:j] hit = 0 for i in range(ncomps): if components[i] == GammaMatrix: hit = 1 break for j in range(i + hit, ncomps): if components[j] != GammaMatrix: break else: j = ncomps numG = j - i if numG == 0: tcoeff = t.coeff return t.nocoeff if tcoeff else t if numG % 2 == 1: return TensMul.from_data(S.Zero, [], [], []) elif numG > 4: # find the open matrix indices and connect them: a = t.split() ind1 = a[i].get_indices()[0] ind2 = a[i + 1].get_indices()[0] aa = a[:i] + a[i + 2:] t1 = tensor_mul(*aa)*g(ind1, ind2) t1 = t1.contract_metric(g) args = [t1] sign = 1 for k in range(i + 2, j): sign = -sign ind2 = a[k].get_indices()[0] aa = a[:i] + a[i + 1:k] + a[k + 1:] t2 = sign*tensor_mul(*aa)*g(ind1, ind2) t2 = t2.contract_metric(g) t2 = simplify_gpgp(t2, False) args.append(t2) t3 = TensAdd(*args) t3 = _trace_single_line(t3) return t3 else: a = t.split() t1 = _gamma_trace1(*a[i:j]) a2 = a[:i] + a[j:] t2 = tensor_mul(*a2) t3 = t1*t2 if not t3: return t3 t3 = t3.contract_metric(g) return t3
def _simplify_gpgp(ex): components = ex.components a = [] comp_map = [] for i, comp in enumerate(components): comp_map.extend([i] * comp.rank) dum = [(i[0], i[1], comp_map[i[0]], comp_map[i[1]]) for i in ex.dum] for i in range(len(components)): if components[i] != GammaMatrix: continue 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', LorentzIndex) hit = True if i == 0: coeff = ex.coeff tx = components[ai[1]](mu) * components[ai[1]](-mu) if len(a) == 2: tx *= 4 # eye(4) tv.append(tx) 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.replace(lambda x: x.is_Matrix, lambda x: 1) return t else: return ex
def _simplify_gpgp(ex): components = ex.components a = [] comp_map = [] for i, comp in enumerate(components): comp_map.extend([i]*comp.rank) dum = [(i[0], i[1], comp_map[i[0]], comp_map[i[1]]) for i in ex.dum] for i in range(len(components)): if components[i] != GammaMatrix: continue 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', LorentzIndex) hit = True if i == 0: coeff = ex.coeff tx = components[ai[1]](mu)*components[ai[1]](-mu) if len(a) == 2: tx *= 4 # eye(4) tv.append(tx) 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.replace(lambda x: x.is_Matrix, lambda x: 1) return t else: return ex
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]].get_indices()[1] ind2 = ta[ai[0] + 1].get_indices()[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
def even_trace_recursion(t): assert isinstance(t, TensMul) if t.rank == 0: return t elif t.rank == 2: free1, free2 = t.free return t.coeff * 4 * GammaMatrix.Lorentz.metric(free1[0], free2[0]) a = t.split() for i in a: assert isinstance(i.components[0], GammaMatrixHead) ind1 = t.free[0][0] r_list = [] metric_list = [] sign = 1 for k in range(1, len(a)): ind2 = t.free[k][0] aa1 = a[1:k] + a[k+1:] r_list.append(sign*tensor_mul(*aa1)) metric_list.append(GammaMatrix.Lorentz.metric(ind1, ind2)) sign *= -1 p_list = [j*even_trace_recursion(i) for i, j in zip(r_list, metric_list)] return t.coeff * TensAdd(*p_list)
def _trace_single_line1(t): t = t.sorted_components() components = t.components ncomps = len(components) g = self.LorentzIndex.metric sg = DiracSpinorIndex.delta # gamma matirices are in a[i:j] hit = 0 for i in range(ncomps): if isinstance(components[i], GammaMatrixHead): hit = 1 break for j in range(i + hit, ncomps): if not isinstance(components[j], GammaMatrixHead): break else: j = ncomps numG = j - i if numG == 0: spinor_free = [_[0] for _ in t._tids.free if _[0].tensortype is DiracSpinorIndex] tcoeff = t.coeff if spinor_free == [DiracSpinorIndex.auto_left, -DiracSpinorIndex.auto_right]: t = t*DiracSpinorIndex.delta(-DiracSpinorIndex.auto_left, DiracSpinorIndex.auto_right) t = t.contract_metric(sg) return t/tcoeff if tcoeff else t else: return t/tcoeff if tcoeff else t if numG % 2 == 1: return TensMul.from_data(S.Zero, [], [], []) elif numG > 4: t = t.substitute_indices((-DiracSpinorIndex.auto_right, -DiracSpinorIndex.auto_index), (DiracSpinorIndex.auto_left, DiracSpinorIndex.auto_index)) a = t.split() ind1, lind1, rind1 = a[i].args[-1] ind2, lind2, rind2 = a[i + 1].args[-1] aa = a[:i] + a[i + 2:] t1 = tensor_mul(*aa)*g(ind1, ind2)*sg(lind1, rind1)*sg(lind2, rind2) t1 = t1.contract_metric(g) t1 = t1.contract_metric(sg) args = [t1] sign = 1 for k in range(i + 2, j): sign = -sign ind2, lind2, rind2 = a[k].args[-1] aa = a[:i] + a[i + 1:k] + a[k + 1:] t2 = sign*tensor_mul(*aa)*g(ind1, ind2)*sg(lind1, rind1)*sg(lind2, rind2) t2 = t2.contract_metric(g) t2 = t2.contract_metric(sg) t2 = GammaMatrixHead.simplify_gpgp(t2, False) args.append(t2) t3 = TensAdd(*args) #aa = _tensorlist_contract_metric(aa, g(ind1, ind2)) #t3 = t3.canon_bp() t3 = self._trace_single_line(t3) return t3 else: a = t.split() if len(t.components) == 1: if t.components[0] is DiracSpinorIndex.delta: return 4 # FIXME only for D=4 t1 = self._gamma_trace1(*a[i:j]) a2 = a[:i] + a[j:] t2 = tensor_mul(*a2) t3 = t1*t2 if not t3: return t3 t3 = t3.contract_metric(g) return t3