Documentation

# `rationalize`

Transform an expression into a rational expression

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

## Syntax

```rationalize(`object`, `options`)
```

## Description

`rationalize(object)` transforms the expression `object` into an equivalent rational expression by replacing non-rational subexpressions by newly generated variables.

By default, a rational expression is an expression that contains only sums, products, powers with integer exponents, integers, rational numbers, and identifiers as subexpressions.

The `rationalize` function returns a sequence ```(rat, subsSet)```. The rationalized expression `rat` contains new variables. The set of substitutions `subsSet` expresses the new variables by the old ones.

If the original expression contains subexpressions, the `rationalize` function can rationalize or replace subexpressions or keep them in their original form. Use the options `DescendInto`, `ReplaceType`, and `StopOn` to control the action `rationalize` takes for particular types of subexpressions.

If `FindRelations = ["exp", "_power", "sin"]`, the `rationalize` function detects maximal number of algebraic dependencies.

If you call `rationalize` with any combination of the following three contradicting options, the function chooses the option using the following priorities: `ReplaceType`, `StopOn`, `DescendInto`. For example, if you specify the same type of subexpression with `StopOn` and `DescendInto`, the `rationalize` function uses only the `StopOn` option for subexpressions of the specified type. If you combine any of these options with the `ReplaceType` option, `rationalize` uses only the `ReplaceType` option.

## Examples

### Example 1

`rationalize` operates on single arithmetical expressions, lists, and sets of expressions:

`rationalize(2*sqrt(3) + 0.5*x^3)`
` `
```rationalize([(x - sqrt(2))*(x^2 + sqrt(3)), (x - sqrt(2))*(x - sqrt(3))])```
` `

### Example 2

Use the `ApproximateFloats` option to replace all floating-point numbers with rational numbers:

`rationalize([0.4, 0.333, 0.74], ApproximateFloats)`
` `

If you use both `ApproximateFloats` and `ReplaceTypes` options, `ApproximateFloats` does not apply to the types of subexpressions specified in `ReplaceTypes`:

```rationalize(0.4*x^2 + sin(0.33/x), ApproximateFloats, ReplaceTypes={DOM_FLOAT})```
` `

Instead of specifying the value of `ReplaceTypes` as a sequence of types, you can specify it as a function. The function must return `TRUE` or `FALSE` as a result. For example, rationalize the same expression . This time, use the function `F` to specify the type of subexpressions which you want to replace by variables:

```F := X -> testtype(X, DOM_FLOAT): rationalize(0.4*x^2 + sin(0.33/x), ApproximateFloats, ReplaceTypes = F)```
` `

### Example 3

By default, `rationalize` rationalizes sums, products, bases of integer powers, lists, and sets:

`rationalize(ln(sin(x)^2 + cos(x)*exp(x)))`
` `

The `DescendInto` option lets you specify the types of subexpressions that you want to rationalize. Each type can be a domain type, a string as returned by the function `type` or a Type object. Note that `DescendInto` overwrites the default types with the types that you specify:

`rationalize(ln(sin(x)^2 + cos(x)*exp(x)), DescendInto = {"ln"})`
` `

If you want to add new types of subexpressions to the default ones, define the value of `DescendInto` as a procedure that specifies all required types explicitly. The procedure must return `TRUE` or `FALSE`:

```F := proc(X) begin hastype(X, {"_plus", "_mult", DOM_SET, DOM_LIST, "ln"}) or (hastype(X, "_power") and hastype(op(X, 2), DOM_INT)) end: rationalize(ln(sin(x)^2 + cos(x)*exp(x)), DescendInto = F)```
` `

### Example 4

Use the `MinimalPolynomials` option to find minimal polynomials of irrational expressions:

`rationalize(x^(7/6) + x^(3/2), MinimalPolynomials)`
` `

### Example 5

Use `Prefix = s`, where `s` is a string, to specify the prefix for generated variables (the default prefix is `X`):

`rationalize(x^(7/6) + x^(3/2), Prefix = "ABC")`
` `

### Example 6

Use the `ReplaceHardToEval` option to replace limits, sums, and integrals with generated variables. Expressions with limits, sums, and integrals tend to be the most computationally expensive:

```rationalize(sum(exp(x)/(x^2 + 1), x) + limit(sin(cos(1/x))*cos(1/x), x), ReplaceHardToEval)```
` `

