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

74 lines
2.6 KiB

  1. from sympy.core.function import (Derivative as D, Function)
  2. from sympy.core.relational import Eq
  3. from sympy.core.symbol import (Symbol, symbols)
  4. from sympy.functions.elementary.trigonometric import (cos, sin)
  5. from sympy.testing.pytest import raises
  6. from sympy.calculus.euler import euler_equations as euler
  7. def test_euler_interface():
  8. x = Function('x')
  9. y = Symbol('y')
  10. t = Symbol('t')
  11. raises(TypeError, lambda: euler())
  12. raises(TypeError, lambda: euler(D(x(t), t)*y(t), [x(t), y]))
  13. raises(ValueError, lambda: euler(D(x(t), t)*x(y), [x(t), x(y)]))
  14. raises(TypeError, lambda: euler(D(x(t), t)**2, x(0)))
  15. raises(TypeError, lambda: euler(D(x(t), t)*y(t), [t]))
  16. assert euler(D(x(t), t)**2/2, {x(t)}) == [Eq(-D(x(t), t, t), 0)]
  17. assert euler(D(x(t), t)**2/2, x(t), {t}) == [Eq(-D(x(t), t, t), 0)]
  18. def test_euler_pendulum():
  19. x = Function('x')
  20. t = Symbol('t')
  21. L = D(x(t), t)**2/2 + cos(x(t))
  22. assert euler(L, x(t), t) == [Eq(-sin(x(t)) - D(x(t), t, t), 0)]
  23. def test_euler_henonheiles():
  24. x = Function('x')
  25. y = Function('y')
  26. t = Symbol('t')
  27. L = sum(D(z(t), t)**2/2 - z(t)**2/2 for z in [x, y])
  28. L += -x(t)**2*y(t) + y(t)**3/3
  29. assert euler(L, [x(t), y(t)], t) == [Eq(-2*x(t)*y(t) - x(t) -
  30. D(x(t), t, t), 0),
  31. Eq(-x(t)**2 + y(t)**2 -
  32. y(t) - D(y(t), t, t), 0)]
  33. def test_euler_sineg():
  34. psi = Function('psi')
  35. t = Symbol('t')
  36. x = Symbol('x')
  37. L = D(psi(t, x), t)**2/2 - D(psi(t, x), x)**2/2 + cos(psi(t, x))
  38. assert euler(L, psi(t, x), [t, x]) == [Eq(-sin(psi(t, x)) -
  39. D(psi(t, x), t, t) +
  40. D(psi(t, x), x, x), 0)]
  41. def test_euler_high_order():
  42. # an example from hep-th/0309038
  43. m = Symbol('m')
  44. k = Symbol('k')
  45. x = Function('x')
  46. y = Function('y')
  47. t = Symbol('t')
  48. L = (m*D(x(t), t)**2/2 + m*D(y(t), t)**2/2 -
  49. k*D(x(t), t)*D(y(t), t, t) + k*D(y(t), t)*D(x(t), t, t))
  50. assert euler(L, [x(t), y(t)]) == [Eq(2*k*D(y(t), t, t, t) -
  51. m*D(x(t), t, t), 0),
  52. Eq(-2*k*D(x(t), t, t, t) -
  53. m*D(y(t), t, t), 0)]
  54. w = Symbol('w')
  55. L = D(x(t, w), t, w)**2/2
  56. assert euler(L) == [Eq(D(x(t, w), t, t, w, w), 0)]
  57. def test_issue_18653():
  58. x, y, z = symbols("x y z")
  59. f, g, h = symbols("f g h", cls=Function, args=(x, y))
  60. f, g, h = f(), g(), h()
  61. expr2 = f.diff(x)*h.diff(z)
  62. assert euler(expr2, (f,), (x, y)) == []