Пример #1
0
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
Пример #2
0
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)
Пример #3
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()
Пример #4
0
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]]
Пример #5
0
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()
Пример #6
0
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
Пример #7
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
Пример #8
0
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()