Main Content

subexpr

Rewrite symbolic expression in terms of common subexpressions

Description

[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).

example

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 = 

(σ-b3a-σ2σσ22σ-b3a-σ2-σ1σ22σ-b3a-σ2+σ1)where  σ1=3σ+σ2σi2  σ2=c3a-b29a2

sigma = 

d2a+b327a3-bc6a22+c3a-b29a23-b327a3-d2a+bc6a21/3

Solve a quadratic equation.

syms a b c x
solutions = solve(a*x^2 + b*x + c == 0, x)
solutions = 

(-b+b2-4ac2a-b-b2-4ac2a)

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+s2a-b-s2a)

s = b2-4ac

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

[abbrSolutions,s] = subexpr(solutions,'s')
abbrSolutions = 

(-b+s2a-b-s2a)

s = b2-4ac

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+s2a-b-s2a)

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+s2a-b-s2a)

t = b2-4ac
subs(abbrSolutions,s,0)
ans = 

(-b2a-b2a)

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