@ratmath/graphics

Plots that are fast, then certified

A JavaScript plotting library for exact rational mathematics. Every picture is drawn twice: a floating-point sketch at once, then a certified version you can trust pixel by pixel.

Move a slider and the sketch follows your hand. When you stop, every pixel column is recomputed with interval arithmetic over the exact rational inputs. The shaded cells are then a guarantee: every point of the graph in view lies in a shaded cell, and an unshaded cell holds no point of the graph. Zeros, extrema and inflection points arrive as exact rationals or proven brackets, drawn as dots.

What it draws

  • Graphs y=f(x)y = f(x) with certified zeros, poles, holes, extrema, inflection points and domain ends. See Graphs and features.
  • Implicit curves F(x,y)=0F(x, y) = 0, with each pixel either excluded, possible, or proven to hold part of the curve. See Implicit curves.
  • Exact geometry: points, lines, segments, rays, polygons and circles, with intersections in exact form such as 118+3158\tfrac{11}{8} + \tfrac{3\sqrt{15}}{8}. See Exact geometry.
  • Regions from inequalities joined by and, or, not. Regions bounded by lines are exact polygons with exact corners. Others are certified pixel by pixel. See Regions and inequalities.
  • Pictures for the Shelf: charts for statistics and finance, graph drawing with Tutte's exact layout, proof trees, truth trees and Venn diagrams, cobwebs and escape-time pictures, phase portraits with proved equilibria, curvature, numerical methods, polygons and solids. Each is a figure that says whether it is exact, certified, estimated or simulated. See Figures for the Shelf.
  • Labels with mathematical notation, typeset with KaTeX, on canvas and in SVG. See Drawing and labels.

Computing is separate from drawing

The library computes plain data: rational coordinates, exact points, pixel cells with a guarantee. Small renderers turn that data into pixels, on a 2D canvas for interactive use or as an SVG string for export, printing and tests. Nothing about the mathematics depends on the renderer, so a calculator app can draw the same results natively.

import { graph, region, intersect, line, circle, viewport, drawScene, sceneSVG } from "@ratmath/graphics";

const vp = viewport({ x: [-4, 4], y: [-3, 3], width: 480, height: 360 });

const g = graph("x^3/4 - x", {}, vp);
g.quick.value;              // float polyline, immediate
await g.settle();           // certified cells, computed in time slices

intersect(line("y = x"), circle([0, 0], 2));   // [(√2, √2), (-√2, -√2)], exact
region("x + y <= 2, x >= 0, y > 0", {}, vp).exact().corners;  // exact corners, included or not

drawScene(ctx, vp, [g.exact(), circle([0, 0], 2)], { axes: true });   // canvas
sceneSVG(vp, [g.exact(), circle([0, 0], 2)], { axes: true });         // SVG string

Topics

Try itEdit the code and run it with @ratmath/graphics and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math, and SHOW(svg) draws a picture.
Ctrl/⌘ + Enter runs