Previous Page Next Page Contents

Dom::Matrix -- matrices

Introduction

Dom::Matrix(R) creates a domain of matrices over the component ring R.

Domain

Dom::Matrix( <R>)

Parameters

R - a ring, i.e., a domain of category Cat::Rng; default is Dom::ExpressionField()

Details

Creating Elements

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, ListOfRows)
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, List)
Dom::Matrix(R)(m, 1, List)

Parameters

Array - a one- or two-dimensional 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
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)

Options

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

Categories

Cat::Matrix(R)

Related Domains

Dom::SquareMatrix, Dom::MatrixGroup

Details

Option: Diagonal

Option: Banded

Entries

isSparse

is always FALSE, as elements of Dom::Matrix(R) use a dense representation of their matrix components.

randomDimen

is set to [10,10]. See the method "random" below for details.

Method _divide: divides matrices

Method _invert: computes the inverse of a matrix

Method _mult: multiplies matrices by matrices, vectors and scalars

Method _negate: negates a matrix

Method _plus: adds matrices

Method _power: the integer power of a matrix

Method conjugate: the complex conjugate of a matrix

Method diff: differentiation of matrix components

Method equal: equality test of matrices

Method exp: the exponential of a matrix

Method expand: expand matrix components

Method factor: scalar-matrix factorization

Method float: floating-point approximation of the matrix components

Method gaussElim: Gaussian elimination

Method identity: identity matrix

Method iszero: test for zero matrices

Method matdim: matrix dimension

Method norm: norm of matrices and vectors

Method normal: simplification of matrix components

Method nonZeros: number of non-zero components of a matrix

Method random: random matrix generation

Method tr: trace of a square matrix

Method transpose: transpose of a matrix

Method _concat: horizontal concatenation of matrices

Method _index: matrix indexing

Method concatMatrix: horizontal concatenation of matrices

Method col: extracting a column

Method delCol: deleting a column

Method delRow: deleting a row

Method evalp: evaluating matrices of polynomials at a certain point

Method length: length of a matrix

Method map: apply a function to matrix components

Method nops: number of components of a matrix

Method op: components of a matrix

Method row: extracting a row

Method setCol: replacing a column

Method setRow: replacing a row

Method stackMatrix: vertical concatenation of matrices

Method subs: substitution of matrix components

Method subsex: extended substitution of matrix components

Method subsop: operand substitution of matrix components

Method swapCol: swapping matrix columns

Method swapRow: swapping matrix rows

Method set_index: setting matrix components

Method zip: combine matrices component-wise

Method convert: conversion to a matrix

Method convert_to: matrix conversion

Method create: defining matrices without component conversions

Method expr: matrix conversion into an object of a kernel domain

Method expr2text: matrix conversion to a string

Method TeX: TeX formatting of a matrix

Method assignElements: multiple assignment to matrices

Method mkDense: conversion of a matrix to an array

Method print: printing matrices

Method unapply: create a procedure from a matrix

Example 1

First we create the domain of matrices over the field of rational numbers:

>> MatQ := Dom::Matrix(Dom::Rational)
                        Dom::Matrix(Dom::Rational)

We assigned this domain to the identifier MatQ. Next we define the 2 x 2 matrix

+-    -+
| 1  5 |
|      |
| 2  0 |
+-    -+

by a list of two rows, where each row is a list of two elements:

>> A := MatQ([[1, 5], [2, 3]])
                                +-      -+
                                |  1, 5  |
                                |        |
                                |  2, 3  |
                                +-      -+

In the same way we define the following 2 x 3 matrix:

>> B := MatQ([[-1, 5/2, 3], [1/3, 0, 2/5]])
                            +-               -+
                            |   -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 4th power of A as well as the scalar multiplication of A times 1/3:

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

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: dimensions don't match [(Dom::Matrix(Dom::Rational))\
      ::_plus]

To compute the inverse of A, just enter:

>> 1/A
                             +-            -+
                             |  -3/7,  5/7  |
                             |              |
                             |   2/7, -1/7  |
                             +-            -+

