test_functions.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from sympy.tensor.functions import TensorProduct
  2. from sympy.matrices.dense import Matrix
  3. from sympy.matrices.expressions.matexpr import MatrixSymbol
  4. from sympy.tensor.array import Array
  5. from sympy.abc import x, y, z
  6. from sympy.abc import i, j, k, l
  7. A = MatrixSymbol("A", 3, 3)
  8. B = MatrixSymbol("B", 3, 3)
  9. C = MatrixSymbol("C", 3, 3)
  10. def test_TensorProduct_construction():
  11. assert TensorProduct(3, 4) == 12
  12. assert isinstance(TensorProduct(A, A), TensorProduct)
  13. expr = TensorProduct(TensorProduct(x, y), z)
  14. assert expr == x*y*z
  15. expr = TensorProduct(TensorProduct(A, B), C)
  16. assert expr == TensorProduct(A, B, C)
  17. expr = TensorProduct(Matrix.eye(2), Array([[0, -1], [1, 0]]))
  18. assert expr == Array([
  19. [
  20. [[0, -1], [1, 0]],
  21. [[0, 0], [0, 0]]
  22. ],
  23. [
  24. [[0, 0], [0, 0]],
  25. [[0, -1], [1, 0]]
  26. ]
  27. ])
  28. def test_TensorProduct_shape():
  29. expr = TensorProduct(3, 4, evaluate=False)
  30. assert expr.shape == ()
  31. assert expr.rank() == 0
  32. expr = TensorProduct(Array([1, 2]), Array([x, y]), evaluate=False)
  33. assert expr.shape == (2, 2)
  34. assert expr.rank() == 2
  35. expr = TensorProduct(expr, expr, evaluate=False)
  36. assert expr.shape == (2, 2, 2, 2)
  37. assert expr.rank() == 4
  38. expr = TensorProduct(Matrix.eye(2), Array([[0, -1], [1, 0]]), evaluate=False)
  39. assert expr.shape == (2, 2, 2, 2)
  40. assert expr.rank() == 4
  41. def test_TensorProduct_getitem():
  42. expr = TensorProduct(A, B)
  43. assert expr[i, j, k, l] == A[i, j]*B[k, l]