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_tensorproduct(): x, y, z, t = symbols("x y z t") from sympy.abc import a, b, c, d assert tensorproduct() == 1 assert tensorproduct([x]) == Array([x]) assert tensorproduct([x], [y]) == Array([[x * y]]) assert tensorproduct([x], [y], [z]) == Array([[[x * y * z]]]) assert tensorproduct([x], [y], [z], [t]) == Array([[[[x * y * z * t]]]]) assert tensorproduct(x) == x assert tensorproduct(x, y) == x * y assert tensorproduct(x, y, z) == x * y * z assert tensorproduct(x, y, z, t) == x * y * z * t for ArrayType in [ImmutableDenseNDimArray, ImmutableSparseNDimArray]: A = ArrayType([x, y]) B = ArrayType([1, 2, 3]) C = ArrayType([a, b, c, d]) assert tensorproduct(A, B, C) == ArrayType([ [ [a * x, b * x, c * x, d * x], [2 * a * x, 2 * b * x, 2 * c * x, 2 * d * x], [3 * a * x, 3 * b * x, 3 * c * x, 3 * d * x], ], [ [a * y, b * y, c * y, d * y], [2 * a * y, 2 * b * y, 2 * c * y, 2 * d * y], [3 * a * y, 3 * b * y, 3 * c * y, 3 * d * y], ], ]) assert tensorproduct([x, y], [1, 2, 3]) == tensorproduct(A, B) assert tensorproduct(A, 2) == ArrayType([2 * x, 2 * y]) assert tensorproduct(A, [2]) == ArrayType([[2 * x], [2 * y]]) assert tensorproduct([2], A) == ArrayType([[2 * x, 2 * y]]) assert tensorproduct(a, A) == ArrayType([a * x, a * y]) assert tensorproduct(a, A, B) == ArrayType([[a * x, 2 * a * x, 3 * a * x], [a * y, 2 * a * y, 3 * a * y]]) assert tensorproduct(A, B, a) == ArrayType([[a * x, 2 * a * x, 3 * a * x], [a * y, 2 * a * y, 3 * a * y]]) assert tensorproduct(B, a, A) == ArrayType([[a * x, a * y], [2 * a * x, 2 * a * y], [3 * a * x, 3 * a * y]]) # tests for large scale sparse array for SparseArrayType in [ImmutableSparseNDimArray, MutableSparseNDimArray]: a = SparseArrayType({1: 2, 3: 4}, (1000, 2000)) b = SparseArrayType({1: 2, 3: 4}, (1000, 2000)) assert tensorproduct(a, b) == ImmutableSparseNDimArray( { 2000001: 4, 2000003: 8, 6000001: 8, 6000003: 16 }, (1000, 2000, 1000, 2000))
def tensorproduct(*args): """ Tensor product among scalars or array-like objects. Examples ======== >>> from sympy.tensor.array import tensorproduct, Array >>> from sympy.abc import x, y, z, t >>> A = Array([[1, 2], [3, 4]]) >>> B = Array([x, y]) >>> tensorproduct(A, B) [[[x, y], [2*x, 2*y]], [[3*x, 3*y], [4*x, 4*y]]] >>> tensorproduct(A, x) [[x, 2*x], [3*x, 4*x]] >>> tensorproduct(A, B, B) [[[[x**2, x*y], [x*y, y**2]], [[2*x**2, 2*x*y], [2*x*y, 2*y**2]]], [[[3*x**2, 3*x*y], [3*x*y, 3*y**2]], [[4*x**2, 4*x*y], [4*x*y, 4*y**2]]]] Applying this function on two matrices will result in a rank 4 array. >>> from sympy import Matrix, eye >>> m = Matrix([[x, y], [z, t]]) >>> p = tensorproduct(eye(3), m) >>> p [[[[x, y], [z, t]], [[0, 0], [0, 0]], [[0, 0], [0, 0]]], [[[0, 0], [0, 0]], [[x, y], [z, t]], [[0, 0], [0, 0]]], [[[0, 0], [0, 0]], [[0, 0], [0, 0]], [[x, y], [z, t]]]] """ from sympy.tensor.array import SparseNDimArray, ImmutableSparseNDimArray if len(args) == 0: return S.One if len(args) == 1: return _arrayfy(args[0]) from sympy.tensor.array.expressions.array_expressions import _CodegenArrayAbstract from sympy.tensor.array.expressions.array_expressions import ArrayTensorProduct from sympy.tensor.array.expressions.array_expressions import _ArrayExpr from sympy.matrices.expressions.matexpr import MatrixSymbol if any( isinstance(arg, (_ArrayExpr, _CodegenArrayAbstract, MatrixSymbol)) for arg in args): return ArrayTensorProduct(*args) if len(args) > 2: return tensorproduct(tensorproduct(args[0], args[1]), *args[2:]) # length of args is 2: a, b = map(_arrayfy, args) if not isinstance(a, NDimArray) or not isinstance(b, NDimArray): return a * b if isinstance(a, SparseNDimArray) and isinstance(b, SparseNDimArray): lp = len(b) new_array = { k1 * lp + k2: v1 * v2 for k1, v1 in a._sparse_array.items() for k2, v2 in b._sparse_array.items() } return ImmutableSparseNDimArray(new_array, a.shape + b.shape) product_list = [i * j for i in Flatten(a) for j in Flatten(b)] return ImmutableDenseNDimArray(product_list, a.shape + b.shape)
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))