# simplify

Algebraic simplification

## Description

example

S = simplify(expr) performs algebraic simplification of expr. If expr is a symbolic vector or matrix, this function simplifies each element of expr.

example

S = simplify(expr,Name,Value) performs algebraic simplification of expr using additional options specified by one or more Name,Value pair arguments.

## Examples

collapse all

Simplify these symbolic expressions:

syms x a b c
S = simplify(sin(x)^2 + cos(x)^2)
S = $1$
S = simplify(exp(c*log(sqrt(a+b))))
S = ${\left(a+b\right)}^{c/2}$

Call simplify for this symbolic matrix. When the input argument is a vector or matrix, simplify tries to find a simpler form of each element of the vector or matrix.

syms x
M = [(x^2 + 5*x + 6)/(x + 2), sin(x)*sin(2*x) + cos(x)*cos(2*x);
(exp(-x*1i)*1i)/2 - (exp(x*1i)*1i)/2, sqrt(16)];
S = simplify(M)
S =

$\left(\begin{array}{cc}x+3& \mathrm{cos}\left(x\right)\\ \mathrm{sin}\left(x\right)& 4\end{array}\right)$

Simplify a symbolic expression that contain logarithms and powers. By default, simplify does not combine powers and logarithms because combining them is not valid for generic complex values.

syms x
expr = (log(x^2 + 2*x + 1) - log(x + 1))*sqrt(x^2);
S = simplify(expr)
S = $-\left(\mathrm{log}\left(x+1\right)-\mathrm{log}\left({\left(x+1\right)}^{2}\right)\right) \sqrt{{x}^{2}}$

To apply the simplification rules that allow the simplify function to combine powers and logarithms, set 'IgnoreAnalyticConstraints' to true:

S = simplify(expr,'IgnoreAnalyticConstraints',true)
S = $x \mathrm{log}\left(x+1\right)$

Simplify this expression:

syms x
expr = ((exp(-x*1i)*1i) - (exp(x*1i)*1i))/(exp(-x*1i) + exp(x*1i));
S = simplify(expr)
S =

$-\frac{{\mathrm{e}}^{2 x \mathrm{i}} \mathrm{i}-\mathrm{i}}{{\mathrm{e}}^{2 x \mathrm{i}}+1}$

By default, simplify uses one internal simplification step. You can get different, often shorter, simplification results by increasing the number of simplification steps:

S10 = simplify(expr,'Steps',10)
S10 =

$\frac{2 \mathrm{i}}{{\mathrm{e}}^{2 x \mathrm{i}}+1}-\mathrm{i}$

S30 = simplify(expr,'Steps',30)
S30 =

$\frac{\left(\mathrm{cos}\left(x\right)-\mathrm{sin}\left(x\right) \mathrm{i}\right) \mathrm{i}}{\mathrm{cos}\left(x\right)}-\mathrm{i}$

S50 = simplify(expr,'Steps',50)
S50 = $\mathrm{tan}\left(x\right)$

If you are unable to return the desired result, try alternate simplification functions. See Choose Function to Rearrange Expression.

Get equivalent results for a symbolic expression by setting the value of 'All' to true.

syms x
expr = cos(x)^2 - sin(x)^2;
S = simplify(expr,'All',true)
S =

$\left(\begin{array}{c}\mathrm{cos}\left(2 x\right)\\ {\mathrm{cos}\left(x\right)}^{2}-{\mathrm{sin}\left(x\right)}^{2}\end{array}\right)$

Increase the number of simplification steps to 10. Find the other equivalent results for the same expression.

S = simplify(expr,'Steps',10,'All',true)
S =

$\left(\begin{array}{c}\mathrm{cos}\left(2 x\right)\\ 1-2 {\mathrm{sin}\left(x\right)}^{2}\\ 2 {\mathrm{cos}\left(x\right)}^{2}-1\\ {\mathrm{cos}\left(x\right)}^{2}-{\mathrm{sin}\left(x\right)}^{2}\\ \mathrm{cot}\left(2 x\right) \mathrm{sin}\left(2 x\right)\\ \frac{{\mathrm{e}}^{-2 x \mathrm{i}}}{2}+\frac{{\mathrm{e}}^{2 x \mathrm{i}}}{2}\end{array}\right)$

