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.
rationalize(object
, options
)
rationalize(object)
transforms the expression object
into
an equivalent rational expression by replacing nonrational 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.
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))])
Use the ApproximateFloats
option to replace
all floatingpoint 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)
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)
Use the MinimalPolynomials
option to find
minimal polynomials of irrational expressions:
rationalize(x^(7/6) + x^(3/2), MinimalPolynomials)
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")
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)
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 floatingpoint 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)
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"])
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)
The ShowDependencies
option shows all original
variables upon which each generated variable depends:
rationalize(sin(x)^3, ShowDependencies)

Any MuPAD^{®} object 

When you use the 

When you use the
By default, the 

When you use the 

When you use the 

Use the 

When you use the 

When you use the 

When you use the
This option allows you to specify and avoid most expensive rationalizations
for your particular expression. If you use this option in combination
with Alternatively, specify the value of this option as a function
that returns 

When you use the 

When you use the
By default, the If 

When you use the 
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.