matrix
-- create a matrix or a
vectormatrix(
m, n, [[a11, a12, ...], [a21, a22, ...],
...])
returns the m x n matrix
+- -+ |a11 a21 ... | |a21 a22 ... | | . . . | | . . . | | . . . | +- -+
matrix(
n, 1, [a1, a2, ...])
returns the
n x 1 column vector
+- -+ | a1 | | a2 | | . | | . | | . | +- -+
matrix(
1, n, [a1, a2, ...])
returns the
1 x n row vector
+- -+ | a1, a2, ... | +- -+
matrix(ListOfRows)
matrix(List)
matrix(Array)
matrix(Matrix)
matrix(m, n)
matrix(m, n, ListOfRows)
matrix(1, n, List)
matrix(m, 1, List)
matrix(m, n, List, Diagonal)
matrix(m, n, List, Banded)
matrix(m, n, f)
matrix(m, n, g, Diagonal)
ListOfRows |
- | a nested list of at most
m rows, each row being a list with at most n
elements |
Array |
- | a one- or two-dimensional array |
Matrix |
- | a matrix, i.e., an object of a data type of category
Cat::Matrix |
m |
- | the number of rows: a positive integer |
n |
- | the number of columns: a positive integer |
List |
- | a list |
f |
- | a function or a functional expression of two arguments |
g |
- | a function or a functional expression of one argument |
Diagonal |
- | create a diagonal matrix |
Banded |
- | create a banded Toeplitz matrix |
a matrix of the domain type Dom::Matrix()
.
matrix
creates matrices and vectors. A vector with
n entries is either an n x 1 matrix (a column
vector) or a 1 x n matrix (a row vector).
Matrix and vector components must be arithmetical expressions. For specific component
domains, refer to the help page of Dom::Matrix
.
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 correct.
Similarly, A^(-1)
or 1/A
computes the
inverse of a square matrix A
if it exists. Otherwise,
FAIL
is returned.
See example 1.
map
, subs
, has
, zip
, conjugate
to compute the complex
conjugate of a matrix, norm
to compute matrix norms, or even
exp
to compute the
exponential of a matrix. See example 4.linalg
work with
matrices. For example, to compute the determinant of a square matrix
A
generated by matrix
, call linalg::det(A)
. The command linalg::gaussJordan(A)
performs Gauss-Jordan elimination on A
to transform
A
to its reduced row echelon form. Cf. example 2.
See the help page of linalg
for a list of available
functions of this package.
matrix
is an abbreviation for the domain Dom::Matrix()
. You find more
information about this data type for matrices on the corresponding help
page.[ ]
, 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, then 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 r1,r1+1,...,r2 and the
columns with the indices c1,c1+1,...,c2 of A.
matrix(
ListOfRows)
creates an m x
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 an inner list has less than n entries, then the remaining components in the corresponding row of the matrix are set to zero. See example 6.
matrix(
List)
creates an m x 1
column vector with components taken from the nonempty list, where m is the number of entries of
List
. See example 5.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 one-dimensional, then the result is a column
vector. Cf. example 7.matrix(
m, n)
returns the m
x n zero matrix.matrix(
m, n, ListOfRows)
creates an
m x 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, then the
remaining components of the corresponding row of the matrix are set to
zero. If there are less than m
inner lists, then the
remaining lower rows of the matrix are filled with zeroes. See
example 6.
matrix(
1, n, List)
returns the 1 x
n row vector with components taken from List
. The
list List
must have at most n
entries. If
there are fewer entries, then the remaining vector components are set
to zero. See example 5.matrix(
m, 1, List)
returns the m x
1 column vector with components taken from List
. The
list List
must have at most m
entries. If
there are fewer entries, then the remaining vector components are set
to zero. See example 5.matrix
(m, n, f)
returns the matrix whose
(i,j)th component is f(i,j)
. The row index
i runs from 1 to m and the column
index j from 1 to n. See
example 8.matrix(
m, n, List, Diagonal)
creates the m x n
diagonal matrix whose diagonal elements are the entries of
List
; see example 9.
List
must have at most min(m, n) entries. If
it has fewer elements, then the remaining diagonal elements are set to
zero.
matrix
(m, n, g, Diagonal)
returns the matrix whose ith
diagonal element is g(i)
, where the index i
runs from 1 to min(n,m). See example 9.A banded matrix has all entries zero outside the main diagonal and some of the adjacent sub- and superdiagonals.
matrix(
m, n, List, Banded)
creates an m x n banded
Toeplitz matrix with the elements of List
as entries. The
number of entries of List
must be odd, say
2h+1, and must not exceed n
. The bandwidth of
the resulting matrix is at most h.
All elements of the main diagonal of the created matrix are
initialized with the middle element of List
. All elements
of the ith subdiagonal are initialized with the
(h+1-i)th element of List
. All elements of the
ith superdiagonal are initialized with the
(h+1+i)th element of List
. All entries on the
remaining sub- and superdiagonals are set to zero.
See example 10.
We create the 2 x 2 matrix
+- -+ | 1 5 | | | | 2 0 | +- -+
by passing a list of two rows to matrix
, where each row
is a list of two elements, as follows:
>> A := matrix([[1, 5], [2, 3]])
+- -+ | 1, 5 | | | | 2, 3 | +- -+
In the same way, we generate the following 2 x 3 matrix:
>> B := matrix([[-1, 5/2, 3], [1/3, 0, 2/5]])
+- -+ | -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 4th power of A, and the scalar multiplication of A by 1/3 are given by:
>> A * B, A^4, 1/3 * A
+- -+ +- -+ +- -+ | 2/3, 5/2, 5 | | 281, 600 | | 1/3, 5/3 | | |, | |, | | | -1, 5, 36/5 | | 240, 521 | | 2/3, 1 | +- -+ +- -+ +- -+
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: dimensions don't match [(Dom::Matrix(Dom::ExpressionFie\ ld()))::_plus]
To compute the inverse of A, enter:
>> 1/A
+- -+ | -3/7, 5/7 | | | | 2/7, -1/7 | +- -+
If a matrix is not invertible, then the result of this
operation is FAIL
:
>> C := matrix([[2, 0], [0, 0]])
+- -+ | 2, 0 | | | | 0, 0 | +- -+
>> C^(-1)
FAIL
In addition to standard matrix arithmetic, the library
linalg
offers a lot
of functions handling matrices. For example, the function linalg::rank
determines the rank
of a matrix:
>> A := matrix([[1, 5], [2, 3]])
+- -+ | 1, 5 | | | | 2, 3 | +- -+
>> linalg::rank(A)
2
The function linalg::eigenvectors
computes
the eigenvalues and the eigenvectors of A
:
>> linalg::eigenvectors(A)
-- -- -- +- -+ -- -- | | | | 1/2 | | | | | | | 11 | | | | | 1/2 | | ----- - 1/2 | | | | | 11 + 2, 1, | | 2 | | |, | | | | | | | | | | | 1 | | | -- -- -- +- -+ -- -- -- -- +- -+ -- -- -- | | | 1/2 | | | | | | | 11 | | | | | 1/2 | | - ----- - 1/2 | | | | | 2 - 11 , 1, | | 2 | | | | | | | | | | | | | | 1 | | | | -- -- +- -+ -- -- --
To determine the dimension of a matrix use the function
linalg::matdim
:
>> linalg::matdim(A)
[2, 2]
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.
Matrix entries can be accessed with the index operator
[ ]
:
>> A := matrix([[1, 2, 3, 4], [2, 0, 4, 1], [-1, 0, 5, 2]])
+- -+ | 1, 2, 3, 4 | | | | 2, 0, 4, 1 | | | | -1, 0, 5, 2 | +- -+
>> A[2, 1] * A[1, 2] - A[3, 1] * A[1, 3]
7
You can redefine a matrix entry by assigning a value to it:
>> A[1, 2] := a^2: A
+- -+ | 2 | | 1, a , 3, 4 | | | | 2, 0, 4, 1 | | | | -1, 0, 5, 2 | +- -+
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]
+- -+ | 2, 0, 4 | | | | -1, 0, 5 | +- -+
The index operator does not allow to replace a
submatrix of a given matrix by another matrix. Use linalg::substitute
to achieve
this.
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)] ])
+- -+ | 2 2 2 | | (a - b) , a + b | | | | 2 2 | | a + b , (a + b) (a - b) | +- -+
>> expand(A)
+- -+ | 2 2 2 2 | | - 2 a b + a + b , a + b | | | | 2 2 2 2 | | a + b , a - b | +- -+
You can differentiate all matrix components with respect to some indeterminate:
>> diff(A, a)
+- -+ | 2 a - 2 b, 2 a | | | | 2 a, 2 a | +- -+
The following command evaluates all matrix components at a given point:
>> subs(A, a = 1, b = -1)
+- -+ | 4, 2 | | | | 2, 0 | +- -+
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)]])
+- -+ | sin(x), x | | | | x, cos(x) | +- -+
Then we substitute x=0 in each matrix component:
>> B := subs(A, x = 0)
+- -+ | sin(0), 0 | | | | 0, cos(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)
+- -+ | 0, 0 | | | | 0, 1 | +- -+
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]]): zip(A, B, `/`)
+- -+ | 2, 2 | | | | 3, -3 | +- -+
A vector is either an m x 1 matrix (a column
vector) or a 1 x 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])
+- -+ | 1, 2, 3 | +- -+ +- -+ | 1 | | | | 2 | | | | 3 | +- -+
If the only argument of matrix
is a
non-nested list or a one-dimensional array, then the result is a column
vector:
>> matrix([1, 2, 3])
+- -+ | 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
.
For example, to extract the second component of the vectors
row_vector
and column_vector
, we enter:
>> row_vector[2], column_vector[2]
2, 2
Use the function linalg::vecdim
to determine the
number of components of a vector:
>> linalg::vecdim(row_vector), linalg::vecdim(column_vector)
3, 3
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)
[1, 3], [3, 1]
See the linalg
package for functions
working with vectors, and the help page of norm
for computing vector norms.
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]])
+- -+ | 1, 2 | | | | 2, 0 | +- -+
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 x 4 matrix:
>> matrix(4, 4, [[1, 2], [2]])
+- -+ | 1, 2, 0, 0 | | | | 2, 0, 0, 0 | | | | 0, 0, 0, 0 | | | | 0, 0, 0, 0 | +- -+
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.
A one- or two-dimensional 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]] )
+- -+ | 1, 1/3, 0 | | | | -2, 3/5, 1/2 | | | | -3/2, 0, -1 | +- -+
can be converted into a matrix as follows:
>> A := matrix(a)
+- -+ | 1, 1/3, 0 | | | | -2, 3/5, 1/2 | | | | -3/2, 0, -1 | +- -+
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)
+- -+ | 5/3, 2/15, -1/6 | 1/2 1/2 | | 450 4037 | -1/20, 113/75, 6/5 |, -------------- | | 450 | -3, 1/2, -3 | +- -+
Note that an array may contain uninitialized entries:
>> b := array(1..4): b[1] := 2: b[4] := 0: b
+- -+ | 2, ?[2], ?[3], 0 | +- -+
matrix
cannot handle arrays that have
uninitialized entries, and responds with an error message:
>> matrix(b)
Error: unable to define matrix over Dom::ExpressionField() [(D\ om::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)
+- -+ | 2 | | | | 0 | | | | -1 | | | | 0 | +- -+
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
1/(i+j-1). Thus the following command creates a 2 x
2 Hilbert matrix:
>> matrix(2, 2, (i, j) -> 1/(i + j - 1))
+- -+ | 1, 1/2 | | | | 1/2, 1/3 | +- -+
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)
+- -+ +- -+ | x(1, 1), x(1, 2) | | x, x | | |, | | | x(2, 1), x(2, 2) | | x, x | +- -+ +- -+
Diagonal matrices can be created by passing the option Diagonal and a list of diagonal entries:
>> matrix(3, 4, [1, 2, 3], Diagonal)
+- -+ | 1, 0, 0, 0 | | | | 0, 2, 0, 0 | | | | 0, 0, 3, 0 | +- -+
Hence, you can generate the 3 x 3 identity matrix as follows:
>> matrix(3, 3, [1 $ 3], Diagonal)
+- -+ | 1, 0, 0 | | | | 0, 1, 0 | | | | 0, 0, 1 | +- -+
Equivalently, you can use a function of one argument:
>> matrix(3, 3, i -> 1, Diagonal)
+- -+ | 1, 0, 0 | | | | 0, 1, 0 | | | | 0, 0, 1 | +- -+
Since the integer 1
also represents a
constant function, the following shorter call creates the same
matrix:
>> matrix(3, 3, 1, Diagonal)
+- -+ | 1, 0, 0 | | | | 0, 1, 0 | | | | 0, 0, 1 | +- -+
Banded Toeplitz matrices (see above) can be created with the option Banded. The following command creates a matrix of bandwidth 3 with all main diagonal entries equal to 2 and all entries on the first sub- and superdiagonal equal to -1:
>> matrix(4, 4, [-1, 2, -1], Banded)
+- -+ | 2, -1, 0, 0 | | | | -1, 2, -1, 0 | | | | 0, -1, 2, -1 | | | | 0, 0, -1, 2 | +- -+