Symbolic Derivative

Calculus rules applied to formulas, not numbers, with every rule shown.

symbolic-derivative.js · output · proof · check · try it in the playground


The question

The derivative of a formula says how fast it changes as x changes. A calculus class teaches a handful of rules for it: the derivative of a sum is the sum of the derivatives, the product rule for u × v, the power rule for xⁿ, the rule for a logarithm, and so on.

Computer algebra systems apply those rules to the formula itself, not to numbers.

What are the derivatives of x × x, x + 3, log(log(x)), (x + 1)(x² + 2)(x³ + 3) and y / (x − 1), and which rule produced each part?


Historical background

Leibniz’s 1684 paper set out rules for differential calculus that could be applied to written expressions. The example takes that rule-based view literally. It matches the outer form of an expression, such as a product or logarithm, then differentiates its parts with the corresponding rules. Leibniz’s paper, Carnegie Mellon University Libraries


What we tell eyemind: the formulas

A formula is a structured term, not text to be parsed:

fact(expr('square', mul(sym('x'), sym('x'))))
fact(expr('linear_plus_const', add(sym('x'), num(3))))
fact(
  expr(
    'product',
    mul(add(sym('x'), num(1)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))),
  ),
)
fact(expr('nested_log', log(log(sym('x')))))

fact(…) states something plainly true: here, that a formula has a name. num(3) is the number 3 and sym('x') the variable x; add, mul, pow and log combine them, so mul(sym('x'), sym('x')) is x × x. Here they are just names of the parts of a formula: nothing is calculated.


What we tell eyemind: the rules of calculus

// A number has derivative 0; X itself 1; any other variable 0.
fact(d(num(_), _, num(0)))
fact(d(sym(X), X, num(1)))
implied_by(d(sym(Y), X, num(0)), not_identical(X, Y))
// (u + v)' = u' + v', and (u - v)' = u' - v'
implied_by(d(add(U, V), X, add(Du, Dv)), [d(U, X, Du), d(V, X, Dv)])
implied_by(d(sub(U, V), X, sub(Du, Dv)), [d(U, X, Du), d(V, X, Dv)])
// (u * v)' = u' * v + u * v'
implied_by(d(mul(U, V), X, add(mul(Du, V), mul(U, Dv))), [d(U, X, Du), d(V, X, Dv)])
// (u^n)' = n * u^(n-1) * u', for a whole number n
implied_by(d(pow(U, N), X, mul(mul(num(N), pow(U, N1)), Du)), [is(N1, sub(N, 1)), d(U, X, Du)])
// (log u)' = u' / u
implied_by(d(log(U), X, divide(Du, U)), d(U, X, Du))

d(U, X, Du) reads “the derivative of U with respect to X is Du”.

Each rule takes a formula apart, differentiates the pieces, and puts the answer together. The program also has a rule for quotients, divide. A forward rule, implies([expr(Name, Formula), d(Formula, 'x', Derivative)], derivative(Name, Derivative)), differentiates every named formula, and two query lines ask for the results and for the derivative of y / (x − 1).


What eyemind concludes

// Answers: each instance of a query() that holds.
derivative('square', add(mul(num(1), sym('x')), mul(sym('x'), num(1))))
derivative('linear_plus_const', add(num(1), num(0)))
derivative('product', add(mul(add(num(1), num(0)), …), …))
derivative('nested_log', divide(divide(num(1), sym('x')), log(sym('x'))))
d(divide(sym('y'), sub(sym('x'), num(1))), 'x', divide(…, pow(sub(sym('x'), num(1)), 2)))

The first line is a comment saying what follows; … marks parts left out here. x × x gives 1·x + x·1, x + 3 gives 1 + 0, and log(log(x)) gives (1/x) / log(x). The product’s derivative is one long line with all three product-rule terms. Nothing is simplified, so every rule application stays visible. The last line differentiates y / (x − 1), where y is a different variable and counts as a constant: the result is (0·(x − 1) − y·(1 − 0)) / (x − 1)².


