# 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
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 11, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8290444612503052, "perplexity": 1314.6378446511126}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-17/segments/1618039550330.88/warc/CC-MAIN-20210421191857-20210421221857-00176.warc.gz"}