# rewrite

Rewrite expression in terms of another function

## Syntax

``R = rewrite(expr,target)``

## Description

example

````R = rewrite(expr,target)` rewrites the symbolic expression `expr` in terms of another function as specified by `target`. The rewritten expression is mathematically equivalent to the original expression. If `expr` is a vector or matrix, `rewrite` acts element-wise on `expr`.```

## Examples

collapse all

Rewrite any trigonometric function in terms of the exponential function by specifying the target `"exp"`.

```syms x sin2exp = rewrite(sin(x),"exp")```
```sin2exp =  $\frac{{\mathrm{e}}^{-x \mathrm{i}} \mathrm{i}}{2}-\frac{{\mathrm{e}}^{x \mathrm{i}} \mathrm{i}}{2}$```
`tan2exp = rewrite(tan(x),"exp")`
```tan2exp =  $-\frac{{\mathrm{e}}^{2 x \mathrm{i}} \mathrm{i}-\mathrm{i}}{{\mathrm{e}}^{2 x \mathrm{i}}+1}$```

Rewrite the exponential function in terms of any trigonometric function by specifying the trigonometric function as the target. For a full list of target options, see target.

```syms x exp2sin = rewrite(exp(x),"sin")```
```exp2sin =  $-2 {\mathrm{sin}\left(\frac{x \mathrm{i}}{2}\right)}^{2}-\mathrm{sin}\left(x \mathrm{i}\right) \mathrm{i}+1$```
`exp2tan = rewrite(-(exp(x*2i)*1i - 1i)/(exp(x*2i) + 1),"tan")`
```exp2tan =  $-\frac{\frac{\left(\mathrm{tan}\left(x\right)-\mathrm{i}\right) \mathrm{i}}{\mathrm{tan}\left(x\right)+\mathrm{i}}+\mathrm{i}}{\frac{\mathrm{tan}\left(x\right)-\mathrm{i}}{\mathrm{tan}\left(x\right)+\mathrm{i}}-1}$```

Simplify `exp2tan` to the expected form by using `simplify`.

`exp2tan = simplify(exp2tan)`
`exp2tan = $\mathrm{tan}\left(x\right)$`

Rewrite any trigonometric function in terms of any other trigonometric function by specifying the target. For a full list of target options, see target.

Rewrite `tan(x)` in terms of the sine function by specifying the target `"sin"`.

```syms x tan2sin = rewrite(tan(x),"sin")```
```tan2sin =  $-\frac{\mathrm{sin}\left(x\right)}{2 {\mathrm{sin}\left(\frac{x}{2}\right)}^{2}-1}$```

Rewrite any inverse trigonometric function in terms of the logarithm function by specifying the target `"log"`. For a full list of target options, see target.

Rewrite `acos(x)` and `acot(x)` in terms of the `log` function.

```syms x acos2log = rewrite(acos(x),"log")```
`acos2log = $-\mathrm{log}\left(x+\sqrt{1-{x}^{2}} \mathrm{i}\right) \mathrm{i}$`
`acot2log = rewrite(acot(x),"log")`
```acot2log =  $\frac{\mathrm{log}\left(1-\frac{\mathrm{i}}{x}\right) \mathrm{i}}{2}-\frac{\mathrm{log}\left(1+\frac{\mathrm{i}}{x}\right) \mathrm{i}}{2}$```

Similarly, rewrite the logarithm function in terms of any inverse trigonometric function by specifying the inverse trigonometric function as the target.

Rewrite each element of a matrix by calling `rewrite` on the matrix.

Rewrite all elements of a matrix in terms of the `exp` function.

```syms x matrix = [sin(x) cos(x); sinh(x) cosh(x)]; R = rewrite(matrix,"exp")```
```R =  $\left(\begin{array}{cc}\frac{{\mathrm{e}}^{-x \mathrm{i}} \mathrm{i}}{2}-\frac{{\mathrm{e}}^{x \mathrm{i}} \mathrm{i}}{2}& \frac{{\mathrm{e}}^{-x \mathrm{i}}}{2}+\frac{{\mathrm{e}}^{x \mathrm{i}}}{2}\\ \frac{{\mathrm{e}}^{x}}{2}-\frac{{\mathrm{e}}^{-x}}{2}& \frac{{\mathrm{e}}^{-x}}{2}+\frac{{\mathrm{e}}^{x}}{2}\end{array}\right)$```

Rewrite the cosine function in terms of the sine function. Here, the `rewrite` function rewrites the cosine function using the identity $\mathrm{cos}\left(\mathit{x}\right)=1-2\text{\hspace{0.17em}}{\mathrm{sin}\left(\frac{\mathit{x}}{2}\right)}^{2}$, which is valid for any $\mathit{x}$.

```syms x R = rewrite(cos(x),"sin")```
```R =  $1-2 {\mathrm{sin}\left(\frac{x}{2}\right)}^{2}$```

`rewrite` does not rewrite `sin(x)` as either $-\sqrt{1-{\mathrm{cos}}^{2}\left(\mathit{x}\right)}$ or $\sqrt{1-{\mathrm{cos}}^{2}\left(\mathit{x}\right)}$ because these expressions are not valid for all `x`. However, using the square of these expressions to represent `sin(x)^2` is valid for all `x`. Thus, `rewrite` can rewrite `sin(x)^2`.

```syms x R1 = rewrite(sin(x),"cos")```
`R1 = $\mathrm{sin}\left(x\right)$`
`R2 = rewrite(sin(x)^2,"cos")`
`R2 = $1-{\mathrm{cos}\left(x\right)}^{2}$`

Since R2023a

Find the root of a polynomial by using `root`. The result is a column vector in terms of the `root` function with `k = 1`, `2`, `3`, `4`, or `5` for the `k`th root of the polynomial.

