# Documentation

### This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

# subexpr

Rewrite symbolic expression in terms of common subexpressions

## Syntax

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

## 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

### Rewrite Expression Using Abbreviations

Solve the following equation. The solutions are very long expressions. To see them, 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^(1/3) - b/(3*a) - (- b^2/(9*a^2) + c/(3*a))/sigma^(1/3) (- b^2/(9*a^2) + c/(3*a))/(2*sigma^(1/3)) -... sigma^(1/3)/2 - (3^(1/2)*(sigma^(1/3) +... (- b^2/(9*a^2) + c/(3*a))/sigma^(1/3))*1i)/2 - b/(3*a) (- b^2/(9*a^2) + c/(3*a))/(2*sigma^(1/3)) -... sigma^(1/3)/2 + (3^(1/2)*(sigma^(1/3) +... (- b^2/(9*a^2) + c/(3*a))/sigma^(1/3))*1i)/2 - b/(3*a) sigma = ((d/(2*a) + b^3/(27*a^3) - (b*c)/(6*a^2))^2 + (- b^2/(9*a^2) +... c/(3*a))^3)^(1/2) - b^3/(27*a^3) - d/(2*a) + (b*c)/(6*a^2)```

### Customize Abbreviation Variables

```syms a b c x solutions = solve(a*x^2 + b*x + c == 0, x)```
```solutions = -(b + (b^2 - 4*a*c)^(1/2))/(2*a) -(b - (b^2 - 4*a*c)^(1/2))/(2*a)```

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 = -(b + s)/(2*a) -(b - s)/(2*a) s = (b^2 - 4*a*c)^(1/2)```

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

`[abbrSolutions,s] = subexpr(solutions,'s')`
```abbrSolutions = -(b + s)/(2*a) -(b - s)/(2*a) s = (b^2 - 4*a*c)^(1/2)```

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 = -(b + s)/(2*a) -(b - s)/(2*a)```

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 = -(b + s)/(2*a) -(b - s)/(2*a) t = (b^2 - 4*a*c)^(1/2)```
`subs(abbrSolutions,s,0)`
```ans = -b/(2*a) -b/(2*a)```

## 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.