gamma-bar.scm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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* ((Gamma-bar f) local)
  21. ((f (osculating-path local)) (time local)))
  22. ;;; An alternative method allows taking derivatives in the
  23. ;;; construction of the Lagrangian.
  24. (define* ((osculating-path state0) t)
  25. (let ((t0 (time state0))
  26. (q0 (coordinate state0))
  27. (k (vector-length state0)))
  28. (let ((dt (- t t0)))
  29. (let loop ((n 2) (sum q0) (dt^n/n! dt))
  30. (if (fix:= n k)
  31. sum
  32. (loop (+ n 1)
  33. (+ sum (* (vector-ref state0 n) dt^n/n!))
  34. (/ (* dt^n/n! dt) n)))))))
  35. ;;; "total time derivative"
  36. #|
  37. (define (Dt-procedure F)
  38. (define (DF-on-path q)
  39. (D (compose F (Gamma q))))
  40. (Gamma-bar DF-on-path))
  41. |#
  42. #|
  43. (define ((Dt-procedure F) state)
  44. (let ((n (vector-length state)))
  45. (define (DF-on-path q)
  46. (D (compose F (Gamma q (- n 1)))))
  47. ((Gamma-bar DF-on-path) state)))
  48. |#
  49. (define (Dt-procedure F)
  50. (define (DtF state)
  51. (let ((n (vector-length state)))
  52. (define (DF-on-path q)
  53. (D (compose F (Gamma q (- n 1)))))
  54. ((Gamma-bar DF-on-path) state)))
  55. DtF)
  56. (define Dt
  57. (make-operator Dt-procedure 'Dt))
  58. #|
  59. (print-expression
  60. ((Dt
  61. (lambda (state)
  62. (let ((t (time state))
  63. (q (coordinate state)))
  64. (square q))))
  65. (up 't (up 'x 'y) (up 'vx 'vy))))
  66. (+ (* 2 vx x) (* 2 vy y))
  67. (print-expression
  68. ((Dt (Dt (lambda (state) (coordinate state))))
  69. (up 't 'x 'v 'a 'j)))
  70. a
  71. (print-expression
  72. ((Dt (Dt (lambda (state)
  73. (square (coordinate state)))))
  74. (up 't 'x 'v 'a 'j)))
  75. (+ (* 2 a x) (* 2 (expt v 2)))
  76. (define L (literal-function 'L (Lagrangian 2)))
  77. (print-expression
  78. ((Dt L) (up 't (up 'x 'y) (up 'vx 'vy))))
  79. <error, not enuf args>
  80. (print-expression
  81. ((Dt L) (up 't (up 'x 'y) (up 'vx 'vy) (up 'ax 'ay))))
  82. (+ (* ax (((partial 2 0) L) (up t (up x y) (up vx vy))))
  83. (* ay (((partial 2 1) L) (up t (up x y) (up vx vy))))
  84. (* vx (((partial 1 0) L) (up t (up x y) (up vx vy))))
  85. (* vy (((partial 1 1) L) (up t (up x y) (up vx vy))))
  86. (((partial 0) L) (up t (up x y) (up vx vy))))
  87. |#
  88. (define (Euler-Lagrange-operator Lagrangian)
  89. (- (Dt ((partial 2) Lagrangian))
  90. (compose ((partial 1) Lagrangian) trim-last-argument)))
  91. (define (trim-last-argument local)
  92. (apply up (except-last-pair (vector->list (up->vector local)))))
  93. (define LE Euler-Lagrange-operator)
  94. (define Lagrange-equations-operator LE)
  95. ;;; Given a local tuple, produces a finite state.
  96. #|
  97. (define ((L-harmonic m k) s)
  98. (- (* 1/2 m (square (velocity s)))
  99. (* 1/2 k (square (coordinate s)))))
  100. (print-expression
  101. ((LE (L-harmonic 'm 'k))
  102. (up 't 'x 'v 'a)))
  103. (+ (* a m) (* k x))
  104. (print-expression
  105. ((LE (L-harmonic 'm 'k))
  106. (up 't #(x y) #(vx vy) #(ax ay))))
  107. (down (+ (* ax m) (* k x))
  108. (+ (* ay m) (* k y)))
  109. (print-expression
  110. ((LE L) (up 't (up 'x 'y) (up 'vx 'vy) (up 'ax 'ay))))
  111. (down
  112. (+ (* ax (((partial 2 0) ((partial 2 0) L)) (up t (up x y) (up vx vy))))
  113. (* ay (((partial 2 0) ((partial 2 1) L)) (up t (up x y) (up vx vy))))
  114. (* vx (((partial 1 0) ((partial 2 0) L)) (up t (up x y) (up vx vy))))
  115. (* vy (((partial 1 1) ((partial 2 0) L)) (up t (up x y) (up vx vy))))
  116. (* -1 (((partial 1 0) L) (up t (up x y) (up vx vy))))
  117. (((partial 0) ((partial 2 0) L)) (up t (up x y) (up vx vy))))
  118. (+ (* ax (((partial 2 0) ((partial 2 1) L)) (up t (up x y) (up vx vy))))
  119. (* ay (((partial 2 1) ((partial 2 1) L)) (up t (up x y) (up vx vy))))
  120. (* vx (((partial 1 0) ((partial 2 1) L)) (up t (up x y) (up vx vy))))
  121. (* vy (((partial 1 1) ((partial 2 1) L)) (up t (up x y) (up vx vy))))
  122. (* -1 (((partial 1 1) L) (up t (up x y) (up vx vy))))
  123. (((partial 0) ((partial 2 1) L)) (up t (up x y) (up vx vy)))))
  124. ;;; Adding extra state components is harmless, because L-harmonic does
  125. ;;; not check the length of the jet.
  126. (print-expression
  127. ((LE (L-harmonic 'm 'k))
  128. (up 't 'x 'v 'a 'j)))
  129. (+ (* a m) (* k x))
  130. ;;; But watch out. If not enuf local componenents
  131. ;;; are specified we lose.
  132. (print-expression
  133. ((LE (L-harmonic 'm 'k))
  134. (up 't 'x 'v)))
  135. ;Cannot extract velocity from #((*diff* ... ...) x)
  136. ;;; error
  137. (print-expression
  138. ((LE (L-central-polar 'm (literal-function 'V)))
  139. (up 't
  140. (up 'r 'phi)
  141. (up 'rdot 'phidot)
  142. (up 'rdotdot 'phidotdot))))
  143. (down (+ (* -1 m (expt phidot 2) r) (* m rdotdot) ((D V) r))
  144. (+ (* 2 m phidot r rdot) (* m phidotdot (expt r 2))))
  145. (print-expression
  146. ((compose (LE (L-central-polar 'm (literal-function 'V)))
  147. (Gamma
  148. (coordinate-tuple (literal-function 'r)
  149. (literal-function 'phi))
  150. 4))
  151. 't))
  152. (down
  153. (+ (* -1 m (expt ((D phi) t) 2) (r t))
  154. (* m (((expt D 2) r) t))
  155. ((D V) (r t)))
  156. (+ (* 2 m ((D r) t) ((D phi) t) (r t))
  157. (* m (((expt D 2) phi) t) (expt (r t) 2))))
  158. |#
  159. (define (clip-state n)
  160. (lambda (u)
  161. (vector-head u n)))
  162. (define (clip state)
  163. (vector-head state
  164. (- (vector-length state)
  165. 1)))
  166. (define* ((generalized-LE Lagrangian) state)
  167. (let ((m (s:length state)))
  168. (assert (and (fix:> m 3) (even? m))
  169. "Incorrect state size for Lagrange Equations")
  170. (let lp ((i (quotient m 2)) (state state))
  171. (if (fix:= i 0)
  172. 0
  173. (- (((expt Dt (fix:- i 1))
  174. ((partial i) Lagrangian))
  175. state)
  176. (lp (fix:- i 1) (clip state)))))))
  177. #|
  178. (define ((L2harmonic m k) state)
  179. (let ((x (coordinate state))
  180. (a (acceleration state)))
  181. (+ (* 1/2 m x a) (* 1/2 k (square x)))))
  182. (print-expression
  183. ((generalized-LE (L2harmonic 'm 'k))
  184. (up 't 'x 'v 'a 'j 'p)))
  185. (+ (* a m) (* k x))
  186. (pe ((generalized-LE
  187. (literal-function 'L (-> (UP Real Real Real) Real)))
  188. (up 't 'x 'v 'a)))
  189. (+ (* a (((partial 2) ((partial 2) L)) (up t x v)))
  190. (* v (((partial 1) ((partial 2) L)) (up t x v)))
  191. (((partial 0) ((partial 2) L)) (up t x v))
  192. (* -1 (((partial 1) L) (up t x v))))
  193. (pe ((generalized-LE
  194. (literal-function 'L (-> (UP Real Real Real Real) Real)))
  195. (up 't 'x 'v 'a 'j 'p)))
  196. (+ (* (expt a 2) (((partial 2) ((partial 2) ((partial 3) L))) (up t x v a)))
  197. (* 2 a j (((partial 2) ((partial 3) ((partial 3) L))) (up t x v a)))
  198. (* 2 a v (((partial 1) ((partial 2) ((partial 3) L))) (up t x v a)))
  199. (* (expt j 2) (((partial 3) ((partial 3) ((partial 3) L))) (up t x v a)))
  200. (* 2 j v (((partial 1) ((partial 3) ((partial 3) L))) (up t x v a)))
  201. (* (expt v 2) (((partial 1) ((partial 1) ((partial 3) L))) (up t x v a)))
  202. (* 2 a (((partial 0) ((partial 2) ((partial 3) L))) (up t x v a)))
  203. (* a (((partial 1) ((partial 3) L)) (up t x v a)))
  204. (* -1 a (((partial 2) ((partial 2) L)) (up t x v a)))
  205. (* 2 j (((partial 0) ((partial 3) ((partial 3) L))) (up t x v a)))
  206. (* p (((partial 3) ((partial 3) L)) (up t x v a)))
  207. (* 2 v (((partial 0) ((partial 1) ((partial 3) L))) (up t x v a)))
  208. (* -1 v (((partial 1) ((partial 2) L)) (up t x v a)))
  209. (((partial 0) ((partial 0) ((partial 3) L))) (up t x v a))
  210. (* -1 (((partial 0) ((partial 2) L)) (up t x v a)))
  211. (((partial 1) L) (up t x v a)))
  212. |#