Attempt to separate real and imaginary parts of an expression by setting the value of 'Criterion' to 'preferReal'.

syms x
f = (exp(x + exp(-x*1i)/2 - exp(x*1i)/2)*1i)/2 -...
(exp(-x - exp(-x*1i)/2 + exp(x*1i)/2)*1i)/2;
S = simplify(f,'Criterion','preferReal','Steps',100)
S = $\mathrm{sin}\left(\mathrm{sin}\left(x\right)\right) \mathrm{cosh}\left(x\right)+\mathrm{cos}\left(\mathrm{sin}\left(x\right)\right) \mathrm{sinh}\left(x\right) \mathrm{i}$

If 'Criterion' is not set to 'preferReal', then simplify returns a shorter result but the real and imaginary parts are not separated.

S = simplify(f,'Steps',100)
S = $\mathrm{sin}\left(\mathrm{sin}\left(x\right)+x \mathrm{i}\right)$

When you set 'Criterion' to 'preferReal', the simplifier disfavors expression forms where complex values appear inside subexpressions. In nested subexpressions, the deeper the complex value appears inside an expression, the least preference this form of an expression gets.

Attempt to avoid imaginary terms in exponents by setting 'Criterion' to 'preferReal'.

Show this behavior by simplifying a complex symbolic expression with and without setting 'Criterion' to 'preferReal'. When 'Criterion' is set to 'preferReal', then simplify places the imaginary term outside the exponent.

expr = sym(1i)^(1i+1);
withoutPreferReal = simplify(expr,'Steps',100)
withoutPreferReal =

${\left(-1\right)}^{\frac{1}{2}+\frac{1}{2} \mathrm{i}}$

withPreferReal = simplify(expr,'Criterion','preferReal','Steps',100)
withPreferReal =

${\mathrm{e}}^{-\frac{\pi }{2}} \mathrm{i}$

Simplify expressions containing symbolic units of the same dimension by using simplify.

u = symunit;
expr = 300*u.cm + 40*u.inch + 2*u.m;
S = simplify(expr)
S =

$\frac{3008}{5} \mathrm{cm}\mathrm{"centimeter - a physical unit of length."}$

simplify automatically chooses the unit to rewrite into. To choose a specific unit, use rewrite.

In most cases, to simplify a symbolic expression using Symbolic Math Toolbox™, you only need to use the simplify function. But for some large and complex expressions, you can obtain a faster and simpler result by using the expand function before applying simplify.

For instance, this workflow gives better results when finding the determinant of a matrix that represents the Kerr metric [1]. Declare the parameters of the Kerr metric.

syms theta real;
syms r rs a real positive;

Define the matrix that represents the Kerr metric.

rho = sqrt(r^2 + a^2*cos(theta)^2);
delta  = r^2 + a^2 - r*rs;
g(1,1) = - (1 - r*rs/rho^2);
g(1,4) = - (rs*a*r*sin(theta)^2)/rho^2;
g(4,1) = - (rs*a*r*sin(theta)^2)/rho^2;
g(2,2) = rho^2/delta;
g(3,3) = rho^2;
g(4,4) = (r^2 + a^2 + rs*a^2*r*sin(theta)^2/rho^2)*sin(theta)^2;

Evaluate the determinant of the Kerr metric.

det_g = det(g)
det_g =

$-\frac{{\mathrm{sin}\left(\theta \right)}^{2} \left({a}^{6} {\mathrm{cos}\left(\theta \right)}^{4}+{a}^{4} {r}^{2} {\mathrm{cos}\left(\theta \right)}^{4}+2 {a}^{4} {r}^{2} {\mathrm{cos}\left(\theta \right)}^{2}+\mathrm{rs} {a}^{4} r {\mathrm{cos}\left(\theta \right)}^{2} {\mathrm{sin}\left(\theta \right)}^{2}-\mathrm{rs} {a}^{4} r {\mathrm{cos}\left(\theta \right)}^{2}+2 {a}^{2} {r}^{4} {\mathrm{cos}\left(\theta \right)}^{2}+{a}^{2} {r}^{4}-\mathrm{rs} {a}^{2} {r}^{3} {\mathrm{cos}\left(\theta \right)}^{2}+\mathrm{rs} {a}^{2} {r}^{3} {\mathrm{sin}\left(\theta \right)}^{2}-\mathrm{rs} {a}^{2} {r}^{3}+{r}^{6}-\mathrm{rs} {r}^{5}\right)}{{a}^{2}+{r}^{2}-\mathrm{rs} r}$

