Пример #1
0
def test_tensorcontraction():
    from sympy.abc import a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x
    B = Array(range(18), (2, 3, 3))
    assert tensorcontraction(B, (1, 2)) == Array([12, 39])
    C1 = Array([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x], (2, 3, 2, 2))

    assert tensorcontraction(C1, (0, 2)) == Array([[a + o, b + p], [e + s, f + t], [i + w, j + x]])
    assert tensorcontraction(C1, (0, 2, 3)) == Array([a + p, e + t, i + x])
    assert tensorcontraction(C1, (2, 3)) == Array([[a + d, e + h, i + l], [m + p, q + t, u + x]])
Пример #2
0
def test_issue_emerged_while_discussing_10972():
    ua = Array([-1,0])
    Fa = Array([[0, 1], [-1, 0]])
    po = tensorproduct(Fa, ua, Fa, ua)
    assert tensorcontraction(po, (1, 2), (4, 5)) == Array([[0, 0], [0, 1]])

    sa = symbols('a0:144')
    po = Array(sa, [2, 2, 3, 3, 2, 2])
    assert tensorcontraction(po, (0, 1), (2, 3), (4, 5)) == sa[0] + sa[108] + sa[111] + sa[124] + sa[127] + sa[140] + sa[143] + sa[16] + sa[19] + sa[3] + sa[32] + sa[35]
    assert tensorcontraction(po, (0, 1, 4, 5), (2, 3)) == sa[0] + sa[111] + sa[127] + sa[143] + sa[16] + sa[32]
    assert tensorcontraction(po, (0, 1), (4, 5)) == Array([[sa[0] + sa[108] + sa[111] + sa[3], sa[112] + sa[115] + sa[4] + sa[7],
                                                             sa[11] + sa[116] + sa[119] + sa[8]], [sa[12] + sa[120] + sa[123] + sa[15],
                                                             sa[124] + sa[127] + sa[16] + sa[19], sa[128] + sa[131] + sa[20] + sa[23]],
                                                            [sa[132] + sa[135] + sa[24] + sa[27], sa[136] + sa[139] + sa[28] + sa[31],
                                                             sa[140] + sa[143] + sa[32] + sa[35]]])
    assert tensorcontraction(po, (0, 1), (2, 3)) == Array([[sa[0] + sa[108] + sa[124] + sa[140] + sa[16] + sa[32], sa[1] + sa[109] + sa[125] + sa[141] + sa[17] + sa[33]],
                                                           [sa[110] + sa[126] + sa[142] + sa[18] + sa[2] + sa[34], sa[111] + sa[127] + sa[143] + sa[19] + sa[3] + sa[35]]])
Пример #3
0
def matrix_product(Asm, Bsm):
    '''
	Matrix product between 2D sympy.tensor.array
	'''

    assert len(Asm.shape)<3 or len(Bsm.shape)<3,\
                      'Attempting matrix product between 3D (or higher) arrays!'
    assert Asm.shape[1] == Bsm.shape[0], 'Matrix dimensions not compatible!'

    P = smarr.tensorproduct(Asm, Bsm)
    Csm = smarr.tensorcontraction(P, (1, 2))

    return Csm
Пример #4
0
def apply_contraction(outer_indices, tensor_indices, array):
    """ Apply the contraction structure to the array of Indexed objects.

    :arg outer_indices: The outer indices (i.e. any indices that are not repeated/summation indices)
    :arg tensor_indices: The indices that need to be summed over.
    :arg array: The Indexed arrays to apply the index contraction structure to.
    """

    contracting_indices = set(tensor_indices).difference(set(outer_indices))
    result = array
    if contracting_indices:
        for index in contracting_indices:
            match = tuple(
                [i for i, x in enumerate(tensor_indices) if x == index])
            result = tensorcontraction(result, match)
            tensor_indices = [i for i in tensor_indices if i != index]
    return result
Пример #5
0
def test_codegen_array_doit():
    M = MatrixSymbol("M", 2, 2)
    N = MatrixSymbol("N", 2, 2)
    P = MatrixSymbol("P", 2, 2)
    Q = MatrixSymbol("Q", 2, 2)

    M = M.as_explicit()
    N = N.as_explicit()
    P = P.as_explicit()
    Q = Q.as_explicit()

    expr = CodegenArrayTensorProduct(M, N, P, Q)
    assert expr.doit() == tensorproduct(M, N, P, Q)
    expr2 = CodegenArrayContraction(expr, (0, 1))
    assert expr2.doit() == tensorcontraction(tensorproduct(M, N, P, Q), (0, 1))
    expr2 = CodegenArrayDiagonal(expr, (0, 1))
    #assert expr2 = ... # TODO: not implemented
    expr = CodegenArrayTensorProduct(M, N)
    exprp = CodegenArrayPermuteDims(expr, [2, 1, 3, 0])
    assert exprp.doit() == permutedims(tensorproduct(M, N), [2, 1, 3, 0])
    expr = CodegenArrayElementwiseAdd(M, N)
    assert expr.doit() == M + N
Пример #6
0
    def covariance_transform(self, *indices):
        """
        Return the array associated with this tensor with indices set according
        to arguments.

        Parameters
        ----------
        indices : TensorIndex
            Defines the covariance and contravariance of the returned array.

        Examples
        --------
        >>> from sympy import diag, symbols, sin
        >>> from einsteinpy.symbolic.tensor import indices
        >>> from einsteinpy.symbolic.metric import Metric
        >>> t, r, th, ph = symbols('t r theta phi')
        >>> schwarzschild = diag(1-1/r, -1/(1-1/r), -r**2, -r**2*sin(th)**2)
        >>> g = Metric('g', [t, r, th, ph], schwarzschild)
        >>> mu, nu = indices('mu nu', g)
        >>> g.covariance_transform(mu, nu)
        [[1/(1 - 1/r), 0, 0, 0], [0, 1 - 1/r, 0, 0], [0, 0, -1/r**2, 0], [0, 0, 0, -1/(r**2*sin(theta)**2)]]

        """
        array = self.as_array()
        for pos, idx in enumerate(indices):
            if idx.is_up ^ (self.covar[pos] > 0):
                if idx.is_up:
                    metric = idx.tensor_index_type.metric.as_inverse()
                else:
                    metric = idx.tensor_index_type.metric.as_array()
                new = tensorcontraction(tensorproduct(metric, array),
                                        (1, 2 + pos))
                permu = list(range(len(indices)))
                permu[0], permu[pos] = permu[pos], permu[0]
                array = permutedims(new, permu)
        return array