Documentation Center |
Matrices
Dom::Matrix(<R>)
Dom::Matrix(R)(Array)
Dom::Matrix(R)(List)
Dom::Matrix(R)(ListOfRows)
Dom::Matrix(R)(Matrix)
Dom::Matrix(R)(m, n)
Dom::Matrix(R)(m, n, Array)
Dom::Matrix(R)(m, n, List)
Dom::Matrix(R)(m, n, ListOfRows)
Dom::Matrix(R)(m, n, Table)
Dom::Matrix(R)(m, n, [(i_{1}, j_{1}) = value_{1}, (i_{2}, j_{2}) = value_{2}, …])
Dom::Matrix(R)(m, n, f)
Dom::Matrix(R)(m, n, List, Diagonal)
Dom::Matrix(R)(m, n, g, Diagonal)
Dom::Matrix(R)(m, n, List, Banded)
Dom::Matrix(R)(1, n, Array)
Dom::Matrix(R)(1, n, List)
Dom::Matrix(R)(1, n, Table)
Dom::Matrix(R)(1, n, [j_{1} = value_{1}, j_{2} = value_{2}, …])
Dom::Matrix(R)(m, 1, Array)
Dom::Matrix(R)(m, 1, List)
Dom::Matrix(R)(m, 1, Table)
Dom::Matrix(R)(m, 1, [i_{1} = value_{1}, i_{2} = value_{2}, …])
Dom::Matrix(R) creates domains of matrices over a component domain R of category Cat::Rng (a ring, possibly without unit).
If the optional parameter R is not given, Dom::ExpressionField() is used as component domain. Matrices of this type accept arbitrary MuPAD^{®} expressions (numbers, symbols etc.) as entries. The name matrix is an alias for this default matrix domain Dom::Matrix().
A vector with n entries is either an n×1 matrix (a column vector), or a 1×n matrix (a row vector).
Arithmetical operations with matrices can be performed by using the standard arithmetical operators of MuPAD.
E.g., if A and B are two matrices defined by Dom::Matrix(R), A + B computes the sum, and A * B computes the product of the two matrices, provided that the dimensions are appropriate.
Similarly, A^(-1) or 1/A computes the inverse of a square matrix A if it exists. Otherwise, FAIL is returned. See Example 1.
Many system functions are overloaded for matrices, such as map, subs, has, zip, E.g., use conjugate to compute the complex conjugate of a matrix, norm to compute matrix norms, or exp to compute the exponential of a matrix.
Most of the functions in the MuPAD linear algebra package linalg work with matrices. For example, the command linalg::gaussJordan(A) performs Gauss-Jordan elimination on A to transform A to its reduced row echelon form.
See the documentation of linalg for a list of available functions of this package.
The domain Dom::Matrix(R) represents matrices over R of arbitrary size. Therefore, it does not have any algebraic structure (other than being a set of matrices).
In this help page, we use the following notations for a matrix A (an element of Dom::Matrix(R)):
nrows(A) denotes the number of rows of A.
ncols(A) denotes the number of columns of A.
A row index is an integer in the range from 1 to nrows(A).
A column index is an integer in the range from 1 to ncols(A).
Note: The components of a matrix are no longer evaluated after the creation of the matrix, i.e., if they contain free identifiers they will not be replaced by their values. |
Dom::Matrix(R)(Array) and Dom::Matrix(R)(Matrix) create a new matrix with the dimension and the components of Array and Matrix, respectively.
The components of Array or Matrix are converted to elements of the domain R. An error message is issued if one of these conversions fails.
The creation of (sparse) matrices via arrays is useful for matrices of moderate size. Note that indexed assignments to arrays are much faster than the corresponding indexed assignments to matrices. However, since all elements of the array (including the zeroes) need to be filled in before conversion to a (sparse) matrix, memory is wasted for very large and very sparse matrices. In such a situation, one should define a table containing only the non-zero elements and convert the table to a matrix (see below).
Dom::Matrix(R)(List) creates an m×1 column vector with components taken from the nonempty list, where m is the number of entries of List.
One may also use a list of equations to create an object of Dom::Matrix. In this case the entries of the list must be of the form (i, j) = value, where i and j denote the row and column index and value the coefficient of the matrix. i and j need to be positive integers.
Dom::Matrix(R)(ListOfRows) creates an m×n matrix with components taken from the nested list ListOfRows, where m is the number of inner lists of ListOfRows, and n is the maximal number of elements of an inner list. Each inner list corresponds to a row of the matrix. Both m and n must be non-zero.
If an inner list has less than n entries, the remaining components in the corresponding row of the matrix are set to zero.
The entries of the inner lists are converted to elements of the domain R. An error message is issued if one of these conversions fails.
The call Dom::Matrix(R)(m, n) returns the m×n zero matrix.
Use the method "identity" to create the n×n identity matrix.
The call Dom::Matrix(R)(m, n, Array) creates an m×n matrix with components taken from Array, which must be an array or an hfarray. Array must have m n operands. The first m operands define the first row, the next m operands define the second row, etc. The formatting of the array is irrelevant. E.g., any array with 6 elements can be used to create a matrix of dimension 1 ×6, or 2×3, or 3×2, or 6 ×1.
Dom::Matrix(R)(m, n, List) creates an m×n matrix with components taken row after row from the non-empty list. The list must contain m n entries.
Dom::Matrix(R)(m, n, ListOfRows) creates an m×n matrix with components taken from the list ListOfRows.
If m ≥ 2 and n ≥ 2, then ListOfRows must consist of at most m inner lists, each having at most n entries. The inner lists correspond to the rows of the returned matrix.
If an inner list has less than n entries, the remaining components of the corresponding row of the matrix are set to zero. If there are less than m inner lists, the remaining lower rows of the matrix are filled with zeroes.
Dom::Matrix(R)(m, n, Table) creates an m×n matrix with components taken from the table Table.
By defining the entries of the table first, one can easily create large and sparse matrices. The entry Table[i, j] of the table will be the entry in the i-th row and the j-th column of the matrix. Therefore, the table needs to be indexed by positive integers i and j.
Dom::Matrix(R)(m, n, [(i1, j1) = value1, (i2, j2) = value2, ...]) is a further way to create a matrix specifying only the non-zero entries A[i1, j1] = value1, A[i2, j2] = value2 etc. The ordering of the entries in the input list is irrelevant.
Dom::Matrix(R)(m, n, f) returns the matrix whose (i, j)-th component is the value of the function call f(i, j). The row index i ranges from 1 to m and the column index j from 1 to n.
The function values are converted to elements of the domain R. An error message is issued if one of these conversions fails.
Dom::Matrix(R)(1, n, Array) returns the 1 ×n row vector with components taken from Array. The array or hfarray Array must have n entries.
The entries of the array are converted to elements of the domain R. An error message is issued if one of these conversions fails.
Dom::Matrix(R)(1, n, List) returns the 1 ×n row vector with components taken from List. The list List must have at most n entries. If there are fewer entries, the remaining vector components are set to zero.
The entries of the list are converted to elements of the domain R. An error message is issued if one of these conversions fails.
Dom::Matrix(R)(1, n, Table) returns the 1 ×n row vector with components taken from Table. The table Table must not have more than n entries. If there are fewer entries, the remaining vector components are regarded as zero.
Dom::Matrix(R)(m, 1, Array) returns the m×1 column vector with components taken from Array. The array or hfarray Array must have m entries.
The entries of the array are converted to elements of the domain R. An error message is issued if one of these conversions fails.
Dom::Matrix(R)(m, 1, List) returns the m×1 column vector with components taken from List. The list List must have at most m entries. If there are fewer entries, the remaining vector components are set to zero.
The entries of the list are converted to elements of the domain R. An error message is issued if one of these conversions fails.
Dom::Matrix(R)(m, 1, Table) returns the m×1 column vector with components taken from Table. The table Table must have no more than m entries. If there are fewer entries, the remaining vector components are regarded as zero.
Whenever possible, one should use Dom::ExpressionField() as the coefficient domain of matrices – therefore Dom::ExpressionField() is the default coefficient domain of matrices.
The components of matrices over Dom::ExpressionField() can be arbitrary arithmetical expressions. Consider
Mat := Dom::Matrix()
We assigned the domain to the identifier Mat and now we can define a matrix A of two rows, where each row is a list of two elements by the following line:
A := Mat([[1, 5], [2, 3]])
In the same way, we define the following 2 ×3 matrix:
B := Mat([[-1, 5/2, 3], [1/3, 0, 2/5]])
and perform matrix arithmetic using the standard arithmetical operators of MuPAD, e.g., the matrix product A B, the fourth power of A as well as the scalar multiplication of A times :
A * B, A ^ 4, 1/3 * A
The matrices A and B have different dimensions, and therefore the sum of A and B is not defined. MuPAD issues an error message:
A + B
Error: The dimensions do not match. [(Dom::Matrix(Dom::ExpressionField()))::_plus]
To compute the inverse of A, just enter:
1/A
If a matrix is not invertible, FAIL is the result of this operation. For example, the matrix:
C := Mat(2, 2, [[2]])
is not invertible, hence:
C^(-1)
delete A, B, C:
We create the domain of matrices over the coeffcient ring Dom::ExpressionField():
Mat := Dom::Matrix()
Beside standard matrix arithmetic, the library linalg offers many functions dealing with matrices. For example, if one wants to compute the rank of a matrix, use linalg::rank:
A := Mat([[1, 2], [2, 4]])
linalg::rank(A)
Use linalg::eigenvectors to compute eigenvalues and eigenvectors of the matrix A:
linalg::eigenvectors(A)
Try info(linalg) for a list of available functions, or enter help(linalg) for details about the library linalg.
Some of the functions in the linalg package simply serve as "interface" functions for methods of a matrix domain described above. For example, linalg::transpose uses the method "transpose" to get the transposed matrix. The function linalg::gaussElim applies Gaussian elimination to a matrix by calling the method "gaussElim":
linalg::gaussElim(A) = A::dom::gaussElim(A)[1]
In contrast to the methods of the domain Dom::Matrix(R), the corresponding functions of the linalg packages do extended checking of their input parameters. Note that there might be minor differences in the functionality of the linalg functions and the matrix methods. E.g., the option ColumnElimination is not available in linalg::gaussElim, but only in the "gaussElim" method of the matrix domain:
A::dom::gaussElim(A, ColumnElimination)
delete A:
We create the default matrix domain Dom::Matrix(). As a shortcut, this domain can also be created via matrix:
A := matrix([[ 1, 2, 3, 4], [ 2, 0, 4, 1], [-1, 0, 5, 2]])
domtype(A)
Matrix components can be extracted by the index operator []:
A[2, 1] * A[1, 2] - A[3, 1] * A[1, 3]
If one of the indices is not in its valid range, an error message is issued. Assignments to matrix components are performed similarly:
delete a: A[1, 2] := a^2: A
Beside the usual indexing of matrix components, it is also possible to extract submatrices from a given matrix. The following call creates the submatrix of A which consists of the rows 2 to 3 and columns 1 to 3 of A:
A[2..3, 1..3]
The index operator does not allow to insert submatrices into a given matrix. This is implemented by the function linalg::substitute.
delete A:
In the following examples, we demonstrate the different ways of creating matrices. We work with matrices defined over the field ℤ_{19}, i.e., the field of integers modulo 19. This component ring can be created with the domain constructor Dom::IntegerMod.
We start by giving a list of rows, where each row is a list of row entries:
MatZ19 := Dom::Matrix(Dom::IntegerMod(19)): MatZ19([[1, 2], [2]])
The elements of the two inner lists, the row entries, were converted to elements of the domain Dom::IntegerMod(19).
The number of rows is the number of sublists of the argument, i.e., m = 2. The number of columns is determined by the length of the inner list with the most entries, which is the first inner list with two entries. Missing entries in the other inner lists are treated as zero components. The call:
MatZ19(4, 4, [[1, 2], [2]])
fixes the dimension of the matrix. Missing entries and inner lists are treated as zero components and zero rows, respectively.
An error message is issued if one of the given entries cannot be converted to an element over ℤ_{19}:
MatZ19([[2, 3], [-1, I]])
Error: Cannot define a matrix over 'Dom::IntegerMod(19)'. [(Dom::Matrix(Dom::IntegerMod(19)))::new]
delete MatZ19:
This example illustrates how to create a matrix with components given as values of an index function. First we create the 2 ×2 Hilbert matrix (see also the functions linalg::hilbert and linalg::invhilbert):
Dom::Matrix()(2, 2, (i, j) -> 1/(i + j - 1))
Note the difference when working with expressions and functions. If you give an expression it is treated as a function in the row and column indices:
delete x: Dom::Matrix()(2, 2, x), Dom::Matrix()(2, 2, (i, j) -> x)
Diagonal matrices can be created with the option Diagonal and a list of diagonal components:
Mat := Dom::Matrix(): Mat(3, 4, [1, 2, 3], Diagonal)
Hence, to define the n×n identity matrix, you can enter:
Mat(3, 3, [1 $ 3], Diagonal)
or call:
Mat(3, 3, x -> 1, Diagonal)
The easiest way to create the identity matrix, however, is to use the method "identity":
Mat::identity(3)
delete Mat:
Toeplitz matrices can be defined with the option Banded. The following call defines a three-banded matrix with the component 2 on the main diagonal and the component - 1 on the first subdiagonal and superdiagonal:
Dom::Matrix()(4, 4, [-1, 2, -1], Banded)
Some system functions can be applied to matrices, such as norm, expand, diff, conjugate, or exp.
For example, to expand the components of the matrix:
delete a, b: A := Dom::Matrix()( [[(a - b)^2, a^2 + b^2], [a^2 + b^2, (a - b)*(a + b)]] )
enter:
expand(A)
If you want to differentiate the matrix components, then call for example:
diff(A, a)
To substitute matrix components by some values, enter:
subs(A, a = 1, b = -1)
The function zip can also be applied to matrices. The following call combines two matrices A and B by dividing each component of A by the corresponding component of B:
A := Dom::Matrix()([[4, 2], [9, 3]]): B := Dom::Matrix()([[2, 1], [3,-1]]): zip(A, B, `/`)
The quoted character `/` is another notation for the function _divide, the functional form of the division operator /.
If one needs to apply a function to the components of a matrix, then use the function map. For example, to simplify the components of the matrix:
C := Dom::Matrix()( [[sin(x)^2 + cos(x)^2, cos(x)*tan(x)], [(a^2 - b^2)/(a + b), 1]] )
call:
map(C, Simplify)
delete A, B, C:
However, there may appear some unexpected results using the function diff in the context of matrices. The derivative of the following unspecified function f of a matrix is computed due to the chain rule:
diff(f(matrix([[a*x^2, b], [c, d]])), x)
Usually, the function f would implicitly be assumed to be scalar. Hence, the derivative of f should be scalar as well. In the above situation the chain rule is applied for differentiation: the inner function is the matrix containing the symbolic components a*x^2, b, c and d. Its derivative is computed by simply applying diff to each component of the matrix:
diff(matrix([[a*x^2, b], [c, d]]), x)
Finally, the exterior unspecified function f is implicitly assumed to be scalar, such that each component of the derivative of the inner function is multiplied by the exterior differentiation.
A column vector is represented by a 2×1 matrix:
Mat := Dom::Matrix(): v := Mat(2, 1, [1, 2])
The dimension of this vector is:
Mat::matdim(v)
The length of a vector may also be queried by linalg::vecdim or nops(v):
linalg::vecdim(v)
The ith component of this vector can be extracted in two ways: either by v[i, 1] or by v[i]:
v[1], v[2]
We compute the 2-norm of v by the following call:
norm(v, 2)
delete Mat, v:
We create random matrices over the field of the rational numbers. Consider a random matrix A1 with 3 rows and 3 columns:
Mat := Dom::Matrix(Dom::Rational): A1 := Mat::random(3, 3)
A second matrix A2 should contain at most 2 non-zero entries. We can create such a matrix by using 2 as the third argument for random:
A2 := Mat::random(3, 3, 2)
The product of these matrices is given by
C := A1 * A2
By default, matrices are displayed like 'dense' arrays with zeroes in the empty places. For sparse matrices of large column and/or row dimension, such a 'dense' print mode is not appropriate: formatting of the print output would be very time consuming. Further, a 'dense' print output is not very informative for sparse matrices. For this reason, the "doprint" method provides a sparse output mode printing only the non-zero entries:
C::dom::doprint(C)
With this method, one can also print large sparse matrices. We create a random sparse matrix with 100 rows, 200 columns and at most 6 non-zero entries:
X := Mat::random(100, 200, 6): print(X)
Warning: This matrix is too large for display. To see all nonzero entries of a matrix A, use 'A::dom::doprint(A)'. [(Dom::Matrix(Dom::Rational))::print]
Warning: This matrix is too large for display. If you want to see all nonzero entries of a matrix, say A, then call 'A::dom::doprint(A)'. [(Dom::Matrix(Dom::Rational))::print]
Warning: This matrix is too large for display. If you want to see all nonzero entries of a matrix, say A, then call 'A::dom::doprint(A)'. [(Dom::Matrix(Dom::Rational))::print]
The warning speaks for itself. X is regarded as 'too large for display' since, with the default 'dense' output mode, the sparse matrix would be printed as a huge array-like structure of dimension 100×200 with (integer) zeroes in the empty places. The sparse print mode should be used:
X::dom::doprint(X)
For convenience, there is a function doprint that calls this method by just entering:
doprint(X)
delete Mat, A1, A2, C, X:
R |
A ring, i.e., a domain of category Cat::Rng. The default ring is Dom::ExpressionField(). |
Array | |
Matrix |
A matrix, i.e., an element of a domain of category Cat::Matrix |
m, n |
Matrix dimension (positive integers) |
List |
A list of matrix components |
ListOfRows |
A list of at most m rows; each row given as a list of at most n matrix components |
Table |
A table of coefficients of the matrix for sparse input |
f |
A function or a functional expression with two parameters (the row and column index) |
g |
A function or a functional expression with one parameter (the row index) |
Diagonal |
Create a diagonal matrix With the option Diagonal, diagonal matrices can be created with diagonal elements taken from a list, or computed by a function or a functional expression. Dom::Matrix(R)(m, n, List, Diagonal) creates the m×n diagonal matrix, whose diagonal elements are the entries of List. List must have at most min(m, n) entries. If it has fewer elements, the remaining diagonal elements are set to zero. The entries of List are converted to elements of the domain R. An error message is issued if one of these conversions fails. Dom::Matrix(R)(m, n, g, Diagonal) returns the sparse matrix whose ith diagonal element is g(i, i), where the index i runs from 1 to min(m, n). The function values are converted to elements of the domain R. An error message is issued if one of these conversions fails. |
Banded |
Create a Toeplitz matrix Dom::Matrix(R)(m, n, List, Banded) creates an m×n Toeplitz matrix with the elements of List as entries. The number of entries of List must be odd, say 2 h + 1, and must not exceed 2 min(m, n) - 1. The bandwidth of the resulting matrix is at most 2 h + 1. A Toeplitz matrix is a matrix where the elements of each band are identical. See also Example 7. All elements of the main diagonal of the created matrix are initialized with the middle element of List. All elements of the i-th subdiagonal are initialized with the (h + 1 - i)-th element of List. All elements of the i-th superdiagonal are initialized with the (h + 1 + i)-th element of List. All entries on the remaining sub- and superdiagonals are set to zero. The entries of List are converted to elements of the domain R. An error message is issued if one of these conversions fails. |
"isSparse" | is always TRUE. |
"randomDimen" | is set to [10, 10]. See the method "random" below for details. |