The proof, line by line

eyemind --proof writes down how it reached each answer. This is the complete proof:

// Answers: each instance of a query() that holds.
derivative('square', add(mul(num(1), sym('x')), mul(sym('x'), num(1))))
derivative('linear_plus_const', add(num(1), num(0)))
derivative('product', add(mul(add(num(1), num(0)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))), mul(add(sym('x'), num(1)), add(mul(add(mul(mul(num(2), pow(sym('x'), 1)), num(1)), num(0)), add(pow(sym('x'), 3), num(3))), mul(add(pow(sym('x'), 2), num(2)), add(mul(mul(num(3), pow(sym('x'), 2)), num(1)), num(0)))))))
derivative('nested_log', divide(divide(num(1), sym('x')), log(sym('x'))))
d(divide(sym('y'), sub(sym('x'), num(1))), 'x', divide(sub(mul(num(0), sub(sym('x'), num(1))), mul(sym('y'), sub(num(1), num(0)))), pow(sub(sym('x'), num(1)), 2)))

// Steps: each derived fact and what it rests on: the clause or control that
// justifies it, the bindings and the goals it uses.
step(derivative('square', add(mul(num(1), sym('x')), mul(sym('x'), num(1)))), clause(14), {Name: 'square', Formula: mul(sym('x'), sym('x')), Derivative: add(mul(num(1), sym('x')), mul(sym('x'), num(1)))}, [expr('square', mul(sym('x'), sym('x'))), d(mul(sym('x'), sym('x')), 'x', add(mul(num(1), sym('x')), mul(sym('x'), num(1))))])
step(derivative('linear_plus_const', add(num(1), num(0))), clause(14), {Name: 'linear_plus_const', Formula: add(sym('x'), num(3)), Derivative: add(num(1), num(0))}, [expr('linear_plus_const', add(sym('x'), num(3))), d(add(sym('x'), num(3)), 'x', add(num(1), num(0)))])
step(derivative('product', add(mul(add(num(1), num(0)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))), mul(add(sym('x'), num(1)), add(mul(add(mul(mul(num(2), pow(sym('x'), 1)), num(1)), num(0)), add(pow(sym('x'), 3), num(3))), mul(add(pow(sym('x'), 2), num(2)), add(mul(mul(num(3), pow(sym('x'), 2)), num(1)), num(0))))))), clause(14), {Name: 'product', Formula: mul(add(sym('x'), num(1)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))), Derivative: add(mul(add(num(1), num(0)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))), mul(add(sym('x'), num(1)), add(mul(add(mul(mul(num(2), pow(sym('x'), 1)), num(1)), num(0)), add(pow(sym('x'), 3), num(3))), mul(add(pow(sym('x'), 2), num(2)), add(mul(mul(num(3), pow(sym('x'), 2)), num(1)), num(0))))))}, [expr('product', mul(add(sym('x'), num(1)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3))))), d(mul(add(sym('x'), num(1)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))), 'x', add(mul(add(num(1), num(0)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))), mul(add(sym('x'), num(1)), add(mul(add(mul(mul(num(2), pow(sym('x'), 1)), num(1)), num(0)), add(pow(sym('x'), 3), num(3))), mul(add(pow(sym('x'), 2), num(2)), add(mul(mul(num(3), pow(sym('x'), 2)), num(1)), num(0)))))))])
step(derivative('nested_log', divide(divide(num(1), sym('x')), log(sym('x')))), clause(14), {Name: 'nested_log', Formula: log(log(sym('x'))), Derivative: divide(divide(num(1), sym('x')), log(sym('x')))}, [expr('nested_log', log(log(sym('x')))), d(log(log(sym('x'))), 'x', divide(divide(num(1), sym('x')), log(sym('x'))))])

