Documentation

# rewrite

Rewrite expression in terms of another function

## Syntax

``rewrite(expr,target)``

## Description

example

````rewrite(expr,target)` rewrites the symbolic expression `expr` in terms of the target function `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') tan2exp = rewrite(tan(x), 'exp')```
```sin2exp = (exp(-x*1i)*1i)/2 - (exp(x*1i)*1i)/2 tan2exp = -(exp(x*2i)*1i - 1i)/(exp(x*2i) + 1)```

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

```syms x exp2sin = rewrite(exp(x), 'sin') exp2tan = rewrite(-(exp(x*2i)*1i - 1i)/(exp(x*2i) + 1), 'tan')```
```exp2sin = 1 - 2*sin((x*1i)/2)^2 - sin(x*1i)*1i exp2tan = -(((tan(x) - 1i)*1i)/(tan(x) + 1i) + 1i)/... ((tan(x) - 1i)/(tan(x) + 1i) - 1)```

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

`exp2tan = simplify(exp2tan)`
```exp2tan = tan(x)```

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

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

```syms x tan2sin = rewrite(tan(x), 'sin')```
```tan2sin = -sin(x)/(2*sin(x/2)^2 - 1)```

Rewrite any hyperbolic function in terms of any trigonometric function by specifying the trigonometric function as the target. For a full list of targets, see `target`.

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

```syms x tanh2sin = rewrite(tanh(x), 'sin')```
```tanh2sin = (sin(x*1i)*1i)/(2*sin((x*1i)/2)^2 - 1)```

Similarly, rewrite trigonometric functions in terms of hyperbolic functions by specifying the hyperbolic function as the target.

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

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

```syms x acos2log = rewrite(acos(x), 'log') acot2log = rewrite(acot(x), 'log')```
```acos2log = -log(x + (1 - x^2)^(1/2)*1i)*1i acot2log = (log(1 - 1i/x)*1i)/2 - (log(1i/x + 1)*1i)/2```

Similarly, rewrite the logarithm function in terms of an 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)]; rewrite(matrix, 'exp')```
```ans = [ (exp(-x*1i)*1i)/2 - (exp(x*1i)*1i)/2, exp(-x*1i)/2 + exp(x*1i)/2] [ exp(x)/2 - exp(-x)/2, exp(-x)/2 + exp(x)/2]```

Rewrite the cosine function in terms of the sine function. Here, `rewrite` replaces the cosine function using the identity `cos(2*x) = 1 – 2*sin(x)^2` which is valid for any `x`.

```syms x rewrite(cos(x),'sin')```
```ans = 1 - 2*sin(x/2)^2```

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

```syms x rewrite(sin(x),'cos') rewrite(sin(x)^2,'cos')```
```ans = sin(x) ans = 1 - cos(x)^2```

## Input Arguments

collapse all

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

Target function, specified as a character vector. This table summarizes the rewriting rules for all allowed targets.

TargetRewrites 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`

## Tips

• `rewrite` replaces symbolic function calls in `expr` with the target function only if the replacement is mathematically valid. Otherwise, it keeps the original function calls.