Explain. Explore. Experience.
Mathterpiece
A language for math you can touch.
- Declare what the figure is, does, and looks like.
- The compiler typechecks every possible state.
- The runtime makes it accessible and interactive.
; chords in a circle: from each point k to the point m × k(int n 200 (const))(real m 1 (>= 1) (<= 12)) ; typechecked: m stays in 1 to 12(func spot ((real k)) (polar 1 (turn (/ k n)))) ; what the figure is(scene chords (circle rim (r 1) (fill none) (stroke 'gray')) (foreach k n (line_ (from (spot k)) (to (spot (* m k))) ; what the figure looks like (stroke (mix 'crimson' 'royalblue' (/ k n))) (stroke-opacity 0.5)))) ; what it does(animation sweep (loop) (move m (from 1) (to 12) (duration 40)) (move m (from 12) (to 1) (duration 40)))(canvas v chords (on-ready (animation sweep))) ; accessible and interactive: mouse, touch or keyboard(slider times (move m (from 1) (to 12)) (label 'multiplier m'))(button again (label 'play') (animation sweep))
; the medians meet at one point, cutting six equal areas(import math clamp)(point a [-0.9 -0.6])(point b [0.9 -0.5])(point c [0.1 0.85])(point g (* (/ 1 3) (+ a b c))) ; what the figure is(scene triangle (group sixths (defaults (stroke 'white') (stroke-width 2)) (foreach t [[a b c] [b c a] [c a b]] ; typechecked: t[2] exists (let ((m (lerp t[0] t[1] 0.5))) ; what the figure looks like (polygon_ (points [t[0] g m]) (fill 'cornflowerblue')) (polygon_ (points [t[1] g m]) (fill 'slateblue'))))) (foreach k 3 (circle #'dot-{k}' (r 0.08) (pos [a b c][k]) (fill 'slateblue')))) ; what it does: drag a corner, or tap it, then tap a spot(func keep ((point p)) [(clamp p.x -1.1 1.1) (clamp p.y -1 1)])(canvas v (x -1.2) (y -1.1) (width 2.4) (height 2.2) triangle (on-drag dot-0 (move a (to (keep pointer)))) (on-drag dot-1 (move b (to (keep pointer)))) (on-drag dot-2 (move c (to (keep pointer)))))
; the birthday problem: among 23 people a shared birthday is likely(int n 23 (>= 2) (<= 60)) ; typechecked: 2 to 60 people(func pair ((int m)) (- 1 (reduce (range m) 1 (fn ((real q) (int k)) (* q (/ (- 365 k) 365))))))(func yours ((int m)) (- 1 (^ (/ 364 365) (- m 1)))) ; what the figure is: both chances, for up to 60 people(scene chance (line_ (from [0 15]) (to [60 15]) (stroke 'gray') (dash [4 4])) (line_ (from [n 0]) (to [n 30]) (stroke 'gray')) ; what the figure looks like (polyline_ (stroke 'slateblue') (stroke-width 3) (points (map (range 61) (fn ((int m)) [m (* 30 (pair m))])))) (polyline_ (stroke 'crimson') (stroke-width 3) (points (map (range 61) (fn ((int m)) [m (* 30 (yours m))])))) (circle_ (r 1) (pos [n (* 30 (pair n))]) (fill 'slateblue')) (circle_ (r 1) (pos [n (* 30 (yours n))]) (fill 'crimson'))) ; what it does: the slider and the numbers sit in the sentence(canvas v (x -2) (y -2) (width 64) (height 34) chance)(slider people (move n (from 2) (to 60)))(readout count $'{n}')(readout odds $'{(* 100 (pair n)):.1f}%')(readout mine $'{(* 100 (yours n)):.1f}%')
In a room of
; up to six dice, all made from one motif, rolled at random(import rand rand-int roll-dice)(int n 3 (>= 1) (<= 6))(list[list[point]] pips [[[0 0]] [[-1 1] [1 -1]] [[-1 1] [0 0] [1 -1]] [[-1 1] [1 1] [-1 -1] [1 -1]] [[-1 1] [1 1] [0 0] [-1 -1] [1 -1]] [[-1 1] [1 1] [-1 0] [1 0] [-1 -1] [1 -1]]] (const)) ; what the figure is: a die, declared once and used n times(motif die ((int face 1 (>= 1) (<= 6))) (rect body (width 2) (height 2) (rx 0.3) (fill 'slateblue') (fill-opacity 0.15) (stroke 'slateblue')) (foreach j 6 (circle_ (pos (* 0.55 (or? pips[(- face 1)][j] [0 0]))) (r 0.18) (fill 'slateblue') (opacity (if (< j face) 1 0)))))(scene table (group dice (foreach k n (die #'die-{k}' (face (rand-int 1 6)) (translate [(* 2.4 (- k (/ (- n 1) 2))) 0]))))) ; what it does(canvas v (x -7.4) (y -1.4) (width 14.8) (height 2.8) table)(slider count (move n (from 1) (to 6)) (label 'dice'))(button roll (animation (roll-dice)) (label 'roll'))(readout total $'{(sum (somes dice.face))}')
The sum is
; Cauchy–Schwarz, as two rectangles of height ‖v‖(point u [1.8 0.4]) (point v [0.5 1.4])(real nu (dist [0 0] u)) (real nv (dist [0 0] v))(point e (or? (* (/ 1 nv) v) [0 1])) ; typechecked: v may be 0(real s (+ (* u.x e.x) (* u.y e.y))) (string lit '') (scene plane (foreach k ['dot' 'bound'] (let ((w (* (if (= k 'dot') s (* (sign s) nu)) e)) (h (* nv (as point [e.y (- e.x)])))) (polygon_ (points [[0 0] w (+ w h) h]) (fill (if (= k 'dot') 'slateblue' 'crimson')) (opacity (if (= lit k) 0.45 0.12))))) (line_ (from u) (to (* s e)) (stroke 'gray') (dash [4 4])) (group arrows (defaults (stroke-width 3) (head-end 'dart')) (line_ (from [0 0]) (to u) (stroke 'crimson')) (line_ (from [0 0]) (to v) (stroke 'royalblue'))) (circle hu (r 0.15) (pos u) (opacity 0)) (circle hv (r 0.15) (pos v) (opacity 0)))(formula cs tex'\Big|\underbrace{\term{dot}{\langle u, v\rangle}}_{\term{a}{}}\Big| \term{rel}{} \underbrace{\term{bound}{\|u\|\,\|v\|}}_{\term{b}{}}' (term a (tex $'{(abs (* s nv)):.2f}')) (term b (tex $'{(* nu nv):.2f}')) (term rel (tex (if (< (* nv (- nu (abs s))) 0.005) '=' '\\le'))) (term dot (background (if (= lit 'dot') 'lavender' none))) (term bound (background (if (= lit 'bound') 'lavender' none))))(canvas c (x -1.2) (y -1.5) (width 4) (height 3.8) plane (on-drag hu (move u (to pointer))) (on-drag hv (move v (to pointer))))(display shown cs (foreach k ['dot' 'bound'] (on-enter #'cs.{k}' (set lit k)) (on-leave #'cs.{k}' (set lit ''))))
Theorem 1 (Cauchy–Schwarz). For all vectors u and v in ℝ2,
with equality exactly when u and v lie on one line.
Figure 1. Both sides of (1) as rectangles of height ‖v‖.
Your next paper, talk or course
Drop a Mathterpiece figure into any web-based paper, slide deck, set of notes or page.
A figure in a web-based paper that readers can explore, next to the claim that it shows.
Readers drag u or v, or point at a side of the inequality, and check the claim for themselves.
A figure in web-based slides that you drag during a talk.
Any slide tool that makes web pages can show a Mathterpiece figure, and you drag it to answer a question.
A figure in lecture notes or a digestion, with the text around it.
The numbers in the text come from the figure, so the prose and the picture always agree.
A figure’s animation, recorded for a lecture video or a short.
The page and the video show the same figure, so they never disagree.
Accessible, interactive, portable by default
Every Mathterpiece figure gets these from the runtime, with no extra code.
- KeyboardSliders, buttons and players work from the keyboard, and Tab reaches every control.
- Screen readersEach control and each canvas carries a label written in the source.
- TouchEvery drag also works by tapping, so a figure works on a phone as well as with a mouse.
- Light and darkEach color has a light and a dark value, and the figure follows the reader’s setting.
- Reduced motionAnimations follow the reader’s setting for reduced motion, with no extra code in the figure.
- Any page or frameworkFigures draw as SVG in standard web components, so they work in HTML, React, Vue or Svelte.
Typesafe figures
Mathterpiece figures never break in the reader’s browser: the compiler rejects any figure that could draw something undefined.
-
1Write the figure
(real k -1 (>= -1) (<= 1))(real r (sqrt (- k)))(scene parabola (line_ (from [-2 0]) (to [2 0]) (stroke 'gray')) (parametric p (for t -2 2) [t (+ (* t t) k)] (stroke 'teal')) (circle lo (r 0.1) (pos [(- r) 0]) (fill 'crimson')) (circle hi (r 0.1) (pos [r 0]) (fill 'crimson')))(canvas c parabola)(slider shift (move k (from -1) (to 1)) (label 'k'))(readout says $'roots at ±{r:.2f}')
-
2The compiler rejects it
The error says what to change, so a person or a coding agent can fix the figure.
-
3Say what happens when there are no roots
(real k -1 (>= -1) (<= 1))(real|none r (sqrt (- k)))(scene parabola (line_ (from [-2 0]) (to [2 0]) (stroke 'gray')) (parametric p (for t -2 2) [t (+ (* t t) k)] (stroke 'teal')) (circle|none lo (r 0.1) (pos [(- r) 0]) (fill 'crimson')) (circle|none hi (r 0.1) (pos [r 0]) (fill 'crimson')))(canvas c parabola)(slider shift (move k (from -1) (to 1)) (label 'k'))(readout says (or? $'roots at ±{r:.2f}' 'no real roots'))
-
4Every value of k draws
Move k above 0. The roots disappear, and nothing breaks.
Make your first Mathterpiece
Install mtp, declare a figure in a few lines, and put it on any page.