Skip to content

Commit dda2806

Browse files
authored
sidebar to sicm (#226)
1 parent 5b421ca commit dda2806

File tree

1 file changed

+108
-20
lines changed

1 file changed

+108
-20
lines changed

src/mentat_collective/emmy/sicm_ch01.clj

Lines changed: 108 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
(ns mentat-collective.emmy.sicm-ch01
1212
(:refer-clojure :exclude [+ - * / zero? compare divide numerator denominator
1313
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]))
2018

2119
^:kindly/hide-code
2220
(def md
@@ -32,6 +30,74 @@
3230
;; In adopting MIT-Scheme's `(define ...)`, I trust that Clojure people will bridge that gap quickly
3331
;; while being sure of the gratitude of all readers of the immutable, dense book.
3432

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+
35101
^:kindly/hide-code
36102
(def velocities velocity)
37103

@@ -42,7 +108,7 @@
42108
(def vector-length count)
43109

44110
^:kindly/hide-code
45-
(defn time [state] (first state))
111+
(def time first)
46112

47113
^:kindly/hide-code
48114
(def tex (comp kind/tex emmy.expression.render/->TeX simplify))
@@ -55,7 +121,21 @@
55121
(+ (* 3 t) 5)
56122
(+ (* 2 t) 1)))
57123

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]
59139

60140
(md "#### Paths of minimum Action")
61141
(md "Show that the action is smaller along a straight-line test path than along nearby paths")
@@ -65,7 +145,7 @@
65145

66146
(define ((varied-free-particle-action mass q ν t1 t2) ε)
67147
(let ((η (make-η ν t1 t2)))
68-
(Lagrangian-action (lg/L-free-particle mass)
148+
(Lagrangian-action (L-free-particle mass)
69149
(+ q (* ε η)) t1 t2)))
70150

71151
((varied-free-particle-action 3.0 test-path (up sin cos square) 0.0 10.0) 0.01)
@@ -143,9 +223,9 @@
143223
(define small-varied-path (make-varied-path 0.01 0 10))
144224
(define large-varied-path (make-varied-path 0.02 0 10))
145225

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)]
149229

150230
(md "Create data to plot the three paths in the xz plane along with their actions.")
151231

@@ -180,10 +260,16 @@
180260
(lg/make-path t0 q0 t1 q1 minimizing-qs)))
181261

182262
(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)))))
184270

185271
(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))
187273

188274
(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.")
189275

@@ -210,14 +296,14 @@
210296

211297
(md "Calculate the Lagrange equation of the harmonic oszillator.")
212298

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))
214300

215301
(md "## 1.5 The Euler-Lagrange Equations")
216302
(md "### 1.5.2 Computing Lagrange's Equations")
217303
(md "#### The free particle")
218304
(md "State the dynamic equation of motion (i.e. the Lagrange equation a.k.a Newton's second law) of the free particle.")
219305

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))
221307

222308
(md "Check that an arbitrary straight-line path satisfies this equation, i.e. that inserting a straight line for q(t)
223309
gives identically zero (strictly speaking the zero covector of three dimensions).")
@@ -228,20 +314,20 @@
228314
(+ (* 'b t) 'b0)
229315
(+ (* 'c t) 'c0)))
230316

231-
(tex (((Lagrange-equations (lg/L-free-particle 'm)) straight-line) 't)))
317+
(tex (((Lagrange-equations (L-free-particle 'm)) straight-line) 't)))
232318

233319
(md "#### The harmonic oscillator")
234320
(md "State the dynamic equation of motion for the harmonic oszillator with arbitrary mass and spring constant.")
235321

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))
237323

238324
(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}$ ")
239325

240326
(do
241327
(define (proposed-solution t)
242328
(* 'A (cos (+ (* 'omega t) 'φ))))
243329

244-
(tex (((Lagrange-equations (lg/L-harmonic 'm 'k)) proposed-solution) 't)))
330+
(tex (((Lagrange-equations (L-harmonic 'm 'k)) proposed-solution) 't)))
245331

246332
(md "#### Exercise 1.11: Kepler's third law")
247333
(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,7 +386,7 @@
300386
(md "#### Coriolis and centrifugal forces")
301387
(md "State, in cartesian coordinates, the Lagrangian for the two dimensional free particle in a rotating coordinate system.")
302388

303-
(define L-free-rectangular lg/L-free-particle)
389+
(define L-free-rectangular L-free-particle)
304390

305391
(define (L-free-polar m)
306392
(compose (L-free-rectangular m) (F->C p->r)))
@@ -503,3 +589,5 @@
503589
(up
504590
((T3-spherical 'm) (->local 't (up 'r 'θ 'φ) (up 'rdot 'θdot 'φdot)))
505591
((Lagrangian->energy (L3-central 'm V)) spherical-state)))
592+
593+
(repl/scittle-sidebar)

0 commit comments

Comments
 (0)