test_matrixutils.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from sympy.core.random import randint
  2. from sympy.core.numbers import Integer
  3. from sympy.matrices.dense import (Matrix, ones, zeros)
  4. from sympy.physics.quantum.matrixutils import (
  5. to_sympy, to_numpy, to_scipy_sparse, matrix_tensor_product,
  6. matrix_to_zero, matrix_zeros, numpy_ndarray, scipy_sparse_matrix
  7. )
  8. from sympy.external import import_module
  9. from sympy.testing.pytest import skip
  10. m = Matrix([[1, 2], [3, 4]])
  11. def test_sympy_to_sympy():
  12. assert to_sympy(m) == m
  13. def test_matrix_to_zero():
  14. assert matrix_to_zero(m) == m
  15. assert matrix_to_zero(Matrix([[0, 0], [0, 0]])) == Integer(0)
  16. np = import_module('numpy')
  17. def test_to_numpy():
  18. if not np:
  19. skip("numpy not installed.")
  20. result = np.array([[1, 2], [3, 4]], dtype='complex')
  21. assert (to_numpy(m) == result).all()
  22. def test_matrix_tensor_product():
  23. if not np:
  24. skip("numpy not installed.")
  25. l1 = zeros(4)
  26. for i in range(16):
  27. l1[i] = 2**i
  28. l2 = zeros(4)
  29. for i in range(16):
  30. l2[i] = i
  31. l3 = zeros(2)
  32. for i in range(4):
  33. l3[i] = i
  34. vec = Matrix([1, 2, 3])
  35. #test for Matrix known 4x4 matricies
  36. numpyl1 = np.array(l1.tolist())
  37. numpyl2 = np.array(l2.tolist())
  38. numpy_product = np.kron(numpyl1, numpyl2)
  39. args = [l1, l2]
  40. sympy_product = matrix_tensor_product(*args)
  41. assert numpy_product.tolist() == sympy_product.tolist()
  42. numpy_product = np.kron(numpyl2, numpyl1)
  43. args = [l2, l1]
  44. sympy_product = matrix_tensor_product(*args)
  45. assert numpy_product.tolist() == sympy_product.tolist()
  46. #test for other known matrix of different dimensions
  47. numpyl2 = np.array(l3.tolist())
  48. numpy_product = np.kron(numpyl1, numpyl2)
  49. args = [l1, l3]
  50. sympy_product = matrix_tensor_product(*args)
  51. assert numpy_product.tolist() == sympy_product.tolist()
  52. numpy_product = np.kron(numpyl2, numpyl1)
  53. args = [l3, l1]
  54. sympy_product = matrix_tensor_product(*args)
  55. assert numpy_product.tolist() == sympy_product.tolist()
  56. #test for non square matrix
  57. numpyl2 = np.array(vec.tolist())
  58. numpy_product = np.kron(numpyl1, numpyl2)
  59. args = [l1, vec]
  60. sympy_product = matrix_tensor_product(*args)
  61. assert numpy_product.tolist() == sympy_product.tolist()
  62. numpy_product = np.kron(numpyl2, numpyl1)
  63. args = [vec, l1]
  64. sympy_product = matrix_tensor_product(*args)
  65. assert numpy_product.tolist() == sympy_product.tolist()
  66. #test for random matrix with random values that are floats
  67. random_matrix1 = np.random.rand(randint(1, 5), randint(1, 5))
  68. random_matrix2 = np.random.rand(randint(1, 5), randint(1, 5))
  69. numpy_product = np.kron(random_matrix1, random_matrix2)
  70. args = [Matrix(random_matrix1.tolist()), Matrix(random_matrix2.tolist())]
  71. sympy_product = matrix_tensor_product(*args)
  72. assert not (sympy_product - Matrix(numpy_product.tolist())).tolist() > \
  73. (ones(sympy_product.rows, sympy_product.cols)*epsilon).tolist()
  74. #test for three matrix kronecker
  75. sympy_product = matrix_tensor_product(l1, vec, l2)
  76. numpy_product = np.kron(l1, np.kron(vec, l2))
  77. assert numpy_product.tolist() == sympy_product.tolist()
  78. scipy = import_module('scipy', import_kwargs={'fromlist': ['sparse']})
  79. def test_to_scipy_sparse():
  80. if not np:
  81. skip("numpy not installed.")
  82. if not scipy:
  83. skip("scipy not installed.")
  84. else:
  85. sparse = scipy.sparse
  86. result = sparse.csr_matrix([[1, 2], [3, 4]], dtype='complex')
  87. assert np.linalg.norm((to_scipy_sparse(m) - result).todense()) == 0.0
  88. epsilon = .000001
  89. def test_matrix_zeros_sympy():
  90. sym = matrix_zeros(4, 4, format='sympy')
  91. assert isinstance(sym, Matrix)
  92. def test_matrix_zeros_numpy():
  93. if not np:
  94. skip("numpy not installed.")
  95. num = matrix_zeros(4, 4, format='numpy')
  96. assert isinstance(num, numpy_ndarray)
  97. def test_matrix_zeros_scipy():
  98. if not np:
  99. skip("numpy not installed.")
  100. if not scipy:
  101. skip("scipy not installed.")
  102. sci = matrix_zeros(4, 4, format='scipy.sparse')
  103. assert isinstance(sci, scipy_sparse_matrix)