# Get a “step by step” evaluation in Mathematica

Is it possible in Mathematica to get a step-by-step evaluation of some functions; that's to say, outputting not only the result but all the stages that have led to it?

Example : Let's say I want to know the steps to get the derivative of $\cos x\times\exp x$; it should first tell me that it's equal to $\frac{d}{dt}(\exp x)\times\cos x+\exp x \times \frac{d}{dt}(\cos x)$ and then render the result to say $\exp{x}\times(\cos x-\sin x)$.

-
This comes up regularly on MathGroup and the answer is usually somewhat negative (though some commercial packages exist). I think (not sure) version 5 had a (very limited) package for this though. –  Szabolcs Jan 18 '12 at 12:12
In this instance, could do WolframAlpha["derivative of exp(x)*cos(x)"] then hit "Show steps" at upper right. –  Daniel Lichtblau Jan 18 '12 at 16:36

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[x_, x_] :> 1, d[(f_)[x_], x_] :> D[f[x], 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/Derivative[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_] :> (D[f[x], x] /. x -> g)*d[g, x],
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. - I hoped it was possible but the answer seems no. I'm accepting the answer in a few days if nobody finds another solution. Thank you ! – Skydreamer Jan 18 '12 at 12:17 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"}]  - I know this but my question is about Mathematica not Wolfram Alpha. I'm looking for something to see the steps of functions in Mathematica using either a feature or another function. – Skydreamer Jan 18 '12 at 12:06 Note that within the current version of Mathematica, you can directly query Wolfram|Alpha (putting an "=" at the beginning of the Input cell, and then clicking the "Show Steps" button on the result returned). But I don't think we have any way of knowing whether the steps shown in the Wolfram|Alpha output are what goes on when you directly let the Mathematica kernel evaluate the derivative. – murray Jan 18 '12 at 16:21 @murray, most likely the steps shown is not what is going on in Mathematica. – user21 Jan 18 '12 at 16:34 To @Skydreamer: yes, I said so in my comment! And what WolframAlpha shows is likely to be even more different from how Mathematica normally does it in the case of symbolic integration. – murray Jan 18 '12 at 22:50 To see the steps for taking indefinite integrals one can use free rule-based integrator nicknamed Rubi crafted by Albert D. Rich: Click on the sample integration problem at the end of the notebook and press Shift-Enter to evaluate it. After a minute or so depending on the speed of your computer, the first step of the integration should be displayed. To see successive steps, click on the intermediate results and press Shift-Enter. In many cases this integrator produces terser output than the built-in. - Oh my. What have we here... +1 – Mr.Wizard Sep 24 '13 at 9:30 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]  - I have improved J. M.'s version of walkD by adding error handling. I have also added walkInt that works like walkD except for integration. Code: Format[d[f_, x_], TraditionalForm] := Module[{paren, boxes}, paren = MatchQ[f,Plus[_,__]]; boxes = RowBox[{f}]; If[paren, boxes = RowBox[{"(", boxes, ")"}] ]; boxes = RowBox[{FractionBox["\[DifferentialD]", RowBox[{"\[DifferentialD]", x}]], boxes}]; DisplayForm[boxes] ]; dSpecificRules = {d[x_, x_] :> 1, d[(f_)[x_], x_] :> D[f[x], x], d[(a_)^(x_), x_] :> D[a^x, x] /; FreeQ[a, x]}; dConstantRule = d[c_, x_] :> 0 /; FreeQ[c, x]; dLinearityRule = {d[f_ + g_, x_] :> d[f, x] + d[g, x], d[c_ f_, x_] :> c d[f, x] /; FreeQ[c, x]}; dPowerRule = {d[x_, x_] :> 1, d[(x_)^(a_), x_] :> a*x^(a - 1) /; FreeQ[a, x]}; dProductRule = d[f_ g_, x_] :> d[f, x] g + f d[g, x]; dQuotientRule = d[(f_)/(g_), x_] :> (d[f, x]*g - f*d[g, x])/g^2; dInverseFunctionRule := d[InverseFunction[f_][x_], x_] :> 1/Derivative[1][f][InverseFunction[f][x]]; dChainRule = {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_] :> (D[f[x], x] /. x -> g)*d[g, x], d[(f_)^(g_), x_] :> f^g*d[g*Log[f], x]};$dRuleNames = {"Specific Rules", "Constant Rule", "Linearity Rule", "Power Rule",
"Product Rule", "Quotient Rule", "Inverse Function Rule", "Chain Rule"};

