Get a "step-by-step" evaluation in Mathematica

For differentiation at least, old versions of Mathematica had a demonstration function called WalkD[] that holds your hand and shows what is done at each stage up until the final answer.

In general, however...

You should realize at the outset that while knowing about the internals of Mathematica may be of intellectual interest, it is usually much less important in practice than you might at first suppose.

Indeed, one of the main points of Mathematica is that it provides an environment where you can perform mathematical and other operations without having to think in detail about how these operations are actually carried out inside your computer.

...

Particularly in more advanced applications of Mathematica, it may sometimes seem worthwhile to try to analyze internal algorithms in order to predict which way of doing a given computation will be the most efficient. And there are indeed occasionally major improvements that you will be able to make in specific computations as a result of such analyses.

But most often the analyses will not be worthwhile. For the internals of Mathematica are quite complicated, and even given a basic description of the algorithm used for a particular purpose, it is usually extremely difficult to reach a reliable conclusion about how the detailed implementation of this algorithm will actually behave in particular circumstances.

A typical problem is that Mathematica has many internal optimizations, and the efficiency of a computation can be greatly affected by whether the details of the computation do or do not allow a given internal optimization to be used.

Put another way: how Mathematica does things doesn't necessarily correspond to "manual" methods.


Here's my modest attempt to (somewhat) modernize WalkD[]:

Format[d[f_, x_], TraditionalForm] := DisplayForm[RowBox[{FractionBox["\[DifferentialD]",
                                                  RowBox[{"\[DifferentialD]", x}]], f}]];

SpecificRules = {d[(f_)[u___, x_, v___], x_] /;
                 FreeQ[{u}, x] && FreeQ[{v}, x] :> D[f[u, x, v], x],
                 d[(a_)^(x_), x_] :> D[a^x, x] /; FreeQ[a, x]};

ConstantRule = d[c_, x_] :> 0 /; FreeQ[c, x];

LinearityRule = {d[f_ + g_, x_] :> d[f, x] + d[g, x],
                 d[c_ f_, x_] :> c d[f, x] /; FreeQ[c, x]};

PowerRule = {d[x_, x_] :> 1, d[(x_)^(a_), x_] :> a*x^(a - 1) /; FreeQ[a, x]};

ProductRule = d[f_ g_, x_] :> d[f, x] g + f d[g, x];

QuotientRule = d[(f_)/(g_), x_] :> (d[f, x]*g - f*d[g, x])/g^2;

InverseFunctionRule = d[InverseFunction[f_][x_], x_] :>
                      1/f'[InverseFunction[f][x]];

ChainRule = {d[(f_)^(a_), x_] :> a*f^(a - 1)*d[f, x] /; FreeQ[a, x],
             d[(a_)^(f_), x_] :> Log[a]*a^f*d[f, x] /; FreeQ[a, x],
             d[(f_)[g__], x_] /; ! FreeQ[{g}, x] :>
             (Derivative[##][f][g] & @@@ IdentityMatrix[Length[{g}]]).(d[#, x] & /@ {g}),
             d[(f_)^(g_), x_] :> f^g*d[g*Log[f], x]};

$RuleNames = {"Specific Rules", "Constant Rule", "Linearity Rule", "Power Rule",
              "Product Rule", "Quotient Rule", "Inverse Function Rule", "Chain Rule"};

displayStart[expr_] := CellPrint[
  Cell[BoxData[MakeBoxes[HoldForm[expr], TraditionalForm]], "Output", 
   Evaluatable -> False, CellMargins -> {{Inherited, Inherited}, {10, 10}}, 
   CellFrame -> False, CellEditDuplicate -> False]]

displayDerivative[expr_, k_Integer] := CellPrint[
  Cell[BoxData[TooltipBox[RowBox[{InterpretationBox["=", Sequence[]], "  ", 
       MakeBoxes[HoldForm[expr], TraditionalForm]}], $RuleNames[[k]], 
     LabelStyle -> "TextStyling"]], "Output", Evaluatable -> False, 
   CellMargins -> {{Inherited, Inherited}, {10, 10}}, 
   CellFrame -> False, CellEditDuplicate -> False]]

WalkD[f_, x_] := Module[{derivative, oldderivative, k}, 
        derivative = d[f, x]; displayStart[derivative];
        While[! FreeQ[derivative, d],
            oldderivative = derivative; k = 0;
            While[oldderivative == derivative,
                      k++;
                      derivative = derivative /. 
                              ToExpression[StringReplace[$RuleNames[[k]], " " -> ""]]];
            displayDerivative[derivative, k]];
        D[f, x]]

I've tried to make the formatting of the derivative look a bit more traditional, as well as having the differentiation rule used be a tooltip instead of an explicitly generated cell (thus combining the best features of WalkD[] and RunD[]); you'll only see the name of the differentiation rule used if you mouseover the corresponding expression.

WalkD[] demonstration


Use:

http://www.wolframalpha.com/input/?i=D[Cos[x]*Exp[x]%2C+x]

and select "Show steps". More in Mathematica would be

WolframAlpha["D[Cos[x]*Exp[x], x]"]

or even

WolframAlpha["D[Cos[x]*Exp[x], x]", IncludePods -> "Input", 
 AppearanceElements -> {"Pods"}, PodStates -> {"Input__Show steps"}]

here is a function based on WolframAlpha[]

ShowSteps[exp_] := 
  WolframAlpha[ ToString@HoldForm@InputForm@exp, 
  {{"Input", 2}, "Content"},  PodStates -> {"Input__Show steps"}]

SetAttributes[ShowSteps, HoldAllComplete]

for limits use

PodStates -> {"Limit__Show steps"}

for integration

PodStates -> {"IndefiniteIntegral__Show steps"} 

Update:

WolframAlpha changed output.

Now ShowSteps should work with:

ShowSteps[exp_] := 
  WolframAlpha[ToString@HoldForm@InputForm@exp, 
  {{"Input", 1}, "Content"}, 
   PodStates -> {"Input__Step-by-step solution","Input__Show all steps"}]

 SetAttributes[ShowSteps, HoldAll]

enter image description here

Update - Nov 2014

ShowSteps[exp_] := 
 WolframAlpha[ToString@HoldForm@InputForm@exp, 
 {{"Input", 2}, "Content"}, 
 PodStates -> {"Input__Step-by-step solution"}]

SetAttributes[ShowSteps, HoldAll]