## `linalg::randomMatrix` -- generate a random matrix

#### Introduction

`linalg::randomMatrix``(m, n)` returns an m x n matrix with random components.

#### Call(s)

`linalg::randomMatrix(m, n <, R>)`
`linalg::randomMatrix(m, n <, R> <, bound>, Diagonal)`
`linalg::randomMatrix(m, n <, R> <, bound>, Unimodular)`

#### Parameters

`m, n` | - | positive integers
`R` | - | the component ring, i.e., a domain of category `Cat::Rng`; default: `Dom::ExpressionField()`
`bound` | - | an arithmetical expression

#### Options

`Diagonal` | - | creates a random m x n diagonal matrix over `R`.
`Unimodular` | - | creates a random m x n unimodular matrix over `R`.

#### Returns

a matrix of the domain `Dom::Matrix(R)`.

#### Related Functions

`random`, `Dom::Matrix`

#### Details

- The call `linalg::randomMatrix``(m, n)` returns a random m x n matrix over the default component ring for matrices, i.e., over the domain `Dom::ExpressionField()`.
- The matrix components are generated by the method `"random"` of the domain `R` (see example 2).
- The parameter `bound` is given as a parameter to the method `"random"` of the domain `R` in order to bound the size of the components of the random matrix. The correct type of `bound` is determined by the method `"random"`. The parameter has no effect if the slot `"random"` does not have a size argument.

#### Option: Unimodular

- Creates a random m x n unimodular matrix over `R`, so that its determinant is a unit in `R`.
- The norm of each component of the matrix returned does not exceed `bound`, which must be a positive integer, if specified. The default value of `bound` is 10.

#### Option: Diagonal

- Creates a random m x n diagonal matrix over `R`.

#### Example 1

We create a random square matrix over the integers. Because the matrix is random the created matrix can vary:

    >> linalg::randomMatrix(2, 2, Dom::Integer)

                                 +-            -+
                                 |   824,  -65  |
                                 |              |
                                 |  -814, -741  |
                                 +-            -+

If you want to bound the size of its components, say between -2 and 2, enter:

    >> linalg::randomMatrix(2, 2, Dom::Integer, -2..2)

                                    +-       -+
                                    |  -1, 1  |
                                    |         |
                                    |  -2, 1  |
                                    +-       -+

#### Example 2

The following input creates a random vector over the default component ring `Dom::ExpressionField()`. Because the vector is random the created vector can vary:

    >> v := linalg::randomMatrix(1, 2)

                   array(1..1, 1..2,
                                         3         5
                                   470 R1  - 494 R1  - 246
                     (1, 1) = ---------------------------------,
                                             2          3
                              381 R1 + 747 R1  - 1150 R1  - 535
                                              3         5
                              1169 R1 - 977 R1  + 932 R1  - 781
                     (1, 2) = ----------------------------------
                                              2          3
                              - 214 R1 + 10 R1  - 1240 R1  + 712
                   )

    >> domtype(v)

                                   Dom::Matrix()

The components of this matrix are random univariate polynomials created by the function `polylib::randpoly`. See the method `"random"` of the domain constructor `Dom::ExpressionField` for details.

#### Example 3

To create a random diagonal matrix over the rationals we enter, for example:

    >> linalg::randomMatrix(3, 3, Dom::Rational, Diagonal)

                          +-                           -+
                          |  -64/305,    0,       0     |
                          |                             |
                          |     0,    41/617,     0     |
                          |                             |
                          |     0,       0,   -167/509  |
                          +-                           -+

#### Example 4

The following command creates a random unimodular matrix over the integers so that its determinant is either 1 or -1:

    >> A := linalg::randomMatrix(3, 3, Dom::Integer, Unimodular)

                                  +-           -+
                                  |   9, 2,  8  |
                                  |             |
                                  |   4, 1,  0  |
                                  |             |
                                  |  -1, 0, -7  |
                                  +-           -+

    >> linalg::det(A)

                                         1

We can bound the size of the components. The following input returns a unimodular matrix A = (a[i,j]) with abs(a[i,j]) <= 2 for i,j=1..3:

    >> A := linalg::randomMatrix(3, 3, 2, Unimodular)

                                 +-            -+
                                 |  -1,  0, -2  |
                                 |              |
                                 |   1,  1,  0  |
                                 |              |
                                 |  -2, -1, -1  |
                                 +-            -+

Since we did not specifiy the component ring, the matrix is defined over the standard component ring for matrices (the domain `Dom::ExpressionField()`):

    >> domtype(A)

                                   Dom::Matrix()

#### Background

- For generating random unimodular matrices, see Jürgen Hansen: Generating Problems in Linear Algebra, MapleTech, Volume 1, No.2, 1994.

#### Changes

- The new options Diagonal and Unimodular were added.

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000