def test_valued_components_with_wrong_symmetry(): IT = TensorIndexType('IT', dim=3) IT.data = [1, 1, 1] A_nosym = tensorhead('A', [IT]*2, [[1]]*2) A_sym = tensorhead('A', [IT]*2, [[1]*2]) A_antisym = tensorhead('A', [IT]*2, [[2]]) mat_nosym = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) mat_sym = mat_nosym + mat_nosym.T mat_antisym = mat_nosym - mat_nosym.T A_nosym.data = mat_nosym A_nosym.data = mat_sym A_nosym.data = mat_antisym def assign(A, dat): A.data = dat A_sym.data = mat_sym pytest.raises(ValueError, lambda: assign(A_sym, mat_nosym)) pytest.raises(ValueError, lambda: assign(A_sym, mat_antisym)) A_antisym.data = mat_antisym pytest.raises(ValueError, lambda: assign(A_antisym, mat_sym)) pytest.raises(ValueError, lambda: assign(A_antisym, mat_nosym)) A_sym.data = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] A_antisym.data = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
def test_sympyissue_11020(): Lorentz = TensorIndexType('Lorentz', metric=False, dummy_fmt='i', dim=2) Lorentz.data = [-1, 1] a, b, c, d = tensor_indices('a, b, c, d', Lorentz) i0, _ = tensor_indices('i_0:2', Lorentz) Vec = TensorType([Lorentz], tensorsymmetry([1])) S2 = TensorType([Lorentz] * 2, tensorsymmetry([1] * 2)) # metric tensor g = S2('g') g.data = Lorentz.data u = Vec('u') u.data = [1, 0] add_1 = g(b, c) * g(d, i0) * u(-i0) - g(b, c) * u(d) assert (add_1.data == numpy.zeros(shape=(2, 2, 2))).all() # Now let us replace index `d` with `a`: add_2 = g(b, c) * g(a, i0) * u(-i0) - g(b, c) * u(a) assert (add_2.data == numpy.zeros(shape=(2, 2, 2))).all() # some more tests # perp is tensor orthogonal to u^\mu perp = u(a) * u(b) + g(a, b) mul_1 = u(-a) * perp(a, b) assert (mul_1.data == numpy.array([0, 0])).all() mul_2 = u(-c) * perp(c, a) * perp(d, b) assert (mul_2.data == numpy.zeros(shape=(2, 2, 2))).all()
def test_TensorManager(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') LorentzH = TensorIndexType('LorentzH', dummy_fmt='LH') i, j = tensor_indices('i,j', Lorentz) ih, jh = tensor_indices('ih,jh', LorentzH) p, q = tensorhead('p q', [Lorentz], [[1]]) ph, qh = tensorhead('ph qh', [LorentzH], [[1]]) Gsymbol = Symbol('Gsymbol') GHsymbol = Symbol('GHsymbol') TensorManager.set_comm(Gsymbol, GHsymbol, 0) G = tensorhead('G', [Lorentz], [[1]], Gsymbol) assert TensorManager._comm_i2symbol[G.comm] == Gsymbol GH = tensorhead('GH', [LorentzH], [[1]], GHsymbol) ps = G(i)*p(-i) psh = GH(ih)*ph(-ih) t = ps + psh t1 = t*t assert t1 == ps*ps + 2*ps*psh + psh*psh qs = G(i)*q(-i) qsh = GH(ih)*qh(-ih) assert _is_equal(ps*qsh, qsh*ps) assert not _is_equal(ps*qs, qs*ps) n = TensorManager.comm_symbols2i(Gsymbol) assert TensorManager.comm_i2symbol(n) == Gsymbol assert GHsymbol in TensorManager._comm_symbols2i pytest.raises(ValueError, lambda: TensorManager.set_comm(GHsymbol, 1, 2)) TensorManager.set_comms((Gsymbol, GHsymbol, 0), (Gsymbol, 1, 1)) assert TensorManager.get_comm(n, 1) == TensorManager.get_comm(1, n) == 1 TensorManager.clear() assert TensorManager.comm == [{0: 0, 1: 0, 2: 0}, {0: 0, 1: 1, 2: None}, {0: 0, 1: None}] assert GHsymbol not in TensorManager._comm_symbols2i TensorManager.comm_symbols2i(GHsymbol) assert GHsymbol in TensorManager._comm_symbols2i
def test_hash(): D = Symbol('D') Lorentz = TensorIndexType('Lorentz', dim=D, dummy_fmt='L') a, b, c, d, e = tensor_indices('a,b,c,d,e', Lorentz) g = Lorentz.metric p, q = tensorhead('p q', [Lorentz], [[1]]) p_type = p.args[1] t1 = p(a)*q(b) t2 = p(a)*p(b) assert hash(t1) != hash(t2) t3 = p(a)*p(b) + g(a, b) t4 = p(a)*p(b) - g(a, b) assert hash(t3) != hash(t4) assert a.func(*a.args) == a assert Lorentz.func(*Lorentz.args) == Lorentz assert g.func(*g.args) == g assert p.func(*p.args) == p assert p_type.func(*p_type.args) == p_type assert p(a).func(*(p(a)).args) == p(a) assert t1.func(*t1.args) == t1 assert t2.func(*t2.args) == t2 assert t3.func(*t3.args) == t3 assert t4.func(*t4.args) == t4 assert hash(a.func(*a.args)) == hash(a) assert hash(Lorentz.func(*Lorentz.args)) == hash(Lorentz) assert hash(g.func(*g.args)) == hash(g) assert hash(p.func(*p.args)) == hash(p) assert hash(p_type.func(*p_type.args)) == hash(p_type) assert hash(p(a).func(*(p(a)).args)) == hash(p(a)) assert hash(t1.func(*t1.args)) == hash(t1) assert hash(t2.func(*t2.args)) == hash(t2) assert hash(t3.func(*t3.args)) == hash(t3) assert hash(t4.func(*t4.args)) == hash(t4) def check_all(obj): return all(isinstance(_, Basic) for _ in obj.args) assert check_all(a) assert check_all(Lorentz) assert check_all(g) assert check_all(p) assert check_all(p_type) assert check_all(p(a)) assert check_all(t1) assert check_all(t2) assert check_all(t3) assert check_all(t4) tsymmetry = tensorsymmetry([2], [1], [1, 1, 1]) assert tsymmetry.func(*tsymmetry.args) == tsymmetry assert hash(tsymmetry.func(*tsymmetry.args)) == hash(tsymmetry) assert check_all(tsymmetry)
def test_TensMul_data(): Lorentz = TensorIndexType('Lorentz', metric=False, dummy_fmt='L', dim=4) Lorentz.data = [-1, 1, 1, 1] mu, nu, alpha, beta = tensor_indices('\\mu, \\nu, \\alpha, \\beta', Lorentz) Vec = TensorType([Lorentz], tensorsymmetry([1])) A2 = TensorType([Lorentz] * 2, tensorsymmetry([2])) u = Vec('u') u.data = [1, 0, 0, 0] F = A2('F') Ex, Ey, Ez, Bx, By, Bz = symbols('E_x E_y E_z B_x B_y B_z') F.data = [[0, Ex, Ey, Ez], [-Ex, 0, Bz, -By], [-Ey, -Bz, 0, Bx], [-Ez, By, -Bx, 0]] E = F(mu, nu) * u(-nu) assert ((E(mu) * E(nu)).data == numpy.array([[0, 0, 0, 0], [0, Ex ** 2, Ex * Ey, Ex * Ez], [0, Ex * Ey, Ey ** 2, Ey * Ez], [0, Ex * Ez, Ey * Ez, Ez ** 2]])).all() assert ((E(mu) * E(nu)).canon_bp().data == (E(mu) * E(nu)).data).all() assert ((F(mu, alpha) * F(beta, nu) * u(-alpha) * u(-beta)).data == - (E(mu) * E(nu)).data).all() assert ((F(alpha, mu) * F(beta, nu) * u(-alpha) * u(-beta)).data == (E(mu) * E(nu)).data).all() S2 = TensorType([Lorentz] * 2, tensorsymmetry([1] * 2)) g = S2('g') g.data = Lorentz.data # tensor 'perp' is orthogonal to vector 'u' perp = u(mu) * u(nu) + g(mu, nu) mul_1 = u(-mu) * perp(mu, nu) assert (mul_1.data == numpy.array([0, 0, 0, 0])).all() mul_2 = u(-mu) * perp(mu, alpha) * perp(nu, beta) assert (mul_2.data == numpy.zeros(shape=(4, 4, 4))).all() Fperp = perp(mu, alpha) * perp(nu, beta) * F(-alpha, -beta) assert (Fperp.data[0, :] == numpy.array([0, 0, 0, 0])).all() assert (Fperp.data[:, 0] == numpy.array([0, 0, 0, 0])).all() mul_3 = u(-mu) * Fperp(mu, nu) assert (mul_3.data == numpy.array([0, 0, 0, 0])).all()
def _get_valued_base_test_variables(): minkowski = Matrix(( (1, 0, 0, 0), (0, -1, 0, 0), (0, 0, -1, 0), (0, 0, 0, -1), )) Lorentz = TensorIndexType('Lorentz', dim=4) Lorentz.data = minkowski i0, i1, i2, i3, i4 = tensor_indices('i0:5', Lorentz) E, px, py, pz = symbols('E px py pz') A = tensorhead('A', [Lorentz], [[1]]) A.data = [E, px, py, pz] B = tensorhead('B', [Lorentz], [[1]], 'Gcomm') B.data = range(4) AB = tensorhead('AB', [Lorentz] * 2, [[1]]*2) AB.data = minkowski ba_matrix = Matrix(( (1, 2, 3, 4), (5, 6, 7, 8), (9, 0, -1, -2), (-3, -4, -5, -6), )) BA = tensorhead('BA', [Lorentz] * 2, [[1]]*2) BA.data = ba_matrix # Let's test the diagonal metric, with inverted Minkowski metric: LorentzD = TensorIndexType('LorentzD') LorentzD.data = [-1, 1, 1, 1] mu0, mu1, mu2 = tensor_indices('mu0:3', LorentzD) C = tensorhead('C', [LorentzD], [[1]]) C.data = [E, px, py, pz] # ### non-diagonal metric ### ndm_matrix = ( (1, 1, 0,), (1, 0, 1), (0, 1, 0,), ) ndm = TensorIndexType('ndm') ndm.data = ndm_matrix n0, n1, n2 = tensor_indices('n0:3', ndm) NA = tensorhead('NA', [ndm], [[1]]) NA.data = range(10, 13) NB = tensorhead('NB', [ndm]*2, [[1]]*2) NB.data = [[i+j for j in range(10, 13)] for i in range(10, 13)] NC = tensorhead('NC', [ndm]*3, [[1]]*3) NC.data = [[[i+j+k for k in range(4, 7)] for j in range(1, 4)] for i in range(2, 5)] return (A, B, AB, BA, C, Lorentz, E, px, py, pz, LorentzD, mu0, mu1, mu2, ndm, n0, n1, n2, NA, NB, NC, minkowski, ba_matrix, ndm_matrix, i0, i1, i2, i3, i4)
def test_TensorIndexType(): D = Symbol('D') G = Metric('g', False) Lorentz = TensorIndexType('Lorentz', metric=G, dim=D, dummy_fmt='L') sym2 = tensorsymmetry([1] * 2) sym2n = tensorsymmetry(*get_symmetric_group_sgs(2)) assert sym2 == sym2n g = Lorentz.metric assert str(g) == 'g(Lorentz,Lorentz)' assert Lorentz.eps_dim == Lorentz.dim TSpace = TensorIndexType('TSpace') i0 = tensor_indices('i0', TSpace) g = TSpace.metric A = tensorhead('A', [TSpace] * 2, [[1] * 2]) assert str(A(i0, -i0).canon_bp()) == 'A(TSpace_0, -TSpace_0)'
def test_pprint(): Lorentz = TensorIndexType('Lorentz') i0, i1, i2, i3, i4 = tensor_indices('i0:5', Lorentz) A = tensorhead('A', [Lorentz], [[1]]) assert pretty(A) == 'A(Lorentz)' assert pretty(A(i0)) == 'A(i0)'
def test_fun(): D = Symbol('D') Lorentz = TensorIndexType('Lorentz', dim=D, dummy_fmt='L') a, b, c, d, e = tensor_indices('a,b,c,d,e', Lorentz) g = Lorentz.metric p, q = tensorhead('p q', [Lorentz], [[1]]) t = q(c)*p(a)*q(b) + g(a, b)*g(c, d)*q(-d) assert t(a, b, c) == t assert t - t(b, a, c) == q(c)*p(a)*q(b) - q(c)*p(b)*q(a) assert t(b, c, d) == q(d)*p(b)*q(c) + g(b, c)*g(d, e)*q(-e) t1 = t.fun_eval((a, b), (b, a)) assert t1 == q(c)*p(b)*q(a) + g(a, b)*g(c, d)*q(-d) # check that g_{a b; c} = 0 # example taken from L. Brewin # "A brief introduction to Cadabra" arxiv:0903.2085 # dg_{a b c} = \partial_{a} g_{b c} is symmetric in b, c dg = tensorhead('dg', [Lorentz]*3, [[1], [1]*2]) # gamma^a_{b c} is the Christoffel symbol gamma = g(a, d)*(dg(-b, -d, -c) + dg(-c, -b, -d) - dg(-d, -b, -c))/2 # t = g_{a b; c} t = dg(-c, -a, -b) - g(-a, -d)*gamma(d, -b, -c) - g(-b, -d)*gamma(d, -a, -c) t = t.contract_metric(g) assert t == 0 t = q(c)*p(a)*q(b) assert t(b, c, d) == q(d)*p(b)*q(c)
def test_bug_correction_tensor_indices(): # to make sure that tensor_indices does not return a list if creating # only one index: A = TensorIndexType('A') i = tensor_indices('i', A) assert not isinstance(i, (tuple, list)) assert isinstance(i, TensorIndex)
def test_riemann_cyclic_replace(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') m0, m1, m2, m3 = tensor_indices('m:4', Lorentz) R = tensorhead('R', [Lorentz]*4, [[2, 2]]) t = R(m0, m2, m1, m3) t1 = riemann_cyclic_replace(t) t1a = -R(m0, m3, m2, m1)/3 + R(m0, m1, m2, m3)/3 + 2*R(m0, m2, m1, m3)/3 assert t1 == t1a
def test_TensorHead(): assert TensAdd() == 0 # simple example of algebraic expression Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') # A, B symmetric A = tensorhead('A', [Lorentz] * 2, [[1] * 2]) assert A.rank == 2 assert A.symmetry == tensorsymmetry([1] * 2)
def test_get_components_with_free_indices(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') m0, m1, m2, m3 = tensor_indices('m0 m1 m2 m3', Lorentz) Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') m0, m1, m2, m3 = tensor_indices('m0 m1 m2 m3', Lorentz) T = tensorhead('T', [Lorentz]*4, [[1]*4]) A = tensorhead('A', [Lorentz], [[1]]) t = TIDS.from_components_and_indices([T], [m0, m1, -m1, m3]) assert t.get_components_with_free_indices() == [(T, [(m0, 0, 0), (m3, 3, 0)])] t2 = (A(m0)*A(-m0))._tids assert t2.get_components_with_free_indices() == [(A, []), (A, [])] t3 = (A(m0)*A(-m1)*A(-m0)*A(m1))._tids assert t3.get_components_with_free_indices() == [(A, []), (A, []), (A, []), (A, [])] t4 = (A(m0)*A(m1)*A(-m0))._tids assert t4.get_components_with_free_indices() == [(A, []), (A, [(m1, 0, 1)]), (A, [])] t5 = (A(m0)*A(m1)*A(m2))._tids assert t5.get_components_with_free_indices() == [(A, [(m0, 0, 0)]), (A, [(m1, 0, 1)]), (A, [(m2, 0, 2)])]
def test_noncommuting_components(): euclid = TensorIndexType('Euclidean') euclid.data = [1, 1] i1, i2, _ = tensor_indices('i1:4', euclid) a, b, c, d = symbols('a b c d', commutative=False) V1 = tensorhead('V1', [euclid] * 2, [[1]]*2) V1.data = [[a, b], (c, d)] V2 = tensorhead('V2', [euclid] * 2, [[1]]*2) V2.data = [[a, c], [b, d]] vtp = V1(i1, i2) * V2(-i2, -i1) assert vtp.data == a**2 + b**2 + c**2 + d**2 assert vtp.data != a**2 + 2*b*c + d**2 Vc = (b * V1(i1, -i1)).data assert Vc.expand() == b * a + b * d
def test_TensorType(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') sym = tensorsymmetry([1]*2) A = tensorhead('A', [Lorentz]*2, [[1]*2]) assert A.typ == TensorType([Lorentz]*2, sym) assert A.types == [Lorentz] typ = TensorType([Lorentz]*2, sym) assert str(typ) == "TensorType(['Lorentz', 'Lorentz'])" pytest.raises(ValueError, lambda: typ(2))
def test_contract_metric1(): D = Symbol('D') Lorentz = TensorIndexType('Lorentz', dim=D, dummy_fmt='L') a, b, c, d, e = tensor_indices('a,b,c,d,e', Lorentz) g = Lorentz.metric p = tensorhead('p', [Lorentz], [[1]]) t = g(a, b)*p(-b) t1 = t.contract_metric(g) assert t1 == p(a) A, B = tensorhead('A,B', [Lorentz]*2, [[1]*2]) # case with g with all free indices t1 = A(a, b)*B(-b, c)*g(d, e) t2 = t1.contract_metric(g) assert t1 == t2 # case of g(d, -d) t1 = A(a, b)*B(-b, c)*g(-d, d) t2 = t1.contract_metric(g) assert t2 == D*A(a, d)*B(-d, c) # g with one free index t1 = A(a, b)*B(-b, -c)*g(c, d) t2 = t1.contract_metric(g) assert t2 == A(a, c)*B(-c, d) # g with both indices contracted with another tensor t1 = A(a, b)*B(-b, -c)*g(c, -a) t2 = t1.contract_metric(g) assert _is_equal(t2, A(a, b)*B(-b, -a)) t1 = A(a, b)*B(-b, -c)*g(c, d)*g(-a, -d) t2 = t1.contract_metric(g) assert _is_equal(t2, A(a, b)*B(-b, -a)) t1 = A(a, b)*g(-a, -b) t2 = t1.contract_metric(g) assert _is_equal(t2, A(a, -a)) assert not t2.free Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') a, b = tensor_indices('a,b', Lorentz) g = Lorentz.metric pytest.raises(ValueError, lambda: g(a, -a).contract_metric(g)) # no dim
def test_from_components_and_indices(): a = TIDS.from_components_and_indices([], []) assert a.components == [] assert a.free == [] assert a.dum == [] Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') m0, m1, m2, m3 = tensor_indices('m0 m1 m2 m3', Lorentz) T = tensorhead('T', [Lorentz]*4, [[1]*4]) assert str(TIDS.from_components_and_indices([T], [m0, m1, -m1, m3])) == str(TIDS([T], [(m0, 0, 0), (m3, 3, 0)], [(1, 2, 0, 0)])) A = tensorhead('A', [Lorentz], [[1]]) assert str(TIDS.from_components_and_indices([A]*4, [m0, m1, -m1, m3])) == str(TIDS([A, A, A, A], [(m0, 0, 0), (m3, 0, 3)], [(0, 0, 1, 2)]))
def test_contract_automatrix_and_data(): L = TensorIndexType('L') S = TensorIndexType('S') G = tensorhead('G', [L, S, S], [[1]] * 3, matrix_behavior=True) def G_data(): G.data = [[[1]]] pytest.raises(ValueError, G_data) L.data = [1, -1] pytest.raises(ValueError, G_data) S.data = [[1, 0], [0, 2]] G.data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] m0, m1, m2 = tensor_indices('m0:3', L) s0, s1, s2 = tensor_indices('s0:3', S) assert (G(-m0).data == numpy.array([[[1, 4], [3, 8]], [[-5, -12], [-7, -16]]])).all() (G(m0) * G(-m0)).data G(m0, s0, -s1).data c1 = G(m0, s0, -s1) * G(-m0, s1, -s2) c2 = G(m0) * G(-m0) assert (c1.data == c2.data).all() del L.data del S.data del G.data assert L.data is None assert S.data is None assert G.data is None
def test_div(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') m0, m1, m2, m3 = tensor_indices('m0:4', Lorentz) R = tensorhead('R', [Lorentz]*4, [[2, 2]]) t = R(m0, m1, -m1, m3) t1 = t/4 assert str(t1) == '1/4*R(m0, L_0, -L_0, m3)' t = t.canon_bp() assert not t1._is_canon_bp t1 = t*4 assert t1._is_canon_bp t1 = t1/4 assert t1._is_canon_bp
def test_add2(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') m, n, p, q = tensor_indices('m,n,p,q', Lorentz) R = tensorhead('R', [Lorentz]*4, [[2, 2]]) A = tensorhead('A', [Lorentz]*3, [[3]]) t1 = 2*R(m, n, p, q) - R(m, q, n, p) + R(m, p, n, q) t2 = t1*A(-n, -p, -q) assert t2 == 0 t1 = Rational(2, 3)*R(m, n, p, q) - Rational(1, 3)*R(m, q, n, p) + Rational(1, 3)*R(m, p, n, q) t2 = t1*A(-n, -p, -q) assert t2 == 0 t = A(m, -m, n) + A(n, p, -p) assert t == 0
def test_noncommuting_components(): (A, B, AB, BA, C, Lorentz, E, px, py, pz, LorentzD, mu0, mu1, mu2, ndm, n0, n1, n2, NA, NB, NC, minkowski, ba_matrix, ndm_matrix, i0, i1, i2, i3, i4) = _get_valued_base_test_variables() euclid = TensorIndexType('Euclidean') euclid.data = [1, 1] i1, i2, i3 = tensor_indices('i1:4', euclid) a, b, c, d = symbols('a b c d', commutative=False) V1 = tensorhead('V1', [euclid] * 2, [[1]] * 2) V1.data = [[a, b], (c, d)] V2 = tensorhead('V2', [euclid] * 2, [[1]] * 2) V2.data = [[a, c], [b, d]] vtp = V1(i1, i2) * V2(-i2, -i1) assert vtp.data == a**2 + b**2 + c**2 + d**2 assert vtp.data != a**2 + 2 * b * c + d**2 Vc = (b * V1(i1, -i1)).data assert Vc.expand() == b * a + b * d
def test_canonicalize3(): D = Symbol('D') Spinor = TensorIndexType('Spinor', dim=D, metric=True, dummy_fmt='S') a0, a1, a2, a3, a4 = tensor_indices('a0:5', Spinor) chi, psi = tensorhead('chi,psi', [Spinor], [[1]], 1) t = chi(a1)*psi(a0) t1 = t.canon_bp() assert t1 == t t = psi(a1)*chi(a0) t1 = t.canon_bp() assert t1 == -chi(a0)*psi(a1)
def test_indices(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') a, b, c, d = tensor_indices('a,b,c,d', Lorentz) assert a.tensortype == Lorentz assert a != -a A, B = tensorhead('A B', [Lorentz] * 2, [[1] * 2]) t = A(a, b) * B(-b, c) indices = t.get_indices() L_0 = TensorIndex('L_0', Lorentz) assert indices == [a, L_0, -L_0, c] pytest.raises(ValueError, lambda: tensor_indices(3, Lorentz)) pytest.raises(ValueError, lambda: A(a, b, c)) pytest.raises(ValueError, lambda: TensorIndex([1, 2], Lorentz))
def test_sympyissue_10972_TensMul_data(): Lorentz = TensorIndexType('Lorentz', metric=False, dummy_fmt='i', dim=2) Lorentz.data = [-1, 1] mu, nu, alpha, beta = tensor_indices('\\mu, \\nu, \\alpha, \\beta', Lorentz) Vec = TensorType([Lorentz], tensorsymmetry([1])) A2 = TensorType([Lorentz] * 2, tensorsymmetry([2])) u = Vec('u') u.data = [1, 0] F = A2('F') F.data = [[0, 1], [-1, 0]] mul_1 = F(mu, alpha) * u(-alpha) * F(nu, beta) * u(-beta) assert (mul_1.data == numpy.array([[0, 0], [0, 1]])).all() mul_2 = F(mu, alpha) * F(nu, beta) * u(-alpha) * u(-beta) assert (mul_2.data == mul_1.data).all() assert ((mul_1 + mul_1).data == 2 * mul_1.data).all()
def test_canonicalize_no_dummies(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') a, b, c, d = tensor_indices('a, b, c, d', Lorentz) sym1 = tensorsymmetry([1]) sym2 = tensorsymmetry([1]*2) # A commuting # A^c A^b A^a # T_c = A^a A^b A^c S1 = TensorType([Lorentz], sym1) A = S1('A') t = A(c)*A(b)*A(a) tc = t.canon_bp() assert str(tc) == 'A(a)*A(b)*A(c)' # A anticommuting # A^c A^b A^a # T_c = -A^a A^b A^c A = S1('A', 1) t = A(c)*A(b)*A(a) tc = t.canon_bp() assert str(tc) == '-A(a)*A(b)*A(c)' # A commuting and symmetric # A^{b,d}*A^{c,a} # T_c = A^{a c}*A^{b d} S2 = TensorType([Lorentz]*2, sym2) A = S2('A') t = A(b, d)*A(c, a) tc = t.canon_bp() assert str(tc) == 'A(a, c)*A(b, d)' # A anticommuting and symmetric # A^{b,d}*A^{c,a} # T_c = -A^{a c}*A^{b d} A = S2('A', 1) t = A(b, d)*A(c, a) tc = t.canon_bp() assert str(tc) == '-A(a, c)*A(b, d)' # A^{c,a}*A^{b,d} # T_c = A^{a c}*A^{b d} t = A(c, a)*A(b, d) tc = t.canon_bp() assert str(tc) == 'A(a, c)*A(b, d)'
def test_riemann_cyclic(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') i, j, k, l, m, n, p, q = tensor_indices('i,j,k,l,m,n,p,q', Lorentz) R = tensorhead('R', [Lorentz]*4, [[2, 2]]) t = R(i, j, k, l) + R(i, l, j, k) + R(i, k, l, j) - \ R(i, j, l, k) - R(i, l, k, j) - R(i, k, j, l) t2 = t*R(-i, -j, -k, -l) t3 = riemann_cyclic(t2) assert t3 == 0 t = R(i, j, k, l)*(R(-i, -j, -k, -l) - 2*R(-i, -k, -j, -l)) t1 = riemann_cyclic(t) assert t1 == 0 t = R(i, j, k, l) t1 = riemann_cyclic(t) assert t1 == -Rational(1, 3)*R(i, l, j, k) + Rational(1, 3)*R(i, k, j, l) + Rational(2, 3)*R(i, j, k, l) t = R(i, j, k, l)*R(-k, -l, m, n)*(R(-m, -n, -i, -j) + 2*R(-m, -j, -n, -i)) t1 = riemann_cyclic(t) assert t1 == 0
def test_mul(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') a, b, c, d = tensor_indices('a,b,c,d', Lorentz) t = TensMul.from_data(Integer(1), [], [], []) 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] ts = A(a, b) assert str(ts) == 'A(a, b)' assert ts.types == [Lorentz] assert ts.rank == 2 assert ts.dum == [] assert ts.coeff == 1 assert sorted(ts.free) == [(a, 0, 0), (b, 1, 0)] assert ts.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(Integer(1), [], [], []) 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 pytest.raises(ValueError, lambda: TIDS.free_dum_from_indices(a, a)) pytest.raises(ValueError, lambda: TIDS.free_dum_from_indices(-a, -a)) pytest.raises(ValueError, lambda: A(a, b)*A(a, c)) t = A(a, b)*A(-a, c) pytest.raises(ValueError, lambda: t(a, b, c))
def test_riemann_products(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') d0, d1, d2, d3, d4, d5, d6 = tensor_indices('d0:7', Lorentz) a0, a1, a2, a3, a4, a5 = tensor_indices('a0:6', Lorentz) a, b = tensor_indices('a,b', Lorentz) R = tensorhead('R', [Lorentz] * 4, [[2, 2]]) # R^{a b d0}_d0 = 0 t = R(a, b, d0, -d0) tc = t.canon_bp() assert tc == 0 # R^{d0 b a}_d0 # T_c = -R^{a d0 b}_d0 t = R(d0, b, a, -d0) tc = t.canon_bp() assert str(tc) == '-R(a, L_0, b, -L_0)' # R^d1_d2^b_d0 * R^{d0 a}_d1^d2; ord=[a,b,d0,-d0,d1,-d1,d2,-d2] # T_c = -R^{a d0 d1 d2}* R^b_{d0 d1 d2} t = R(d1, -d2, b, -d0) * R(d0, a, -d1, d2) tc = t.canon_bp() assert str(tc) == '-R(a, L_0, L_1, L_2)*R(b, -L_0, -L_1, -L_2)' # A symmetric commuting # R^{d6 d5}_d2^d1 * R^{d4 d0 d2 d3} * A_{d6 d0} A_{d3 d1} * A_{d4 d5} # g = [12,10,5,2, 8,0,4,6, 13,1, 7,3, 9,11,14,15] # T_c = -R^{d0 d1 d2 d3} * R_d0^{d4 d5 d6} * A_{d1 d4}*A_{d2 d5}*A_{d3 d6} V = tensorhead('V', [Lorentz] * 2, [[1] * 2]) t = R(d6, d5, -d2, d1) * R(d4, d0, d2, d3) * V(-d6, -d0) * V(-d3, -d1) * V( -d4, -d5) tc = t.canon_bp() assert str( tc ) == '-R(L_0, L_1, L_2, L_3)*R(-L_0, L_4, L_5, L_6)*V(-L_1, -L_4)*V(-L_2, -L_5)*V(-L_3, -L_6)' # R^{d2 a0 a2 d0} * R^d1_d2^{a1 a3} * R^{a4 a5}_{d0 d1} # T_c = R^{a0 d0 a2 d1}*R^{a1 a3}_d0^d2*R^{a4 a5}_{d1 d2} t = R(d2, a0, a2, d0) * R(d1, -d2, a1, a3) * R(a4, a5, -d0, -d1) tc = t.canon_bp() assert str( tc) == 'R(a0, L_0, a2, L_1)*R(a1, a3, -L_0, L_2)*R(a4, a5, -L_1, -L_2)'
def test_TensExpr(): Lorentz = TensorIndexType('Lorentz', dummy_fmt='L') a, b, c, d = tensor_indices('a,b,c,d', Lorentz) g = Lorentz.metric A, B = tensorhead('A B', [Lorentz]*2, [[1]*2]) pytest.raises(ValueError, lambda: g(c, d)/g(a, b)) pytest.raises(ValueError, lambda: 1/g(a, b)) pytest.raises(ValueError, lambda: (A(c, d) + g(c, d))/g(a, b)) pytest.raises(ValueError, lambda: 1/(A(c, d) + g(c, d))) pytest.raises(ValueError, lambda: A(a, b) + A(a, c)) t = A(a, b) + B(a, b) pytest.raises(NotImplementedError, lambda: TensExpr.__mul__(t, 'a')) pytest.raises(NotImplementedError, lambda: TensExpr.__add__(t, 'a')) pytest.raises(NotImplementedError, lambda: TensExpr.__radd__(t, 'a')) pytest.raises(NotImplementedError, lambda: TensExpr.__sub__(t, 'a')) pytest.raises(NotImplementedError, lambda: TensExpr.__rsub__(t, 'a')) pytest.raises(NotImplementedError, lambda: TensExpr.__truediv__(t, 'a')) pytest.raises(NotImplementedError, lambda: TensExpr.__rtruediv__(t, 'a')) pytest.raises(ValueError, lambda: A(a, b)**2) pytest.raises(NotImplementedError, lambda: 2**A(a, b)) pytest.raises(NotImplementedError, lambda: abs(A(a, b)))
def test_canonicalize2(): D = Symbol('D') Eucl = TensorIndexType('Eucl', metric=0, dim=D, dummy_fmt='E') i0, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14 = \ tensor_indices('i0:15', Eucl) A = tensorhead('A', [Eucl]*3, [[3]]) # two examples from Cvitanovic, Group Theory page 59 # of identities for antisymmetric tensors of rank 3 # contracted according to the Kuratowski graph eq.(6.59) t = A(i0, i1, i2)*A(-i1, i3, i4)*A(-i3, i7, i5)*A(-i2, -i5, i6)*A(-i4, -i6, i8) t1 = t.canon_bp() assert t1 == 0 # eq.(6.60) # t = A(i0,i1,i2)*A(-i1,i3,i4)*A(-i2,i5,i6)*A(-i3,i7,i8)*A(-i6,-i7,i9)* # A(-i8,i10,i13)*A(-i5,-i10,i11)*A(-i4,-i11,i12)*A(-i3,-i12,i14) t = A(i0, i1, i2)*A(-i1, i3, i4)*A(-i2, i5, i6)*A(-i3, i7, i8)*A(-i6, -i7, i9) *\ A(-i8, i10, i13)*A(-i5, -i10, i11)*A(-i4, -i11, i12)*A(-i9, -i12, i14) t1 = t.canon_bp() assert t1 == 0