math::calculus::symdiff

The math::calculus::symdiff package in Tcllib performs symbolic differentiation - that is, it accepts a Tcl expression and the name of a Tcl variable, and returns the derivative of the given expression with respect to the variable.

It's a particular kind of symbolic manipulation that's useful for a fairly wide range of mathematical applications.

Note that it's fairly stupid in that it doesn't attempt to perform any algebraic simplification (beyond elimination of multiplication by zero and one, and addition and subtraction of zero). It therefore often generates results that contain many common subexpressions and are inefficient to evaluate.

Examples:

% package require math::calculus::symdiff
1.0
% namespace import math::calculus::symdiff::symdiff
% symdiff {sin(\$a + \$b)} a
cos((\$a + \$b))
% symdiff {sin(\$a/\$b)} a
((1.0 / \$b) * cos((\$a / \$b)))
% symdiff {\$A*\$x**3 + \$B*\$x**2 + \$C*\$x + \$D} x
((((3 * pow(\$x, 2)) * \$A) + ((2 * \$x) * \$B)) + \$C)