Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phase vectors, update examples #71

Merged
merged 21 commits into from
Sep 19, 2023
Merged
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

## [unreleased]

- #70:

- Adds

- `emmy.mathbox.physics.{phase-vectors,lagrangian-phase-vectors,hamiltonian-phase-vectors,routhian-phase-vectors}`
for building out phase portraits like those in the Phase Portrait and
Quartic Well examples.

- many geodesics to the Klein bottle examples.

- a matrix basis visualization example to `examples.mathbox.geom`.

- `ode-curve` now sets `:simplify?` to `false` by default.

- Converts the phase portrait and quartic well examples to use all server side
code and components.

- `emmy.viewer.components.physics/Evolve` now adds a `:tick` entry into its
state to force re-renders, since the `:state` array is mutable.

- #69:

- Adds
Expand Down
2 changes: 0 additions & 2 deletions dev/emmy_viewers/mathbox.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

;; ## Server-Side MathBox

(def ->tex (comp clerk/tex ->TeX simplify))

{::clerk/width :wide}

^{::clerk/visibility {:code :hide :result :hide}}
Expand Down
1 change: 1 addition & 0 deletions dev/emmy_viewers/notebook.clj
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
;; - [examples.manifold.pq-knot](/dev/examples/manifold/pq_knot)
;; - [examples.mathbox.cube-controls](/dev/examples/mathbox/cube_controls)
;; - [examples.mathbox.functions](/dev/examples/mathbox/functions)
;; - [examples.mathbox.feig](/dev/examples/mathbox/feig)
;; - [examples.mathbox.geom](/dev/examples/mathbox/geom)
;; - [examples.mathbox.ode](/dev/examples/mathbox/ode)
;; - [examples.mathbox.quickstart](/dev/examples/mathbox/quickstart)
Expand Down
2 changes: 0 additions & 2 deletions dev/emmy_viewers/sci_extensions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
(:require [emmy.viewer.sci]
[examples.simulation.cylinder-flow]
[examples.simulation.lorenz]
[examples.simulation.phase-portrait]
[examples.simulation.quartic-well]
[nextjournal.clerk.sci-env]))

(emmy.viewer.sci/install!)
18 changes: 9 additions & 9 deletions dev/examples/expression.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
(ns examples.expression
{:nextjournal.clerk/toc true}
(:refer-clojure
:exclude [+ - * / = zero? compare numerator denominator ref partial])
:exclude [+ - * / = zero? compare abs
numerator denominator ref partial infinite?])
(:require [nextjournal.clerk :as clerk]
[nextjournal.clerk.viewer :as-alias viewer]
[emmy.env :as e :refer [+ * / ->TeX cos expt sin square]]
[emmy.env :as e :refer :all]
[emmy.clerk :as ec]
[reagent.core :as-alias reagent]))

Expand All @@ -25,17 +26,16 @@
;; We can convert this to LaTeX, but clerk won't know what to do with the
;; string.

