# subexpr

Rewrite symbolic expression in terms of common subexpressions

## Syntax

``[r,sigma] = subexpr(expr)``
``````[r,var] = subexpr(expr,'var')``````
``````[r,var] = subexpr(expr,var)``````

## Description

example

````[r,sigma] = subexpr(expr)` rewrites the symbolic expression `expr` in terms of a common subexpression, substituting this common subexpression with the symbolic variable `sigma`. The input expression `expr` cannot contain the variable `sigma`.```

example

``````[r,var] = subexpr(expr,'var')``` substitutes the common subexpression by `var`. The input expression `expr` cannot contain the symbolic variable `var`.```

example

``````[r,var] = subexpr(expr,var)``` is equivalent to `[r,var] = subexpr(expr,'var')`, except that the symbolic variable `var` must already exist in the MATLAB® workspace.This syntax overwrites the value of the variable `var` with the common subexpression found in `expr`. To avoid overwriting the value of `var`, use another variable name as the second output argument. For example, use ```[r,var1] = subexpr(expr,var)```.```

## Examples

collapse all

Solve the following equation. The solutions are very long expressions. To display the solutions, remove the semicolon at the end of the `solve` command.

```syms a b c d x solutions = solve(a*x^3 + b*x^2 + c*x + d == 0, x, 'MaxDegree', 3);```

These long expressions have common subexpressions. To shorten the expressions, abbreviate the common subexpression by using `subexpr`. If you do not specify the variable to use for abbreviations as the second input argument of `subexpr`, then `subexpr` uses the variable `sigma`.

`[r, sigma] = subexpr(solutions)`
```r =  ```
```sigma =  ${\left(\sqrt{{\left(\frac{d}{2 a}+\frac{{b}^{3}}{27 {a}^{3}}-\frac{b c}{6 {a}^{2}}\right)}^{2}+{\left(\frac{c}{3 a}-\frac{{b}^{2}}{9 {a}^{2}}\right)}^{3}}-\frac{{b}^{3}}{27 {a}^{3}}-\frac{d}{2 a}+\frac{b c}{6 {a}^{2}}\right)}^{1/3}$```

```syms a b c x solutions = solve(a*x^2 + b*x + c == 0, x)```
```solutions =  $\left(\begin{array}{c}-\frac{b+\sqrt{{b}^{2}-4 a c}}{2 a}\\ -\frac{b-\sqrt{{b}^{2}-4 a c}}{2 a}\end{array}\right)$```

Use `syms` to create the symbolic variable `s`, and then replace common subexpressions in the result with this variable.

```syms s [abbrSolutions,s] = subexpr(solutions,s)```
```abbrSolutions =  $\left(\begin{array}{c}-\frac{b+s}{2 a}\\ -\frac{b-s}{2 a}\end{array}\right)$```
`s = $\sqrt{{b}^{2}-4 a c}$`

Alternatively, use `'s'` to specify the abbreviation variable.

`[abbrSolutions,s] = subexpr(solutions,'s')`
```abbrSolutions =  $\left(\begin{array}{c}-\frac{b+s}{2 a}\\ -\frac{b-s}{2 a}\end{array}\right)$```
`s = $\sqrt{{b}^{2}-4 a c}$`

Both syntaxes overwrite the value of the variable `s` with the common subexpression. Therefore, you cannot, for example, substitute `s` with some value.

`subs(abbrSolutions,s,0)`
```ans =  $\left(\begin{array}{c}-\frac{b+s}{2 a}\\ -\frac{b-s}{2 a}\end{array}\right)$```

To avoid overwriting the value of the variable `s`, use another variable name for the second output argument.

```syms s [abbrSolutions,t] = subexpr(solutions,'s')```
```abbrSolutions =  $\left(\begin{array}{c}-\frac{b+s}{2 a}\\ -\frac{b-s}{2 a}\end{array}\right)$```
`t = $\sqrt{{b}^{2}-4 a c}$`
`subs(abbrSolutions,s,0)`
```ans =  $\left(\begin{array}{c}-\frac{b}{2 a}\\ -\frac{b}{2 a}\end{array}\right)$```

## Input Arguments

collapse all

Long expression containing common subexpressions, specified as a symbolic expression or function.

Variable to use for substituting common subexpressions, specified as a character vector or symbolic variable.

`subexpr` throws an error if the input expression `expr` already contains `var`.

## Output Arguments

collapse all

Expression with common subexpressions replaced by abbreviations, returned as a symbolic expression or function.

Variable used for abbreviations, returned as a symbolic variable.

## Version History

Introduced before R2006a