# rewrite

Rewrite expression in new terms

## Syntax

`rewrite(expr,target)rewrite(A,target)`

## Description

`rewrite(expr,target)` rewrites the symbolic expression `expr` in terms of `target`. The returned expression is mathematically equivalent to the original expression.

`rewrite(A,target)` rewrites each element of `A` in terms of `target`.

## Input Arguments

 `expr` Symbolic expression. `A` Vector or matrix of symbolic expressions. `target` One of these strings: `exp`, `log`, `sincos`, `sin`, `cos`, `tan`, `sqrt`, or `heaviside`.

## Examples

Rewrite these trigonometric functions in terms of the exponential function:

```syms x rewrite(sin(x), 'exp') rewrite(cos(x), 'exp') rewrite(tan(x), 'exp')```
```ans = (exp(-x*1i)*1i)/2 - (exp(x*1i)*1i)/2 ans = exp(-x*1i)/2 + exp(x*1i)/2 ans = -(exp(x*2i)*1i - 1i)/(exp(x*2i) + 1)```

Rewrite the tangent function in terms of the sine function:

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

Rewrite the hyperbolic tangent function in terms of the sine function:

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

Rewrite these inverse trigonometric functions in terms of the natural logarithm:

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

Rewrite the rectangular pulse function in terms of the Heaviside step function:

```syms a b x rewrite(rectangularPulse(a, b, x), 'heaviside')```
```ans = heaviside(x - a) - heaviside(x - b)```

Rewrite the triangular pulse function in terms of the Heaviside step function:

```syms a b c x rewrite(triangularPulse(a, b, c, x), 'heaviside')```
```ans = (heaviside(x - a)*(a - x))/(a - b) - (heaviside(x - b)*(a - x))/(a - b)... - (heaviside(x - b)*(c - x))/(b - c) + (heaviside(x - c)*(c - x))/(b - c)```

Call `rewrite` to rewrite each element of this matrix of symbolic expressions in terms of the exponential function:

```syms x A = [sin(x) cos(x); sinh(x) cosh(x)]; rewrite(A, '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 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 the sine function with either $-\sqrt{1-{\mathrm{cos}}^{2}\left(x\right)}$ or $\sqrt{1-{\mathrm{cos}}^{2}\left(x\right)}$ because these expressions are only valid for `x` within particular intervals:

```syms x rewrite(sin(x),'cos')```
```ans = sin(x)```

expand all

### Tips

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