If a matrix is not invertible, FAIL is the result of this operation. For example, the matrix:

>> C := matrix(2, 2, [[2]])
                                +-      -+
                                |  2, 0  |
                                |        |
                                |  0, 0  |
                                +-      -+

is not invertible, hence:

>> C^(-1)
                                   FAIL

Example 2

We create the domain of matrices over the reals:

>> MatR := Dom::Matrix(Dom::Real)
                          Dom::Matrix(Dom::Real)

Beside standard matrix arithmetic, the library linalg offers a lot of functions dealing with matrices. For example, if one wants to compute the rank of a matrix, use linalg::rank:

>> A := MatR([[1, 2], [2, 4]])
                                +-      -+
                                |  1, 2  |
                                |        |
                                |  2, 4  |
                                +-      -+
>> linalg::rank(A)
                                     1

Use linalg::eigenvectors to compute eigenvalues and eigenvectors of the matrix A:

>> linalg::eigenvectors(A)
       -- --       -- +-    -+ -- --  --       -- +-     -+ -- -- --
       |  |        |  |  -2  |  |  |  |        |  |  1/2  |  |  |  |
       |  |  0, 1, |  |      |  |  |, |  5, 1, |  |       |  |  |  |
       |  |        |  |   1  |  |  |  |        |  |   1   |  |  |  |
       -- --       -- +-    -+ -- --  --       -- +-     -+ -- -- --

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, such as:

>> linalg::gaussElim(A)
                                +-      -+
                                |  1, 2  |
                                |        |
                                |  0, 0  |
                                +-      -+

The computation is performed by the method "gaussElim" as described above. Such functions of the linalg packages, in contrast to the corresponding methods of the domain Dom::Matrix(R), check their incoming parameters, and some of them offer extended functionalities.

Example 3

In this example, we use the default matrix domain which is created by Dom::Matrix(). This domain represents matrices whose components can be arbitrary arithmetical expressions (i.e., the component ring is the domain Dom::ExpressionField()).

This domain is already known to MuPAD by the name matrix:

>> 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  |
                             +-             -+
>> domtype(A)
                               Dom::Matrix()

Matrix components can be extracted by the index operator []:

>> A[2, 1] * A[1, 2] - A[3, 1] * A[1, 3]
                                     7

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
                            +-              -+
                            |       2        |
                            |   1, a , 3, 4  |
                            |                |
                            |   2,  0, 4, 1  |
                            |                |
                            |  -1,  0, 5, 2  |
                            +-              -+

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]
                              +-          -+
                              |   2, 0, 4  |
                              |            |
                              |  -1, 0, 5  |
                              +-          -+

The index operator does not allow to insert submatrices into a given matrix. This is implemented by the function linalg::substitute.

Example 4

In the following examples, we demonstrate the different ways of creating matrices. We work with matrices defined over the field Z19, 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]])
                         +-                    -+
                         |  1 mod 19, 2 mod 19  |
                         |                      |
                         |  2 mod 19, 0 mod 19  |
                         +-                    -+

The elements of the two inner lists, the row entries, were converted into 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]])
               +-                                        -+
               |  1 mod 19, 2 mod 19, 0 mod 19, 0 mod 19  |
               |                                          |
               |  2 mod 19, 0 mod 19, 0 mod 19, 0 mod 19  |
               |                                          |
               |  0 mod 19, 0 mod 19, 0 mod 19, 0 mod 19  |
               |                                          |
               |  0 mod 19, 0 mod 19, 0 mod 19, 0 mod 19  |
               +-                                        -+

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 into an element over Z19:

>> MatZ19([[2, 3], [-1, I]])
      Error: unable to define matrix over Dom::IntegerMod(19) \
      [(Dom::Matrix(Dom::IntegerMod(19)))::new]

Example 5

This example illustrates how to create a matrix with components given as values of an index function. First we create the 2 x 2 Hilbert matrix (see also the functions linalg::hilbert and linalg::invhilbert):

>> matrix(2, 2, (i, j) -> 1/(i + j - 1))
                              +-          -+
                              |   1,  1/2  |
                              |            |
                              |  1/2, 1/3  |
                              +-          -+

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:
   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 6

