Skip to content

Commit 1d84cc7

Browse files
committed
emmy
1 parent cb212b2 commit 1d84cc7

File tree

6 files changed

+56
-25
lines changed

6 files changed

+56
-25
lines changed

deps.edn

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{:paths ["dev" "notebooks" "resources"]
2-
:deps {io.github.nextjournal/clerk {:git/sha "fa0ddcf000bbcd211008c93d8bd0b3047629f6b6"}
2+
:deps {io.github.nextjournal/clerk {:git/sha "9fa6b3761fec5544bacdf4117d0ec9b87b411a40"}
33
io.github.nextjournal/clerk-slideshow {:git/sha "ea9b27e5ec2e5ad57e889060521451ed9138325b"}
44

55
;; keep 1.10 until `kixi/stats` and `sicmutils` fix warnings
@@ -16,8 +16,8 @@
1616
;; some statistical routines
1717
kixi/stats {:mvn/version "0.5.4"}
1818

19-
;; SICMUtils numerical and physics routines
20-
sicmutils/sicmutils {:mvn/version "0.22.0"}
19+
;; Emmy numerical and physics routines
20+
io.github.mentat-collective/emmy {:git/sha "b98fef51d80d3edcff3100562b929f9980ff34d7"}
2121

2222
;; semantic web goodies and box/arrow graphs
2323
io.github.jackrusher/mundaneum {:git/sha "d2c934a12388d88ddb3e53fef92ec2eef97d6140"}
@@ -44,7 +44,7 @@
4444
"notebooks/images.clj"
4545
"notebooks/logo.clj"
4646
"notebooks/semantic.clj"
47-
"notebooks/sicmutils.clj"
47+
"notebooks/emmy.clj"
4848
"notebooks/rule_30.clj"
4949
"notebooks/zipper_with_scars.clj"]}
5050
:main-opts ["-m" "babashka.cli.exec"]}

dev/user.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
;; or call `clerk/show!` explicitly
1818
(clerk/show! "notebooks/introduction.clj")
1919
(clerk/show! "notebooks/data_science.clj")
20-
(clerk/show! "notebooks/sicmutils.clj")
20+
(clerk/show! "notebooks/emmy.clj")
2121
(clerk/show! "notebooks/rule_30.clj")
2222
(clerk/show! "notebooks/semantic.clj")
2323
(clerk/show! "notebooks/images.clj")

index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
:description "Some examples about dealing with images."}
4040
{:title "The double pendulum"
4141
:preview "https://cdn.nextjournal.com/data/Qmdhk9WEogAvH9cgehnpq3REkATa91JiWgYNUtoSW74Q9A?filename=CleanShot%202021-11-30%20at%[email protected]&content-type=image/png"
42-
:path "notebooks/sicmutils.clj"
42+
:path "notebooks/emmy.clj"
4343
:description "Simulate and visualize physical systems from the REPL."}
4444
{:title "Rule 30"
4545
:preview "https://cdn.nextjournal.com/data/QmQCrqkdYtKfNm9CGbXhzY4cy6qG8xhpWaCRPF5m6biLgV?filename=CleanShot%202021-11-30%20at%[email protected]&content-type=image/png"

notebooks/sicmutils.clj renamed to notebooks/emmy.clj

+32-18
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
;; 1.44](https://tgvaughan.github.io/sicm/chapter001.html#Exe_1-44) from Sussman
55
;; and Wisdom's [Structure and Interpretation of Classical
66
;; Mechanics](https://tgvaughan.github.io/sicm/), using
7-
;; the [SICMUtils](https://github.com/sicmutils/sicmutils) Clojure library and
7+
;; the [Emmy](https://github.com/mentat-collective/emmy) Clojure library and
88
;; the Clerk rendering environment.
99

1010
#_{:clj-kondo/ignore [:refer-all]}
11-
(ns sicmutils
11+
(ns emmy
1212
(:refer-clojure
13-
:exclude [+ - * / partial ref zero? numerator denominator compare = run!])
13+
:exclude [+ - * / partial ref zero? numerator denominator compare = run!
14+
abs infinite?])
1415
(:require [nextjournal.clerk :as clerk]
15-
[sicmutils.env :as e :refer :all]
16-
[sicmutils.expression.render :as xr]))
16+
[emmy.env :as e :refer :all]
17+
[emmy.expression.compile :as xc]
18+
[emmy.expression.render :as xr]))
1719

1820
;; ## Lagrangian
1921
;;
@@ -39,7 +41,6 @@
3941
(* 1/2 m2 (+ (square xdot2)
4042
(square ydot2))))))
4143

42-
4344
;; `V` describes a uniform gravitational potential with coefficient `g`, acting
4445
;; on two particles with masses of, respectively, `m1` and `m2`. Again, this is
4546
;; written in rectangular coordinates.
@@ -86,6 +87,8 @@
8687

8788
;; And here are the equations of motion for the system:
8889

