Lie-transform.scm 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;; The Lie transform is just the time-advance operator using the Lie
  21. ;;; derivative (see Hamiltonian.scm).
  22. (define (Lie-transform H delta-t)
  23. (make-operator
  24. (exp (* delta-t (Lie-derivative H)))
  25. `(Lie-transform ,H ,delta-t)))
  26. ;;; The generalization of Lie-transform to include time dependence.
  27. (define (flow-transform H delta-t)
  28. (make-operator
  29. (exp (* delta-t (flow-derivative H)))
  30. `(flow-transform ,H ,delta-t)))
  31. #|
  32. ;;; The general solution for a trajectory is:
  33. ;;;
  34. ;;; q(t,q0,p0) = A(q0,p0) cos (sqrt(k/m)*t + phi(q0,p0))
  35. ;;;
  36. ;;; where A(q0,p0) = sqrt(2/k)*sqrt(p0^2/(2*m) + (k/2)*q0^2)
  37. ;;; = sqrt((2/k)*E0)
  38. ;;;
  39. ;;; and phi(q0,p0) = - atan((1/sqrt(k*m))*(p0/q0))
  40. ;;;
  41. ;;; Thus, with initial conditions q0, p0
  42. ;;; we should get q(t) = q0*cos(sqrt(k/m)*t)+p0*sin(sqrt(k/m)*t)
  43. ;;;
  44. ;;; We can expand this as a Lie series:
  45. (define ((H-harmonic m k) state)
  46. (let ((q (coordinate state))
  47. (p (momentum state)))
  48. (+ (/ (square p) (* 2 m))
  49. (* 1/2 k (square q)))))
  50. ;;; This works, but it takes forever! -- hung in deriv, not in simplify!
  51. (series:for-each print-expression
  52. (((Lie-transform (H-harmonic 'm 'k) 'dt)
  53. state->q)
  54. (->H-state 0 'x_0 'p_0))
  55. 6)
  56. x_0
  57. (/ (* dt p_0) m)
  58. (/ (* -1/2 (expt dt 2) k x_0) m)
  59. (/ (* -1/6 (expt dt 3) k p_0) (expt m 2))
  60. (/ (* 1/24 (expt dt 4) (expt k 2) x_0) (expt m 2))
  61. (/ (* 1/120 (expt dt 5) (expt k 2) p_0) (expt m 3))
  62. ;Value: ...
  63. (series:for-each print-expression
  64. (((Lie-transform (H-harmonic 'm 'k) 'dt)
  65. momentum)
  66. (->H-state 0 'x_0 'p_0))
  67. 6)
  68. p_0
  69. (* -1 dt k x_0)
  70. (/ (* -1/2 (expt dt 2) k p_0) m)
  71. (/ (* 1/6 (expt dt 3) (expt k 2) x_0) m)
  72. (/ (* 1/24 (expt dt 4) (expt k 2) p_0) (expt m 2))
  73. (/ (* -1/120 (expt dt 5) (expt k 3) x_0) (expt m 2))
  74. ;Value: ...
  75. (series:for-each print-expression
  76. (((Lie-transform (H-harmonic 'm 'k) 'dt)
  77. (H-harmonic 'm 'k))
  78. (->H-state 0 'x_0 'p_0))
  79. 6)
  80. (/ (+ (* 1/2 k m (expt x_0 2)) (* 1/2 (expt p_0 2))) m)
  81. 0
  82. 0
  83. 0
  84. 0
  85. 0
  86. ;Value: ...
  87. |#
  88. #|
  89. (define ((H-central-polar m V) state)
  90. (let ((q (coordinate state))
  91. (p (momentum state)))
  92. (let ((r ((component 0) q))
  93. (phi ((component 1) q))
  94. (pr ((component 0) p))
  95. (pphi ((component 1) p)))
  96. (+ (/ (+ (square pr)
  97. (square (/ pphi r)))
  98. (* 2 m))
  99. (V r)))))
  100. (series:for-each print-expression
  101. (((Lie-transform
  102. (H-central-polar 'm (literal-function 'U))
  103. 'dt)
  104. state->q)
  105. (->H-state 0
  106. (coordinate-tuple 'r_0 'phi_0)
  107. (momentum-tuple 'p_r_0 'p_phi_0)))
  108. 4)
  109. (up r_0 phi_0)
  110. (up (/ (* dt p_r_0) m) (/ (* dt p_phi_0) (* m (expt r_0 2))))
  111. (up
  112. (+ (/ (* -1/2 (expt dt 2) ((D U) r_0)) m)
  113. (/ (* 1/2 (expt dt 2) (expt p_phi_0 2)) (* (expt m 2) (expt r_0 3))))
  114. (/ (* -1 (expt dt 2) p_phi_0 p_r_0) (* (expt m 2) (expt r_0 3))))
  115. (up
  116. (+
  117. (/ (* -1/6 (expt dt 3) p_r_0 (((expt D 2) U) r_0)) (expt m 2))
  118. (/ (* -1/2 (expt dt 3) (expt p_phi_0 2) p_r_0) (* (expt m 3) (expt r_0 4))))
  119. (+ (/ (* 1/3 (expt dt 3) p_phi_0 ((D U) r_0)) (* (expt m 2) (expt r_0 3)))
  120. (/ (* -1/3 (expt dt 3) (expt p_phi_0 3)) (* (expt m 3) (expt r_0 6)))
  121. (/ (* (expt dt 3) p_phi_0 (expt p_r_0 2)) (* (expt m 3) (expt r_0 4)))))
  122. ;Value: ...
  123. |#
  124. #|
  125. (define ((L-central-polar m V) local)
  126. (let ((q (coordinate local))
  127. (qdot (velocity local)))
  128. (let ((r (ref q 0))
  129. (phi (ref q 1))
  130. (rdot (ref qdot 0))
  131. (phidot (ref qdot 1)))
  132. (- (* 1/2 m
  133. (+ (square rdot)
  134. (square (* r phidot))) )
  135. (V r)))))
  136. ;;; I left this one that uses the Lagrangian because it appears to be
  137. ;;; used for timings
  138. (show-time
  139. (lambda ()
  140. (series:print
  141. (((Lie-transform
  142. (Lagrangian->Hamiltonian
  143. (L-central-polar 'm (lambda (r) (- (/ 'GM r)))))
  144. 'dt)
  145. state->q)
  146. (->H-state 0
  147. (coordinate-tuple 'r_0 'phi_0)
  148. (momentum-tuple 'p_r_0 'p_phi_0)))
  149. 4)))
  150. #|
  151. ;;; 13 March 2012: I changed the system so that the original
  152. ;;; normalization is available, without causing the original gcd bug.
  153. ;;; This is done by adding an additional stage of simplification.
  154. ;;; This new stage is enabled by "(divide-numbers-through-simplify
  155. ;;; true/false)" The control is in simplify/rules.scm. The default is
  156. ;;; now true, yielding the old representation.
  157. (up r_0 phi_0)
  158. (up (/ (* dt p_r_0) m) (/ (* dt p_phi_0) (* m (expt r_0 2))))
  159. (up
  160. (+ (/ (* -1/2 GM (expt dt 2)) (* m (expt r_0 2)))
  161. (/ (* 1/2 (* (expt dt 2) (expt p_phi_0 2))) (* (expt m 2) (expt r_0 3))))
  162. (/ (* -1 (expt dt 2) p_r_0 p_phi_0) (* (expt m 2) (expt r_0 3))))
  163. (up
  164. (+ (/ (* 1/3 (* GM (expt dt 3) p_r_0)) (* (expt m 2) (expt r_0 3)))
  165. (/ (* -1/2 (expt dt 3) p_r_0 (expt p_phi_0 2)) (* (expt m 3) (expt r_0 4))))
  166. (+ (/ (* (expt dt 3) p_phi_0 (expt p_r_0 2)) (* (expt m 3) (expt r_0 4)))
  167. (/ (* 1/3 (* GM (expt dt 3) p_phi_0)) (* (expt m 2) (expt r_0 5)))
  168. (/ (* -1/3 (expt dt 3) (expt p_phi_0 3)) (* (expt m 3) (expt r_0 6)))))
  169. ;process time: 1570 (1570 RUN + 0 GC); real time: 1573#| ... |#
  170. ;;; 30 Jan 2011: I changed the normalization of rational functions to
  171. ;;; favor integer coefficients. This was to eliminate a bug in the
  172. ;;; construction of polynomial gcds.
  173. ;;; This is the new result. It is algebraically equivalent to the old
  174. ;;; result.
  175. (up r_0 phi_0)
  176. (up (/ (* dt p_r_0) m) (/ (* dt p_phi_0) (* m (expt r_0 2))))
  177. (up
  178. (+ (/ (* -1 GM (expt dt 2)) (* 2 m (expt r_0 2)))
  179. (/ (* (expt dt 2) (expt p_phi_0 2)) (* 2 (expt m 2) (expt r_0 3))))
  180. (/ (* -1 (expt dt 2) p_r_0 p_phi_0) (* (expt m 2) (expt r_0 3))))
  181. (up
  182. (+ (/ (* GM (expt dt 3) p_r_0) (* 3 (expt m 2) (expt r_0 3)))
  183. (/ (* -1 (expt dt 3) (expt p_phi_0 2) p_r_0) (* 2 (expt m 3) (expt r_0 4))))
  184. (+ (/ (* (expt dt 3) (expt p_r_0 2) p_phi_0) (* (expt m 3) (expt r_0 4)))
  185. (/ (* GM (expt dt 3) p_phi_0) (* 3 (expt m 2) (expt r_0 5)))
  186. (/ (* -1 (expt dt 3) (expt p_phi_0 3)) (* 3 (expt m 3) (expt r_0 6)))))
  187. ;;; Binah 30 Jan 2011
  188. ;process time: 1600 (1600 RUN + 0 GC); real time: 1607#| ... |#
  189. |#
  190. #|
  191. (up r_0 phi_0)
  192. (up (/ (* dt p_r_0) m) (/ (* dt p_phi_0) (* m (expt r_0 2))))
  193. (up
  194. (+ (/ (* -1/2 GM (expt dt 2)) (* m (expt r_0 2)))
  195. (/ (* 1/2 (expt dt 2) (expt p_phi_0 2)) (* (expt m 2) (expt r_0 3))))
  196. (/ (* -1 (expt dt 2) p_phi_0 p_r_0) (* (expt m 2) (expt r_0 3))))
  197. (up
  198. (+
  199. (/ (* 1/3 GM (expt dt 3) p_r_0) (* (expt m 2) (expt r_0 3)))
  200. (/ (* -1/2 (expt dt 3) (expt p_phi_0 2) p_r_0) (* (expt m 3) (expt r_0 4))))
  201. (+ (/ (* (expt dt 3) p_phi_0 (expt p_r_0 2)) (* (expt m 3) (expt r_0 4)))
  202. (/ (* 1/3 GM (expt dt 3) p_phi_0) (* (expt m 2) (expt r_0 5)))
  203. (/ (* -1/3 (expt dt 3) (expt p_phi_0 3)) (* (expt m 3) (expt r_0 6)))))
  204. |#
  205. ;;; Binah: 9 December 2009
  206. ;;; With simple-derivative-internal memoized
  207. ;;; process time: 2830 (2830 RUN + 0 GC); real time: 2846
  208. ;;; Without memoization
  209. ;;; process time: 1360 (1360 RUN + 0 GC); real time: 1377
  210. ;;; But memoization makes some stuff feasible (see calculus/tensor.scm).
  211. ;;;
  212. ;;; Earlier
  213. ;;; MAHARAL
  214. ;;; process time: 3940 (3710 RUN + 230 GC); real time: 3956
  215. ;;; HOD
  216. ;;; process time: 14590 (13610 RUN + 980 GC); real time: 14588
  217. ;;; PLANET003 600MHz PIII
  218. ;;; process time: 19610 (17560 RUN + 2050 GC); real time: 19610
  219. ;;; HEIFETZ xeon 400MHz 512K
  220. ;;; process time: 27380 (24250 RUN + 3130 GC); real time: 27385
  221. ;;; GEVURAH 300 MHz
  222. ;;; process time: 36070 (33800 RUN + 2270 GC); real time: 36072
  223. ;;; MAHARAL
  224. ;;; process time: 56390 (50970 RUN + 5420 GC); real time: 56386
  225. ;;; ACTION1 200MHz Pentium Pro
  226. ;;; process time: 55260 (49570 RUN + 5690 GC); real time: 55257
  227. ;;; PPA 200MHz Pentium Pro
  228. ;;; process time: 58840 (56500 RUN + 2340 GC); real time: 59165
  229. ;;; ZOHAR 33MHz 486
  230. ;;; process time: 463610 (443630 RUN + 19980 GC); real time: 485593
  231. |#