Diagonal matrices can be created with the option Diagonal and a list of diagonal components:

>> MatC := Dom::Matrix(Dom::Complex):
   MatC(3, 4, [1, 2, 3], Diagonal)
                             +-            -+
                             |  1, 0, 0, 0  |
                             |              |
                             |  0, 2, 0, 0  |
                             |              |
                             |  0, 0, 3, 0  |
                             +-            -+

Hence, to define the n x n identity matrix, you can enter:

>> MatC(3, 3, [1 $ 3], Diagonal)
                               +-         -+
                               |  1, 0, 0  |
                               |           |
                               |  0, 1, 0  |
                               |           |
                               |  0, 0, 1  |
                               +-         -+

or even call:

>> MatC(3, 3, x -> 1, Diagonal)
                               +-         -+
                               |  1, 0, 0  |
                               |           |
                               |  0, 1, 0  |
                               |           |
                               |  0, 0, 1  |
                               +-         -+

The easiest way to create the identity matrix, however, is to use the method "identity":

>> MatC::identity(3)
                               +-         -+
                               |  1, 0, 0  |
                               |           |
                               |  0, 1, 0  |
                               |           |
                               |  0, 0, 1  |
                               +-         -+

Example 7

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 subdiagonals:

>> matrix(4, 4, [-1, 2, -1], Banded)
                           +-                -+
                           |   2, -1,  0,  0  |
                           |                  |
                           |  -1,  2, -1,  0  |
                           |                  |
                           |   0, -1,  2, -1  |
                           |                  |
                           |   0,  0, -1,  2  |
                           +-                -+

Example 8

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 := 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)  |
                      +-                           -+

enter:

>> expand(A)
                     +-                            -+
                     |             2    2   2    2  |
                     |  - 2 a b + a  + b , a  + b   |
                     |                              |
                     |        2    2        2    2  |
                     |       a  + b ,      a  - b   |
                     +-                            -+

If you want to differentiate the matrix components, then call for example:

>> diff(A, a)
                           +-                -+
                           |  2 a - 2 b, 2 a  |
                           |                  |
                           |     2 a,    2 a  |
                           +-                -+

To substitute matrix components by some values, enter:

>> subs(A, a = 1, b = -1)
                                +-      -+
                                |  4, 2  |
                                |        |
                                |  2, 0  |
                                +-      -+

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 := matrix([[4, 2], [9, 3]]): 
   B := matrix([[2, 1], [3,-1]]):
   zip(A, B, `/`)
                                +-       -+
                                |  2,  2  |
                                |         |
                                |  3, -3  |
                                +-       -+

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 := matrix(
     [[sin(x)^2 + cos(x)^2, exp(x) - exp(x/2)^2], 
     [(a^2 - b^2)/(a + b), 1]]
   )
                +-                                       -+
                |        2         2              / x \2  |
                |  cos(x)  + sin(x) , exp(x) - exp| - |   |
                |                                 \ 2 /   |
                |                                         |
                |        2    2                           |
                |       a  - b                            |
                |       -------,               1          |
                |        a + b                            |
                +-                                       -+

call:

>> map(C, simplify)
                              +-          -+
                              |    1,   0  |
                              |            |
                              |  a - b, 1  |
                              +-          -+

Example 9

A column vector is represented as a 2 x 1 matrix:

>> MatR := Dom::Matrix(Dom::Real):
   v := MatR(2, 1, [1, 2])
                                  +-   -+
                                  |  1  |
                                  |     |
                                  |  2  |
                                  +-   -+

The dimension of this vector is:

>> MatR::matdim(v)
                                  [2, 1]

Use linalg::vecdim, or even call nops(v) to get the length of a vector:

>> linalg::vecdim(v)
                                     2

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]
                                   1, 2

We get the 2-norm of v by the following call:

>> norm(v, 2)
                                    1/2
                                   5

Super-Domain

Dom::BaseDomain

Axioms

if R has Ax::canonicalRep
Ax::canonicalRep

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000