|
| 1 | +^{:kindly/hide-code true |
| 2 | + :clay {:title "Emmy, the Algebra System: Differentail Geometry Chapter One" |
| 3 | + :quarto {:author :kloimhardt |
| 4 | + :type :post |
| 5 | + :date "2025-09-10" |
| 6 | + :image "sicm_ch01.png" |
| 7 | + :category :libs |
| 8 | + :tags [:emmy :physics]}}} |
| 9 | + |
| 10 | +(ns mentat-collective.emmy.fdg-ch01 |
| 11 | + (:require [scicloj.kindly.v4.api :as kindly] |
| 12 | + [scicloj.kindly.v4.kind :as kind] |
| 13 | + [civitas.repl :as repl])) |
| 14 | + |
| 15 | +(kind/hiccup |
| 16 | + [:div |
| 17 | + [:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen/dist/scittle.js"}] |
| 18 | + [:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen/dist/scittle.emmy.js"}] |
| 19 | + [:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen/dist/scittle.cljs-ajax.js"}] |
| 20 | + [:script {:src "https://cdn.jsdelivr.net/npm/react@18/umd/react.production.min.js", :crossorigin ""}] |
| 21 | + [:script {:src "https://cdn.jsdelivr.net/npm/react-dom@18/umd/react-dom.production.min.js", :crossorigin ""}] |
| 22 | + [:script {:src "https://cdn.jsdelivr.net/npm/scittle-kitchen/dist/scittle.reagent.js"}]]) |
| 23 | + |
| 24 | +^:kindly/hide-code |
| 25 | +(def md |
| 26 | + (comp kindly/hide-code kind/md)) |
| 27 | + |
| 28 | +(md "The following examples are taken from the open-access book [Structure and Interpretation of Classical Mechanics (SICM)](https://mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/9579/sicm_edition_2.zip/chapter001.html).") |
| 29 | + |
| 30 | +(kind/scittle |
| 31 | + '(defn walk [inner outer form] |
| 32 | + (cond |
| 33 | + (list? form) (outer (apply list (map inner form))) |
| 34 | + (seq? form) (outer (doall (map inner form))) |
| 35 | + (coll? form) (outer (into (empty form) (map inner form))) |
| 36 | + :else (outer form)))) |
| 37 | + |
| 38 | +(kind/scittle |
| 39 | + '(defn postwalk [f form] |
| 40 | + (walk (partial postwalk f) f form))) |
| 41 | + |
| 42 | +(kind/scittle |
| 43 | + '(defn postwalk-replace [smap form] |
| 44 | + (postwalk (fn [x] (if (contains? smap x) (smap x) x)) form))) |
| 45 | + |
| 46 | +(kind/scittle |
| 47 | + '(defmacro let-scheme [b & e] |
| 48 | + (concat (list 'let (into [] (apply concat b))) e))) |
| 49 | + |
| 50 | +(kind/scittle |
| 51 | + '(defmacro define-1 [h & b] |
| 52 | + (let [body (postwalk-replace {'let 'let-scheme} b)] |
| 53 | + (if (coll? h) |
| 54 | + (if (coll? (first h)) |
| 55 | + (list 'defn (ffirst h) (into [] (rest (first h))) |
| 56 | + (concat (list 'fn (into [] (rest h))) body)) |
| 57 | + (concat (list 'defn (first h) (into [] (rest h))) |
| 58 | + body)) |
| 59 | + (concat (list 'def h) body))))) |
| 60 | + |
| 61 | +(kind/scittle |
| 62 | + '(defmacro define [h & b] |
| 63 | + (if (and (coll? h) (= (first h) 'tex-inspect)) |
| 64 | + (list 'do |
| 65 | + (concat ['define-1 (second h)] b) |
| 66 | + h) |
| 67 | + (concat ['define-1 h] b)))) |
| 68 | + |
| 69 | +(kind/scittle |
| 70 | + '(defmacro lambda [h b] |
| 71 | + (list 'fn (into [] h) b))) |
| 72 | + |
| 73 | +(kind/scittle |
| 74 | + '(require '[emmy.env :refer :all :exclude [Lagrange-equations Gamma]])) |
| 75 | + |
| 76 | +(kind/scittle |
| 77 | + '(def show-expression (comp ->infix simplify))) |
| 78 | + |
| 79 | +(kind/scittle |
| 80 | + '(def velocities velocity)) |
| 81 | + |
| 82 | +(kind/scittle |
| 83 | + '(def coordinates coordinate)) |
| 84 | + |
| 85 | +(kind/scittle |
| 86 | + '(def vector-length count)) |
| 87 | + |
| 88 | +(kind/scittle |
| 89 | + '(defn time [state] (first state))) |
| 90 | + |
| 91 | +(defmacro define [& b] |
| 92 | + (list 'kind/scittle (list 'quote (cons 'define b)))) |
| 93 | + |
| 94 | +(defmacro show-expression [& b] |
| 95 | + (list 'kind/reagent [:p (list 'quote (cons 'show-expression b))])) |
| 96 | + |
| 97 | +(kind/scittle '(declare Gamma)) |
| 98 | + |
| 99 | +(define ((Lagrange-equations Lagrangian) w) |
| 100 | + (- (D (compose ((partial 2) Lagrangian) (Gamma w))) |
| 101 | + (compose ((partial 1) Lagrangian) (Gamma w)))) |
| 102 | + |
| 103 | +(define ((Gamma w) t) |
| 104 | + (up t (w t) ((D w) t))) |
| 105 | + |
| 106 | +(define ((L-harmonic m k) local) |
| 107 | + (let ((q (coordinate local)) |
| 108 | + (v (velocity local))) |
| 109 | + (- (* 1/2 m (square v)) |
| 110 | + (* 1/2 k (square q))))) |
| 111 | + |
| 112 | +(define (proposed-solution t) |
| 113 | + (* 'a (cos (+ (* 'omega t) 'phi)))) |
| 114 | + |
| 115 | +(show-expression |
| 116 | + (((Lagrange-equations (L-harmonic 'm 'k)) |
| 117 | + proposed-solution) |
| 118 | + 't)) |
| 119 | + |
| 120 | +(repl/scittle-sidebar) |
0 commit comments