# Abbreviate Common Terms in Long Expressions

Long expressions often contain several instances of the same subexpression. Such expressions look shorter if the same subexpression is replaced with an abbreviation. You can use sympref to specify whether or not to use abbreviated output format of symbolic expressions in live scripts.

For example, solve the equation $\sqrt{x}+\frac{1}{x}=1$ using solve.

syms x
sols = solve(sqrt(x) + 1/x == 1, x)
sols =

The solve function returns exact solutions as symbolic expressions. By default, live scripts display symbolic expressions in abbreviated output format. The symbolic preference setting uses an internal algorithm to choose which subexpressions to abbreviate, which can also include nested abbreviations. For example, the term ${\sigma }_{1}$ contains the subexpression abbreviated as ${\sigma }_{2}$. The symbolic preference setting does not provide any options to choose which subexpressions to abbreviate.

You can turn off abbreviated output format by setting the 'AbbreviateOutput' preference to false. The returned result is a long expression that is difficult to read.

sympref('AbbreviateOutput',false);
sols
sols =

$\left(\begin{array}{c}{\left(\frac{1}{18 {\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}}+\frac{{\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}}{2}+\frac{1}{3}-\frac{\sqrt{3} \left(\frac{1}{9 {\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}}-{\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}\right) \mathrm{i}}{2}\right)}^{2}\\ {\left(\frac{1}{18 {\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}}+\frac{{\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}}{2}+\frac{1}{3}+\frac{\sqrt{3} \left(\frac{1}{9 {\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}}-{\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}\right) \mathrm{i}}{2}\right)}^{2}\end{array}\right)$

The preferences you set using sympref persist through your current and future MATLAB® sessions. Restore the default values of 'AbbreviateOutput' by specifying the 'default' option.

sympref('AbbreviateOutput','default');

subexpr is another function that you can use to shorten long expressions. This function abbreviates only one common subexpression, and unlike sympref, it does not support nested abbreviations. Like sympref, subexpr also does not let you choose which subexpressions to replace.

Use the second input argument of subexpr to specify the variable name that replaces the common subexpression. For example, replace the common subexpression in sols with the variable t.

[sols1,t] = subexpr(sols,'t')
sols1 =

$\left(\begin{array}{c}{\left(\frac{t}{2}+\frac{1}{18 t}+\frac{1}{3}+\frac{\sqrt{3} \left(t-\frac{1}{9 t}\right) \mathrm{i}}{2}\right)}^{2}\\ {\left(\frac{t}{2}+\frac{1}{18 t}+\frac{1}{3}-\frac{\sqrt{3} \left(t-\frac{1}{9 t}\right) \mathrm{i}}{2}\right)}^{2}\end{array}\right)$

t =

${\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}$

Although sympref and subexpr do not provide a way to choose which subexpressions to replace in a solution, you can define these subexpressions as symbolic variables and manually rewrite the solution.

For example, define new symbolic variables a1 and a2.

syms a1 a2

Rewrite the solutions sols in terms of a1 and a2 before assigning the values of a1 and a2 to avoid evaluating sols.

sols = [(1/2*a1 + 1/3 + sqrt(3)/2*a2*1i)^2;...
(1/2*a1 + 1/3 - sqrt(3)/2*a2*1i)^2]
sols =

$\left(\begin{array}{c}{\left(\frac{{a}_{1}}{2}+\frac{1}{3}+\frac{\sqrt{3} {a}_{2} \mathrm{i}}{2}\right)}^{2}\\ {\left(\frac{{a}_{1}}{2}+\frac{1}{3}-\frac{\sqrt{3} {a}_{2} \mathrm{i}}{2}\right)}^{2}\end{array}\right)$

Assign the values $\left(t+\frac{1}{9t}\right)$ and $\left(t-\frac{1}{9t}\right)$ to a1 and a2, respectively.

a1 = t + 1/(9*t)
a1 =

$\frac{1}{9 {\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}}+{\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}$

a2 = t - 1/(9*t)
a2 =

${\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}-\frac{1}{9 {\left(\frac{25}{54}-\frac{\sqrt{23} \sqrt{108}}{108}\right)}^{1/3}}$

Evaluate sols using subs. The result is identical to the first output in this example.

sols_eval = subs(sols)
sols_eval =

## Support

#### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos