Exemple #1
0
def test_derivative_by_array():
    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,y,z
    from sympy.tensor.array import MutableSparseNDimArray, ImmutableSparseNDimArray

    bexpr = x*y**2*exp(z)*log(t)
    sexpr = sin(bexpr)
    cexpr = cos(bexpr)

    a = Array([sexpr])

    assert derive_by_array(sexpr, t) == x*y**2*exp(z)*cos(x*y**2*exp(z)*log(t))/t
    assert derive_by_array(sexpr, [x, y, z]) == Array([bexpr/x*cexpr, 2*y*bexpr/y**2*cexpr, bexpr*cexpr])
    assert derive_by_array(a, [x, y, z]) == Array([[bexpr/x*cexpr], [2*y*bexpr/y**2*cexpr], [bexpr*cexpr]])

    assert derive_by_array(sexpr, [[x, y], [z, t]]) == Array([[bexpr/x*cexpr, 2*y*bexpr/y**2*cexpr], [bexpr*cexpr, bexpr/log(t)/t*cexpr]])
    assert derive_by_array(a, [[x, y], [z, t]]) == Array([[[bexpr/x*cexpr], [2*y*bexpr/y**2*cexpr]], [[bexpr*cexpr], [bexpr/log(t)/t*cexpr]]])
    assert derive_by_array([[x, y], [z, t]], [x, y]) == Array([[[1, 0], [0, 0]], [[0, 1], [0, 0]]])
    assert derive_by_array([[x, y], [z, t]], [[x, y], [z, t]]) == Array([[[[1, 0], [0, 0]], [[0, 1], [0, 0]]],
                                                                         [[[0, 0], [1, 0]], [[0, 0], [0, 1]]]])

    assert diff(sexpr, t) == x*y**2*exp(z)*cos(x*y**2*exp(z)*log(t))/t
    assert diff(sexpr, Array([x, y, z])) == Array([bexpr/x*cexpr, 2*y*bexpr/y**2*cexpr, bexpr*cexpr])
    assert diff(a, Array([x, y, z])) == Array([[bexpr/x*cexpr], [2*y*bexpr/y**2*cexpr], [bexpr*cexpr]])

    assert diff(sexpr, Array([[x, y], [z, t]])) == Array([[bexpr/x*cexpr, 2*y*bexpr/y**2*cexpr], [bexpr*cexpr, bexpr/log(t)/t*cexpr]])
    assert diff(a, Array([[x, y], [z, t]])) == Array([[[bexpr/x*cexpr], [2*y*bexpr/y**2*cexpr]], [[bexpr*cexpr], [bexpr/log(t)/t*cexpr]]])
    assert diff(Array([[x, y], [z, t]]), Array([x, y])) == Array([[[1, 0], [0, 0]], [[0, 1], [0, 0]]])
    assert diff(Array([[x, y], [z, t]]), Array([[x, y], [z, t]])) == Array([[[[1, 0], [0, 0]], [[0, 1], [0, 0]]],
                                                                         [[[0, 0], [1, 0]], [[0, 0], [0, 1]]]])

    # test for large scale sparse array
    b = MutableSparseNDimArray.zeros(10000, 20000)
    b[0, 0] = i
    b[0, 1] = j
    assert derive_by_array(b, i) == ImmutableSparseNDimArray({0: 1}, (10000, 20000))
    assert derive_by_array(b, (i, j)) == ImmutableSparseNDimArray({0: 1, 200000001: 1}, (2, 10000, 20000))
Exemple #2
0
        sqrt(1.0 / 3.0), 1.0 / 3.0,
        sqrt(1.0 / 3.0), 1.0 / 3.0
    ],
])

#--------------------------------------------------------------------

# Matrix modes_to_nodes
val_r_inv = val_r**(-1)

# Computes coordiantes modes
coords_modes_ = val_r_inv * coords_
coords_modes = lambdify(coords_, coords_modes_, "numpy")

# Initialized coordiantes
interp_coords_ = MutableSparseNDimArray.zeros(nnodes_ie, 3)
for inode in range(0, nnodes_ie):
    for idir in range(0, 3):
        interp_coords_[inode, idir] = val_i[inode, :] * coords_modes_[:, idir]

# Initialized jacobian
jacobian_ = MutableSparseNDimArray.zeros(3, 3, nnodes_ie)
for inode in range(0, nnodes_ie):
    jacobian_[0, 0, inode] = ddxi_i[inode, :] * coords_modes_[:, 0]
    jacobian_[0, 1, inode] = ddeta_i[inode, :] * coords_modes_[:, 0]
    jacobian_[0, 2, inode] = ddzeta_i[inode, :] * coords_modes_[:, 0]
    jacobian_[1, 0, inode] = ddxi_i[inode, :] * coords_modes_[:, 1]
    jacobian_[1, 1, inode] = ddeta_i[inode, :] * coords_modes_[:, 1]
    jacobian_[1, 2, inode] = ddzeta_i[inode, :] * coords_modes_[:, 1]
    jacobian_[2, 0, inode] = ddxi_i[inode, :] * coords_modes_[:, 2]
    jacobian_[2, 1, inode] = ddeta_i[inode, :] * coords_modes_[:, 2]