### Example 7

By default, `rationalize` avoids rationalization of integers, rational numbers, and identifiers:

`rationalize(2*sqrt(3) + 0.5*x^3)`
` `

The `DescendInto` option lets you avoid rationalization of particular types of subexpressions. Each type can be specified as a domain type, a string as returned by the function `type`, or a Type object. For example, rationalize the same expression leaving the subexpression `x^3` (of the type `"_power"`) unchanged:

`rationalize(2*sqrt(3) + 0.5*x^3, StopOn = {"_power"})`
` `

Rationalize the same expression including all subexpressions. Keep floating-point numbers, integers, and identifiers (do not replace them with generated variables):

```rationalize(2*sqrt(3) + 0.5*x^3, StopOn = {DOM_FLOAT, DOM_INT, DOM_IDENT})```
` `

Note that `StopOn` overwrites the default types with the types that you specify. If you want to add new types of subexpressions to the default ones, specify all the types explicitly:

```rationalize(2*sqrt(3) + 0.5*x^3, StopOn = {DOM_INT, DOM_IDENT, DOM_RAT, DOM_FLOAT})```
` `
```rationalize(2*sqrt(3) + 0.5*x^3, StopOn = {DOM_INT, DOM_IDENT, DOM_RAT, DOM_FLOAT, "_power"})```
` `

The `StopOn` option also can accept a function as its value. The function must return `TRUE` or `FALSE`. For example, use generated variables to replace only subexpressions that contain `sin`. Keep all other subexpressions intact:

```F := X -> not hastype(X, "sin"): rationalize(sin(x^2) + x^3 + exp(x) + 1/x, StopOn = F)```
` `

### Example 8

Use the `FindRelations` option to detect algebraic dependencies between exponentials:

`rationalize(exp(x/2) + exp(x/3), FindRelations = ["exp"])`
` `

Detect algebraic dependencies for different powers of the same base by specifying the type `"_power"`:

`rationalize(x^(3/2) + x^(7/4), FindRelations = ["_power"])`
` `

Detect algebraic dependencies for trigonometric functions by specifying the type `"sin"` or `"cos"`:

```rationalize(sin(x) + cos(x), FindRelations = ["sin"]); rationalize(sin(x)^3 + cos(x)^3, FindRelations = ["cos"])```
` `
` `

### Example 9

For nested exponentials, use the `Recursive` option to obtain a list of substitutions:

`rationalize(exp(exp(x)), FindRelations = ["exp"], Recursive)`
` `

The option also works for trigonometric functions:

`rationalize(sin(sin(x)), FindRelations = ["sin"], Recursive)`
` `

### Example 10

The `ShowDependencies` option shows all original variables upon which each generated variable depends:

`rationalize(sin(x)^3, ShowDependencies)`
` `

## Parameters

 `object` Any MuPAD® object

