Subexpressions

Jon McLoone, Wolfram Research (jonm@wolfram.co.uk)

A package which extracts out repeated sub-expressions from a large expression in order to display a more compact form to the user. This package is intended for easier examination by the user and is not intended to optimise the expression for computational efficiency.

<< Subexpressions.m

?Subexpressions

Solving the general quadratic function gives two solutions which are related

ans1 = Solve[a  (x^2) + b  x + c == 0, x]

Using the Subexpressions function identifies and extracts the common part (the discriminant in this case).

Subexpressions[ans1]

To return the result to its original form use ReleaseHold

ReleaseHold[%]

The usefullness becomes more pronounced in larger expressions. It is not easy to see the relationship between the three solution of the order three equation

ans2 = Solve[a  (x^3) + b  (x^2) + c  x + d == 0, x]

Subexpressions[ans2]

?MaxSubexpressions

Subexpressions[ans2, MaxSubexpressions -> 3]

?MinLeafCount

Subexpressions[ans2, MinLeafCount -> 50]