← All Base Form Math projects
BM-17NOT STARTEDBASE FORM · CALCULUSMATHPYTHON

Derivative calculator

A calculator that gives the exact derivative of any formula you type, built on your own forward- and reverse-mode autodiff.

EST.7 h
REWARD+100 XP
AFTERMax Slater

ML-ARENA BENCHMARKS

YOUR NUMBER VS THE INDUSTRY'S
METRICINDUSTRYMINE
derivatives on 100 formulas
GOAL · match within 1e-6
SymPy diff · finite differences—
no results yet
YOUR RESULTS

Your recorded results appear here.

Brief

There are three ways to get a derivative out of a computer. Symbolic algebra, which SymPy does, rewrites the formula, and the result can grow huge. Finite differences are easy, but rounding costs you a third to a half of your digits. Automatic differentiation is the third way, and every deep learning library runs on it.

The idea: any formula is built from a few elementary operations, and each has a known derivative. Apply the chain rule at every step and you get the exact derivative value, up to floating-point rounding. Forward mode carries a derivative along with each value. A neat way to do that is with dual numbers a+bεa + b\varepsilon where ε2=0\varepsilon^2 = 0, because then

f(x+ε)=f(x)+f′(x) εf(x + \varepsilon) = f(x) + f'(x)\,\varepsilon

Reverse mode goes the other way. First run the formula and record every operation. Then start at the output with fˉ=1\bar f = 1 and walk backwards. Each node passes its adjoint to its inputs, multiplied by the local derivative. When a value feeds several nodes, the contributions add up: vˉ=∑wwˉ ∂w/∂v\bar v = \sum_w \bar w \, \partial w / \partial v, summed over every node ww that uses vv. One backward pass gives the derivative with respect to every input at once. That's backpropagation, and it's why training a model with millions of weights is affordable.

VISUALFig. 1
x = 2 value 2 y = 3 value 3 b = sin x value 0.909 a = x·y value 6 f = a + b value 6.909 x̄ = ā·y + b̄·cos x = 2.584 ȳ = ā·x = 2 ā = 1 b̄ = 1 f̄ = 1 FORWARD: values, left to right BACKWARD: adjoints, right to left An adjoint is ∂f/∂(that node). x feeds two nodes, so its adjoint adds both paths. Check: ∂f/∂x = y + cos x = 3 + cos 2 ≈ 2.584
f = x·y + sin x at x = 2, y = 3. Values flow forward; adjoints flow backward, and x collects from two paths. Why it's here: Reverse mode is easiest to get right once you have traced one small graph by hand.
HINT · OBJECTIVE 3
Stuck? Hints stay hidden until you ask.

DATA · formulas you write

Objectives

0 / 6 CLEARED

Read-only. Log in to update progress.

VICTORY CONDITIONS

  1. On 100 formulas, your forward- and reverse-mode derivatives both match SymPy within 1e-6 relative error at random points. Record the worst error with `python tools/mlarena.py record BM-17 "<worst rel error>"`.
  2. Reverse mode returns a 100-input gradient in one backward pass, and it agrees with forward mode.
  3. The README lists finite-difference results next to yours and notes where they lose digits.

Side quests

Print the derivative as a formula too: build the derivative's expression tree and simplify trivial terms like x·1 and 0 + y.
Get second derivatives by nesting dual numbers, or by running forward mode over your reverse mode.
Compare speed and results with jax.grad or torch.autograd on the same formulas.

Terms in plain words

Automatic differentiation (autodiff)
Computing exact derivative values by applying the chain rule to every elementary operation as the formula runs. No symbolic algebra and no finite-difference step.
Dual number
A number a + bε with ε² = 0. Evaluating f at x + ε gives f(x) + f′(x)ε, so the derivative rides along with the value.
Forward mode
Pushes derivatives from the inputs toward the output. One pass per input variable.
Reverse mode
Records the computation, then pushes adjoints from the output back to the inputs. One backward pass gives the whole gradient. This is backpropagation.
Adjoint
For an intermediate value v, the derivative of the final output with respect to v, written v̄.