Emmy / SICMUtils + Nextjournal Quick Start
This page contains a series of "Getting Started" warmup exercises for the SICMUtils Clojure(script) library.
If you see a "Remix" button at the top right, Make this page interactive by visiting the "try" version of the page, at: https://nextjournal.com/try/samritchie/emmy.
If you have any examples you think would be great for this introductory tutorial, please submit an issue at the SICMUtils Github issue tracker. This is hugely helpful!
SICMUtils Overview
SICMUtils is a system for math and physics investigations in the Clojure and Clojurescript languages. SICMUtils provides facilities for
symbolic computation, including state of the art TeX rendering and expression simplification
investigations in differential geometry and Lagrangian and Hamiltonian mechanics
And implementations of many different mathematical objects, all built on a tower of generic, extensible mathematical operations.
SICMUtils is extensively used in the textbooks The Structure and Interpretation of Classical Mechanics and Functional Differential Geometry by G.J. Sussman and J. Wisdom.
Quickstart
To use the library from any Nextjournal page, first create a cljs
code block, and evaluate the following form:
(require [sicmutils.env :refer :all])
Every subsequent cljs
block will have every function in the sicmutils.env
namespace available. (Open the API Docs and feel free to try out anything at the REPL.)
Make sure everything's working:
(take 10 (((exp D) sin) x))
Do you see the rendered inline_formula not implemented expression? Great!
Arithmetic, Numeric Tower
Math works as expected (see Generics for the full menu of operations). Notice that the numeric tower includes support for ratios in Clojurescript:
(let [x (/ 3 2)]
(and
(ratio? x)
(= 3 (numerator x))
(= 2 (denominator x))))
complex numbers are supported too:
(let [x (complex 1 2)]
(and
(complex? x)
(= 1 (real-part x))
(= 2 (imag-part x))))
You can also build complex numbers in polar form. make-polar
takes a magnitude and radius and returns a complex number:
(let [x (make-polar 5 pi)]
(and
(complex? x)
(= 5 (magnitude x))
(= pi (angle x))))
Symbolic Expressions
Symbols are interpreted as abstract complex numbers, and arithmetic on them generates symbolic expressions. You can render these with ->TeX
and ->infix
:
(def render
(comp ->infix simplify))
(render
(square (sin (+ a 3))))
Nextjournal renders symbolic expressions to inline_formula not implemented by default:
(square (sin (+ a 3)))
If you name a symbol after a greek letter, it will render to that letter. Capitalize the first letter to get the capital version of the character:
(+ Theta alpha)
Special suffixes like dot
, dotdot
, prime
, primeprime
, var
, vec
and tilde
will modify the symbol's infix or inline_formula not implemented representation. _
triggers a subscript, and the unicode character ↑ will trigger a superscript.
Here's a selection of examples:
(up
alphadot_beta
xdotdot
zetaprime_alphadot
alphaprimeprime_mubar
vbar
Pivec
alphatilde)
Automatic Differentiation
Use the D
operator to perform forward-mode automatic differentiation . Nextjournal applies simplify
automatically to collapse symbolic expressions into tidy form:
((D cube) x)
Of course sometimes you do NOT want to simplify an expression! Breaking out this default depends on the work in this ticket. Follow along there for updates.
Physics, Classical Mechanics
SICMUtils is based on the engine behind Sussman and Wisdom's The Structure and Interpretation of Classical Mechanics, and has a built-in API for exploring Lagrangian and Hamiltonian mechanics.
Define a Lagrangian for a central potential U
acting on a particle with mass m
:
(defn L-central-polar [m U]
(fn [[_ [r] [rdot thetadot]]]
(- (* 1/2 m
(+ (square rdot)
(square (* r thetadot))))
(U r))))
and generate the two Euler-Lagrange equations of motion for the r
and theta
coordinates:
(let [potential-fn (literal-function U)
L (L-central-polar m potential-fn)
state (up (literal-function r)
(literal-function theta))]
(((Lagrange-equations L) state) t))
What Next?
There is so much more! This is a dense library, and lots of documentation remains to be written. Some suggested next steps, for now:
Read the SICMUtils Reference Manual("refman") for inspiration. All of the code snippets in the refman will work in this Nextjournal environment. Use the two together!
Visit our CLJDocs page for an introduction and detailed documentation
Watch Colin Smith's "Physics in Clojure" talk for on overview of SICMUtils and its implementation
Visit the HTML version of Structure and Interpretation of Classical Mechanics. Many of the SICM exercises have been worked using SICMUtils; they live at this Nextjournal page.
Explore the learning resources page on the SICMUtils Wiki. This page has many resources and suggested ways to learn more about the math and physics that SICMUtils is aimed at exploring.
If you have any examples you think would be great for this introductory tutorial, please submit an issue at the SICMUtils Github issue tracker. This is hugely helpful!