MuPAD® notebooks are not recommended. 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.
When creating new objects in MuPAD®, the best practice is to consider which domain the object must belong to. Choosing the correct data type from the beginning helps you avoid unnecessary conversions. Nevertheless, some computation tasks require data types conversions. For such tasks, MuPAD enables you to convert the elements of some domains to elements of other domains.
To convert an object to a different domain type, use one of the following alternatives. Some of the alternatives do not work for conversions between particular domains.
Use the coerce Function. Call the
coerce function to convert an object
to an element of a specified domain. The
coerce function can call the
coerce domain methods. If the conversion is
not possible or if none of these methods is implemented for the domains
participating in conversion, the
coerce function returns
Use the expr Function. Call the
expr function to convert an object to
an element of a basic domain. The
expr function calls the
If the conversion is not possible or if the
is not implemented for the specified domains, the
expr function returns
Use Constructors. Call the domain constructor of the domain to which you want to convert an object. This approach typically works, but it can fail in some cases.
Calling the conversion methods of a domain directly.
To use this approach, you must know which conversion methods are implemented
for the participating domains. This alternative is not recommended.
expr function instead.
If you implement a new domain, consider implementing the conversion methods for that domain.
L := [1, 2, 3, 4, 5, 6]; S := coerce(L, DOM_SET)
Results of the conversion can depend on the original domain type. For example, create an array and a matrix with the same number of elements and equal dimensions:
M := matrix(2, 3, [1, 2, 3, 4, 5, 6]); A := array(1..2, 1..3, [1, 2, 3, 4, 5, 6])
Verify that matrix
M belongs to the domain
A belongs to the domain
to convert both objects to elements of the domain
coerce function converts
M to a nested list, where the inner lists
represent the rows of the original matrix. At the same time,
coerce converts the
A to a flat list:
LM := coerce(M, DOM_LIST); LA := coerce(A, DOM_LIST)
Both new objects belong to the basic domain of lists
For further computations, delete the identifiers
delete L, S, M, A, LM, LA
To convert an element of a library domain to an element of a
basic kernel domain, use the
This function does not allow you to select the basic domain to which
it converts an object. If the conversion to basic domains is not possible,
the function returns
tries to find the simplest basic domain to which it can convert a
given object. The function also can convert an element of a more complicated
basic domain to the simpler basic domain. For example, it converts
the polynomial represented by a single variable
y := poly(x); expr(y)
The original object
y belongs to the domain
of polynomials. The result of conversion belongs to the domain of
If you call the
for a more complicated polynomial, the function converts that polynomial
to the expression:
p := poly(x^2 + x + 2); expr(p)
Again, the original polynomial belongs to the domain of polynomials. This time, the result of the conversion belongs to the domain of expressions:
MuPAD can apply the
expr function to an object recursively.
If an object contains terms that belong to library domains or complicated
tries to convert those terms elements of simpler basic domains. For
example, if the polynomial
p is an element of a
list, applying the
to the list converts the polynomial
p to the expression:
matrix(2, 2, [p, 0, 0, 1]); expr(matrix(2, 2, [p, 0, 0, 1]))
expr function converts a matrix of the
Dom::Matrix() to an array of the
basic kernel domain
M := matrix(2, 3, [1, 2, 3, 4, 5, 6])
expr function converts an element of
the series domain to the expression of a basic domain
Typically, the order of terms in the resulting expression changes:
s := series(exp(x), x); expr(s)
For further computations, delete the identifiers
delete y, p, M, s
Instead of calling the conversion functions, you can use the constructor of a domain to which you want to convert an object. This approach works for most conversions, but it can fail in some cases.
Calling the constructor of the required domain is often the
simplest way to convert an object to the element of that domain. Suppose
you want to convert the following list
L to a matrix:
L := [1, 2, 3, 4, 5, 6]
To convert a list to a matrix, call the matrix constructor
matrix. Use the arguments
of the matrix constructor to specify the dimensions of the required
matrix and the list
L of the elements of a matrix.
The resulting object is the 2×3 matrix
of the domain
matrix(2, 3, L); domtype(%)
Alternatively, you can convert a list to a matrix by using
Dom::Matrix()(2, 3, L); domtype(%)
For further computations, delete the identifier