```syms x sols = root(x^5 - x^4 - 1,x)```
```sols =  $\left(\begin{array}{c}\mathrm{root}\left({x}^{5}-{x}^{4}-1,x,1\right)\\ \mathrm{root}\left({x}^{5}-{x}^{4}-1,x,2\right)\\ \mathrm{root}\left({x}^{5}-{x}^{4}-1,x,3\right)\\ \mathrm{root}\left({x}^{5}-{x}^{4}-1,x,4\right)\\ \mathrm{root}\left({x}^{5}-{x}^{4}-1,x,5\right)\end{array}\right)$```

Expand the `root` function in `sols` by using `rewrite` with the `"expandroot"` option. The result is in terms of arithmetic operations such as ^, *, /, +, and – that operate on exact symbolic numbers. Because the expanded result can involve many terms that operate arithmetically, numerical approximation of this result can be inaccurate (due to accumulation of round-off errors).

`R = rewrite(sols,"expandroot")`
```R =  ```

As an alternative, you can numerically approximate `sols` directly by using `vpa` to return variable-precision symbolic numbers. The resulting numeric values have the default 32 significant digits, which are more accurate.

`solsVpa = vpa(sols)`
```solsVpa =  $\left(\begin{array}{c}-0.66235897862237301298045442723905-0.56227951206230124389918214490937 \mathrm{i}\\ -0.66235897862237301298045442723905+0.56227951206230124389918214490937 \mathrm{i}\\ 0.5-0.86602540378443864676372317075294 \mathrm{i}\\ 0.5+0.86602540378443864676372317075294 \mathrm{i}\\ 1.3247179572447460259609088544781\end{array}\right)$```

To use `sols` without Symbolic Math Toolbox™, you can generate code and convert `sols` to a MATLAB® function by using `matlabFunction`. The generated file uses the `roots` function that operates on the numeric `double` data type.

```matlabFunction(sols,"File","myfile"); type myfile```
```function sols = myfile %MYFILE % SOLS = MYFILE % This function was generated by the Symbolic Math Toolbox version 9.3. % 03-Mar-2023 07:29:21 t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]); t2 = t0(1); t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]); t3 = t0(2); t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]); t4 = t0(3); t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]); t5 = t0(4); t0 = roots([1.0,-1.0,0.0,0.0,0.0,-1.0]); t6 = t0(5); sols = [t2;t3;t4;t5;t6]; end ```

Since R2023a

Find the indefinite integral of a polynomial fraction. The result is in terms of the `symsum` and `root` functions as represented by the $\sum$ and $\mathrm{root}$ symbols.

```syms x F = int(1/(x^3 + x - 1),x)```
```F =  ${\sum }_{k=1}^{3}\mathrm{log}\left(\mathrm{root}\left({z}^{3}-\frac{3 z}{31}-\frac{1}{31},z,k\right) \left(3 x+\mathrm{root}\left({z}^{3}-\frac{3 z}{31}-\frac{1}{31},z,k\right) \left(6 x-9\right)\right)\right) \mathrm{root}\left({z}^{3}-\frac{3 z}{31}-\frac{1}{31},z,k\right)$```

Rewrite the result in terms of arithmetic operations. Because the symbolic summation is the outermost operation, first apply the `rewrite` function with the `"expandsum"` option to expand `symsum`. Then, apply `rewrite` again with the `"expandroot"` option to rewrite the `root` function. The resulting symbolic expression is in terms of arithmetic operations such as ^, *, /, +, and –.

`R = rewrite(rewrite(F,"expandsum"),"expandroot")`
```R =  ```

## Input Arguments

collapse all

Input to rewrite or replace, specified as a symbolic number, variable, expression, function, vector, matrix, or multidimensional array.

Target function or function to expand, specified as a string scalar or character vector. This table summarizes the rewriting rules for all allowed `target` options.

TargetRewrite or Replace These FunctionsIn Terms of These Functions
`"exp"`All trigonometric and hyperbolic functions including inverse functions`exp`, `log`
`"log"`All inverse trigonometric and hyperbolic functions`log`
`"sincos"``tan`, `cot`, `exp`, `sinh`, `cosh`, `tanh`, `coth``sin`, `cos`
`"sin"`, `"cos"`, `"tan"`, or `"cot"``sin`, `cos`, `exp`, `tan`, `cot`, `sinh`, `cosh`, `tanh`, `coth` except the targetTarget trigonometric function
`"sinhcosh"``tan`, `cot`, `exp`, `sin`, `cos`, `tanh`, `coth``sinh`, `cosh`
`"sinh"`, `"cosh"`, `"tanh"`, `"coth"``tan`, `cot`, `exp`, `sin`, `cos`, `sinh`, `cosh`, `tanh`, `coth` except the targetTarget hyperbolic function
`"asin"`, `"acos"`, `"atan"`, `"acot"``log`, and all inverse trigonometric and inverse hyperbolic functionsTarget inverse trigonometric function
`"asinh"`, `"acosh"`, `"atanh"`, `"acoth"``log`, and all inverse trigonometric and inverse hyperbolic functionsTarget inverse hyperbolic function
`"sqrt"``abs(x + 1i*y)``sqrt(x^2 + y^2)`
`"heaviside"``sign`, `triangularPulse`, `rectangularPulse``heaviside`
`"piecewise"``abs`, `heaviside`, `sign`, `triangularPulse`, `rectangularPulse``piecewise`
`"expandroot"``root`Arithmetic operations such as ^, *, /, +, and –
`"expandsum"``symsum`Arithmetic operations such as + and –

## Version History

Introduced in R2012a

expand all