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.

62 lines
2.0 KiB

6 months ago
  1. from sympy.core.singleton import S
  2. from sympy.strategies.rl import (rm_id, glom, flatten, unpack, sort, distribute,
  3. subs, rebuild)
  4. from sympy.core.basic import Basic
  5. def test_rm_id():
  6. rmzeros = rm_id(lambda x: x == 0)
  7. assert rmzeros(Basic(S(0), S(1))) == Basic(S(1))
  8. assert rmzeros(Basic(S(0), S(0))) == Basic(S(0))
  9. assert rmzeros(Basic(S(2), S(1))) == Basic(S(2), S(1))
  10. def test_glom():
  11. from sympy.core.add import Add
  12. from sympy.abc import x
  13. key = lambda x: x.as_coeff_Mul()[1]
  14. count = lambda x: x.as_coeff_Mul()[0]
  15. newargs = lambda cnt, arg: cnt * arg
  16. rl = glom(key, count, newargs)
  17. result = rl(Add(x, -x, 3*x, 2, 3, evaluate=False))
  18. expected = Add(3*x, 5)
  19. assert set(result.args) == set(expected.args)
  20. def test_flatten():
  21. assert flatten(Basic(S(1), S(2), Basic(S(3), S(4)))) == \
  22. Basic(S(1), S(2), S(3), S(4))
  23. def test_unpack():
  24. assert unpack(Basic(S(2))) == 2
  25. assert unpack(Basic(S(2), S(3))) == Basic(S(2), S(3))
  26. def test_sort():
  27. assert sort(str)(Basic(S(3),S(1),S(2))) == Basic(S(1),S(2),S(3))
  28. def test_distribute():
  29. class T1(Basic): pass
  30. class T2(Basic): pass
  31. distribute_t12 = distribute(T1, T2)
  32. assert distribute_t12(T1(S(1), S(2), T2(S(3), S(4)), S(5))) == \
  33. T2(T1(S(1), S(2), S(3), S(5)), T1(S(1), S(2), S(4), S(5)))
  34. assert distribute_t12(T1(S(1), S(2), S(3))) == T1(S(1), S(2), S(3))
  35. def test_distribute_add_mul():
  36. from sympy.core.add import Add
  37. from sympy.core.mul import Mul
  38. from sympy.core.symbol import symbols
  39. x, y = symbols('x, y')
  40. expr = Mul(2, Add(x, y), evaluate=False)
  41. expected = Add(Mul(2, x), Mul(2, y))
  42. distribute_mul = distribute(Mul, Add)
  43. assert distribute_mul(expr) == expected
  44. def test_subs():
  45. rl = subs(1, 2)
  46. assert rl(1) == 2
  47. assert rl(3) == 3
  48. def test_rebuild():
  49. from sympy.core.add import Add
  50. expr = Basic.__new__(Add, S(1), S(2))
  51. assert rebuild(expr) == 3