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 _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 test_add1(): assert TensAdd() == 0 # simple example of algebraic expression Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') a,b,d0,d1,i,j,k = tensor_indices('a,b,d0,d1,i,j,k', Lorentz) # A, B symmetric A, B = tensorhead('A,B', [Lorentz]*2, [[1]*2]) t1 = A(b,-d0)*B(d0,a) assert TensAdd(t1).equals(t1) t2a = B(d0,a) + A(d0, a) t2 = A(b,-d0)*t2a assert str(t2) == 'A(a, L_0)*A(b, -L_0) + A(b, L_0)*B(a, -L_0)' t2b = t2 + t1 assert str(t2b) == '2*A(b, L_0)*B(a, -L_0) + A(a, L_0)*A(b, -L_0)' p, q, r = tensorhead('p,q,r', [Lorentz], [[1]]) t = q(d0)*2 assert str(t) == '2*q(d0)' t = 2*q(d0) assert str(t) == '2*q(d0)' t1 = p(d0) + 2*q(d0) assert str(t1) == '2*q(d0) + p(d0)' t2 = p(-d0) + 2*q(-d0) assert str(t2) == '2*q(-d0) + p(-d0)' t1 = p(d0) t3 = t1*t2 assert str(t3) == '2*p(L_0)*q(-L_0) + p(L_0)*p(-L_0)' t3 = t2*t1 assert str(t3) == '2*p(L_0)*q(-L_0) + p(L_0)*p(-L_0)' t1 = p(d0) + 2*q(d0) t3 = t1*t2 assert str(t3) == '4*p(L_0)*q(-L_0) + 4*q(L_0)*q(-L_0) + p(L_0)*p(-L_0)' t1 = p(d0) - 2*q(d0) assert str(t1) == '-2*q(d0) + p(d0)' t2 = p(-d0) + 2*q(-d0) t3 = t1*t2 assert t3 == p(d0)*p(-d0) - 4*q(d0)*q(-d0) t = p(i)*p(j)*(p(k) + q(k)) + p(i)*(p(j) + q(j))*(p(k) - 3*q(k)) assert t == 2*p(i)*p(j)*p(k) - 2*p(i)*p(j)*q(k) + p(i)*p(k)*q(j) - 3*p(i)*q(j)*q(k) t1 = (p(i) + q(i) + 2*r(i))*(p(j) - q(j)) t2 = (p(j) + q(j) + 2*r(j))*(p(i) - q(i)) t = t1 + t2 assert t == 2*p(i)*p(j) + 2*p(i)*r(j) + 2*p(j)*r(i) - 2*q(i)*q(j) - 2*q(i)*r(j) - 2*q(j)*r(i) t = p(i)*q(j)/2 assert 2*t == p(i)*q(j) t = (p(i) + q(i))/2 assert 2*t == p(i) + q(i) t = S.One - p(i)*p(-i) assert (t + p(-j)*p(j)).equals(1) t = S.One + p(i)*p(-i) assert (t - p(-j)*p(j)).equals(1) t = A(a, b) + B(a, b) assert t.rank == 2 t1 = t - A(a, b) - B(a, b) assert t1 == 0 t = 1 - (A(a, -a) + B(a, -a)) t1 = 1 + (A(a, -a) + B(a, -a)) assert (t + t1).equals(2) t2 = 1 + A(a, -a) assert t1 != t2 assert t2 != TensMul.from_data(0, [], [], []) t = p(i) + q(i) raises(ValueError, lambda: t(i, j))
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
def test_add1(): assert TensAdd() == 0 # simple example of algebraic expression Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') a, b, d0, d1, i, j, k = tensor_indices('a,b,d0,d1,i,j,k', Lorentz) # A, B symmetric A, B = tensorhead('A,B', [Lorentz] * 2, [[1] * 2]) t1 = A(b, -d0) * B(d0, a) assert TensAdd(t1).equals(t1) t2a = B(d0, a) + A(d0, a) t2 = A(b, -d0) * t2a assert str(t2) == 'A(a, L_0)*A(b, -L_0) + A(b, L_0)*B(a, -L_0)' t2b = t2 + t1 assert str(t2b) == '2*A(b, L_0)*B(a, -L_0) + A(a, L_0)*A(b, -L_0)' p, q, r = tensorhead('p,q,r', [Lorentz], [[1]]) t = q(d0) * 2 assert str(t) == '2*q(d0)' t = 2 * q(d0) assert str(t) == '2*q(d0)' t1 = p(d0) + 2 * q(d0) assert str(t1) == '2*q(d0) + p(d0)' t2 = p(-d0) + 2 * q(-d0) assert str(t2) == '2*q(-d0) + p(-d0)' t1 = p(d0) t3 = t1 * t2 assert str(t3) == '2*p(L_0)*q(-L_0) + p(L_0)*p(-L_0)' t3 = t2 * t1 assert str(t3) == '2*p(L_0)*q(-L_0) + p(L_0)*p(-L_0)' t1 = p(d0) + 2 * q(d0) t3 = t1 * t2 assert str(t3) == '4*p(L_0)*q(-L_0) + 4*q(L_0)*q(-L_0) + p(L_0)*p(-L_0)' t1 = p(d0) - 2 * q(d0) assert str(t1) == '-2*q(d0) + p(d0)' t2 = p(-d0) + 2 * q(-d0) t3 = t1 * t2 assert t3 == p(d0) * p(-d0) - 4 * q(d0) * q(-d0) t = p(i) * p(j) * (p(k) + q(k)) + p(i) * (p(j) + q(j)) * (p(k) - 3 * q(k)) assert t == 2 * p(i) * p(j) * p(k) - 2 * p(i) * p(j) * q(k) + p(i) * p( k) * q(j) - 3 * p(i) * q(j) * q(k) t1 = (p(i) + q(i) + 2 * r(i)) * (p(j) - q(j)) t2 = (p(j) + q(j) + 2 * r(j)) * (p(i) - q(i)) t = t1 + t2 assert t == 2 * p(i) * p(j) + 2 * p(i) * r(j) + 2 * p(j) * r(i) - 2 * q( i) * q(j) - 2 * q(i) * r(j) - 2 * q(j) * r(i) t = p(i) * q(j) / 2 assert 2 * t == p(i) * q(j) t = (p(i) + q(i)) / 2 assert 2 * t == p(i) + q(i) t = S.One - p(i) * p(-i) assert (t + p(-j) * p(j)).equals(1) t = S.One + p(i) * p(-i) assert (t - p(-j) * p(j)).equals(1) t = A(a, b) + B(a, b) assert t.rank == 2 t1 = t - A(a, b) - B(a, b) assert t1 == 0 t = 1 - (A(a, -a) + B(a, -a)) t1 = 1 + (A(a, -a) + B(a, -a)) assert (t + t1).equals(2) t2 = 1 + A(a, -a) assert t1 != t2 assert t2 != TensMul.from_data(0, [], [], []) t = p(i) + q(i) raises(ValueError, lambda: t(i, j))