test_function_diffgeom_book.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from sympy.diffgeom.rn import R2, R2_p, R2_r, R3_r
  2. from sympy.diffgeom import intcurve_series, Differential, WedgeProduct
  3. from sympy.core import symbols, Function, Derivative
  4. from sympy.simplify import trigsimp, simplify
  5. from sympy.functions import sqrt, atan2, sin, cos
  6. from sympy.matrices import Matrix
  7. # Most of the functionality is covered in the
  8. # test_functional_diffgeom_ch* tests which are based on the
  9. # example from the paper of Sussman and Wisdom.
  10. # If they do not cover something, additional tests are added in other test
  11. # functions.
  12. # From "Functional Differential Geometry" as of 2011
  13. # by Sussman and Wisdom.
  14. def test_functional_diffgeom_ch2():
  15. x0, y0, r0, theta0 = symbols('x0, y0, r0, theta0', real=True)
  16. x, y = symbols('x, y', real=True)
  17. f = Function('f')
  18. assert (R2_p.point_to_coords(R2_r.point([x0, y0])) ==
  19. Matrix([sqrt(x0**2 + y0**2), atan2(y0, x0)]))
  20. assert (R2_r.point_to_coords(R2_p.point([r0, theta0])) ==
  21. Matrix([r0*cos(theta0), r0*sin(theta0)]))
  22. assert R2_p.jacobian(R2_r, [r0, theta0]) == Matrix(
  23. [[cos(theta0), -r0*sin(theta0)], [sin(theta0), r0*cos(theta0)]])
  24. field = f(R2.x, R2.y)
  25. p1_in_rect = R2_r.point([x0, y0])
  26. p1_in_polar = R2_p.point([sqrt(x0**2 + y0**2), atan2(y0, x0)])
  27. assert field.rcall(p1_in_rect) == f(x0, y0)
  28. assert field.rcall(p1_in_polar) == f(x0, y0)
  29. p_r = R2_r.point([x0, y0])
  30. p_p = R2_p.point([r0, theta0])
  31. assert R2.x(p_r) == x0
  32. assert R2.x(p_p) == r0*cos(theta0)
  33. assert R2.r(p_p) == r0
  34. assert R2.r(p_r) == sqrt(x0**2 + y0**2)
  35. assert R2.theta(p_r) == atan2(y0, x0)
  36. h = R2.x*R2.r**2 + R2.y**3
  37. assert h.rcall(p_r) == x0*(x0**2 + y0**2) + y0**3
  38. assert h.rcall(p_p) == r0**3*sin(theta0)**3 + r0**3*cos(theta0)
  39. def test_functional_diffgeom_ch3():
  40. x0, y0 = symbols('x0, y0', real=True)
  41. x, y, t = symbols('x, y, t', real=True)
  42. f = Function('f')
  43. b1 = Function('b1')
  44. b2 = Function('b2')
  45. p_r = R2_r.point([x0, y0])
  46. s_field = f(R2.x, R2.y)
  47. v_field = b1(R2.x)*R2.e_x + b2(R2.y)*R2.e_y
  48. assert v_field.rcall(s_field).rcall(p_r).doit() == b1(
  49. x0)*Derivative(f(x0, y0), x0) + b2(y0)*Derivative(f(x0, y0), y0)
  50. assert R2.e_x(R2.r**2).rcall(p_r) == 2*x0
  51. v = R2.e_x + 2*R2.e_y
  52. s = R2.r**2 + 3*R2.x
  53. assert v.rcall(s).rcall(p_r).doit() == 2*x0 + 4*y0 + 3
  54. circ = -R2.y*R2.e_x + R2.x*R2.e_y
  55. series = intcurve_series(circ, t, R2_r.point([1, 0]), coeffs=True)
  56. series_x, series_y = zip(*series)
  57. assert all(
  58. [term == cos(t).taylor_term(i, t) for i, term in enumerate(series_x)])
  59. assert all(
  60. [term == sin(t).taylor_term(i, t) for i, term in enumerate(series_y)])
  61. def test_functional_diffgeom_ch4():
  62. x0, y0, theta0 = symbols('x0, y0, theta0', real=True)
  63. x, y, r, theta = symbols('x, y, r, theta', real=True)
  64. r0 = symbols('r0', positive=True)
  65. f = Function('f')
  66. b1 = Function('b1')
  67. b2 = Function('b2')
  68. p_r = R2_r.point([x0, y0])
  69. p_p = R2_p.point([r0, theta0])
  70. f_field = b1(R2.x, R2.y)*R2.dx + b2(R2.x, R2.y)*R2.dy
  71. assert f_field.rcall(R2.e_x).rcall(p_r) == b1(x0, y0)
  72. assert f_field.rcall(R2.e_y).rcall(p_r) == b2(x0, y0)
  73. s_field_r = f(R2.x, R2.y)
  74. df = Differential(s_field_r)
  75. assert df(R2.e_x).rcall(p_r).doit() == Derivative(f(x0, y0), x0)
  76. assert df(R2.e_y).rcall(p_r).doit() == Derivative(f(x0, y0), y0)
  77. s_field_p = f(R2.r, R2.theta)
  78. df = Differential(s_field_p)
  79. assert trigsimp(df(R2.e_x).rcall(p_p).doit()) == (
  80. cos(theta0)*Derivative(f(r0, theta0), r0) -
  81. sin(theta0)*Derivative(f(r0, theta0), theta0)/r0)
  82. assert trigsimp(df(R2.e_y).rcall(p_p).doit()) == (
  83. sin(theta0)*Derivative(f(r0, theta0), r0) +
  84. cos(theta0)*Derivative(f(r0, theta0), theta0)/r0)
  85. assert R2.dx(R2.e_x).rcall(p_r) == 1
  86. assert R2.dx(R2.e_x) == 1
  87. assert R2.dx(R2.e_y).rcall(p_r) == 0
  88. assert R2.dx(R2.e_y) == 0
  89. circ = -R2.y*R2.e_x + R2.x*R2.e_y
  90. assert R2.dx(circ).rcall(p_r).doit() == -y0
  91. assert R2.dy(circ).rcall(p_r) == x0
  92. assert R2.dr(circ).rcall(p_r) == 0
  93. assert simplify(R2.dtheta(circ).rcall(p_r)) == 1
  94. assert (circ - R2.e_theta).rcall(s_field_r).rcall(p_r) == 0
  95. def test_functional_diffgeom_ch6():
  96. u0, u1, u2, v0, v1, v2, w0, w1, w2 = symbols('u0:3, v0:3, w0:3', real=True)
  97. u = u0*R2.e_x + u1*R2.e_y
  98. v = v0*R2.e_x + v1*R2.e_y
  99. wp = WedgeProduct(R2.dx, R2.dy)
  100. assert wp(u, v) == u0*v1 - u1*v0
  101. u = u0*R3_r.e_x + u1*R3_r.e_y + u2*R3_r.e_z
  102. v = v0*R3_r.e_x + v1*R3_r.e_y + v2*R3_r.e_z
  103. w = w0*R3_r.e_x + w1*R3_r.e_y + w2*R3_r.e_z
  104. wp = WedgeProduct(R3_r.dx, R3_r.dy, R3_r.dz)
  105. assert wp(
  106. u, v, w) == Matrix(3, 3, [u0, u1, u2, v0, v1, v2, w0, w1, w2]).det()
  107. a, b, c = symbols('a, b, c', cls=Function)
  108. a_f = a(R3_r.x, R3_r.y, R3_r.z)
  109. b_f = b(R3_r.x, R3_r.y, R3_r.z)
  110. c_f = c(R3_r.x, R3_r.y, R3_r.z)
  111. theta = a_f*R3_r.dx + b_f*R3_r.dy + c_f*R3_r.dz
  112. dtheta = Differential(theta)
  113. da = Differential(a_f)
  114. db = Differential(b_f)
  115. dc = Differential(c_f)
  116. expr = dtheta - WedgeProduct(
  117. da, R3_r.dx) - WedgeProduct(db, R3_r.dy) - WedgeProduct(dc, R3_r.dz)
  118. assert expr.rcall(R3_r.e_x, R3_r.e_y) == 0