A powerful computer algebra system written in Clojure(Script).
The interactive documentation on this page was generated using Clerk. Follow the instructions in the README to run and modify this notebook on your machine!
See the Github project for more details, and the cljdoc page for detailed API documentation.
Emmy is a Clojure(Script) implementation of the scmutils system for math and physics investigations in the Clojure and ClojureScript languages. Emmy provides facilities for
And implementations of many different mathematical objects, all built on a tower of generic, extensible mathematical operations.
Scmutils is extensively used in the textbooks The Structure and Interpretation of Classical Mechanics and Functional Differential Geometry by G.J. Sussman and J. Wisdom.
Install Emmy
into your Clojurescript project using the instructions at its Clojars page:
Or grab the most recent code using a Git dependency:
Require emmy.env
in your Clojure(Script) namespace:
These guides are currently woefully incomplete... we need more! Please hold tight while we work on these...
Math works as expected (se Generics for the full menu of operations), but notice that the numeric tower includes complex numbers, and proper ratios in ClojureScript:
Symbols are interpreted as abstract complex numbers, and arithmetic on them generates symbolic expressions.
Render to an infix string with ->infix
:
Or to Loading... with ->TeX
:
Note the additional
nextjournal.clerk/tex
wrapper here, signaling to the notebook that we should render the string returned by->TeX
as Loading....
Let's define a render
function that does this for us:
simplify
can simplify symbolic expressions:
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:
Special suffixes like dot
, dotdot
, prime
, primeprime
, var
, vec
and tilde
will modify the symbol's infix or Loading... representation. _
triggers a subscript, and the unicode character ↑ will trigger a superscript.
Here's a selection of examples:
Use the D
operator to perform forward-mode automatic differentiation.
Generate the Taylor series expansion of a literal function f
by exponentiating the D
operator:
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
:
and generate the two Euler-Lagrange equations of motion for the r
and theta
coordinates:
Emmy
is compatible with SCI, the Small Clojure Interpreter.
To install Emmy
into your SCI context, require the emmy.sci
namespace and call emmy.sci/install!
:
If you want more granular control, see the cljdoc page for emmy.sci
for an SCI config and distinct SCI namespace objects that you can piece together.
Note that
Emmy
does not ship with a dependency on SCI, so you'll need to install your own version.
Using Emmy
with Nextjournal's Clerk gives you the ability to write notebooks like this one.
Doing this requires that you generate a custom ClojureScript build for your Clerk project. The easiest way to do this for an existing project is with the clerk-utils
project. Follow the instructions on the clerk-utils
guide for custom ClojureScript.
If this is your first time using Clerk, use the emmy/clerk
template described below to generate a new project with all steps described in "Emmy via SCI" already completed.
Emmy
includes a deps-new
template called emmy/clerk
that makes it easy to configure a new Clerk project with everything described in "Emmy via SCI" already configured.
First, install the deps-new
tool:
To create a new Clerk project based on emmy/clerk
in a folder called my-notebook-project
, run the following command:
The README.md file in the generated project contains information on how to develop within the new project.
If you have an existing Clerk notebook project and are considering adding Emmy
, you might consider using emmy/clerk
to get some ideas on how to structure your own project.
Emmy
ships with a configuration that allows clj-kondo to lint the library's macros.
To install the exported linter configuration:
Install clj-kondo using these instructions. I highly recommend configuring editor integration for your text editor.
If it doesn't exist yet, create a .clj-kondo
folder in your project:
clj-kondo
using the following command. This will import the emmy
config and populate clj-kondo
's cache with linting information about all of your dependencies:The steps listed here mirror the instructions in the clj-kondo repo.
Many of the namespaces in the project are written up as literate essays. Expect more organization here in coming versions; for now, here is the full list of namespace essays.
The following projects use Emmy:
clj-tiles
.If you want to show off your use of Emmy, please file a ticket and let us know!
To support this work and my other open source projects, consider sponsoring me via my GitHub Sponsors page. Thank you to my current sponsors!
For more information on me and my work, visit https://samritchie.io.
Copyright © 2022-2023 Sam Ritchie and Colin Smith.
Distributed under the GPLv3 License. See LICENSE.