Leonardo is a Scala 3 symbolic mathematics library and Computer Algebra System (CAS). It parses mathematical expressions into an immutable AST and evaluates them either numerically — when all variables are bound — or symbolically, returning a simplified expression tree.
Named after Leonardo Pisano, known as Fibonacci, the author of the Liber Abaci.
Try it in your browser
The whole system compiled to JavaScript — no install, no sign-up, no server. Differentiate, integrate, solve, invert a matrix, take a Laplace transform, or plot a function. Sessions are saved in your own browser and a link carries its session in the URL fragment, so nothing you type ever leaves the tab.
Quick start
import it.grypho.scala.leonardo.core.*
import it.grypho.scala.leonardo.scalar.*
import it.grypho.scala.leonardo.parser.Parser
Parse an expression and differentiate it symbolically:
val expr = Parser.parse("x^3 + 2*x").get
// expr: _Expression = Sum(
// a = Power(base = _Variable("x"), exp = _Number(3.0)),
// b = Product(a = _Number(2.0), b = _Variable("x"))
// )
val d = derive(expr, _Variable("x"))
// d: _Expression = Sum(
// a = Product(
// a = _Number(3.0),
// b = Power(base = _Variable("x"), exp = _Number(2.0))
// ),
// b = _Number(2.0)
// )
simplify(d).toString
// res0: String = "((3.0 * (x ^ 2.0)) + 2.0)"
Evaluate it numerically at x = 2:
val env = new Environment(5, Map("x" -> _Number(2.0)))
// env: Environment = it.grypho.scala.leonardo.core.Environment@36ce4c25
d.eval(env)
// res1: Either[_Expression, _Value] = Right(_Number(14.0))
Features
| Domain | Capability |
|---|---|
| Parsing | Recursive descent; implicit multiplication, multi-character names, right-associative ^ |
| Algebra | + - * / ^; sin cos tan asin acos atan exp log; pi e i |
| Calculus | Symbolic differentiation, indefinite integration (rule table), definite integration (antiderivative or certified adaptive quadrature) |
| Simplification | Single-pass structural reduction; fixpoint simplifyFully |
| Matrices | Symbolic _Matrix + dense _MatrixValue; arithmetic, det, inv, the decompositions (lu, qr, eig, jordan, chol, svd), block matrices, norm, dot, cross, trace, rank, pinv |
| Equations | _Equation relation; solve (linear exact, quadratic, numeric bisection, inequalities, matrix unknowns); solveSystem, with more equations than unknowns solved when consistent |
| Complex | _Complex(re, im); full field arithmetic; exp log sin cos tan on complex args; principal roots |
| Transforms | Laplace, the two-sided and causal Fourier transforms, inverse Laplace, the one-sided z-transform and its inverse, dft/idft/conv |
| Series | Taylor and Maclaurin, numeric Fourier series, Padé approximants, Laurent series about a pole |
| Logic | Boolean, three-valued (Kleene), symmetric ternary, and fuzzy — one shared rule table |
| Probability & statistics | Distributions as first-class values; expect/variance by linearity; Bayes’ theorem and conjugate posteriors; seeded sampling and PRBS; descriptive and order statistics, regression by QR (ridge and weighted too), recursive least squares, t, proportion, variance and independence tests, ANOVA |
| Optimization | Stationary points solved and classified; convexity; Lagrange and KKT conditions; certified numeric minimize, qp, lsqfit, nlp and nsolve; exact lp |
| Vector calculus | grad div curl laplacian in Cartesian, cylindrical, and spherical coordinates; jacobian hessian in Cartesian |
| Control | Transfer-function algebra, poles and stability, the Routh table, step/impulse response, Bode/Nyquist, the root locus, state space, controllability and pole placement, discretisation, linearisation, ARX identification, MPC prediction matrices, LQR and the Kalman filter |
| ODEs | Initial-value problems: closed forms where possible, linear systems by the matrix exponential, certified Runge–Kutta for nonlinear systems |
| Exact arithmetic | Opt-in rational tier, square roots kept as exact surds (√13), arbitrary-precision transcendentals and a user-settable working precision; rationalize brings a decimal back |
| Intervals | [lo, hi] values with rigorous, outward-rounded bounds through arithmetic and the elementary functions; a comparison of overlapping intervals answers unknown |
| Sampling | sample(e, v, lo, hi, n) → Vector[(Double, Double)]; compiled Double ⇒ Double fast path |
| REPL | Interactive session with bindings, named functions, session scripts |
| MCP server | The REPL as a tool an AI assistant calls over the Model Context Protocol (leonardo-mcp) |
New here? Start with a lesson
Twenty-one short lessons, each ending in an exercise, and every example on them is executed when this site is built. Two branches, by what you are holding:
- Lessons: at the prompt — the REPL, in a terminal or in your browser. Nothing to install; R1 opens in the browser REPL.
- Lessons: in your program — Leonardo as a library in your own Scala. Starts at L1 with a dependency line.
The pages below are the reference: they answer “what does this do?”, where a lesson asks “how do I get started?”.
Pages
- Getting Started — add to your project, first expressions
- Features — the complete feature reference
- Expressions & Evaluation — the AST and dual eval model
- Calculus — differentiation, integration, sampling
- Matrices — the matrix domain
- Equations — relations, solver, complex numbers
- Logic — boolean, three-valued, symmetric ternary, and fuzzy logic
- Sequences — Fibonacci and friends, and the generic tabulator
- Control Systems — transfer functions, stability, response, discretisation
- Optimization — stationary points, convexity, constrained and numeric optimisation
- Interactive REPL — session commands and scripts
- Browser REPL — the same REPL, in your browser
- Cheat sheet — every REPL command on one page
- MCP server — Leonardo as a tool for an AI assistant
- Architecture — package diagram and design decisions
- Developer Guide — onboarding reference for contributors
- Lessons: at the prompt · Lessons: in your program — the guided path through both of the above
Licence
Leonardo is licensed under the Apache License, Version 2.0 — Copyright 2023-2026 Cosimo Attanasi. Use, modification and redistribution are permitted, including in closed-source and commercial work, provided the licence and copyright notices are kept and changes are stated.