图片解析应用
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.

90 lines
3.4 KiB

  1. """Tests for tools for manipulation of expressions using paths. """
  2. from sympy.simplify.epathtools import epath, EPath
  3. from sympy.testing.pytest import raises
  4. from sympy.core.numbers import E
  5. from sympy.functions.elementary.trigonometric import (cos, sin)
  6. from sympy.abc import x, y, z, t
  7. def test_epath_select():
  8. expr = [((x, 1, t), 2), ((3, y, 4), z)]
  9. assert epath("/*", expr) == [((x, 1, t), 2), ((3, y, 4), z)]
  10. assert epath("/*/*", expr) == [(x, 1, t), 2, (3, y, 4), z]
  11. assert epath("/*/*/*", expr) == [x, 1, t, 3, y, 4]
  12. assert epath("/*/*/*/*", expr) == []
  13. assert epath("/[:]", expr) == [((x, 1, t), 2), ((3, y, 4), z)]
  14. assert epath("/[:]/[:]", expr) == [(x, 1, t), 2, (3, y, 4), z]
  15. assert epath("/[:]/[:]/[:]", expr) == [x, 1, t, 3, y, 4]
  16. assert epath("/[:]/[:]/[:]/[:]", expr) == []
  17. assert epath("/*/[:]", expr) == [(x, 1, t), 2, (3, y, 4), z]
  18. assert epath("/*/[0]", expr) == [(x, 1, t), (3, y, 4)]
  19. assert epath("/*/[1]", expr) == [2, z]
  20. assert epath("/*/[2]", expr) == []
  21. assert epath("/*/int", expr) == [2]
  22. assert epath("/*/Symbol", expr) == [z]
  23. assert epath("/*/tuple", expr) == [(x, 1, t), (3, y, 4)]
  24. assert epath("/*/__iter__?", expr) == [(x, 1, t), (3, y, 4)]
  25. assert epath("/*/int|tuple", expr) == [(x, 1, t), 2, (3, y, 4)]
  26. assert epath("/*/Symbol|tuple", expr) == [(x, 1, t), (3, y, 4), z]
  27. assert epath("/*/int|Symbol|tuple", expr) == [(x, 1, t), 2, (3, y, 4), z]
  28. assert epath("/*/int|__iter__?", expr) == [(x, 1, t), 2, (3, y, 4)]
  29. assert epath("/*/Symbol|__iter__?", expr) == [(x, 1, t), (3, y, 4), z]
  30. assert epath(
  31. "/*/int|Symbol|__iter__?", expr) == [(x, 1, t), 2, (3, y, 4), z]
  32. assert epath("/*/[0]/int", expr) == [1, 3, 4]
  33. assert epath("/*/[0]/Symbol", expr) == [x, t, y]
  34. assert epath("/*/[0]/int[1:]", expr) == [1, 4]
  35. assert epath("/*/[0]/Symbol[1:]", expr) == [t, y]
  36. assert epath("/Symbol", x + y + z + 1) == [x, y, z]
  37. assert epath("/*/*/Symbol", t + sin(x + 1) + cos(x + y + E)) == [x, x, y]
  38. def test_epath_apply():
  39. expr = [((x, 1, t), 2), ((3, y, 4), z)]
  40. func = lambda expr: expr**2
  41. assert epath("/*", expr, list) == [[(x, 1, t), 2], [(3, y, 4), z]]
  42. assert epath("/*/[0]", expr, list) == [([x, 1, t], 2), ([3, y, 4], z)]
  43. assert epath("/*/[1]", expr, func) == [((x, 1, t), 4), ((3, y, 4), z**2)]
  44. assert epath("/*/[2]", expr, list) == expr
  45. assert epath("/*/[0]/int", expr, func) == [((x, 1, t), 2), ((9, y, 16), z)]
  46. assert epath("/*/[0]/Symbol", expr, func) == [((x**2, 1, t**2), 2),
  47. ((3, y**2, 4), z)]
  48. assert epath(
  49. "/*/[0]/int[1:]", expr, func) == [((x, 1, t), 2), ((3, y, 16), z)]
  50. assert epath("/*/[0]/Symbol[1:]", expr, func) == [((x, 1, t**2),
  51. 2), ((3, y**2, 4), z)]
  52. assert epath("/Symbol", x + y + z + 1, func) == x**2 + y**2 + z**2 + 1
  53. assert epath("/*/*/Symbol", t + sin(x + 1) + cos(x + y + E), func) == \
  54. t + sin(x**2 + 1) + cos(x**2 + y**2 + E)
  55. def test_EPath():
  56. assert EPath("/*/[0]")._path == "/*/[0]"
  57. assert EPath(EPath("/*/[0]"))._path == "/*/[0]"
  58. assert isinstance(epath("/*/[0]"), EPath) is True
  59. assert repr(EPath("/*/[0]")) == "EPath('/*/[0]')"
  60. raises(ValueError, lambda: EPath(""))
  61. raises(ValueError, lambda: EPath("/"))
  62. raises(ValueError, lambda: EPath("/|x"))
  63. raises(ValueError, lambda: EPath("/["))
  64. raises(ValueError, lambda: EPath("/[0]%"))
  65. raises(NotImplementedError, lambda: EPath("Symbol"))