Previous Page Next Page Contents

matrix -- create a matrix or a vector

Introduction

matrix(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, ... |
+-           -+


Call(s)

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)

Parameters

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

Options

Diagonal - create a diagonal matrix
Banded - create a banded Toeplitz matrix

Returns

a matrix of the domain type Dom::Matrix().

Related Functions

array, DOM_ARRAY, Dom::Matrix

Details

Option: Diagonal

Option: Banded

Example 1

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

Example 2

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.

Example 3

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.

Example 4

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  |
                                +-       -+

Example 5

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.

Example 6

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.

Example 7

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  |
                                 +-    -+

Example 8

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  |
                    +-                  -+  +-      -+

Example 9

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  |
                               +-         -+

Example 10

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  |
                           +-                -+

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000