Hamiltonian.scm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. ;;; Hamiltonian mechanics requires a phase
  21. ;;; space QxP, and a function H:RxQxP --> R
  22. ;;; A system has a dynamic state, which has the time, the
  23. ;;; configuration, and the momenta. Hamiltonian mechanics is
  24. ;;; formulated in terms of the dynamic state.
  25. (define (->H-state t q p)
  26. (vector t q p))
  27. (define (H-state? s)
  28. (and (up? s)
  29. (fix:= (s:length s) 3)
  30. (numerical-quantity? (ref s 0))
  31. (or (and (numerical-quantity? (ref s 1))
  32. (numerical-quantity? (ref s 2)))
  33. (and (up? (ref s 1))
  34. (down? (ref s 2))
  35. (= (s:dimension (ref s 1))
  36. (s:dimension (ref s 2)))))))
  37. (define (compatible-H-state? s)
  38. (and (down? s)
  39. (fix:= (s:length s) 3)
  40. (numerical-quantity? (ref s 0))
  41. (or (and (numerical-quantity? (ref s 1))
  42. (numerical-quantity? (ref s 2)))
  43. (and (down? (ref s 1))
  44. (up? (ref s 2))
  45. (= (s:dimension (ref s 1))
  46. (s:dimension (ref s 2)))))))
  47. (define (state->p state)
  48. (if (not (and (vector? state) (fix:> (vector-length state) 2)))
  49. (error "Cannot extract momentum from" state))
  50. (ref state 2))
  51. (define momentum state->p)
  52. (define momenta state->p)
  53. (define P state->p)
  54. (define (state->qp dynamic-state)
  55. (vector-tail dynamic-state 1))
  56. (define (literal-Hamiltonian-state n-dof)
  57. (up (literal-number (generate-uninterned-symbol 't))
  58. (s:generate n-dof 'up
  59. (lambda (i)
  60. (literal-number (generate-uninterned-symbol 'x))))
  61. (s:generate n-dof 'down
  62. (lambda (i)
  63. (literal-number (generate-uninterned-symbol 'p))))))
  64. (define* ((Lstate->Hstate L) Ls)
  65. (up (time Ls)
  66. (coordinate Ls)
  67. (((partial 2) L) Ls)))
  68. (define* ((Hstate->Lstate H) Hs)
  69. (up (time Hs)
  70. (coordinate Hs)
  71. (((partial 2) H) Hs)))
  72. (define (H-state->matrix s)
  73. (s->m (compatible-shape s) s 1))
  74. ;; (define (matrix->H-state m)
  75. ;; (assert (= (m:num-cols m) 1))
  76. ;; (assert (and (odd? (m:num-rows m))
  77. ;; (> (m:num-rows m) 2)))
  78. ;; (let ((n (quotient (- (m:num-rows m) 1) 2)))
  79. ;; (let ((s (up (generate-uninterned-symbol)
  80. ;; (s:generate n 'up generate-uninterned-symbol)
  81. ;; (s:generate n 'down generate-uninterned-symbol))))
  82. ;; (m->s (compatible-shape s) m 1))))
  83. (define (matrix->H-state m s)
  84. (assert (= (m:num-cols m) 1))
  85. (assert (and (odd? (m:num-rows m))
  86. (> (m:num-rows m) 2)))
  87. (m->s (compatible-shape s) m 1))
  88. (define (degrees-of-freedom H-state)
  89. (assert (= (s:length H-state) 3))
  90. (assert (= (s:dimension (coordinate H-state))
  91. (s:dimension (momentum H-state))))
  92. (s:dimension (coordinate H-state)))
  93. #|
  94. (matrix->H-state (H-state->matrix (up 't (up 'x 'y) (down 'p_x 'p_y))))
  95. #|
  96. (up t (up x y) (down p_x p_y))
  97. |#
  98. (H-state->matrix
  99. (matrix->H-state
  100. (matrix-by-rows (list 't)
  101. (list 'x)
  102. (list 'y)
  103. (list 'p_x)
  104. (list 'p_y))))
  105. #|
  106. (matrix-by-rows (list t) (list x) (list y) (list p_x) (list p_y))
  107. |#
  108. |#
  109. (define (make-Hamiltonian kinetic-energy potential-energy)
  110. (+ kinetic-energy potential-energy))
  111. (define* ((Hamilton-equations Hamiltonian) q p)
  112. (let ((H-state-path (qp->H-state-path q p))
  113. (dH (Hamiltonian->state-derivative Hamiltonian)))
  114. (- (D H-state-path)
  115. (compose dH
  116. H-state-path))))
  117. (define* ((qp->H-state-path q p) t)
  118. (->H-state t (q t) (p t)))
  119. (define* ((Hamiltonian->state-derivative Hamiltonian) H-state)
  120. (->H-state 1
  121. (((partial 2) Hamiltonian) H-state)
  122. (- (((partial 1) Hamiltonian) H-state))))
  123. ;;; For compatibility with 1st edition
  124. (define phase-space-derivative
  125. Hamiltonian->state-derivative)
  126. (define* ((D-phase-space H) s)
  127. (up 0 (((partial 2) H) s) (- (((partial 1) H) s))))
  128. ;;; If we express the energy in terms of t,Q,P we have the Hamiltonian.
  129. ;;; A Hamiltonian is an example of an H-function: an H-function takes
  130. ;;; 2 vector arguments and a scalar argument (t, Q, P). It produces a
  131. ;;; scalar result.
  132. #|
  133. (define ((H-rectangular m V) H-state)
  134. (let ((q (coordinate H-state))
  135. (p (momentum H-state)))
  136. (+ (/ (square p) (* 2 m))
  137. (V (ref q 0) (ref q 1)))))
  138. (show-expression
  139. (((Hamilton-equations
  140. (H-rectangular
  141. 'm
  142. (literal-function 'V (-> (X Real Real) Real))))
  143. (coordinate-tuple (literal-function 'x)
  144. (literal-function 'y))
  145. (momentum-tuple (literal-function 'p_x)
  146. (literal-function 'p_y)))
  147. 't))
  148. (up
  149. 0
  150. (up (+ ((D x) t) (/ (* -1 (p_x t)) m))
  151. (+ ((D y) t) (/ (* -1 (p_y t)) m)))
  152. (down (+ ((D p_x) t) (((partial 0) V) (x t) (y t)))
  153. (+ ((D p_y) t) (((partial 1) V) (x t) (y t)))))
  154. |#
  155. ;;; If we express the energy in terms of t,Q,P we have the Hamiltonian
  156. ;;; H(t,Q,P) = P*Qdot - L(t, Q, Qdot(t, Q, P))
  157. ;;; To do this we need to invert P(t, Q, Qdot) to get Qdot(t, Q, P).
  158. ;;; This is easy when L is a quadratic form in Qdot:
  159. ;;; L(t, Q, Qdot) = 1/2*Qdot*M*Qdot + B*Qdot - V
  160. ;;; Fortunately this is the case in almost all of Newtonian mechanics,
  161. ;;; otherwise the P(t,Q,Qdot) function would be much more difficult to
  162. ;;; invert to obtain Qdot(t,Q,P).
  163. ;;; Assume that F is quadratic in its arguments
  164. ;;; F(u) = 1/2 A u u + b u + c
  165. ;;; then v = A u + b, so u = A^(-1) (v - b)
  166. #|
  167. (define (Legendre-transform-procedure F)
  168. (let ((w-of-v (D F)))
  169. (define (G w)
  170. (let ((z (dual-zero w)))
  171. (let ((M ((D w-of-v) z))
  172. (b (w-of-v z)))
  173. (let ((v (/ (- w b) M)))
  174. (- (* w v) (F v))))))
  175. G))
  176. (define (dual-zero v)
  177. (if (structure? v)
  178. (s:generate (s:length v) (s:opposite v)
  179. (lambda (i) :zero))
  180. :zero))
  181. |#
  182. ;;; A better definition of Legendre transform that works for
  183. ;;; structured coordinates that have substructure
  184. (define (Legendre-transform-procedure F)
  185. (let ((w-of-v (D F)))
  186. (define (G w)
  187. (let ((z (compatible-zero w)))
  188. (let ((M ((D w-of-v) z))
  189. (b (w-of-v z)))
  190. ;; DM=0 for this code to be correct.
  191. (let ((v (solve-linear-left M (- w b))))
  192. (- (* w v) (F v))))))
  193. G))
  194. (define Legendre-transform
  195. (make-operator Legendre-transform-procedure
  196. 'Legendre-transform))
  197. ;;; Notice that Lagrangians and Hamiltonians are symmetrical with
  198. ;;; respect to the Legendre transform.
  199. (define* ((Lagrangian->Hamiltonian-procedure the-Lagrangian) H-state)
  200. (let ((t (time H-state))
  201. (q (coordinate H-state))
  202. (p (momentum H-state)))
  203. (define (L qdot)
  204. (the-Lagrangian (->L-state t q qdot)))
  205. ((Legendre-transform-procedure L) p)))
  206. (define Lagrangian->Hamiltonian
  207. (make-operator Lagrangian->Hamiltonian-procedure
  208. 'Lagrangian->Hamiltonian))
  209. (define* ((Hamiltonian->Lagrangian-procedure the-Hamiltonian) L-state)
  210. (let ((t (time L-state))
  211. (q (coordinate L-state))
  212. (qdot (velocity L-state)))
  213. (define (H p)
  214. (the-Hamiltonian (->H-state t q p)))
  215. ((Legendre-transform-procedure H) qdot)))
  216. (define Hamiltonian->Lagrangian
  217. (make-operator Hamiltonian->Lagrangian-procedure
  218. 'Hamiltonian->Lagrangian))
  219. #|
  220. (define ((L-rectangular m V) local)
  221. (let ((q (coordinate local))
  222. (qdot (velocity local)))
  223. (- (* 1/2 m (square qdot))
  224. (V (ref q 0) (ref q 1)))))
  225. (show-expression
  226. ((Lagrangian->Hamiltonian
  227. (L-rectangular 'm
  228. (literal-function 'V
  229. (-> (X Real Real) Real))))
  230. (->H-state 't
  231. (coordinate-tuple 'x 'y)
  232. (momentum-tuple 'p_x 'p_y))))
  233. (+ (V x y)
  234. (/ (* 1/2 (expt p_x 2)) m)
  235. (/ (* 1/2 (expt p_y 2)) m))
  236. |#
  237. #|
  238. (define ((L-central-polar m V) local)
  239. (let ((q (coordinate local))
  240. (qdot (velocity local)))
  241. (let ((r (ref q 0))
  242. (phi (ref q 1))
  243. (rdot (ref qdot 0))
  244. (phidot (ref qdot 1)))
  245. (- (* 1/2 m
  246. (+ (square rdot)
  247. (square (* r phidot))) )
  248. (V r)))))
  249. (show-expression
  250. ((Lagrangian->Hamiltonian
  251. (L-central-polar 'm (literal-function 'V)))
  252. (->H-state 't
  253. (coordinate-tuple 'r 'phi)
  254. (momentum-tuple 'p_r 'p_phi))))
  255. (+ (V r)
  256. (/ (* 1/2 (expt p_r 2)) m)
  257. (/ (* 1/2 (expt p_phi 2)) (* m (expt r 2))))
  258. (show-expression
  259. (((Hamilton-equations
  260. (Lagrangian->Hamiltonian
  261. (L-central-polar 'm (literal-function 'V))))
  262. (coordinate-tuple (literal-function 'r)
  263. (literal-function 'phi))
  264. (momentum-tuple (literal-function 'p_r)
  265. (literal-function 'p_phi)))
  266. 't))
  267. (up
  268. 0
  269. (up (+ ((D r) t) (/ (* -1 (p_r t)) m))
  270. (+ ((D phi) t) (/ (* -1 (p_phi t)) (* m (expt (r t) 2)))))
  271. (down
  272. (+ ((D p_r) t)
  273. ((D V) (r t))
  274. (/ (* -1 (expt (p_phi t) 2)) (* m (expt (r t) 3))))
  275. ((D p_phi) t)))
  276. ;;; If we substitute a Coulomb potential in for V we get the equations
  277. ;;; for satellite motion around a spherical primary.
  278. (show-expression
  279. (((Hamilton-equations
  280. (Lagrangian->Hamiltonian
  281. (L-central-polar 'm
  282. (lambda (r)
  283. (- (/ (* 'GM 'm) r))))))
  284. (coordinate-tuple (literal-function 'r)
  285. (literal-function 'phi))
  286. (momentum-tuple (literal-function 'p_r)
  287. (literal-function 'p_phi)))
  288. 't))
  289. (up 0
  290. (up (+ ((D r) t) (/ (* -1 (p_r t)) m))
  291. (+ ((D phi) t) (/ (* -1 (p_phi t)) (* m (expt (r t) 2)))))
  292. (down
  293. (+ ((D p_r) t)
  294. (/ (* GM m) (expt (r t) 2))
  295. (/ (* -1 (expt (p_phi t) 2)) (* m (expt (r t) 3))))
  296. ((D p_phi) t)))
  297. (define ((H-central-polar m V) state)
  298. (let ((q (coordinate state))
  299. (p (momentum state)))
  300. (let ((r ((component 0) q))
  301. (phi ((component 1) q))
  302. (pr ((component 0) p))
  303. (pphi ((component 1) p)))
  304. (+ (/ (+ (square pr)
  305. (square (/ pphi r)))
  306. (* 2 m))
  307. (V r)))))
  308. |#
  309. #|
  310. (define ((L-harmonic m k) local)
  311. (let ((q (coordinate local))
  312. (v (velocity local)))
  313. (- (* 1/2 m (square v))
  314. (* 1/2 k (square q)))))
  315. (show-expression
  316. (((Hamilton-equations
  317. (Lagrangian->Hamiltonian (L-harmonic 'm 'k)))
  318. (coordinate-tuple (literal-function 'x_1)
  319. (literal-function 'x_2))
  320. (momentum-tuple (literal-function 'p_1)
  321. (literal-function 'p_2)))
  322. 't))
  323. (up 0
  324. (up (+ ((D x_1) t) (/ (* -1 (p_1 t)) m_1))
  325. (+ ((D x_2) t) (/ (* -1 (p_2 t)) m_2)))
  326. (down (+ (* c (x_2 t)) (* k_1 (x_1 t)) ((D p_1) t))
  327. (+ (* c (x_1 t)) (* k_2 (x_2 t)) ((D p_2) t))))
  328. ;;; Continuing with our coupled harmonic oscillators
  329. ;;; we obtain the Hamiltonian:
  330. (define ((L-coupled-harmonic m k) state)
  331. (let ((q (coordinate state))
  332. (qdot (velocity state)))
  333. (- (* 1/2 qdot m qdot)
  334. (* 1/2 q k q))))
  335. (show-expression
  336. ((Lagrangian->Hamiltonian
  337. (L-coupled-harmonic (down (down 'm_1 0)
  338. (down 0 'm_2))
  339. (down (down 'k_1 'c)
  340. (down 'c 'k_2))))
  341. (->H-state 't
  342. (coordinate-tuple 'x_1 'x_2)
  343. (momentum-tuple 'p_1 'p_2))))
  344. (+ (* c x_1 x_2)
  345. (* 1/2 k_1 (expt x_1 2))
  346. (* 1/2 k_2 (expt x_2 2))
  347. (/ (* 1/2 (expt p_2 2)) m_2)
  348. (/ (* 1/2 (expt p_1 2)) m_1))
  349. (show-expression
  350. (((Hamilton-equations
  351. (Lagrangian->Hamiltonian
  352. (L-coupled-harmonic (down (down 'm_1 0)
  353. (down 0 'm_2))
  354. (down (down 'k_1 'c)
  355. (down 'c 'k_2)))))
  356. (coordinate-tuple (literal-function 'x_1)
  357. (literal-function 'x_2))
  358. (momentum-tuple (literal-function 'p_1)
  359. (literal-function 'p_2)))
  360. 't))
  361. (up
  362. 0
  363. (up (+ ((D x_1) t) (/ (* -1 (p_1 t)) m_1))
  364. (+ ((D x_2) t) (/ (* -1 (p_2 t)) m_2)))
  365. (down (+ (* c (x_2 t)) (* k_1 (x_1 t)) ((D p_1) t))
  366. (+ (* c (x_1 t)) (* k_2 (x_2 t)) ((D p_2) t))))
  367. |#
  368. #|
  369. ;;; Continuation of demonstration of bundled coordinates.
  370. (define ((L-two-particle m1 m2) local)
  371. (let ((x (coordinate local))
  372. (v (velocity local))
  373. (V (literal-function 'V (-> (X (^ Real 2) (^ Real 2)) Real))))
  374. (let ((x1 (ref x 0)) (x2 (ref x 1))
  375. (v1 (ref v 0)) (v2 (ref v 1)))
  376. (- (+ (* 1/2 m1 (square v1))
  377. (* 1/2 m2 (square v2)))
  378. (V x1 x2)))))
  379. (show-expression
  380. (((Hamilton-equations
  381. (Lagrangian->Hamiltonian
  382. (L-two-particle 'm_1 'm_2)))
  383. (coordinate-tuple (coordinate-tuple (literal-function 'x_1)
  384. (literal-function 'y_1))
  385. (coordinate-tuple (literal-function 'x_2)
  386. (literal-function 'y_2)))
  387. (momentum-tuple (momentum-tuple (literal-function 'p_x_1)
  388. (literal-function 'p_y_1))
  389. (momentum-tuple (literal-function 'p_x_2)
  390. (literal-function 'p_y_2))))
  391. 't))
  392. (up 0
  393. (up (up (+ ((D x_1) t) (/ (* -1 (px_1 t)) m_1))
  394. (+ ((D y_1) t) (/ (* -1 (py_1 t)) m_1)))
  395. (up (+ ((D x_2) t) (/ (* -1 (px_2 t)) m_2))
  396. (+ ((D y_2) t) (/ (* -1 (py_2 t)) m_2))))
  397. (down (down
  398. (+ ((D px_1) t)
  399. (((partial 0 0) V)
  400. (up (x_1 t) (y_1 t)) (up (x_2 t) (y_2 t))))
  401. (+ ((D py_1) t)
  402. (((partial 0 1) V)
  403. (up (x_1 t) (y_1 t)) (up (x_2 t) (y_2 t)))))
  404. (down
  405. (+ ((D px_2) t)
  406. (((partial 1 0) V)
  407. (up (x_1 t) (y_1 t)) (up (x_2 t) (y_2 t))))
  408. (+ ((D py_2) t)
  409. (((partial 1 1) V)
  410. (up (x_1 t) (y_1 t)) (up (x_2 t) (y_2 t)))))))
  411. |#
  412. #|
  413. ;;; From 3.3 -- Phase-space reduction
  414. (define ((L-axisymmetric-top A C gMR) local)
  415. (let ((q (coordinate local))
  416. (qdot (velocity local)))
  417. (let ((theta (ref q 0))
  418. (thetadot (ref qdot 0))
  419. (phidot (ref qdot 1))
  420. (psidot (ref qdot 2)))
  421. (+ (* 1/2 A
  422. (+ (square thetadot)
  423. (square (* phidot (sin theta)))))
  424. (* 1/2 C
  425. (square (+ psidot (* phidot (cos theta)))))
  426. (* -1 gMR (cos theta))))))
  427. (show-expression
  428. ((Lagrangian->Hamiltonian (L-axisymmetric-top 'A 'C 'gMR))
  429. (->H-state 't
  430. (vector 'theta 'phi 'psi)
  431. (vector 'p_theta 'p_phi 'p_psi))))
  432. (+ (* gMR (cos theta))
  433. (/ (* 1/2 (expt p_psi 2)) C)
  434. (/ (* 1/2 (expt p_psi 2) (expt (cos theta) 2)) (* A (expt (sin theta) 2)))
  435. (/ (* 1/2 (expt p_theta 2)) A)
  436. (/ (* -1 p_phi p_psi (cos theta)) (* A (expt (sin theta) 2)))
  437. (/ (* 1/2 (expt p_phi 2)) (* A (expt (sin theta) 2))))
  438. |#
  439. #|
  440. ;;; ********** This got ugly *************
  441. (show-expression
  442. (((Hamilton-equations
  443. (Lagrangian->Hamiltonian
  444. (L-axisymmetric-top 'A 'C 'gMR)))
  445. (coordinate-tuple (literal-function 'theta)
  446. (literal-function 'phi)
  447. (literal-function 'psi))
  448. (momentum-tuple (literal-function 'p_theta)
  449. (literal-function 'p_phi)
  450. (literal-function 'p_psi)))
  451. 't))
  452. (up
  453. 0
  454. (up
  455. (+ ((D theta) t) (/ (* -1 (p_theta t)) A))
  456. (+ ((D phi) t)
  457. (/ (* (cos (theta t)) (p_psi t)) (* A (expt (sin (theta t)) 2)))
  458. (/ (* -1 (p_phi t)) (* A (expt (sin (theta t)) 2))))
  459. (+
  460. ((D psi) t)
  461. (/ (* -1 (p_psi t)) C)
  462. (/ (* -1 (expt (cos (theta t)) 2) (p_psi t)) (* A (expt (sin (theta t)) 2)))
  463. (/ (* (p_phi t) (cos (theta t))) (* A (expt (sin (theta t)) 2)))))
  464. (down
  465. (+
  466. (/ (* -1 gMR (expt (cos (theta t)) 4)) (expt (sin (theta t)) 3))
  467. ((D p_theta) t)
  468. (/ (* 2 gMR (expt (cos (theta t)) 2)) (expt (sin (theta t)) 3))
  469. (/ (* (p_phi t) (expt (cos (theta t)) 2) (p_psi t))
  470. (* A (expt (sin (theta t)) 3)))
  471. (/ (* -1 (cos (theta t)) (expt (p_psi t) 2)) (* A (expt (sin (theta t)) 3)))
  472. (/ (* -1 (expt (p_phi t) 2) (cos (theta t))) (* A (expt (sin (theta t)) 3)))
  473. (/ (* -1 gMR) (expt (sin (theta t)) 3))
  474. (/ (* (p_phi t) (p_psi t)) (* A (expt (sin (theta t)) 3))))
  475. ((D p_phi) t)
  476. ((D p_psi) t)))
  477. |#
  478. ;;; The Poisson Bracket is a differential operator on H-functions:
  479. #|
  480. ;;; This can give WRONG ANSWERS on structure-valued functions...
  481. (define (Poisson-bracket f g)
  482. (- (* ((partial 1) f) ((partial 2) g))
  483. (* ((partial 2) f) ((partial 1) g))))
  484. (define (Poisson-bracket f g)
  485. (cond ((and (structure? f) (structure? g))
  486. (s:map/r (lambda (fi)
  487. (s:map/r (lambda (gj)
  488. (Poisson-bracket fi gj))
  489. g))
  490. f)
  491. #;(error "Poisson bracket of two structures" f g)
  492. )
  493. ((structure? f)
  494. (s:generate (s:length f) (s:same f)
  495. (lambda (i)
  496. (Poisson-bracket (ref f i) g))))
  497. ((structure? g)
  498. (s:generate (s:length g) (s:same g)
  499. (lambda (i)
  500. (Poisson-bracket f (ref g i)))))
  501. (else
  502. (- (* ((partial 1) f) ((partial 2) g))
  503. (* ((partial 2) f) ((partial 1) g))))))
  504. |#
  505. (define* ((Poisson-bracket f g) x)
  506. (let ((fx (f x)) (gx (g x)))
  507. (cond ((or (structure? fx) (structure? gx))
  508. (s:map/r (lambda (af)
  509. (s:map/r (lambda (ag)
  510. ((Poisson-bracket
  511. (compose (apply component af) f)
  512. (compose (apply component ag) g))
  513. x))
  514. (structure->access-chains gx)))
  515. (structure->access-chains fx)))
  516. (else
  517. ((- (* ((partial 1) f) ((partial 2) g))
  518. (* ((partial 2) f) ((partial 1) g)))
  519. x)))))
  520. #|
  521. (pe ((Poisson-bracket
  522. (up (compose (component 0) coordinate)
  523. (compose (component 1) coordinate)
  524. (compose (component 2) coordinate))
  525. (down (compose (component 0) momentum)
  526. (compose (component 1) momentum)
  527. (compose (component 2) momentum)))
  528. a-state))
  529. (up (down 1 0 0) (down 0 1 0) (down 0 0 1))
  530. |#
  531. #|
  532. (define FF
  533. (literal-function 'F
  534. (-> (UP Real
  535. (UP Real Real)
  536. (DOWN Real Real))
  537. Real)))
  538. (define GG
  539. (literal-function 'G
  540. (-> (UP Real
  541. (UP Real Real)
  542. (DOWN Real Real))
  543. Real)))
  544. (pe ((* (D FF)
  545. (Poisson-bracket identity identity)
  546. (D GG))
  547. (up 't (up 'x 'y) (down 'px 'py))))
  548. (+ (* -1
  549. (((partial 1 0) G) (up t (up x y) (down px py)))
  550. (((partial 2 0) F) (up t (up x y) (down px py))))
  551. (* -1
  552. (((partial 1 1) G) (up t (up x y) (down px py)))
  553. (((partial 2 1) F) (up t (up x y) (down px py))))
  554. (* (((partial 2 0) G) (up t (up x y) (down px py)))
  555. (((partial 1 0) F) (up t (up x y) (down px py))))
  556. (* (((partial 2 1) G) (up t (up x y) (down px py)))
  557. (((partial 1 1) F) (up t (up x y) (down px py)))))
  558. |#
  559. #|
  560. (define F (literal-function 'F (Hamiltonian 2)))
  561. (define G (literal-function 'G (Hamiltonian 2)))
  562. (define H (literal-function 'H (Hamiltonian 2)))
  563. ;;; Jacobi identity
  564. (pe ((+ (Poisson-bracket F (Poisson-bracket G H))
  565. (Poisson-bracket G (Poisson-bracket H F))
  566. (Poisson-bracket H (Poisson-bracket F G)))
  567. (up 't (up 'x 'y) (down 'px 'py))))
  568. 0
  569. |#
  570. #|
  571. (define Sx (compose (component 0) coordinate))
  572. (define Sy (compose (component 1) coordinate))
  573. (define Sz (compose (component 2) coordinate))
  574. (define Spx (compose (component 0) momentum))
  575. (define Spy (compose (component 1) momentum))
  576. (define Spz (compose (component 2) momentum))
  577. ;;; for example L = [Lx, Ly, Lz]
  578. ;;; where Li are components of angular momentum
  579. (define Lx (- (* Sy Spz) (* Spy Sz)))
  580. (define Ly (- (* Sz Spx) (* Spz Sx)))
  581. (define Lz (- (* Sx Spy) (* Spx Sy)))
  582. (define L (down Lx Ly Lz))
  583. (define 3-state
  584. (->H-state 't
  585. (coordinate-tuple 'x 'y 'z)
  586. (momentum-tuple 'p_x 'p_y 'p_z)))
  587. (pe ((Poisson-bracket Lx L) 3-state))
  588. (down 0 (+ (* -1 p_x y) (* p_y x)) (+ (* -1 p_x z) (* p_z x)))
  589. ;;; Poisson brackets are compositional with canonical transformations
  590. ;;; (see point-transformations.scm for F->CT and time-varying.scm for
  591. ;;; C-rotating, repeated here.
  592. (define ((rotating n) state)
  593. (let ((t (time state))
  594. (q (coordinate state)))
  595. (let ((x (ref q 0))
  596. (y (ref q 1))
  597. (z (ref q 2)))
  598. (coordinate-tuple (+ (* (cos (* n t)) x) (* (sin (* n t)) y))
  599. (- (* (cos (* n t)) y) (* (sin (* n t)) x))
  600. z))))
  601. (define (C-rotating n) (F->CT (rotating n)))
  602. (pe ((- (compose (Poisson-bracket Lx Ly) (C-rotating 'n))
  603. (Poisson-bracket (compose Lx (C-rotating 'n))
  604. (compose Ly (C-rotating 'n))) )
  605. 3-state))
  606. 0
  607. |#
  608. #|
  609. ;;; Poisson brackets in terms of J
  610. ;;; Guaranteed to work only for scalar valued functions
  611. #|
  612. ;;; From canonical.scm
  613. (define (J-func DH)
  614. (->H-state 0
  615. (ref DH 2)
  616. (- (ref DH 1))))
  617. |#
  618. (define ((PB f g) s)
  619. (* ((D f) s) (J-func ((D g) s))))
  620. (define a-state
  621. (->H-state 't
  622. (coordinate-tuple 'x 'y 'z)
  623. (momentum-tuple 'p_x 'p_y 'p_z)))
  624. (pe ((- (Poisson-bracket Lx Ly) Lz) a-state))
  625. 0
  626. (pe ((- (PB Lx Ly) Lz) a-state))
  627. 0
  628. (define ((PB f g) s)
  629. (let ((J ((D J-func) ((D g) s))))
  630. (* ((D f) s) (* J ((D g) s)))))
  631. (define ((PB f g) s)
  632. (let ((J (linear-function->multiplier J-func ((D g) s))))
  633. (* ((D f) s) (* J ((D g) s)))))
  634. (pe
  635. (- ((Poisson-bracket (H-harmonic 'm 'k)
  636. ((component 0) coordinate))
  637. a-state)
  638. ((PB (H-harmonic 'm 'k)
  639. (compose (component 0) coordinate))
  640. a-state)))
  641. 0
  642. (pe
  643. (- ((Poisson-bracket (H-harmonic 'm 'k) coordinate)
  644. a-state)
  645. ((PB (H-harmonic 'm 'k) coordinate)
  646. a-state)))
  647. (up 0 0 0)
  648. (pe ((PB momentum (H-harmonic 'm 'k))
  649. a-state))
  650. (down (* -1 k x) (* -1 k y) (* -1 k z))
  651. (pe ((PB coordinate (H-harmonic 'm 'k))
  652. a-state))
  653. (up (/ p_x m) (/ p_y m) (/ p_z m))
  654. |#
  655. (define (commutator op1 op2)
  656. (- (* op1 op2) (* op2 op1)))
  657. (define (anticommutator op1 op2)
  658. (+ (* op1 op2) (* op2 op1)))
  659. ;;; We define the Lie derivative of F, as a derivative-like operator,
  660. ;;; relative to the given Hamiltonian-like function, H.
  661. ;;; Generalization and redefinition in calculus/Lie.scm
  662. (define (Lie-derivative H)
  663. (make-operator
  664. (lambda (F)
  665. (Poisson-bracket F H))
  666. `(Lie-derivative ,H)))
  667. ;;; the flow derivative generalizes the Lie derivative to
  668. ;;; allow for time dependent H and F ---
  669. ;;; computes the "time" derivative of F along the flow specified by H
  670. (define (flow-derivative H)
  671. (make-operator
  672. (lambda (F)
  673. (+ ((partial 0) F)
  674. (Poisson-bracket F H)))
  675. `(flow-derivative ,H)))
  676. #|
  677. ;;; for Lie derivatives
  678. (define F (literal-function 'F (Hamiltonian 2)))
  679. (define G (literal-function 'G (Hamiltonian 2)))
  680. (define L_F (Lie-derivative F))
  681. (define L_G (Lie-derivative G))
  682. (pe (((+ (commutator L_F L_G)
  683. (Lie-derivative (Poisson-bracket F G)))
  684. H)
  685. (up 't (up 'x 'y) (down 'px 'py))))
  686. 0
  687. |#