m2m模型翻译
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.

130 lines
4.0 KiB

6 months ago
  1. from sympy.core.relational import (Eq, Ne)
  2. from sympy.core.singleton import S
  3. from sympy.core.symbol import symbols
  4. from sympy.functions.elementary.miscellaneous import sqrt
  5. from sympy.functions.elementary.trigonometric import (cos, sin)
  6. from sympy.external import import_module
  7. from sympy.testing.pytest import skip
  8. from sympy.utilities.matchpy_connector import WildDot, WildPlus, WildStar, Replacer
  9. matchpy = import_module("matchpy")
  10. x, y, z = symbols("x y z")
  11. def _get_first_match(expr, pattern):
  12. from matchpy import ManyToOneMatcher, Pattern
  13. matcher = ManyToOneMatcher()
  14. matcher.add(Pattern(pattern))
  15. return next(iter(matcher.match(expr)))
  16. def test_matchpy_connector():
  17. if matchpy is None:
  18. skip("matchpy not installed")
  19. from multiset import Multiset
  20. from matchpy import Pattern, Substitution
  21. w_ = WildDot("w_")
  22. w__ = WildPlus("w__")
  23. w___ = WildStar("w___")
  24. expr = x + y
  25. pattern = x + w_
  26. p, subst = _get_first_match(expr, pattern)
  27. assert p == Pattern(pattern)
  28. assert subst == Substitution({'w_': y})
  29. expr = x + y + z
  30. pattern = x + w__
  31. p, subst = _get_first_match(expr, pattern)
  32. assert p == Pattern(pattern)
  33. assert subst == Substitution({'w__': Multiset([y, z])})
  34. expr = x + y + z
  35. pattern = x + y + z + w___
  36. p, subst = _get_first_match(expr, pattern)
  37. assert p == Pattern(pattern)
  38. assert subst == Substitution({'w___': Multiset()})
  39. def test_matchpy_optional():
  40. if matchpy is None:
  41. skip("matchpy not installed")
  42. from matchpy import Pattern, Substitution
  43. from matchpy import ManyToOneReplacer, ReplacementRule
  44. p = WildDot("p", optional=1)
  45. q = WildDot("q", optional=0)
  46. pattern = p*x + q
  47. expr1 = 2*x
  48. pa, subst = _get_first_match(expr1, pattern)
  49. assert pa == Pattern(pattern)
  50. assert subst == Substitution({'p': 2, 'q': 0})
  51. expr2 = x + 3
  52. pa, subst = _get_first_match(expr2, pattern)
  53. assert pa == Pattern(pattern)
  54. assert subst == Substitution({'p': 1, 'q': 3})
  55. expr3 = x
  56. pa, subst = _get_first_match(expr3, pattern)
  57. assert pa == Pattern(pattern)
  58. assert subst == Substitution({'p': 1, 'q': 0})
  59. expr4 = x*y + z
  60. pa, subst = _get_first_match(expr4, pattern)
  61. assert pa == Pattern(pattern)
  62. assert subst == Substitution({'p': y, 'q': z})
  63. replacer = ManyToOneReplacer()
  64. replacer.add(ReplacementRule(Pattern(pattern), lambda p, q: sin(p)*cos(q)))
  65. assert replacer.replace(expr1) == sin(2)*cos(0)
  66. assert replacer.replace(expr2) == sin(1)*cos(3)
  67. assert replacer.replace(expr3) == sin(1)*cos(0)
  68. assert replacer.replace(expr4) == sin(y)*cos(z)
  69. def test_replacer():
  70. if matchpy is None:
  71. skip("matchpy not installed")
  72. x1_ = WildDot("x1_")
  73. x2_ = WildDot("x2_")
  74. a_ = WildDot("a_", optional=S.One)
  75. b_ = WildDot("b_", optional=S.One)
  76. c_ = WildDot("c_", optional=S.Zero)
  77. replacer = Replacer(common_constraints=[
  78. matchpy.CustomConstraint(lambda a_: not a_.has(x)),
  79. matchpy.CustomConstraint(lambda b_: not b_.has(x)),
  80. matchpy.CustomConstraint(lambda c_: not c_.has(x)),
  81. ])
  82. # Rewrite the equation into implicit form, unless it's already solved:
  83. replacer.add(Eq(x1_, x2_), Eq(x1_ - x2_, 0), conditions_nonfalse=[Ne(x2_, 0), Ne(x1_, 0), Ne(x1_, x), Ne(x2_, x)])
  84. # Simple equation solver for real numbers:
  85. replacer.add(Eq(a_*x + b_, 0), Eq(x, -b_/a_))
  86. disc = b_**2 - 4*a_*c_
  87. replacer.add(
  88. Eq(a_*x**2 + b_*x + c_, 0),
  89. Eq(x, (-b_ - sqrt(disc))/(2*a_)) | Eq(x, (-b_ + sqrt(disc))/(2*a_)),
  90. conditions_nonfalse=[disc >= 0]
  91. )
  92. replacer.add(
  93. Eq(a_*x**2 + c_, 0),
  94. Eq(x, sqrt(-c_/a_)) | Eq(x, -sqrt(-c_/a_)),
  95. conditions_nonfalse=[-c_*a_ > 0]
  96. )
  97. assert replacer.replace(Eq(3*x, y)) == Eq(x, y/3)
  98. assert replacer.replace(Eq(x**2 + 1, 0)) == Eq(x**2 + 1, 0)
  99. assert replacer.replace(Eq(x**2, 4)) == (Eq(x, 2) | Eq(x, -2))
  100. assert replacer.replace(Eq(x**2 + 4*y*x + 4*y**2, 0)) == Eq(x, -2*y)