Create a matrix or a vector
For matrices in MATLAB^{®}, see Matrices and Arrays.
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.
matrix(Array
) matrix(List
) matrix(ListOfRows
) matrix(Matrix
) matrix(m
,n
) matrix(m
,n
,Array
) matrix(m
,n
,List
) matrix(m
,n
,ListOfRows
) matrix(m
,n
,Table
) matrix(m
,n
,[(i_{1}, j_{1}) = value_{1}, (i_{2}, j_{2}) = value_{2}, …]
) matrix(m
,n
,f
) matrix(m
,n
,List
, Diagonal) matrix(m
,n
,g
, Diagonal) matrix(m
,n
,List
, Banded) matrix(1,n
,[j_{1} = value_{1}, j_{2} = value_{2}, …]
) matrix(m
,1
,[i_{1} = value_{1}, i_{2} = value_{2}, …]
)
matrix(m, n, [[a11, a12, ...], [a21, a22, ...], ...])
returns
an m×n matrix
of the domain
type Dom::Matrix()
.
matrix(m, n, [a11, a12, ..., a21, a22, ..., a.m.n])
returns
an m×n matrix
of the domain
type Dom::Matrix()
.
matrix(m, 1, [a1, a2, ...])
returns an m×1 column
vector of the domain
type Dom::Matrix()
.
matrix(1, n, [a1, a2, ...])
returns an 1
×n row vector of the domain type Dom::Matrix()
.
matrix
is equivalent to Dom::Matrix()
.
matrix
creates matrices and vectors. A column
vector is represented as an m×1 matrix.
A row vector is represented as a 1×n matrix.
Matrix and vector components must be arithmetical expressions (numbers
and/or symbolic expressions). If matrices over special component rings
are desired, use the domain constructor Dom::Matrix
with a suitable component ring.
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 matrix
, then 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 can be inverted.
Otherwise, FAIL
is
returned.
Cf. Example 1.
Many system functions accept matrices as input, such as map
, subs
, has
, zip
, conjugate
, norm
or exp
. Cf. Example 4.
Most of the functions in the MuPAD linear algebra package linalg work
with matrices. For example, the command linalg::gaussJordan(A)
performs
GaussJordan elimination on A
to transform A
to
its reduced row echelon form.
For numerical matrix computations, the corresponding functions
of the numeric
package accept matrices.
Matrix components can be extracted by the usual index operator [ ]
,
which also works for lists, arrays, and tables.
The call A[i, j]
extracts the matrix component
in the ith
row and the jth
column.
Assignments to matrix components
are performed similarly. The call A[i, j] := c
replaces
the matrix component in the ith
row and the jth
column of A by c
.
If one of the indices is not in its valid range, an error message is issued.
The index operator also extracts submatrices. The call A[r1..r2,
c1..c2]
creates the submatrix of A comprising
the rows with the indices r_{1}, r_{1} +
1, …, r_{2} and
the columns with the indices c_{1}, c_{1} +
1, …, c_{2} of A.
matrix(Array)
or matrix(Matrix)
create
a new matrix with the same dimension and the components of Array
or Matrix
,
respectively. The array must not contain
any uninitialized entries. If Array
is onedimensional,
the result is a column vector. Cf. Example 8.
matrix(List)
creates an m×1 column
vector with components taken from the nonempty list,
where m is
the number of entries of List
. Cf. Example 5.
matrix(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 nonzero.
If a row has less than n entries, the remaining entries in the corresponding row of the matrix are regarded as zero. Cf. Example 7.
The call matrix(m, n)
returns the m×n zero
matrix.
The call matrix(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 matrices of dimension 1
×6, or 2×3,
or 3×2, or 6
×1.
matrix(m, n, List)
creates an m×n matrix
with components taken row after row from the nonempty list. The list must contain m n entries.
Cf. Example 7.
matrix(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 a row has less than n
entries, the remaining
components of the corresponding row of the matrix are regarded as
zero. If there are less than m
rows, the remaining
lower rows of the matrix are filled with zeroes. Cf. Example 7.
matrix(m,n,Table)
creates an m×n matrix
with components taken from the table Table
.
The table entries Table[i,j]
with positive integer
values of i
and j
define the
corresponding entries of the matrix. Zero entries need not be specified
in the table. This way, sparse table input can be used to create the
matrix.
For large sparse matrices, the fastest way of creation is the
generation of an empty table that is filled by indexed assignments
and then passed to matrix
. Alternatively, one may
first create an empty sparse matrix via matrix(m, n)
and
then fill in the nonzero entries via indexed assignments. Note that
the indexed assignment to a matrix is somewhat slower than the indexed
assignment to a table.
matrix(m, n, [(i1, j1) = value1, (i2, j2) = value2,
...])
is a further way to create a matrix specifying only
the nonzero entries A[i1, j1] = value1
, A[i2,
j2] = value2
etc. The ordering of the entries in the input
list is irrelevant.
matrix(m, n, f)
returns the matrix whose (i, j)th
component is the return value of the function call f(i,j)
.
The row index i runs
from 1 to m and
the column index j from 1 to n.
Cf. Example 9.
matrix(m, 1, Array)
returns the m×1 column
vector with components taken from Array
. The array
or hfarray
Array
must
have m
entries.
matrix(m, 1, List)
returns the m×1 column
vector with components taken from List
. The list List
must
have no more than m
entries. If there are fewer
entries, the remaining vector components are regarded as zero. Cf. Example 5.
matrix(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. Cf. Example 6.
matrix(m, 1, [i1 = value1, i2 = value2, ...])
provides
a way to create a sparse column vector specifying only the nonzero
entries A[i1] = value1
, A[i2] = value2
etc.
The ordering of the entries in the input list is irrelevant.
matrix(1, n, Array)
returns the 1
×n row vector with components
taken from Array
. The array
or hfarray
Array
must
have n
entries.
matrix(1, n, List)
returns the 1
×n row vector with components
taken from List
. The list List
must
not have more than n
entries. If there are fewer
entries, the remaining vector components are regarded as zero. Cf. Example 5.
matrix(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. Cf. Example 6.
matrix(1, n, [j1 = value1, j2 = value2, ...])
provides
a way to create a sparse row vector specifying only the nonzero entries A[j1]
= value1
, A[j2] = value2
etc. The ordering
of the entries in the input list is irrelevant.
Note: The number of rows and columns, respectively, of a matrix must be less than 2^{31}. 
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. 
We create a 2×2 matrix
by passing a list of two rows to matrix
, where
each row is a list of two elements:
A := matrix([[1, 5], [2, 3]])
In the same way, we generate the following 2 ×3 matrix:
B := matrix([[1, 5/2, 3], [1/3, 0, 2/5]])
We can do matrix arithmetic using the standard arithmetical operators of MuPAD. For example, the matrix product A B, the fourth power of A, and the scalar multiplication of A by are given by:
A * B, A^4, 1/3 * A
Since the dimensions of the matrices A and B differ, the sum of A and B is not defined and MuPAD returns an error message:
A + B
Error: The dimensions do not match. [(Dom::Matrix(Dom::ExpressionField()))::_plus]
To compute the inverse of A, enter:
1/A
If a matrix is not invertible, the result of this operation
is FAIL
:
C := matrix([[2, 0], [0, 0]])
C^(1)
delete A, B, C:
In addition to standard matrix arithmetic, the library linalg offers
numerous functions handling matrices. For example, the function linalg::rank
determines
the rank of a matrix:
A := matrix([[1, 5], [2, 3]])
linalg::rank(A)
The function linalg::eigenvectors
computes
the eigenvalues and the eigenvectors of A
:
linalg::eigenvectors(A)
To determine the dimension of a matrix, use the function linalg::matdim
:
linalg::matdim(A)
The result is a list of two positive integers, the row and column number of the matrix.
Use info(linalg)
to obtain a list of available
functions, or enter ?linalg
for details about this
library.
delete A:
Matrix entries can be accessed with the index operator [ ]
:
A := matrix([[1, 2, 3, 4], [2, 0, 4, 1], [1, 0, 5, 2]])
A[2, 1] * A[1, 2]  A[3, 1] * A[1, 3]
You can redefine a matrix entry by assigning a value to it:
A[1, 2] := a^2: A
The index operator can also be used to extract submatrices. The following call creates a copy of the submatrix of A comprising the second and the third row and the first three columns of A:
A[2..3, 1..3]
The index operator does not allow to replace
a submatrix of a given matrix by another matrix. Use linalg::substitute
to
achieve this.
delete A:
Some system functions can be applied to matrices. For example,
if you have a matrix with symbolic entries and want to have all entries
in expanded form, simply apply the function expand
:
delete a, b: A := matrix([ [(a  b)^2, a^2 + b^2], [a^2 + b^2, (a  b)*(a + b)] ])
expand(A)
You can differentiate all matrix components with respect to some indeterminate:
diff(A, a)
The following command evaluates all matrix components at a given point:
subs(A, a = 1, b = 1)
Note that the function subs
does
not evaluate the result of the substitution. For example, we define
the following matrix:
A := matrix([[sin(x), x], [x, cos(x)]])
Then we substitute x = 0 in each matrix component:
B := subs(A, x = 0)
You see that the matrix components are not evaluated completely.
For example, if you enter sin(0)
directly, it evaluates
to zero.
The function eval
can
be used to evaluate the result of the function subs
.
However, eval
does
not operate on matrices directly, and you must use the function map
to apply the function eval
to
each matrix component:
map(B, eval)
The function zip
can
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 := matrix([[4, 2], [9, 3]]): B := matrix([[2, 1], [3, 1]]): A, B, zip(A, B, `/`)
delete A, B:
A vector is either an m×1 matrix
(a column vector) or a 1×n matrix
(a row vector). To create a vector with matrix
,
pass the dimension of the vector and a list of vector components as
argument to matrix
:
row_vector := matrix(1, 3, [1, 2, 3]); column_vector := matrix(3, 1, [1, 2, 3])
If the only argument of matrix
is a nonnested
list or a onedimensional array, the result is a column vector:
matrix([1, 2, 3])
For a row vector r
, the calls r[1,
i]
and r[i]
both return the ith
vector component of r
. Similarly, for a column
vector c
, the calls c[i, 1]
and c[i]
both
return the ith
vector component of c
.
We extract the second component of the vectors defined above:
row_vector[2] = row_vector[1, 2], column_vector[2] = column_vector[2, 1]
Use the function linalg::vecdim
to
determine the number of components of a vector:
linalg::vecdim(row_vector), linalg::vecdim(column_vector)
The number of components of a vector can also be determined
directly by the call nops(vector)
.
The dimension of a vector can be determined as described above in the case of matrices:
linalg::matdim(row_vector), linalg::matdim(column_vector)
See the linalg package for functions working with
vectors, and the help page of norm
for computing vector norms.
delete row_vector, column_vector:
A vector is either an m×1 matrix
(a column vector) or a 1×n matrix
(a row vector). To create a vector with matrix
,
one may also pass the dimension of the vector and a table of vector
components as argument to matrix
:
delete v1, v2, t1, t2: t1 := table(): t1[1,1] := 1: t1[1,2] := 2: t1[1,3] := 3: v1 := matrix(1, 3, t1);
t2 := table(): t2[1,1] := 1: t2[2,1] := 2: t2[3,1] := 3: v2 := matrix(3, 1, t2);
All functions applied to the vectors in the previous example (see above) can can also be used on these vectors.
delete t1, t2, v1, v2:
In the following examples, we illustrate various calls of matrix
as
described above. We start by passing a nested list to matrix
,
where each inner list corresponds to a row of the matrix:
matrix([[1, 2], [2]])
The number of rows of the created matrix is the number of inner lists, namely m = 2. The number of columns is determined by the maximal number of entries of an inner list. In the example above, the first list is the longest one, and hence n = 2. The second list has only one element and, therefore, the second entry in the second row of the returned matrix was set to zero.
In the following call, we use the same nested list, but in addition pass two dimension parameters to create a 4×4 matrix:
matrix(4, 4, [[1, 2], [2]])
In this case, the dimension of the matrix is given by the dimension parameters. As before, missing entries in an inner list correspond to zero, and in addition missing rows are treated as zero rows.
If the dimension m×n of the matrix is stated explicitly, the entries may also be specified by a plain list with m n elements. The matrix is filled with these elements row by row:
matrix(2, 3, [1, 2, 3, 4, 5, 6])
matrix(3, 2, [1, 2, 3, 4, 5, 6])
A one or twodimensional array of arithmetical expressions, such as:
a := array(1..3, 2..4, [[1, 1/3, 0], [2, 3/5, 1/2], [3/2, 0, 1]] )
can be converted into a matrix as follows:
A := matrix(a)
Arrays serve, for example, as an efficient structured data type for programming. However, arrays do not have any algebraic meaning, and no mathematical operations are defined for them. If you convert an array into a matrix, you can use the full functionality defined for matrices as described above. For example, let us compute the matrix 2 A  A^{2} and the Frobenius norm of A:
2*A  A^2, norm(A, Frobenius)
Note that an array may contain uninitialized entries:
b := array(1..4): b[1] := 2: b[4] := 0: b
matrix
cannot handle arrays that have uninitialized
entries, and responds with an error message:
matrix(b)
Error: Cannot define a matrix over 'Dom::ExpressionField()'. [(Dom::Matrix(Dom::ExpressionField()))::new]
We initialize the remaining entries of the array b
and
convert it into a matrix, or more precisely, into a column vector:
b[2] := 0: b[3] := 1: matrix(b)
delete a, A, b:
We show how to create a matrix whose components are defined
by a function of the row and the column index. The entry in the ith
row and the jth
column of a Hilbert matrix (see also linalg::hilbert
) is
.
Thus the following command creates a 2×2 Hilbert
matrix:
matrix(2, 2, (i, j) > 1/(i + j  1))
The following two calls produce different results. In the first
call, x
is regarded as an unknown function, while
it is a constant in the second call:
delete x: matrix(2, 2, x), matrix(2, 2, (i, j) > x)
Diagonal matrices can be created by passing the option Diagonal
and
a list of diagonal entries:
matrix(3, 4, [1, 2, 3], Diagonal)
One can generate the 3×3 identity matrix as follows:
matrix::identity(3)
Here are alternative ways to create this matrix:
matrix(3, 3, [1 $ 3], Diagonal)
Equivalently, you can use a function of one argument:
matrix(3, 3, i > 1, Diagonal)
Since the integer 1
also represents a constant
function, the following shorter call creates the same matrix:
matrix(3, 3, 1, Diagonal)
To demonstrate the use of tables for creating (sparse) matrices we can also create the identity matrix above by the lines:
t := table(): t[1, 1] := 1: t[2, 2] := 1: t[3, 3] := 1: matrix(3, 3, t)
delete t:
Banded Toeplitz matrices can be created with the option Banded
.
The following command creates a tridiagonal matrix with constant
bands:
matrix(4, 4, [1, 2, 1], Banded)
Matrices can also be created by using a table
:
t := table(): t[1, 2] := 12: t[3, 1] := 31: t[3, 2] := 32: t
The missing table entries correspond to empty matrix entries:
A := matrix(4, 6, t)
By using tables, one can easily create large (sparse) matrices without being forced to define all zero entries of the matrix. Note that this is a great advantage over using arrays where every component has to be initialized before.
delete t, A:
The method "doprint"
of Dom::Matrix()
prints
only the nonzero components of a sparse matrix:
A := matrix(4, 6): A[1, 2]:= 12: A[3, 1]:= 31: A[3, 2]:= 32: print(A::dom::doprint(A)):
delete A:
 
 

A nested list of rows, each row being a list of arithmetical expressions 

A matrix, i.e., an object of a data type of category 

A table of matrix components 

The number of rows: a positive integer 

The number of columns: a positive integer 

A function or a functional expression of two arguments 

A function or a functional expression of one argument 

Row indices: integers between 1 and m 

Column indices: integers between 1 and m 

Matrix entries: arithmetical expressions 

Create a diagonal matrix With this option, diagonal matrices can be created with diagonal elements taken from a list, or computed by a function or a functional expression.


Create a banded Toeplitz matrix A banded matrix has zero entries outside the main diagonal and some of the adjacent sub and superdiagonals.
All elements of the main diagonal of the created matrix are
initialized with the middle element of Cf. Example 11. 
Matrix of the domain
type Dom::Matrix()
.