// Certificate: the derived facts and every answer of each call the questions make.
derived(derivative(A, B), [derivative('square', add(mul(num(1), sym('x')), mul(sym('x'), num(1)))), derivative('linear_plus_const', add(num(1), num(0))), derivative('product', add(mul(add(num(1), num(0)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))), mul(add(sym('x'), num(1)), add(mul(add(mul(mul(num(2), pow(sym('x'), 1)), num(1)), num(0)), add(pow(sym('x'), 3), num(3))), mul(add(pow(sym('x'), 2), num(2)), add(mul(mul(num(3), pow(sym('x'), 2)), num(1)), num(0))))))), derivative('nested_log', divide(divide(num(1), sym('x')), log(sym('x'))))])
called(derivative(A, B), [derivative('square', add(mul(num(1), sym('x')), mul(sym('x'), num(1)))), derivative('linear_plus_const', add(num(1), num(0))), derivative('product', add(mul(add(num(1), num(0)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))), mul(add(sym('x'), num(1)), add(mul(add(mul(mul(num(2), pow(sym('x'), 1)), num(1)), num(0)), add(pow(sym('x'), 3), num(3))), mul(add(pow(sym('x'), 2), num(2)), add(mul(mul(num(3), pow(sym('x'), 2)), num(1)), num(0))))))), derivative('nested_log', divide(divide(num(1), sym('x')), log(sym('x'))))])
called(d(mul(sym('x'), sym('x')), 'x', C), [d(mul(sym('x'), sym('x')), 'x', add(mul(num(1), sym('x')), mul(sym('x'), num(1))))])
called(d(sym('x'), 'x', D), [d(sym('x'), 'x', num(1))])
called(d(add(sym('x'), num(3)), 'x', C), [d(add(sym('x'), num(3)), 'x', add(num(1), num(0)))])
called(d(num(3), 'x', E), [d(num(3), 'x', num(0))])
called(d(mul(add(sym('x'), num(1)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))), 'x', C), [d(mul(add(sym('x'), num(1)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))), 'x', add(mul(add(num(1), num(0)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))), mul(add(sym('x'), num(1)), add(mul(add(mul(mul(num(2), pow(sym('x'), 1)), num(1)), num(0)), add(pow(sym('x'), 3), num(3))), mul(add(pow(sym('x'), 2), num(2)), add(mul(mul(num(3), pow(sym('x'), 2)), num(1)), num(0)))))))])
called(d(add(sym('x'), num(1)), 'x', F), [d(add(sym('x'), num(1)), 'x', add(num(1), num(0)))])
called(d(num(1), 'x', G), [d(num(1), 'x', num(0))])
called(d(mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3))), 'x', H), [d(mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3))), 'x', add(mul(add(mul(mul(num(2), pow(sym('x'), 1)), num(1)), num(0)), add(pow(sym('x'), 3), num(3))), mul(add(pow(sym('x'), 2), num(2)), add(mul(mul(num(3), pow(sym('x'), 2)), num(1)), num(0)))))])
called(d(add(pow(sym('x'), 2), num(2)), 'x', I), [d(add(pow(sym('x'), 2), num(2)), 'x', add(mul(mul(num(2), pow(sym('x'), 1)), num(1)), num(0)))])
called(d(pow(sym('x'), 2), 'x', J), [d(pow(sym('x'), 2), 'x', mul(mul(num(2), pow(sym('x'), 1)), num(1)))])
called(d(num(2), 'x', K), [d(num(2), 'x', num(0))])
called(d(add(pow(sym('x'), 3), num(3)), 'x', L), [d(add(pow(sym('x'), 3), num(3)), 'x', add(mul(mul(num(3), pow(sym('x'), 2)), num(1)), num(0)))])
called(d(pow(sym('x'), 3), 'x', M), [d(pow(sym('x'), 3), 'x', mul(mul(num(3), pow(sym('x'), 2)), num(1)))])
called(d(log(log(sym('x'))), 'x', C), [d(log(log(sym('x'))), 'x', divide(divide(num(1), sym('x')), log(sym('x'))))])
called(d(log(sym('x')), 'x', N), [d(log(sym('x')), 'x', divide(num(1), sym('x')))])
called(d(divide(sym('y'), sub(sym('x'), num(1))), 'x', O), [d(divide(sym('y'), sub(sym('x'), num(1))), 'x', divide(sub(mul(num(0), sub(sym('x'), num(1))), mul(sym('y'), sub(num(1), num(0)))), pow(sub(sym('x'), num(1)), 2)))])
called(d(sym('y'), 'x', P), [d(sym('y'), 'x', num(0))])
called(d(sub(sym('x'), num(1)), 'x', Q), [d(sub(sym('x'), num(1)), 'x', sub(num(1), num(0)))])