(->TeX
(ec/->TeX
(+ (square (sin 'x))
(square (cos 'x))))

;; We can do better by invoking `clerk/tex` to tell Clerk to interpret this
;; string as TeX:
;; We can do better by invoking `emmy.clerk/->TeX` to tell Clerk to interpret
;; this string as TeX:

(clerk/tex
(->TeX
(+ (square (sin 'x))
(square (cos 'x)))))
(ec/->TeX
(+ (square (sin 'x))
(square (cos 'x))))

;; Try it out:

Expand Down
5 changes: 1 addition & 4 deletions dev/examples/mafs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,9 @@
;;
;; tabbed viewer between TeX and plot:

(def ->tex
(comp clerk/tex ->TeX simplify))

(let [f ((expt D 5) tanh)]
(ec/multi
{:TeX (->tex (f 'x))
{:TeX (ec/->TeX (f 'x) :simplify? true)
:Mafs (mafs/mafs
{:zoom {:min 0.1 :max 2}}
(mafs/cartesian)
Expand Down Expand Up @@ -264,7 +261,7 @@

(let [f ((expt D 3) tanh)]
(clerk/col
(->tex (f 'x))

Check failure on line 264 in dev/examples/mafs.clj

View workflow job for this annotation

GitHub Actions / lint

Unresolved symbol: ->tex
(mafs/mafs {:zoom {:min 0.1 :max 2}}
(mafs/cartesian)
(mafs/of-x f {:color :blue}))))
Expand Down
1 change: 0 additions & 1 deletion dev/examples/mafs/edit.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
:nextjournal.clerk/doc-css-class [:overflow-hidden :p-0]}
(:require [nextjournal.clerk :as clerk]))


(clerk/with-viewer
{:render-fn 'nextjournal.clerk.render.editor/view
:transform-fn clerk/mark-presented}
Expand Down
35 changes: 35 additions & 0 deletions dev/examples/mathbox/color_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
^{:nextjournal.clerk/visibility {:code :hide}}
(ns examples.mathbox.color-test
#:nextjournal.clerk{:toc true}
(:refer-clojure
:exclude [+ - * / = zero? compare numerator denominator ref partial
infinite? abs])
(:require [emmy.clerk :as ec]
[emmy.leva]
[emmy.env :as e :refer :all]
[emmy.mathbox.plot :as plot]
[emmy.viewer :as ev]))

{:nextjournal.clerk/width :wide}

^{:nextjournal.clerk/visibility {:code :hide :result :hide}}
(ec/install!)

;; # Color Testing

;; ## Demo
;;
;; Let's make a function to try!

(ev/with-let [!state {:amplitude 1}]
(plot/scene
(emmy.leva/controls {:atom !state})
(plot/of-xy
{:x-samples 128
:y-samples 128
#_#_:color "LimeGreen"
:z (ev/with-params {:atom !state :params [:amplitude]}
(fn [amplitude]
(fn [[x y]]
(* amplitude
(cos (* x y))))))})))
71 changes: 71 additions & 0 deletions dev/examples/mathbox/feig.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
;; # Feigenbaum

(ns examples.mathbox.feig
{:nextjournal.clerk/toc true}
(:require [emmy.clerk :as ec]
[emmy.mathbox :as mb]
[nextjournal.clerk :as clerk]))

^{::clerk/visibility {:code :hide :result :hide}}
(ec/install!)

(defn f [p x] (* p x (- 1 x)))

(defn fixps [r]
(->> (iterate (partial f r) (rand))
(drop 300)
(take 300)
set
(map (fn [x] [r x]))))

(def tree (into [] (mapcat fixps) (range 2.5 4.0 0.005)))
(def width (count tree))
(def boundary (Math/floor (Math/sqrt width)))

(defn Feigenbaum []
[:<>
(mb/matrix
{:width (quot width boundary)
:height (mod width boundary)
:channels 2
:live false
:expr `(fn [emit# i# j#]
(apply emit# (nth ~tree (+ (* i# ~boundary) j#) nil)))})
(mb/point
{:color "#3090FF"
:size 1})])

(defn axis
([axis-no] (axis axis-no [0 0 0 0]))
([axis-no origin]
[:<>
(mb/axis {:axis axis-no
:width 2
:color "black"
:origin origin})
(mb/scale {:divide 10
:axis axis-no
:origin origin})
(mb/ticks {:width 5
:size 15
:color "black"})
(mb/format {:digits 2
:weight "bold"})
(mb/label {:color "red"
:zIndex 1})]))

^{::clerk/width :wide}
(mb/mathbox
{:container {:style {:height "1000px" :width "100%"}}}
(mb/camera {:position [0 0 2.1]
:proxy true})
(mb/layer
(mb/cartesian
{:range [[2.5 4 0] [0 1 0]]
:scale [0.8 0.8 1]}
(mb/grid {:width 1
:divideX 10
:divideY 10})
(axis 1)
(axis 2 [2.5 0 0 0])
(Feigenbaum))))
24 changes: 22 additions & 2 deletions dev/examples/mathbox/geom.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(:require [emmy.clerk :as ec]
[emmy.leva]
[emmy.env :as e :refer :all]
[emmy.matrix]
[emmy.viewer :as ev]
[emmy.mathbox.plot :as p]))

Expand All @@ -15,9 +16,28 @@
^{:nextjournal.clerk/visibility {:code :hide :result :hide}}
(ec/install!)

(defn basis [m]
(let [[x y z] (emmy.matrix/transpose m)]
(-> [:<>
(p/vector {:tip x :arrow-size 3 :color "red"})
(p/vector {:tip y :arrow-size 3 :color "green"})
(p/vector {:tip z :arrow-size 3 :color "blue"})]
(ev/fragment p/scene))))

(ev/with-let [!size {:y 1}]
(p/scene
(emmy.leva/controls
{:atom !size :schema {:y {:min 0 :max 10 :step 0.01}}})
(basis
(matrix-by-rows
[1 0 0]
[0 (ev/get !size :y) 0]
[1 0 2]))))

(ev/with-let [!size {:size 20 :pos 1}]
[:<> (emmy.leva/controls
{:atom !size :schema {:size {:min 20 :max 100 :step 1}}})
[:<>
(emmy.leva/controls
{:atom !size :schema {:size {:min 20 :max 100 :step 1}}})
(p/scene
(p/point
{:coords [0.5 -0.5 (ev/get !size :pos)]
Expand Down
16 changes: 8 additions & 8 deletions dev/examples/simulation/ellipsoid.clj
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@
;;
;; Lagrange equations of motion for the ellipsoid:

(def ->tex (comp clerk/tex ->TeX simplify))

(->tex
(ec/->TeX
(let [L (L-central-triaxial 'm 'g 'a 'b 'c)
theta (literal-function 'theta)
phi (literal-function 'phi)]
(((Lagrange-equations L) (up theta phi))
't)))
(simplify
(((Lagrange-equations L) (up theta phi))
't))))

;; And for the sphere:

(->tex
(ec/->TeX
(let [L (L-central-triaxial 'm 'g 'r 'r 'r)
theta (literal-function 'theta)
phi (literal-function 'phi)]
(((Lagrange-equations L) (up theta phi))
't)))
(simplify
(((Lagrange-equations L) (up theta phi))
't))))

;; This is fairly horrifying. This really demands animation, as I bet it looks
;; cool, but it's not comprehensible in this form.
Expand Down
Loading
Loading