Leonardo

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

Open the browser REPL

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(variable = "x"), exp = _Number(d = 3.0)),
//   b = Product(a = _Number(d = 2.0), b = _Variable(variable = "x"))
// )
val d    = derive(expr, _Variable("x"))
// d: _Expression = Sum(
//   a = Product(
//     a = _Number(d = 3.0),
//     b = Power(base = _Variable(variable = "x"), exp = _Number(d = 2.0))
//   ),
//   b = _Number(d = 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@67c1a04b
d.eval(env)
// res1: Either[_Expression, _Value] = Right(value = _Number(d = 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 (Simpson’s rule)
Simplification Single-pass structural reduction; fixpoint simplifyFully
Matrices Symbolic _Matrix + dense _MatrixValue; sum, product, transpose, scale, determinant, inverse (det, inv, 1/A)
Equations _Equation relation; solve (linear exact, quadratic, numeric bisection); solveSystem (Gaussian elimination)
Complex _Complex(re, im); full field arithmetic; exp log sin cos tan on complex args; principal roots
Transforms Laplace, Fourier, inverse Laplace, and the one-sided z-transform and its inverse
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; descriptive statistics, regression by QR, elementary inference
Vector calculus grad div curl laplacian jacobian hessian in Cartesian, cylindrical, and spherical coordinates
Control Transfer-function algebra, poles and stability, step/impulse response, Bode/Nyquist, state space, discretisation
ODEs First-order initial-value problems: closed forms where possible, Runge–Kutta otherwise
Exact arithmetic Opt-in rational tier with arbitrary-precision transcendentals and a user-settable working precision
Sampling sample(e, v, lo, hi, n)Vector[(Double, Double)]; compiled Double ⇒ Double fast path
REPL Interactive session with bindings, named functions, session scripts

Pages

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.


This site uses Just the Docs, a documentation theme for Jekyll.