|
11 | 11 | (ns mentat-collective.emmy.sicm-ch01 |
12 | 12 | (:refer-clojure :exclude [+ - * / zero? compare divide numerator denominator |
13 | 13 | time infinite? abs ref partial =]) |
14 | | - (:require [emmy.env :refer :all :exclude [r->p]] |
15 | | - [emmy.mechanics.lagrange :as lg] |
16 | | - [emmy.numerical.minimize :as mn] |
17 | | - [mentat-collective.emmy.scheme :refer :all] |
18 | | - [scicloj.kindly.v4.api :as kindly] |
19 | | - [scicloj.kindly.v4.kind :as kind])) |
| 14 | + (:require [scicloj.kindly.v4.api :as kindly] |
| 15 | + [scicloj.kindly.v4.kind :as kind] |
| 16 | + [mentat-collective.emmy.scheme :refer [define define-1 let-scheme lambda]] |
| 17 | + [civitas.repl :as repl])) |
20 | 18 |
|
21 | 19 | ^:kindly/hide-code |
22 | 20 | (def md |
|
32 | 30 | ;; In adopting MIT-Scheme's `(define ...)`, I trust that Clojure people will bridge that gap quickly |
33 | 31 | ;; while being sure of the gratitude of all readers of the immutable, dense book. |
34 | 32 |
|
| 33 | +^:kindly/hide-code |
| 34 | +(kind/hiccup |
| 35 | + [:div |
| 36 | + [:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen/dist/scittle.js"}] |
| 37 | + [:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen/dist/scittle.emmy.js"}] |
| 38 | + [:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen/dist/scittle.cljs-ajax.js"}] |
| 39 | + [:script {:src "https://cdn.jsdelivr.net/npm/react@18/umd/react.production.min.js", :crossorigin ""}] |
| 40 | + [:script {:src "https://cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.production.min.js", :crossorigin ""}] |
| 41 | + [:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen/dist/scittle.reagent.js"}] |
| 42 | + [:script {:type "application/x-scittle" :src "scheme.cljc"}]]) |
| 43 | + |
| 44 | +^:kindly/hide-code |
| 45 | +(defmacro defines [& b] |
| 46 | + (list 'do |
| 47 | + (cons 'mentat-collective.emmy.scheme/define b) |
| 48 | + (list 'kind/scittle (list 'quote (cons 'define b))))) |
| 49 | + |
| 50 | +^:kindly/hide-code |
| 51 | +(defines emmy-env |
| 52 | + '[emmy.env :refer :all :exclude [r->p]]) |
| 53 | + |
| 54 | +^:kindly/hide-code |
| 55 | +(defines emmy-lg |
| 56 | + '[emmy.mechanics.lagrange :as lg]) |
| 57 | + |
| 58 | +^:kindly/hide-code |
| 59 | +(defines emmy-mn |
| 60 | + '[emmy.numerical.minimize :as mn]) |
| 61 | + |
| 62 | +^:kindly/hide-code |
| 63 | +(do |
| 64 | + (require emmy-env) |
| 65 | + (require emmy-lg) |
| 66 | + (require emmy-mn)) |
| 67 | + |
| 68 | +^:kindly/hide-code |
| 69 | +(kind/scittle |
| 70 | + '(do |
| 71 | + (require emmy-env) |
| 72 | + #_(require emmy-lg) |
| 73 | + #_(require emmy-mn))) |
| 74 | + |
| 75 | +^:kindly/hide-code |
| 76 | +(defines show-exp (comp str simplify)) |
| 77 | + |
| 78 | +^:kindly/hide-code |
| 79 | +(kind/scittle |
| 80 | + '(def show-expression show-exp)) |
| 81 | + |
| 82 | +^:kindly/hide-code |
| 83 | +(defmacro show-expression [b & c] |
| 84 | + (case b |
| 85 | + :calc-on-server |
| 86 | + (list 'simplify (first c)) |
| 87 | + :browser |
| 88 | + (list 'kind/reagent |
| 89 | + [:tt (list 'quote |
| 90 | + (list 'show-exp (first c)))]) |
| 91 | + (let [serg (show-exp (eval b))] |
| 92 | + (list 'kind/reagent |
| 93 | + [:div (list 'quote |
| 94 | + (list 'let ['a (list 'show-exp b)] |
| 95 | + (list 'if (list '= serg 'a) |
| 96 | + [:tt 'a] |
| 97 | + [:div |
| 98 | + ;; [:tt 'a] ;; comment this in prod |
| 99 | + [:tt serg]])))])))) |
| 100 | + |
35 | 101 | ^:kindly/hide-code |
36 | 102 | (def velocities velocity) |
37 | 103 |
|
|
42 | 108 | (def vector-length count) |
43 | 109 |
|
44 | 110 | ^:kindly/hide-code |
45 | | -(defn time [state] (first state)) |
| 111 | +(def time first) |
46 | 112 |
|
47 | 113 | ^:kindly/hide-code |
48 | 114 | (def tex (comp kind/tex emmy.expression.render/->TeX simplify)) |
|
55 | 121 | (+ (* 3 t) 5) |
56 | 122 | (+ (* 2 t) 1))) |
57 | 123 |
|
58 | | -(Lagrangian-action (lg/L-free-particle 3.0) test-path 0.0 10.0) |
| 124 | +^:kindly/hide-code |
| 125 | +(defines (test-path t) |
| 126 | + (up (+ (* 4 t) 7) |
| 127 | + (+ (* 3 t) 5) |
| 128 | + (+ (* 2 t) 1))) |
| 129 | + |
| 130 | +^:kindly/hide-code |
| 131 | +(defines ((L-free-particle mass) local) |
| 132 | + (let ((v (velocity local))) |
| 133 | + (* 1/2 mass (square v)))) |
| 134 | + |
| 135 | +(show-expression |
| 136 | + (Lagrangian-action (L-free-particle 3.0) test-path 0.0 10.0)) |
| 137 | + |
| 138 | +;; [hint MAK: the above expression also works in the sidebar, all of the below needs manual copy paste of every expression] |
59 | 139 |
|
60 | 140 | (md "#### Paths of minimum Action") |
61 | 141 | (md "Show that the action is smaller along a straight-line test path than along nearby paths") |
|
65 | 145 |
|
66 | 146 | (define ((varied-free-particle-action mass q ν t1 t2) ε) |
67 | 147 | (let ((η (make-η ν t1 t2))) |
68 | | - (Lagrangian-action (lg/L-free-particle mass) |
| 148 | + (Lagrangian-action (L-free-particle mass) |
69 | 149 | (+ q (* ε η)) t1 t2))) |
70 | 150 |
|
71 | 151 | ((varied-free-particle-action 3.0 test-path (up sin cos square) 0.0 10.0) 0.01) |
|
143 | 223 | (define small-varied-path (make-varied-path 0.01 0 10)) |
144 | 224 | (define large-varied-path (make-varied-path 0.02 0 10)) |
145 | 225 |
|
146 | | -[(Lagrangian-action (lg/L-free-particle 3.0) path-along-x 0.0 10.0) |
147 | | - (Lagrangian-action (lg/L-free-particle 3.0) small-varied-path 0.0 10.0) |
148 | | - (Lagrangian-action (lg/L-free-particle 3.0) large-varied-path 0.0 10.0)] |
| 226 | +[(Lagrangian-action (L-free-particle 3.0) path-along-x 0.0 10.0) |
| 227 | + (Lagrangian-action (L-free-particle 3.0) small-varied-path 0.0 10.0) |
| 228 | + (Lagrangian-action (L-free-particle 3.0) large-varied-path 0.0 10.0)] |
149 | 229 |
|
150 | 230 | (md "Create data to plot the three paths in the xz plane along with their actions.") |
151 | 231 |
|
|
180 | 260 | (lg/make-path t0 q0 t1 q1 minimizing-qs))) |
181 | 261 |
|
182 | 262 | (define free-path |
183 | | - (fnd-path (lg/L-free-particle 3.0) 0.0 1.0 pi-half 0.0 initial-qs)) |
| 263 | + (fnd-path (L-free-particle 3.0) 0.0 1.0 pi-half 0.0 initial-qs)) |
| 264 | + |
| 265 | +(define ((L-harmonic m k) local) |
| 266 | + (let ((q (coordinate local)) |
| 267 | + (v (velocity local))) |
| 268 | + (- (* 1/2 m (square v)) |
| 269 | + (* 1/2 k (square q))))) |
184 | 270 |
|
185 | 271 | (define harmonic-path |
186 | | - (fnd-path (lg/L-harmonic 1.0 1.0) 0.0 1.0 pi-half 0.0 initial-qs)) |
| 272 | + (fnd-path (L-harmonic 1.0 1.0) 0.0 1.0 pi-half 0.0 initial-qs)) |
187 | 273 |
|
188 | 274 | (md "Make a plot of these one dimensional paths, this time not in the x-z plane but in the t-z plane. This shows that, upon optimization, the initial-path turns into a streight line and a sinusoidal curve respectively.") |
189 | 275 |
|
|
210 | 296 |
|
211 | 297 | (md "Calculate the Lagrange equation of the harmonic oszillator.") |
212 | 298 |
|
213 | | -(tex (((Lagrange-equations (lg/L-harmonic 'm 'k)) (literal-function 'q)) 't)) |
| 299 | +(tex (((Lagrange-equations (L-harmonic 'm 'k)) (literal-function 'q)) 't)) |
214 | 300 |
|
215 | 301 | (md "## 1.5 The Euler-Lagrange Equations") |
216 | 302 | (md "### 1.5.2 Computing Lagrange's Equations") |
217 | 303 | (md "#### The free particle") |
218 | 304 | (md "State the dynamic equation of motion (i.e. the Lagrange equation a.k.a Newton's second law) of the free particle.") |
219 | 305 |
|
220 | | -(tex (((Lagrange-equations (lg/L-free-particle 'm)) (literal-function 'q)) 't)) |
| 306 | +(tex (((Lagrange-equations (L-free-particle 'm)) (literal-function 'q)) 't)) |
221 | 307 |
|
222 | 308 | (md "Check that an arbitrary straight-line path satisfies this equation, i.e. that inserting a straight line for q(t) |
223 | 309 | gives identically zero (strictly speaking the zero covector of three dimensions).") |
|
228 | 314 | (+ (* 'b t) 'b0) |
229 | 315 | (+ (* 'c t) 'c0))) |
230 | 316 |
|
231 | | - (tex (((Lagrange-equations (lg/L-free-particle 'm)) straight-line) 't))) |
| 317 | + (tex (((Lagrange-equations (L-free-particle 'm)) straight-line) 't))) |
232 | 318 |
|
233 | 319 | (md "#### The harmonic oscillator") |
234 | 320 | (md "State the dynamic equation of motion for the harmonic oszillator with arbitrary mass and spring constant.") |
235 | 321 |
|
236 | | -(tex (((Lagrange-equations (lg/L-harmonic 'm 'k)) (literal-function 'q)) 't)) |
| 322 | +(tex (((Lagrange-equations (L-harmonic 'm 'k)) (literal-function 'q)) 't)) |
237 | 323 |
|
238 | 324 | (md "Plug in a sinusoid with arbitrary amplitude $A$, frequency $\\omega$ and phase $\\phi$ and show that the only solutions allowed are ones where $\\omega = \\sqrt{k/m}$ ") |
239 | 325 |
|
240 | 326 | (do |
241 | 327 | (define (proposed-solution t) |
242 | 328 | (* 'A (cos (+ (* 'omega t) 'φ)))) |
243 | 329 |
|
244 | | - (tex (((Lagrange-equations (lg/L-harmonic 'm 'k)) proposed-solution) 't))) |
| 330 | + (tex (((Lagrange-equations (L-harmonic 'm 'k)) proposed-solution) 't))) |
245 | 331 |
|
246 | 332 | (md "#### Exercise 1.11: Kepler's third law") |
247 | 333 | (md "Show that a planet in circular orbit satisfies Kepler's third law $n^2a^3=G(M_1+m_2)$, where $n$ is the angular frequency of the orbit and $a$ is the distance between sun and planet. (Hint: use the reduced mass to construct the Lagrangian)") |
|
300 | 386 | (md "#### Coriolis and centrifugal forces") |
301 | 387 | (md "State, in cartesian coordinates, the Lagrangian for the two dimensional free particle in a rotating coordinate system.") |
302 | 388 |
|
303 | | -(define L-free-rectangular lg/L-free-particle) |
| 389 | +(define L-free-rectangular L-free-particle) |
304 | 390 |
|
305 | 391 | (define (L-free-polar m) |
306 | 392 | (compose (L-free-rectangular m) (F->C p->r))) |
|
503 | 589 | (up |
504 | 590 | ((T3-spherical 'm) (->local 't (up 'r 'θ 'φ) (up 'rdot 'θdot 'φdot))) |
505 | 591 | ((Lagrangian->energy (L3-central 'm V)) spherical-state))) |
| 592 | + |
| 593 | +(repl/scittle-sidebar) |
0 commit comments