Skip to content

Commit 47d04bd

Browse files
authored
improve SILCM draft (#294)
1 parent f04ab61 commit 47d04bd

File tree

2 files changed

+282
-33
lines changed

2 files changed

+282
-33
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
^{:kindly/hide-code true
2+
:clay {:title "interactive"
3+
:quarto {:author :kloimhardt
4+
:type :draft
5+
:description "Demo of reagent and tex"
6+
:sidebar "emmy-fdg"
7+
:date "2026-01-11"
8+
:category :clay
9+
:tags [:browser :reagent]}}}
10+
11+
(ns mentat-collective.emmy.emmy_interactive
12+
(:require [scicloj.kindly.v4.api :as kindly]
13+
[scicloj.kindly.v4.kind :as kind]
14+
[mentat-collective.emmy.scheme :refer [define-1 let-scheme lambda]]))
15+
16+
^:kindly/hide-code
17+
(def render-mode {:browser true})
18+
19+
^:kindly/hide-code
20+
(kind/hiccup
21+
[:div
22+
[:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen@0.7.28-59/dist/scittle.js"}]
23+
[:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen@0.7.28-59/dist/scittle.emmy.js"}]
24+
[:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen@0.7.28-59/dist/scittle.cljs-ajax.js"}]
25+
[:script {:src "https://cdn.jsdelivr.net/npm/react@18/umd/react.production.min.js", :crossorigin ""}]
26+
[:script {:src "https://cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.production.min.js", :crossorigin ""}]
27+
[:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen@0.7.28-59/dist/scittle.reagent.js"}]
28+
[:script {:type "application/x-scittle" :src "scheme.cljc"}]])
29+
30+
^:kindly/hide-code
31+
(defmacro define [& b]
32+
(list 'do
33+
(cons 'mentat-collective.emmy.scheme/define b)
34+
(list 'kind/scittle (list 'quote (cons 'define b)))))
35+
36+
(define simplify identity)
37+
(define ->infix identity)
38+
(define down vector)
39+
40+
^:kindly/hide-code
41+
(kind/scittle
42+
'(defn show-expression [e]
43+
(simplify e)))
44+
45+
^:kindly/hide-code
46+
(kind/scittle
47+
'(defn show-tex-expression [e]
48+
(->infix (simplify e))))
49+
50+
^:kindly/hide-code
51+
(kind/scittle
52+
'(defn show-tex [e]
53+
(->infix e)))
54+
55+
^:kindly/hide-code
56+
(define (eq-transformation f)
57+
(lambda (tuple)
58+
(apply down (map f tuple))))
59+
60+
^:kindly/hide-code
61+
(kind/scittle
62+
'(defn show-eq [tuple]
63+
(->infix (simplify (down (first tuple) '= (second tuple))))))
64+
65+
^:kindly/hide-code
66+
(def tex (comp kind/tex emmy.expression.render/->TeX))
67+
68+
^:kindly/hide-code
69+
(def tex-simp (comp tex simplify))
70+
71+
^:kindly/hide-code
72+
(defn fn-show-eq [tuple]
73+
(tex-simp (down (first tuple) '= (second tuple))))
74+
75+
^:kindly/hide-code
76+
(define show-exp (comp str simplify))
77+
78+
^:kindly/hide-code
79+
(defn reag-comp [b]
80+
(let [server-erg (show-exp (eval b))]
81+
(list 'kind/reagent
82+
[:div (list 'quote
83+
(list 'let ['a (list 'show-exp b)]
84+
[:div
85+
(when (:browser render-mode)
86+
[:div
87+
[:tt 'a]
88+
[:p (list 'str (list '= server-erg 'a))]])
89+
[:tt server-erg]]))])))
90+
91+
^:kindly/hide-code
92+
(defmacro show-expression [e]
93+
(if (:browser render-mode)
94+
(reag-comp e)
95+
(list 'simplify e)
96+
))
97+
98+
^:kindly/hide-code
99+
(defmacro show-tex-expression [e]
100+
(if (:browser render-mode)
101+
(reag-comp e)
102+
(list 'tex-simp e)))
103+
104+
^:kindly/hide-code
105+
(defmacro show-tex [e]
106+
(if (:browser render-mode)
107+
(reag-comp e)
108+
(list 'tex e)))
109+
110+
^:kindly/hide-code
111+
(defmacro show-eq [e]
112+
(if (:browser render-mode)
113+
(reag-comp e)
114+
(list 'fn-show-eq e)))
115+
116+
(kind/tex "x^1")
117+
118+
(kind/hiccup [:div
119+
[:div#kt2 "2"]
120+
[:div#kt3 "3"]
121+
[:div#kt4 "4"]
122+
[:script "katex.render(\"x^2\", document.getElementById(\"kt2\"))"]])
123+
124+
(kind/scittle
125+
'(.render js/katex "x^3" (.getElementById js/document "kt3")))
126+
127+
(kind/scittle
128+
'(def *click-count (reagent.core/atom 4)))
129+
130+
(kind/scittle
131+
'(.render js/katex (str "x^" @*click-count) (.getElementById js/document "kt4")))
132+
133+
(kind/reagent
134+
['(fn []
135+
[:div
136+
"The atom " [:code "*click-count"] " has value: "
137+
@*click-count ". "
138+
[:input {:type "button" :value "Click me!"
139+
:on-click #(swap! *click-count inc)}]])])
140+
141+
(show-tex "x^2")

src/mentat_collective/emmy/silcm_ch01.clj

Lines changed: 141 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
[mentat-collective.emmy.scheme :refer [define-1 let-scheme lambda]]
1717
[civitas.repl :as repl]))
1818

19+
;; TODO: Make a button for @path-dimension to make dimensions switchable from 4 -> 2
20+
1921
;; I investigate a Lorentz covariant Lagrangian that is not widely known. It is discussed in the textbooks of Greiner, "Systems of Particles and Hamiltonian Mechanics" (chapter 21), and also [on-line in: Cline, "Variational Principles in Classical Mechanics"](https://phys.libretexts.org/Bookshelves/Classical_Mechanics/Variational_Principles_in_Classical_Mechanics_(Cline)/17%3A_Relativistic_Mechanics/17.06%3A_Lorentz-Invariant_Formulation_of_Lagrangian_Mechanics).
2022

2123
;; Then I derive, along deBroglie's argument, the momentum-wavelength relation of a free particle.
@@ -80,12 +82,25 @@
8082
'(defn show-tex [e]
8183
(->infix e)))
8284

85+
^:kindly/hide-code
86+
(define (eq-transformation f)
87+
(lambda (tuple)
88+
(apply down (map f tuple))))
89+
90+
^:kindly/hide-code
91+
(kind/scittle
92+
'(defn show-eq [tuple]
93+
(->infix (simplify (down (first tuple) '= (second tuple))))))
94+
8395
^:kindly/hide-code
8496
(def tex (comp kind/tex emmy.expression.render/->TeX))
8597

8698
^:kindly/hide-code
8799
(def tex-simp (comp tex simplify))
88100

101+
^:kindly/hide-code
102+
(defn fn-show-eq [tuple]
103+
(tex-simp (down (first tuple) '= (second tuple))))
89104

90105
^:kindly/hide-code
91106
(define show-exp (comp str simplify))
@@ -121,6 +136,12 @@
121136
(list 'tex e)
122137
(reag-comp e)))
123138

139+
^:kindly/hide-code
140+
(defmacro show-eq [e]
141+
(if prod
142+
(list 'fn-show-eq e)
143+
(reag-comp e)))
144+
124145
^:kindly/hide-code
125146
(define velocities velocity)
126147

@@ -157,9 +178,7 @@
157178
(let ((v (velocity local)))
158179
(* 1/2 mass (square c)
159180
(- (* (/ 1 (square c))
160-
(+ (square (ref v 1))
161-
(square (ref v 2))
162-
(square (ref v 3))))
181+
(square (apply up (rest v))))
163182
(square (ref v 0))
164183
1))))
165184

@@ -187,21 +206,32 @@
187206
(show-tex-expression
188207
((Gamma Lc-test-path) 's))
189208

209+
^:kindly/hide-code
210+
(define path-dimension (atom true)) #_"true = 4D , false = 2D"
190211

191-
(define three-path (up (literal-function 'x)
192-
(literal-function 'y)
193-
(literal-function 'z)))
212+
^:kindly/hide-code
213+
(define three-path
214+
(if @path-dimension
215+
(up (literal-function 'x)
216+
(literal-function 'y)
217+
(literal-function 'z))
218+
(up (literal-function 'x))))
194219

195220
(show-tex-expression
196221
((Gamma three-path) 't))
197222

198223
(show-tex-expression
199224
((compose (L-free-particle 'm) (Gamma three-path)) 't))
200225

201-
(define four-path (up identity
202-
(literal-function 'x)
203-
(literal-function 'y)
204-
(literal-function 'z)))
226+
^:kindly/hide-code
227+
(define four-path
228+
(if @path-dimension
229+
(up identity
230+
(literal-function 'x)
231+
(literal-function 'y)
232+
(literal-function 'z))
233+
(up identity
234+
(literal-function 'x))))
205235

206236
(show-tex-expression
207237
((Gamma four-path) 's))
@@ -225,10 +255,15 @@
225255

226256
;; The first $0$ is because we set $t=s$ for the path. This reduces the extended Lagrangian to the "conventional" case. Reassuring. Now we can allow arbitrary functions $t(s)$, making the path truely 4-dimensional.
227257

228-
(define Lc-path (up (literal-function 't)
229-
(literal-function 'x)
230-
(literal-function 'y)
231-
(literal-function 'z)))
258+
^:kindly/hide-code
259+
(define Lc-path
260+
(if @path-dimension
261+
(up (literal-function 't)
262+
(literal-function 'x)
263+
(literal-function 'y)
264+
(literal-function 'z))
265+
(up (literal-function 't)
266+
(literal-function 'x))))
232267

233268
#_(define Lc-path (up (literal-function 't)
234269
(literal-function 'x)))
@@ -245,42 +280,115 @@
245280

246281
;; It is very interesting that a constant term can lead to a new formalism. Cline: "It provides a plausible manifestly-covariant Lagrangian for the one-body system, but serious problems exist extending this to the N-body system when N>1".
247282

248-
;; Nobody possesses a Lorentz covariant N-body mechanics. Goldstein "Classical Mechanics", chapter 7,9: "Hitherto, there does not exist a satisfying description of the relativistic many body system."
283+
;; Nobody possesses a Lorentz covariant N-body mechanics. Goldstein "Classical Mechanics", chapter 7.9: "Hitherto, there does not exist a satisfying description of the relativistic many body system."
249284

250-
;; This non-homogeneous business requires an implicit $L - pv = 0$ constraint (Cline Eq. 17.6.12, Struckmeier Eq. 21.9)
285+
;; ### The Constraint on the four dimensional path
251286

252-
(define (Lc1-Lagrange-constraint Lagrangian)
287+
;; The Lagranian above looks very nice because it is symmtreic in t, x, y, z. But there is a caveat which ultimately brings it in line with the orthodox formulation.
288+
289+
;; Non-homogeneous but symmetric Lagrangians of this type require a general implicit constraint: $L - pv = 0$ (Cline Eq. 17.6.12, Struckmeier Eq. 21.9)
290+
291+
(define (Lc-Lagrange-constraint Lagrangian)
253292
(- Lagrangian (* ((partial 2) Lagrangian) velocity)))
254293

255-
;; By multiplication this with a constant factor, this leads to Struckmeier Eq. 21.12:
294+
;; Applied to the free particle (and multiplication with a constant factor), leads to (Struckmeier Eq. 21.12):
256295

257-
(define Lc1-constr (compose (* (/ 2 (square 'c) 'm_0)
258-
(Lc1-Lagrange-constraint (Lc-free-particle 'm_0 'c))) (Gamma Lc-path)))
296+
(define Lc-constraint-s
297+
(down (constantly 0) (compose (* (/ 2 (square 'c) 'm_0)
298+
(Lc-Lagrange-constraint (Lc-free-particle 'm_0 'c)))
299+
(Gamma Lc-path))))
259300

260-
(show-tex-expression
261-
(Lc1-constr 's))
301+
^:kindly/hide-code
302+
(show-eq
303+
(Lc-constraint-s 's))
262304

263-
;; This constraint cannot be dealt with easily (contrary to constraints that do not depend on velocity but position which can be treated via Lagrange multipliers.)
305+
;; The above is a constraint, i.e. needs to be zero, needs to vanish for all physical paths.
264306

265-
;; Calculate $dt/ds$
266-
(define t-prime (sqrt (+ (- Lc1-constr)
267-
(square (ref (ref (Gamma Lc-path) 2) 0)))))
307+
;; Contrary to constraints that depend on position, this constraint (being about velocity) cannot be easily treated via Lagrange multipliers.
268308

269-
(show-tex-expression
270-
(t-prime 's))
309+
;; We proceed to explicitely calculate $(Dt(s))^2$
271310

272-
;; Maybe not useful, but one could also calculate $d^2t/ds^2$
311+
(define t (literal-function 't))
273312

274-
^{:kindly/kind :kind/hidden}
275-
(show-tex-expression
276-
((D t-prime) 's))
313+
(define Dt-squared
314+
((eq-transformation
315+
#(- (square (D t)) %))
316+
Lc-constraint-s))
317+
318+
^:kindly/hide-code
319+
(show-eq
320+
(Dt-squared 's))
277321

278-
;; Important is that $dt/ds$ leads to $ds/dt$ and the famous $\gamma$ factor (see e.g. Cline)
322+
;; The above is meant as an equality.
323+
324+
;; We now upgrade the parameter $s$ to a function $s(t)$ which is the inverse of $t(s)$, $s = t^{-1}$.
325+
326+
(define s (literal-function 's))
327+
328+
;; Inverse means that $t(s(y)) = y$. While in the math rendering there is a minor risk of confusion, in the underlying code we do not need the auxilary `y`, the function `t` and the symbol `'t` are clearly distinct:
329+
330+
(define t°s (down (comp t s) identity))
331+
332+
(show-eq
333+
(t°s 't))
334+
335+
;; The derivative results in the number `one`
336+
337+
(show-eq
338+
((D t°s) 't))
339+
340+
;; With this interlude, we return to the constraint. Just by formally replacing $s \rightarrow s(t)$, it reads as
341+
342+
(show-eq
343+
(Dt-squared (s 't)))
344+
345+
;; We'd like to get rid of the function `t` and only keep the symbol/parameter `'t`. For this, we multiply the above equation by $(Ds(t))^2$
346+
347+
(define eq1
348+
((eq-transformation
349+
#(* ((square (D s)) 't) %))
350+
(* (Dt-squared (s 't)))))
351+
352+
(show-eq eq1)
353+
354+
;; We introduce the following slighly sloppy notation
355+
356+
(show-eq
357+
((down three-path (comp three-path s)) 't))
358+
359+
;; The derivative of this definition is
360+
(show-eq
361+
((D (down three-path (comp three-path s))) 't))
362+
363+
;; With this the constraint becomes
364+
365+
^:kindly/hide-code
366+
(define constraint-1
367+
(down 1 (/ (+ (* (square 'c)
368+
((square (D (literal-function 's))) 't))
369+
((square (D three-path)) 't))
370+
(square 'c))))
371+
372+
(show-eq
373+
constraint-1)
374+
375+
;; Remember that, as shown above, the number `one` appears because of $s = t^{-1}$
376+
377+
;; Isolating $Ds(t)$ on the right hand side
378+
379+
(show-eq
380+
((eq-transformation
381+
#(sqrt (- % (/ ((square (D three-path)) 't) (square 'c)))))
382+
constraint-1))
383+
384+
;; The above equation, which is just another expression of the constraint $L - pv = 0$, is conventionally written as below, introducing the famous $\gamma$ factor of Special Relativity.
279385

280386
^:kindly/hide-code
281387
(kind/tex
282388
"\\frac{ds}{dt} = \\sqrt{1- \\frac{v^2}{c^2}} = \\sqrt{1 - \\beta ^ 2} = \\frac{1}{\\gamma}")
283389

390+
;; Thus, the function $s(t)$ is nothing but the proper time.
391+
284392
;; ## The deBroglie wavelength
285393

286394
^:kindly/hide-code

0 commit comments

Comments
 (0)