Accelerating the pace of engineering and science

# Documentation Center

• Trial Software
• Product Updates

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

example

[r,var] = subexpr(expr,var) substitutes the common subexpression by the symbolic variable var.

example

[r,var] = subexpr(expr,'var') is equivalent to [r,var] = subexpr(expr,var).

## Examples

### Rewrite an Expression Using Abbreviations

subexpr(expr) finds a common subexpression in the expression expr, and replaces it with the symbolic variable sigma.

Solve this 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);```

These long expressions have common subexpressions. Abbreviating these common subexpressions shortens the expressions. To abbreviate subexpressions, use 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))*i)/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))*i)/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

subexpr(expr,var) lets you specify the variable name to use for abbreviations.

Solve this 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);```

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 =
s^(1/3) - b/(3*a)...
- (- b^2/(9*a^2) + c/(3*a))/s^(1/3)
(- b^2/(9*a^2) + c/(3*a))/(2*s^(1/3)) - s^(1/3)/2 + (3^(1/2)*(s^(1/3) + (- b^2/(9*a^2)...
+ c/(3*a))/s^(1/3))*i)/2 - b/(3*a)
(- b^2/(9*a^2) + c/(3*a))/(2*s^(1/3)) - s^(1/3)/2 - (3^(1/2)*(s^(1/3) + (- b^2/(9*a^2)...
+ c/(3*a))/s^(1/3))*i)/2 - b/(3*a)

s =
((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)```

Alternatively, use the string s to specify the abbreviation variable.

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

s =
((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)```

## Input Arguments

expand all

### expr — Long expression containing common subexpressionssymbolic expression | symbolic function

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

### var — Variable to use for substituting common subexpressionsstring | symbolic variable

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

## Output Arguments

expand all

### r — Expression with common subexpressions replaced by abbreviationssymbolic expression | symbolic function

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

### var — Variable used for abbreviationssymbolic variable

Variable used for abbreviations, returned as a symbolic variable.

## Related Examples

Was this topic helpful?