Simplify the determinant using the simplify function.

D = simplify(det_g)
D = $-{\mathrm{sin}\left(\theta \right)}^{2} \left({a}^{2} {\mathrm{cos}\left(\theta \right)}^{2}+{r}^{2}\right) \left(-{a}^{2} {\mathrm{sin}\left(\theta \right)}^{2}+{a}^{2}+{r}^{2}\right)$

Instead, flatten the expression using the expand function, and then apply the simplify function. The result is simpler with this extra step.

D = simplify(expand(det_g))
D = $-{\mathrm{sin}\left(\theta \right)}^{2} {\left(-{a}^{2} {\mathrm{sin}\left(\theta \right)}^{2}+{a}^{2}+{r}^{2}\right)}^{2}$

## Input Arguments

collapse all

Input expression, specified as a symbolic expression, function, vector, or matrix.

### Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Seconds',60 limits the simplification process to 60 seconds.

Option to return equivalent results, specified as the comma-separated pair consisting of 'All' and either of the two logical values. When you use this option, the input argument expr must be a scalar.

 false Use the default option to return only the final simplification result. true Return a column vector of equivalent results for the input expression. You can use this option along with the 'Steps' option to obtain alternative expressions in the simplification process.

Simplification criterion, specified as the comma-separated pair consisting of 'Criterion' and one of these character vectors.

 'default' Use the default (internal) simplification criteria. 'preferReal' Favor the forms of S containing real values over the forms containing complex values. If any form of S contains complex values, the simplifier disfavors the forms where complex values appear inside subexpressions. In case of nested subexpressions, the deeper the complex value appears inside an expression, the least preference this form of an expression gets.

Simplification rules, specified as the comma-separated pair consisting of 'IgnoreAnalyticConstraints' and one of these values.

 false Use strict simplification rules. simplify always returns results that are analytically equivalent to the initial expression. true Apply purely algebraic simplifications to expressions. Setting IgnoreAnalyticConstraints to true can give you simpler solutions, which could lead to results not generally valid. In other words, this option applies mathematical identities that are convenient, but the results might not hold for all possible values of the variables. In some cases, the results might not be equivalent to the initial expression.

Time limit for the simplification process, specified as the comma-separated pair consisting of 'Seconds' and a positive value that denotes the maximal time in seconds.

Number of simplification steps, specified as the comma-separated pair consisting of 'Steps' and a positive value that denotes the maximal number of internal simplification steps. Note that increasing the number of simplification steps can slow down your computations.

simplify(expr,'Steps',n) is equivalent to simplify(expr,n), where n is the number of simplification steps.

## Tips

• Simplification of mathematical expression is not a clearly defined subject. There is no universal idea as to which form of an expression is simplest. The form of a mathematical expression that is simplest for one problem might be complicated or even unsuitable for another problem.

## Algorithms

When you use IgnoreAnalyticConstraints, then simplify follows these rules:

• log(a) + log(b) = log(a·b) for all values of a and b. In particular, the following equality is valid for all values of a, b, and c:

(a·b)c = ac·bc.

• log(ab) = b·log(a) for all values of a and b. In particular, the following equality is valid for all values of a, b, and c:

(ab)c = ab·c.

• If f and g are standard mathematical functions and f(g(x)) = x for all small positive numbers, f(g(x)) = x is assumed to be valid for all complex values of x. In particular:

• log(ex) = x

• asin(sin(x)) = x, acos(cos(x)) = x, atan(tan(x)) = x

• asinh(sinh(x)) = x, acosh(cosh(x)) = x, atanh(tanh(x)) = x

• Wk(x·ex) = x for all branch indices k of the Lambert W function.

## References

[1] Zee, A. Einstein Gravity in a Nutshell. Princeton: Princeton University Press, 2013.