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 = 

(σ-0.3333ba-σ1σσ-0.5000-0.8660i-0.3333ba+σ10.5000-0.8660iσσ-0.5000+0.8660i-0.3333ba+σ10.5000+0.8660iσ)where  σ1=0.3333ca-0.1111b2a2

sigma = 

0.5000da+0.0370b3a3-0.1667bca22+0.3333ca-0.1111b2a23-0.5000da-0.0370b3a3+0.1667bca20.3333

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.

Introduced before R2006a