图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.0 KiB

  1. from sympy.matrices.expressions.slice import MatrixSlice
  2. from sympy.matrices.expressions import MatrixSymbol
  3. from sympy.abc import a, b, c, d, k, l, m, n
  4. from sympy.testing.pytest import raises, XFAIL
  5. from sympy.functions.elementary.integers import floor
  6. from sympy.assumptions import assuming, Q
  7. X = MatrixSymbol('X', n, m)
  8. Y = MatrixSymbol('Y', m, k)
  9. def test_shape():
  10. B = MatrixSlice(X, (a, b), (c, d))
  11. assert B.shape == (b - a, d - c)
  12. def test_entry():
  13. B = MatrixSlice(X, (a, b), (c, d))
  14. assert B[0,0] == X[a, c]
  15. assert B[k,l] == X[a+k, c+l]
  16. raises(IndexError, lambda : MatrixSlice(X, 1, (2, 5))[1, 0])
  17. assert X[1::2, :][1, 3] == X[1+2, 3]
  18. assert X[:, 1::2][3, 1] == X[3, 1+2]
  19. def test_on_diag():
  20. assert not MatrixSlice(X, (a, b), (c, d)).on_diag
  21. assert MatrixSlice(X, (a, b), (a, b)).on_diag
  22. def test_inputs():
  23. assert MatrixSlice(X, 1, (2, 5)) == MatrixSlice(X, (1, 2), (2, 5))
  24. assert MatrixSlice(X, 1, (2, 5)).shape == (1, 3)
  25. def test_slicing():
  26. assert X[1:5, 2:4] == MatrixSlice(X, (1, 5), (2, 4))
  27. assert X[1, 2:4] == MatrixSlice(X, 1, (2, 4))
  28. assert X[1:5, :].shape == (4, X.shape[1])
  29. assert X[:, 1:5].shape == (X.shape[0], 4)
  30. assert X[::2, ::2].shape == (floor(n/2), floor(m/2))
  31. assert X[2, :] == MatrixSlice(X, 2, (0, m))
  32. assert X[k, :] == MatrixSlice(X, k, (0, m))
  33. def test_exceptions():
  34. X = MatrixSymbol('x', 10, 20)
  35. raises(IndexError, lambda: X[0:12, 2])
  36. raises(IndexError, lambda: X[0:9, 22])
  37. raises(IndexError, lambda: X[-1:5, 2])
  38. @XFAIL
  39. def test_symmetry():
  40. X = MatrixSymbol('x', 10, 10)
  41. Y = X[:5, 5:]
  42. with assuming(Q.symmetric(X)):
  43. assert Y.T == X[5:, :5]
  44. def test_slice_of_slice():
  45. X = MatrixSymbol('x', 10, 10)
  46. assert X[2, :][:, 3][0, 0] == X[2, 3]
  47. assert X[:5, :5][:4, :4] == X[:4, :4]
  48. assert X[1:5, 2:6][1:3, 2] == X[2:4, 4]
  49. assert X[1:9:2, 2:6][1:3, 2] == X[3:7:2, 4]
  50. def test_negative_index():
  51. X = MatrixSymbol('x', 10, 10)
  52. assert X[-1, :] == X[9, :]