Documentation

This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

rewrite

Rewrite expression in terms of another function

Syntax

``rewrite(expr,target)``
``rewrite(unit,unitSystem)``
``rewrite(unit,unitSystem,'Derived')``
``___ = rewrite(___,Name,Value)``

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

example

````rewrite(unit,unitSystem)` converts the symbolic unit `unit` to the unit system `unitSystem`. By default, the SI, CGS, and US unit systems are available. You can also define custom unit systems.```

example

````rewrite(unit,unitSystem,'Derived')` converts the symbolic unit `unit` to derived units of `unitSystem`.```

example

````___ = rewrite(___,Name,Value)` uses additional options specified by one or more `Name,Value` pair arguments.```

Examples

Rewrite Between Trigonometric and Exponential Functions

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 Between Trigonometric Functions

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 Between Symbolic Units

Rewrite a symbolic unit to another unit by using `rewrite`. You can also rewrite to SI units.

Rewrite `5` cm in terms of inches.

```u = symunit; length = 5*u.cm; length = rewrite(length,u.in)```
```length = (250/127)*[in]```

Rewrite `length` in terms of SI units. The result is in meters.

`length = rewrite(length,'SI')`
```length = (1/20)*[m]```

By default, temperatures are assumed to represent temperature differences. To rewrite between absolute temperatures, specify the `Temperature` input as `'absolute'`.

Rewrite `23` degrees Celsius to degrees Kelvin, treating it first as a temperature difference and then as an absolute temperature.

```u = symunit; T = 23*u.Celsius; relK = rewrite(T,u.K,'Temperature','difference')```
```relK = 23*[K]```
`absK = rewrite(T,u.K,'Temperature','absolute')`
```absK = (5923/20)*[K]```

For defining unit systems and converting between unit systems, see Unit Conversions and Unit Systems.

Rewrite Between Hyperbolic Functions and Trigonometric Functions

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 Between Inverse Trigonometric Functions and Logarithm Function

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 Elements of Matrix

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 Between Sine and Cosine Functions

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`
Symbolic unitUnitsTarget unit

Unit of measurement, specified as a symbolic unit, or an array of symbolic units.

Unit system, specified as a string or character vector. By default, the SI, CGS, and US unit systems are available. You can also define custom unit systems. See Unit Conversions and Unit Systems.

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 single quotes (`' '`). You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

Example: `rewrite(23*u.Celsius,u.K,'Temperature','absolute')`

collapse all

Assume that temperatures represent absolute temperatures or temperature differences, specified as `'difference'` or `'absolute'`. The `Temperature` argument only affects conversion between units of temperature.

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.