90+
;; TODO this currently causes a notebook failure.
91+
#_
8992
(let [L (L-double-pendulum 'm_1 'm_2 'l_1 'l_2 'g)]
9093
(binding [xr/*TeX-vertical-down-tuples* true]
9194
(render-eq
@@ -172,7 +175,8 @@
172175
;; state. Chaotic first:
173176

174177
(def raw-chaotic-data
175-
(run! step horizon chaotic-initial-q))
178+
(time
179+
(run! step horizon chaotic-initial-q)))
176180

177181
;; Looks good:
178182

@@ -181,12 +185,14 @@
181185
;; Next, the regular initial condition:
182186

183187
(def raw-regular-data
184-
(run! step horizon regular-initial-q))
188+
(time
189+
(run! step horizon regular-initial-q)))
185190

186191
;; Peek at the final state:
187192

188193
(peek raw-regular-data)
189194

195+
190196
;; ## Measurements, Data Transformation
191197

192198
;; Next we'll chart the measurements trapped in those sequences of state tuples.
@@ -225,8 +231,16 @@
225231
#_{:clj-kondo/ignore [:unresolved-symbol]}
226232
(defn transform-data [xs]
227233
(let [energy-fn (L-energy m1 m2 l1 l2 g)
228-
monitor (energy-monitor energy-fn (first xs))
229-
xform (angles->rect l1 l2)
234+
monitor (xc/compile-state-fn
235+
(energy-monitor energy-fn (first xs))
236+
false
237+
(first xs)
238+
{:calling-convention :structure})
239+
xform (xc/compile-state-fn
240+
(angles->rect l1 l2)
241+
false
242+
(first xs)
243+
{:calling-convention :structure})
230244
pv (principal-value Math/PI)]
231245
(map (fn [[t [theta1 theta2] [thetadot1 thetadot2] :as state]]
232246
(let [[x1 y1 x2 y2] (xform state)]
@@ -245,11 +259,11 @@
245259
;; The following forms transform the raw data for each initial condition and
246260
;; bind the results to `chaotic-data` and `regular-data` for exploration.
247261

248-
(def chaotic-data
262+
(defonce chaotic-data
249263
(doall
250264
(transform-data raw-chaotic-data)))
251265

252-
(def regular-data
266+
(defonce regular-data
253267
(doall
254268
(transform-data raw-regular-data)))
255269

@@ -307,9 +321,9 @@
307321
;; a helper function that should be in clojure.core
308322
(defn deep-merge [v & vs]
309323
(letfn [(rec-merge [v1 v2]
310-
(if (and (map? v1) (map? v2))
311-
(merge-with deep-merge v1 v2)
312-
v2))]
324+
(if (and (map? v1) (map? v2))
325+
(merge-with deep-merge v1 v2)
326+
v2))]
313327
(when (some identity vs)
314328
(reduce #(rec-merge %1 %2) v vs))))
315329

@@ -439,9 +453,9 @@
439453
(defn L-double-double-pendulum [m1 m2 l1 l2 g]
440454
(fn [[t [thetas1 thetas2] [qdots1 qdots2]]]
441455
(let [s1 (up t thetas1 qdots1)
442-
s2 (up t thetas2 qdots2)]
443-
(+ ((L-double-pendulum m1 m2 l1 l2 g) s1)
444-
((L-double-pendulum m1 m2 l1 l2 g) s2)))))
456+
s2 (up t thetas2 qdots2)]
457+
(+ ((L-double-pendulum m1 m2 l1 l2 g) s1)
458+
((L-double-pendulum m1 m2 l1 l2 g) s2)))))
445459

446460
(def dd-state-derivative
447461
(compose

notebooks/emmy_repro.clj

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(ns emmy-repro
2+
(:require [emmy.env :as e]
3+
[emmy.mechanics.lagrange]))
4+
5+
;; ## BUG 1:
6+
7+
;; This notebook takes close to 2 seconds to evaluate:
8+
9+
;; Clerk evaluated '/Users/sritchie/code/clj/clerk-demo/notebooks/emmy_repro.clj' in 1853.674042ms.
10+
11+
(defn angles->rect [theta1]
12+
(e/sin theta1))
13+
14+
15+
;; Form the final Langrangian in generalized coordinates (the angles of each
16+
;; segment) by composing `L-rect` with a properly transformed `angles->rect`
17+
;; coordinate transform!

notebooks/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
## 🥁 [Intro](#/notebooks/introduction.clj)
44
## 📈 [Data Science](#/notebooks/data_science.clj)
55
## 🕸 [Semantic Queries](#/notebooks/semantic.clj)
6-
## 🔬 [SICMUtils Double Pendulum](#/notebooks/sicmutils.clj)
6+
## 🔬 [Emmy Double Pendulum](#/notebooks/emmy.clj)
77
## 🕹 [Rule 30](#/notebooks/rule_30.clj)
88
## ✏️ [Markdown Files](#/notebooks/markdown.md)

0 commit comments

Comments
 (0)