def test_derivative_by_array(): from sympy.abc import i, j, t, x, y, z 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 for SparseArrayType in [ImmutableSparseNDimArray, MutableSparseNDimArray]: b = MutableSparseNDimArray({0:i, 1:j}, (10000, 20000)) 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))
def test_NDim_array_conv(): MD = MutableDenseNDimArray([x, y, z]) MS = MutableSparseNDimArray([x, y, z]) ID = ImmutableDenseNDimArray([x, y, z]) IS = ImmutableSparseNDimArray([x, y, z]) assert MD.as_immutable() == ID assert MD.as_mutable() == MD assert MS.as_immutable() == IS assert MS.as_mutable() == MS assert ID.as_immutable() == ID assert ID.as_mutable() == MD assert IS.as_immutable() == IS assert IS.as_mutable() == MS
def test_NDim_array_conv(): MD = MutableDenseNDimArray([x, y, z]) MS = MutableSparseNDimArray([x, y, z]) ID = ImmutableDenseNDimArray([x, y, z]) IS = ImmutableSparseNDimArray([x, y, z]) assert MD.as_immutable() == ID assert MD.as_mutable() == MD assert MS.as_immutable() == IS assert MS.as_mutable() == MS assert ID.as_immutable() == ID assert ID.as_mutable() == MD assert IS.as_immutable() == IS assert IS.as_mutable() == MS
def test_NDArray(): from sympy.tensor.array import ( MutableDenseNDimArray, ImmutableDenseNDimArray, MutableSparseNDimArray, ImmutableSparseNDimArray, ) example = MutableDenseNDimArray( [ [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], [[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]], ] ) assert ( mcode(example) == "{{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}, " "{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}}" ) example = ImmutableDenseNDimArray(example) assert ( mcode(example) == "{{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}, " "{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}}" ) example = MutableSparseNDimArray(example) assert ( mcode(example) == "SparseArray[{" "{1, 1, 1} -> 1, {1, 1, 2} -> 2, {1, 1, 3} -> 3, " "{1, 1, 4} -> 4, {1, 2, 1} -> 5, {1, 2, 2} -> 6, " "{1, 2, 3} -> 7, {1, 2, 4} -> 8, {1, 3, 1} -> 9, " "{1, 3, 2} -> 10, {1, 3, 3} -> 11, {1, 3, 4} -> 12, " "{2, 1, 1} -> 13, {2, 1, 2} -> 14, {2, 1, 3} -> 15, " "{2, 1, 4} -> 16, {2, 2, 1} -> 17, {2, 2, 2} -> 18, " "{2, 2, 3} -> 19, {2, 2, 4} -> 20, {2, 3, 1} -> 21, " "{2, 3, 2} -> 22, {2, 3, 3} -> 23, {2, 3, 4} -> 24" "}, {2, 3, 4}]" ) example = ImmutableSparseNDimArray(example) assert ( mcode(example) == "SparseArray[{" "{1, 1, 1} -> 1, {1, 1, 2} -> 2, {1, 1, 3} -> 3, " "{1, 1, 4} -> 4, {1, 2, 1} -> 5, {1, 2, 2} -> 6, " "{1, 2, 3} -> 7, {1, 2, 4} -> 8, {1, 3, 1} -> 9, " "{1, 3, 2} -> 10, {1, 3, 3} -> 11, {1, 3, 4} -> 12, " "{2, 1, 1} -> 13, {2, 1, 2} -> 14, {2, 1, 3} -> 15, " "{2, 1, 4} -> 16, {2, 2, 1} -> 17, {2, 2, 2} -> 18, " "{2, 2, 3} -> 19, {2, 2, 4} -> 20, {2, 3, 1} -> 21, " "{2, 3, 2} -> 22, {2, 3, 3} -> 23, {2, 3, 4} -> 24" "}, {2, 3, 4}]" )
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))
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]