## Options

 `ApproximateFloats` When you use the `ApproximateFloats` option, the `rationalize` function replaces floating-point numbers with rational numbers. By default, `ApproximateFloats````= FALSE```: the `rationalize` function replaces all floating-point numbers with the new variables. If you rationalize an expression using both `ApproximateFloats` and `StopOn` options, `StopOn` does not prevent rationalization of floating-point numbers in the specified subexpressions. If you rationalize an expression using both `ApproximateFloats` and `ReplaceTypes` options, `ApproximateFloats` does not apply to the types of subexpressions specified in `ReplaceTypes`. See Example 2. `DescendInto` When you use the `DescendInto` option, the `rationalize` function rationalizes all subexpressions of the specified types. You can specify the value of this option as a set (even if there is only one type) or a procedure that returns `TRUE` or `FALSE`. Each type can be A domain type (such as `DOM_INT`, `DOM_EXPR`, and so on)A string as returned by the function `type` (such as `"_plus"`, `"_mult"`, `"sin"`, and so on)A Type object (`Type::Boolean`, `Type::Equation`, and so on) By default, the `rationalize` function rationalizes the following types of subexpresssions: sums, products, bases of integer powers, lists, and sets. When you specify other types of subexpressions, `rationalize` uses them instead of the default types. (`DescendInto` overwrites the default types with the types that you specify.) If you want to extend the set of types of subexpressions retaining the default types, define the value of `DescendInto` as a procedure that specifies all default and additional types explicitly. See Example 3. `FindRelations` When you use the `FindRelations` option, the `rationalize` function detects algebraic dependencies for subexpressions of specified types. This option accepts the types of subexpressions in the form of a list. The following types are available: `"sin"`, `"cos"`, `"exp"`, and `"_power"`. By default, `rationalize` does not look for dependencies for irrational subexpressions: `FindRelations````= []```. `MinimalPolynomials` When you use the `MinimalPolynomials` option, the `rationalize` function returns the minimal polynomials of irrational expressions. The function returns the rationalized expression, the set of substitution equations, and minimal polynomials. By default, `MinimalPolynomials````= FALSE```. See Example 4. `Prefix` Use the `Prefix` option to specify the prefix for new variables generated by the `rationalize` function. The value of this option must be a string. By default, `Prefix````= "X"```. See Example 5. `Recursive` When you use the `Recursive` option, the `rationalize` function recursively rationalizes nested subexpressions, and returns a list of substitution equations. Each generated variable in the returned list can depend on other variables in the list. By default, `Recursive````= FALSE```. See Example 9. `ReplaceHardToEval` When you use the `ReplaceHardToEval` option, the `rationalize` function replaces all limits, sums, and integrals by generated variables. Generally, this option allows you to avoid most expensive rationalizations of sums, limits, and integrals. By default, `ReplaceHardToEval````= FALSE```. See Example 6. `ReplaceTypes` When you use the `ReplaceTypes` option, the `rationalize` function replaces all subexpressions of the specified types with generated variables. You can specify the value of this option as a set (even if there is only one type) or a procedure that returns `TRUE` or `FALSE`. Each type can be A domain type (such as `DOM_INT`, `DOM_EXPR`, and so on)A string as returned by the function `type` (such as `"_plus"`, `"_mult"`, `"sin"`, and so on)A Type object (`Type::Boolean`, `Type::Equation`, and so on) This option allows you to specify and avoid most expensive rationalizations for your particular expression. If you use this option in combination with `ReplaceHardToEval`, the `rationalize` function uses generated variables to replace all limits, sums, integrals, and the types that you specify. If `ReplaceTypes` specifies the same type of subexpression as `DescendInto`, the `ReplaceTypes` option prevails. By default, `ReplaceTypes````= {}```. Alternatively, specify the value of this option as a function that returns `TRUE` or `FALSE`. See Example 2. `ShowDependencies` When you use the `ShowDependencies` option, the `rationalize` function replaces any irrational subexpression containing the identifiers `vars` with an expression of the form `newvar(vars)`, showing the dependencies of the generated variables on the original variables. By default, `ShowDependencies``= FALSE`. `StopOn` When you use the `StopOn` option, the `rationalize` function does not rationalize the specified types of subexpressions. You can specify the value of this option as a set (even if there is only one type) or a function that returns `TRUE` or `FALSE`. Each type can be A domain type (such as `DOM_INT`, `DOM_EXPR`, and so on)A string as returned by the function `type` (such as `"_plus"`, `"_mult"`, `"sin"`, and so on)A Type object (`Type::Boolean`, `Type::Equation`, and so on) By default, the `rationalize` function does not rationalize or replace integers, rational numbers, and identifiers. When you specify other types of subexpressions, `rationalize` uses them instead of the default types. (`StopOn` overwrites the default types with the types that you specify.) If you want to extend the set of types of subexpressions retaining the default types, specify `StopOn = {DOM_INT, DOM_IDENT, DOM_RAT, extra types}`, where `extra types` are the additional types of subexpressions that you do not want to rationalize. See Example 7. If `StopOn` specifies the same type of subexpression as `DescendInto`, the `StopOn` option prevails. `StopOnConstants` When you use the `StopOnConstants` option, the `rationalize` function does not rationalize the object of the type `Type::Constant`: numbers, strings, Boolean constants, `NIL`, `FAIL`, PI, EULER, and CATALAN in the set `Type::ConstantIdents`. By default, `StopOnConstants``= FALSE`.

## Return Values

Sequence consisting of the rationalized object and a set of substitution equations. If you use the `Recursive` option, the `rationalize` function returns a list of substitution equations instead of a set. If you use the `MinimalPolynomials` option, the returned value has a third argument: the minimal polynomials.