symplectic.scm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. (define (symplectic-two-form zeta1 zeta2)
  21. (- (* (momenta zeta2) (coordinates zeta1))
  22. (* (momenta zeta1) (coordinates zeta2))))
  23. ;;; Without matrices
  24. (define* ((canonical-transform? C) s)
  25. (let ((J ((D J-func) (compatible-shape s)))
  26. (DCs ((D C) s)))
  27. (let ((DCsT (transpose DCs s)))
  28. (- J (* DCs J DCsT)))))
  29. #|
  30. (print-expression
  31. ((canonical-transform? (F->CT p->r))
  32. (up 't
  33. (up 'r 'phi)
  34. (down 'p_r 'p_phi))))
  35. (up (up 0 (up 0 0) (down 0 0))
  36. (up (up 0 (up 0 0) (down 0 0)) (up 0 (up 0 0) (down 0 0)))
  37. (down (up 0 (up 0 0) (down 0 0)) (up 0 (up 0 0) (down 0 0))))
  38. (print-expression
  39. ((canonical-transform? (polar-canonical 'alpha))
  40. (up 't 'a 'I)))
  41. (up (up 0 0 0) (up 0 0 0) (up 0 0 0))
  42. ;;; but not all transforms are
  43. (define (a-non-canonical-transform Istate)
  44. (let ((t (time Istate))
  45. (theta (coordinate Istate))
  46. (p (momentum Istate)))
  47. (let ((x (* p (sin theta)))
  48. (p_x (* p (cos theta))))
  49. (up t x p_x))))
  50. (print-expression
  51. ((canonical-transform? a-non-canonical-transform)
  52. (up 't 'theta 'p)))
  53. (up (up 0 0 0) (up 0 0 (+ -1 p)) (up 0 (+ 1 (* -1 p)) 0))
  54. |#
  55. #|
  56. (define (Cmix H-state)
  57. (let ((t (time H-state))
  58. (q (coordinate H-state))
  59. (p (momentum H-state)))
  60. (up t
  61. (up (ref q 0) (- (ref p 1)))
  62. (down (ref p 0) (ref q 1)))))
  63. (define a-state
  64. (up 't (up 'x 'y) (down 'p_x 'p_y)))
  65. (print-expression
  66. ((canonical-transform? Cmix) a-state))
  67. (up (up 0 (up 0 0) (down 0 0))
  68. (up (up 0 (up 0 0) (down 0 0)) (up 0 (up 0 0) (down 0 0)))
  69. (down (up 0 (up 0 0) (down 0 0)) (up 0 (up 0 0) (down 0 0))))
  70. (define (Cmix2 H-state)
  71. (let ((t (time H-state))
  72. (q (coordinate H-state))
  73. (p (momentum H-state)))
  74. (up t
  75. (flip-outer-index p)
  76. (- (flip-outer-index q)))))
  77. (print-expression
  78. ((canonical-transform? Cmix2)
  79. a-state))
  80. (up (up 0 (up 0 0) (down 0 0))
  81. (up (up 0 (up 0 0) (down 0 0)) (up 0 (up 0 0) (down 0 0)))
  82. (down (up 0 (up 0 0) (down 0 0)) (up 0 (up 0 0) (down 0 0))))
  83. |#
  84. #|
  85. (define ((C m0 m1) state)
  86. (let ((x (coordinate state))
  87. (p (momentum state)))
  88. (let ((x0 (ref x 0))
  89. (x1 (ref x 1))
  90. (p0 (ref p 0))
  91. (p1 (ref p 1)))
  92. (up (time state)
  93. (up (/ (+ (* m0 x0) (* m1 x1)) (+ m0 m1))
  94. (- x1 x0))
  95. (down (+ p0 p1)
  96. (/ (- (* m0 p1) (* m1 p0))
  97. (+ m0 m1)))))))
  98. (define b-state
  99. (up 't
  100. (up (up 'x_1 'y_1)
  101. (up 'x_2 'y_2))
  102. (down (down 'p_x_1 'p_y_1)
  103. (down 'p_x_2 'p_y_2))))
  104. (print-expression
  105. ((canonical-transform? (C 'm1 'm2)) b-state))
  106. (up
  107. (up 0 (up (up 0 0) (up 0 0)) (down (down 0 0) (down 0 0)))
  108. (up
  109. (up (up 0 (up (up 0 0) (up 0 0)) (down (down 0 0) (down 0 0)))
  110. (up 0 (up (up 0 0) (up 0 0)) (down (down 0 0) (down 0 0))))
  111. (up (up 0 (up (up 0 0) (up 0 0)) (down (down 0 0) (down 0 0)))
  112. (up 0 (up (up 0 0) (up 0 0)) (down (down 0 0) (down 0 0)))))
  113. (down
  114. (down (up 0 (up (up 0 0) (up 0 0)) (down (down 0 0) (down 0 0)))
  115. (up 0 (up (up 0 0) (up 0 0)) (down (down 0 0) (down 0 0))))
  116. (down (up 0 (up (up 0 0) (up 0 0)) (down (down 0 0) (down 0 0)))
  117. (up 0 (up (up 0 0) (up 0 0)) (down (down 0 0) (down 0 0))))))
  118. |#
  119. (define (J-matrix n) ;degrees of freedom
  120. (let ((2n+1 (fix:+ (fix:* 2 n) 1)))
  121. (m:generate 2n+1 2n+1
  122. (lambda (a b)
  123. (cond ((fix:= a 0) 0)
  124. ((fix:= b 0) 0)
  125. ((fix:= (fix:+ a n) b) 1)
  126. ((fix:= (fix:+ b n) a) -1)
  127. (else 0))))))
  128. ;;; Symplectic test in terms of matrices
  129. (define* ((symplectic? C) s)
  130. (let ((J (J-matrix (degrees-of-freedom s)))
  131. (DC ((D-as-matrix C) s)))
  132. (- J (* DC J (transpose DC)))))
  133. #|
  134. (print-expression
  135. ((symplectic? (F->CT p->r))
  136. (up 't
  137. (up 'r 'phi)
  138. (down 'p_r 'p_phi))))
  139. (matrix-by-rows (list 0 0 0 0 0)
  140. (list 0 0 0 0 0)
  141. (list 0 0 0 0 0)
  142. (list 0 0 0 0 0)
  143. (list 0 0 0 0 0))
  144. ;;; but not all transforms are
  145. (define (a-non-canonical-transform Istate)
  146. (let ((t (time Istate))
  147. (theta (coordinate Istate))
  148. (p (momentum Istate)))
  149. (let ((x (* p (sin theta)))
  150. (p_x (* p (cos theta))))
  151. (up t x p_x))))
  152. (print-expression
  153. ((symplectic? a-non-canonical-transform)
  154. (up 't 'theta 'p)))
  155. (matrix-by-rows (list 0 0 0)
  156. (list 0 0 (+ 1 (* -1 p)))
  157. (list 0 (+ -1 p) 0))
  158. (print-expression
  159. ((symplectic? (polar-canonical 'alpha))
  160. (up 't 'a 'I)))
  161. (matrix-by-rows (list 0 0 0)
  162. (list 0 0 0)
  163. (list 0 0 0))
  164. (define (Cmix H-state)
  165. (let ((t (time H-state))
  166. (q (coordinate H-state))
  167. (p (momentum H-state)))
  168. (up t
  169. (up (ref q 0) (- (ref p 1)))
  170. (down (ref p 0) (ref q 1)))))
  171. (define a-state
  172. (up 't (up 'x 'y) (down 'p_x 'p_y)))
  173. (print-expression ((symplectic? Cmix) a-state))
  174. (matrix-by-rows (list 0 0 0 0 0)
  175. (list 0 0 0 0 0)
  176. (list 0 0 0 0 0)
  177. (list 0 0 0 0 0)
  178. (list 0 0 0 0 0))
  179. |#
  180. #|
  181. (define (Cmix2 H-state)
  182. (let ((t (time H-state))
  183. (q (coordinate H-state))
  184. (p (momentum H-state)))
  185. (up t
  186. (flip-outer-index p)
  187. (- (flip-outer-index q)))))
  188. (print-expression
  189. ((canonical-transform? Cmix2)
  190. a-state))
  191. (matrix-by-rows (list 0 0 0 0 0)
  192. (list 0 0 0 0 0)
  193. (list 0 0 0 0 0)
  194. (list 0 0 0 0 0)
  195. (list 0 0 0 0 0))
  196. (define* ((C m0 m1) state)
  197. (let ((x (coordinate state))
  198. (p (momentum state)))
  199. (let ((x0 (ref x 0))
  200. (x1 (ref x 1))
  201. (p0 (ref p 0))
  202. (p1 (ref p 1)))
  203. (up (time state)
  204. (up (/ (+ (* m0 x0) (* m1 x1)) (+ m0 m1))
  205. (- x1 x0))
  206. (down (+ p0 p1)
  207. (/ (- (* m0 p1) (* m1 p0))
  208. (+ m0 m1)))))))
  209. (define b-state
  210. (up 't
  211. (up (up 'x_1 'y_1)
  212. (up 'x_2 'y_2))
  213. (down (down 'p_x_1 'p_y_1)
  214. (down 'p_x_2 'p_y_2))))
  215. (print-expression
  216. ((canonical-transform? (C 'm1 'm2)) b-state))
  217. (matrix-by-rows (list 0 0 0 0 0 0 0 0 0)
  218. (list 0 0 0 0 0 0 0 0 0)
  219. (list 0 0 0 0 0 0 0 0 0)
  220. (list 0 0 0 0 0 0 0 0 0)
  221. (list 0 0 0 0 0 0 0 0 0)
  222. (list 0 0 0 0 0 0 0 0 0)
  223. (list 0 0 0 0 0 0 0 0 0)
  224. (list 0 0 0 0 0 0 0 0 0)
  225. (list 0 0 0 0 0 0 0 0 0))
  226. |#
  227. ;;; qp version below.
  228. (define* ((symplectic-transform? C) s)
  229. (symplectic-matrix? (qp-submatrix ((D-as-matrix C) s))))
  230. (define (qp-submatrix m)
  231. (m:submatrix m 1 (m:num-rows m) 1 (m:num-cols m)))
  232. (define (symplectic-matrix? M)
  233. (let ((2n (m:dimension M)))
  234. (if (not (even? 2n))
  235. (error "Wrong type -- SYMPLECTIC-MATRIX?" M))
  236. (let ((J (symplectic-unit (quotient 2n 2))))
  237. (- J (* M J (transpose M))))))
  238. (define (symplectic-unit n)
  239. (let ((2n (fix:* 2 n)))
  240. (m:generate 2n 2n
  241. (lambda (a b)
  242. (cond ((fix:= (fix:+ a n) b) 1)
  243. ((fix:= (fix:+ b n) a) -1)
  244. (else 0))))))
  245. #|
  246. ;;; For example, point transforms are canonical
  247. (print-expression
  248. ((symplectic-transform? (F->CT p->r))
  249. (up 't
  250. (up 'r 'theta)
  251. (down 'p_r 'p_theta))))
  252. (matrix-by-rows (list 0 0 0 0)
  253. (list 0 0 0 0)
  254. (list 0 0 0 0)
  255. (list 0 0 0 0))
  256. |#
  257. #|
  258. (print-expression
  259. ((symplectic-transform? a-non-canonical-transform)
  260. (up 't 'theta 'p)))
  261. (matrix-by-rows (list 0 (+ 1 (* -1 p))) (list (+ -1 p) 0))
  262. |#
  263. #|
  264. ;;; One particularly useful canonical transform is the
  265. ;;; Poincare transform, which is good for simplifying
  266. ;;; oscillators.
  267. (define ((polar-canonical alpha) Istate)
  268. (let ((t (time Istate))
  269. (theta (coordinate Istate))
  270. (I (momentum Istate)))
  271. (let ((x (* (sqrt (/ (* 2 I) alpha)) (sin theta)))
  272. (p_x (* (sqrt (* 2 alpha I)) (cos theta))))
  273. (up t x p_x))))
  274. (define ((polar-canonical-inverse alpha) s)
  275. (let ((t (time s))
  276. (x (coordinate s))
  277. (p (momentum s)))
  278. (let ((I (/ (+ (* alpha (square x))
  279. (/ (square p) alpha))
  280. 2)))
  281. (let ((theta (atan (/ x (sqrt (/ (* 2 I) alpha)))
  282. (/ p (sqrt (* 2 I alpha))))))
  283. (up t theta I)))))
  284. (pe
  285. ((compose (polar-canonical-inverse 'alpha)
  286. (polar-canonical 'alpha))
  287. (up 't 'x 'p)))
  288. (up t x p)
  289. |#
  290. #|
  291. ;;; It is clearly canonical.
  292. (print-expression
  293. ((symplectic-transform? (polar-canonical 'alpha))
  294. (up 't 'a 'I)))
  295. (matrix-by-rows (list 0 0) (list 0 0))
  296. |#