It has three parts:

Read the lines as sentences:

  1. The derivative of square is 1·x + x·1 — by rule 14, because square is x × x and the derivative of x × x is 1·x + x·1. The other three steps say the same for linear_plus_const, product and nested_log.
  2. The derivative of x × x has exactly one answer, 1·x + x·1 — the table for d(mul(sym('x'), sym('x')), 'x', C). The product rule (rule 10) derives it from the table for d(sym('x'), 'x', D): the derivative of x is 1.
  3. The tables below it break each formula down the same way, one rule at a time: the sum rule for x + 3 and x + 1, the product rule again for (x² + 2)(x³ + 3), the power rule for x² and x³, the logarithm rule twice for log(log(x)), and fact 5 for every number.
  4. The last three tables answer the second question, the quotient rule on y / (x − 1): the derivative of y with respect to x is 0, because x and y are different names, and the derivative of x − 1 is 1 − 0.

A piece needed twice, such as “the derivative of x is 1”, has one table, consulted wherever it is needed.


Checked: what the report says

A proof is only worth something if someone else can check it. eyemind -c generates the proof and checks it against the program; --check-proof checks a saved proof. The checker never reasons by itself: it reads the clauses from the program and confirms the proof against them in one procedure:

This is the complete report:

// Checked answers: these follow from the input program and are every answer of its questions.
derivative('square', add(mul(num(1), sym('x')), mul(sym('x'), num(1))))
derivative('linear_plus_const', add(num(1), num(0)))
derivative('product', add(mul(add(num(1), num(0)), mul(add(pow(sym('x'), 2), num(2)), add(pow(sym('x'), 3), num(3)))), mul(add(sym('x'), num(1)), add(mul(add(mul(mul(num(2), pow(sym('x'), 1)), num(1)), num(0)), add(pow(sym('x'), 3), num(3))), mul(add(pow(sym('x'), 2), num(2)), add(mul(mul(num(3), pow(sym('x'), 2)), num(1)), num(0)))))))
derivative('nested_log', divide(divide(num(1), sym('x')), log(sym('x'))))
d(divide(sym('y'), sub(sym('x'), num(1))), 'x', divide(sub(mul(num(0), sub(sym('x'), num(1))), mul(sym('y'), sub(num(1), num(0)))), pow(sub(sym('x'), num(1)), 2)))

verdict('checked')

The verdict is checked: the five derivatives follow from the program’s rules of calculus, and they are every answer of the original questions. It does not judge whether the calculus rules themselves are written correctly.


Try it

eyemind examples/symbolic-derivative.js
eyemind --proof examples/symbolic-derivative.js
eyemind --goal "d(mul(num(5), sym('x')), 'x', D)" examples/symbolic-derivative.js

The --goal line asks for the derivative of 5x and gets add(mul(num(0), sym('x')), mul(num(5), num(1))), that is 0·x + 5·1. Or open it in the playground. Change the square formula to pow(sym('x'), 2) and run again: the power rule answers mul(mul(num(2), pow(sym('x'), 1)), num(1)), that is 2·x¹·1.


Takeaway

Rules of calculus are rules about the shape of a formula, so a few lines of logic apply them. Leaving the results unsimplified keeps the reasoning in plain sight, and the proof names the rule behind every part.