displayStart[expr_] := CellPrint[
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]}], "Differentation: " <> $dRuleNames[[k]], LabelStyle -> "TextStyling"]], "Output", Evaluatable -> False, CellMargins -> {{Inherited, Inherited}, {10, 10}}, CellFrame -> False, CellEditDuplicate -> False]]; walkD::differentationError = "Failed to differentiate expression!"; 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++; If[k > Length@$dRuleNames,
Message[walkD::differentationError];
Return[D[f, x]];
];
derivative = derivative /. ToExpression["d" <> StringReplace[$dRuleNames[[k]], " " -> ""]] ]; displayDerivative[derivative, k]]; D[f, x] ]; Format[int[f_,x_],TraditionalForm]:= ( paren = MatchQ[f,Plus[_,__]]; boxes = RowBox[{f}]; If[paren, boxes = RowBox[{"(", boxes, ")"}] ]; boxes = RowBox[{boxes, "\[DifferentialD]", x}]; boxes = RowBox[{"\[Integral]", boxes}]; DisplayForm[boxes] ); intSpecificRules = {int[(f_)[x_], x_] :> Integrate[f[x], x], int[(a_)^(x_), x_] :> Integrate[a^x, x] /; FreeQ[a, x]}; intConstantRule = int[c_, x_] :> c*x /; FreeQ[c, x]; intLinearityRule = {int[f_ + g_, x_] :> int[f, x] + int[g, x], int[c_ f_, x_] :> c int[f, x] /; FreeQ[c, x]}; intPowerRule = {int[x_, x_] :> x^2 / 2, int[1/x_, x_] :> Log[x], int[(x_)^(a_), x_] :> x^(a + 1)/(a + 1) /; FreeQ[a, x]}; intSubstitutionRule = { int[(f_)^(a_), x_] :> ((Integrate[u^a, u] / d[f, x]) /. u -> f) /; FreeQ[a, x] && FreeQ[D[f, x], x], int[(f_)^(a_) g_, x_] :> ((Integrate[u^a, u] / d[f, x]) * g /. u -> f) /; FreeQ[a, x] && FreeQ[FullSimplify[D[f, x] / g], x], int[(a_)^(f_), x_] :> (a ^ f)/(d[f, x] * Log[a]) /; FreeQ[a, x] && FreeQ[D[f, x], x], int[(a_)^(f_) g_, x_] :> (a ^ f)/(d[f, x] * Log[a]) * g /; FreeQ[a, x] && FreeQ[FullSimplify[D[f, x] / g], x], int[(f_)[g_], x_] :> (Integrate[f[u], u] /. u -> g) / d[g, x] /; FreeQ[D[g, x], x], int[(f_)[g_] h_, x_] :> (Integrate[f[u], u] /. u -> g) / d[g, x] * h /; FreeQ[FullSimplify[D[g, x] / h], x] }; intProductRule = int[f_ g_, x_] :> int[f, x] g - int[int[f, x] * d[g, x], x];$intRuleNames = {"Specific Rules", "Constant Rule", "Linearity Rule", "Power Rule", "Substitution Rule", "Product Rule"};

displayIntegral[expr_, k_Integer] := CellPrint[
Cell[BoxData[TooltipBox[RowBox[{InterpretationBox["=", Sequence[]], "  ",
MakeBoxes[HoldForm[expr], TraditionalForm]}], "Integration: " <> $intRuleNames[[k]], LabelStyle -> "TextStyling"]], "Output", Evaluatable -> False, CellMargins -> {{Inherited, Inherited}, {10, 10}}, CellFrame -> False, CellEditDuplicate -> False]]; walkInt::integrationError = "Failed to integrate expression!"; walkInt::differentationError = "Failed to differentiate expression!"; walkInt[f_, x_] := Module[{integral, oldintegral, k, leafcounts, ruleused}, integral = int[f, x]; displayStart[integral]; leafcounts = {}; ruleused = ""; While[! FreeQ[integral, int], If[ruleused == "Product Rule", AppendTo[leafcounts, LeafCount @ integral]; If[Length @ leafcounts >= 5 && OrderedQ @ Take[leafcounts, -5], Message[walkInt::integrationError]; Return[Integrate[f, x]]; ]; ]; oldintegral = integral; k = 0; While[oldintegral == integral, k++; If[k > Length@$intRuleNames,
Message[walkInt::integrationError];
Return[Integrate[f, x]];
];
integral = integral /. ToExpression["int" <> StringReplace[$intRuleNames[[k]], " " -> ""]] ]; ruleused =$intRuleNames[[k]];
displayIntegral[integral, k];
While[! FreeQ[integral, d],
oldintegral = integral; k = 0;
While[oldintegral == integral,
k++;
If[k > Length@$dRuleNames, Message[walkInt::differentationError]; Return[Integrate[f, x]]; ]; integral = integral /. ToExpression["d" <> StringReplace[$dRuleNames[[k]], " " -> ""]]
];
displayDerivative[integral, k]];
];
Integrate[f, x]
];


Sample output:

-
the walkD has problem when evaluate the following: walkInt[x^2/(1 + x^4), x], and will be trapped into dead loop –  LCFactorization Aug 13 at 6:52
@LCFactorization it should fail now instead. –  Tyilo Aug 23 at 18:31
thank you for the update! Two suggestions for reference: (1)I still got error message for the problem I mentioned: walkInt::integrationError: Failed to integrate expression!; can this be removed? (2)Is it possible to use simplify in some of the intermediate steps? –  LCFactorization Aug 24 at 1:57
PS:  LabelStyle -> "TextStyling" option may not be valid in all MMA versions. –  LCFactorization Aug 24 at 1:59
@LCFactorization (1) I'm a noob at math, so I just implemented a few integration rules and walkInt doesn't know how to evaluate that integral. The error is there to show that walkInt couldn't get to the result one step at a time and it just gets Mathematica to integrate it. (2) Certainly although I'm not sure if this could have any adverse effects. –  Tyilo Aug 24 at 2:46